Syntax specific highlighting with Sublime Text 2

2019-02-17 14:15发布

问题:

I'm wondering if there is a way to have two different tag colours ("colors" for those in the US) for different language tags in the same file.

For example, lets say I have ColdFusion code and HTML code in the same .cfm file. Could I make the ColdFusion tags red and the HTML tags Blue?

For instance, lets call the below file HelloWorld.cfm - could I colour the tags differently?

<cfset myvar = "hello, world" />
<html>
<head>
  <title>This is my title</title>
</head>
<body>

<div><cfoutput>#myvar#</cfoutput></div>

</body>
</html>

Thanks!

回答1:

Yes, as long as the tags can be identified as having different scopes by your installed language definitions, you can edit your colour scheme to target those scopes with specific colours and other styles.

In your packages folder, language scopes are defined in the .tmLanguage files for your installed languages, while styles are defined in the .tmTheme files in the "color scheme - default" folder.

If you position your cursor inside a tag, and press shift+ctrl+alt+p (shift-cmd-p in OSX I think) the status bar will display the current scope. You can also copy this to the clipboard via the console with this command:

sublime.set_clipboard(view.syntax_name(view.sel()[0].b))

You can use this information to create your styles, a bit like css selectors, but with XML. For example I use this Coldfusion package and I have the scope selectors shown below in my custom .tmTheme file to distinguish cf tags from HTML tags.

<dict>
    <key>name</key>
    <string>Tag name</string>
    <key>scope</key>
    <string>entity.name.tag</string>
    <key>settings</key>
    <dict>
        <key>background</key>
        <string>#D8D0B6</string>
        <key>fontStyle</key>
        <string>bold</string>
        <key>foreground</key>
        <string>#647A4F</string>
    </dict>
</dict>
<dict>
    <key>name</key>
    <string>CF tag name</string>
    <key>scope</key>
    <string>entity.name.tag.conditional.cfml, entity.name.tag.declaration.cfml, entity.name.tag.other, entity.name.tag.cf, entity.name.tag.inline.other.cfml</string>
    <key>settings</key>
    <dict>
        <key>background</key>
        <string>#D8D0B6</string>
        <key>fontStyle</key>
        <string>bold</string>
        <key>foreground</key>
        <string>#990033</string>
    </dict>
</dict>

More info on scope selectors.



回答2:

I've updated the ColdFusion.tmLanguage so that you'll only need to target entity.name.tag.cf to color all cf tags. You can also target specific tags for example entity.name.tag.cf.script or entity.name.tag.cf.query for cfscript and cfquery respectively. HTH



标签: sublimetext