1

我有许多使用类似代码的不同类型的项目,我的目标是在 SublimeText2 中制作仅在给定项目上激活的片段和键绑定。

所以,例如,我有jsg1.htmlthrough jsg40.html,我也有kwa1.htmlthrough kwa40.html。这两个组中的每一个都有自己独特的代码,我想为每个组创建不同的片段和键绑定。

这导致我想要创建自定义文件类型,例如.kwaand .jsg,以便我可以创建范围设置为这些文件类型的片段/键绑定。

但是,我无法让它工作。这是我尝试过的:

a.sublime-snippet

<snippet>
<content><![CDATA[
<a href="$1" style="font-family: Segoe UI, Lucida Sans Unicode, Arial;  color:#2f6497; font-size:14px; text-decoration:underline;">$2</a>
]]></content>
<tabTrigger>aj</tabTrigger>
<scope>source.jsg</scope>
</snippet>

jsg.tmLanguage

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>fileTypes</key>
  <array>
    <string>jsg</string>
  </array>
  <key>name</key>
  <string>jsg</string>
  <key>scopeName</key>
  <string>source.jsg</string>
</dict>
</plist>

这两个文件都在我的Packages/User文件夹中。但是,我有一个文件 ,test.jsg并且片段不会在其中激活。

所以问题是:

为将使用相同键绑定和片段的 HTML 文件组创建自定义范围的最佳方法是什么?是否通过为新文件类型创建自定义语法来创建自定义范围?或者,还有更好的方法?如果这是最好的方法,为什么我的片段在我的.jsg文件中不起作用?

4

1 回答 1

0

所以我想通了。要生成您自己的键绑定和片段,您需要创建一个自定义的scope. 要显示文档的当前范围,可以使用热键:

Ctrl + ⇧ + Alt + P

如果您打开一个html文件并运行此命令,您应该会看到它显示text.html. 目标是创建一个突出html语法的文件类型,但显示我们自定义文件类型scopetext.we位置.we(也就是说,它可以是我们想要的任何东西)。然后我们可以scope在我们的键绑定和片段文件中使用它。

首先要做的事情是:我们需要确保我们的自定义文件类型尚未与html. 如果您在 下设置设置View - Syntax - Open all with current extension as...,则会发生这种情况,您最初可能会这样做以使您的文件类型具有与html. 别担心,我们仍然会这样做,但我们必须以另一种方式去做。

如果您已经这样做了,请找到该JSON文件../Path/to/Sublime Text 2/Packages/Users/HTML.sublime-settings并从属性中删除您关联的扩展名extensions。如果您在 Windows 上找不到该Packages文件夹​​,它通常隐藏在User/AppData/Roaming.

所以现在我们已经解除了文件的关联,我们必须首先找到使用我们想要的突出显示的语言的设置文件。就我而言,它是html,所以我去了../Path/to/Sublime Text 2/Packages/HTML。复制文件html.tmLanguage并将Comments.tmPreferences它们放在Packages. 对于组织,我将它们放在我的自定义文件类型Packages/Users/xxx所在的位置。xxx当我们稍后制作片段时,我还建议您将它们保留在那里,以便与您的文件类型相关的所有内容都在一个地方。

Anyway, rename the files accordingly, based on your filetype. Within the file, you'll need to change three attributes: fileTypes (it's at the beginning), name, and scope (those last two are at the very end). Replace the content of those accordingly. Be sure the scopeName takes the form text.___.

Change up the scope of the Comments.tmPreferences file and you'll be good to go with your custom filetype. It'll now display the highlighting of html files and allow you to use it as a scope in snippets and key-bindings.

Also, if your custom files are HTML files, browsers won't have any problem displaying them when you open them up.

Mission accomplished!

于 2012-11-21T17:35:38.520 回答