Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2: Different language highlighting based on context? (a la Webstorm)

I was watching some videos on Egghead.io about AngularJS. The creator of the videos uses Webstorm (and, I believe, works for them). One feature I noticed is that he can set different syntax highlighting within different scopes or quotation marks. So, in code like the following (from an AngularJS directive)

return {
    template: '<div>something</div>',
    //         ^^^ these guys ^^^
}

...he can get the inside of the quotation marks to highlight as HTML.

I use Sublime Text 2, and am fairly wedded to it. Is there an existing feature/plugin for Sublime that could handle a case like this? If not, is something like this technically possible using the Sublime Text 2 API?

like image 362
Ben Jacobs Avatar asked Mar 15 '13 15:03

Ben Jacobs


People also ask

What is syntax highlighting in Sublime Text 3?

Sublime Text 3 is a very flexible text editor with a lot of helpful features. One of those features is called “Syntax Highlighting”. Syntax Highlighting applies colours to syntax within your code to make it easier to see what certain parts of the code mean at a quick glance.

What is package control in Sublime Text?

Package Control is a package manager for Sublime Text that makes it simple to find, install and keep packages up-to-date. 2. Install ‘PackageDev’ Package PackageDev is a Sublime Text package that helps create and edit syntax definitions, snippets, completions files, build systems and other Sublime Text extension files. 3.

How can I provide input for Sublime Text build system?

Sublime text support most of the popular language. In order to see the list of language you can click on bottom left corner. How can I provide input for a program that I run using Sublime Text Build System? you can goto Tools->Build System->New Build System.

How do I edit text in Sublime Text?

While editing in Sublime Text, you can check what scopes have been applied to the text under the caret by pressing control + shift + p (Mac) or ctrl + alt + shift + p (Windows/Linux). The string context has two patterns: the first matches a backslash character followed by any other, and the second matches a quote character.


1 Answers

I don't think it's built in, but it's certainly possible. I've been doing some work with graphviz and wanted to do something similar. Labels can be generated with html like syntax. Anyways, I played around with the .tmLanguage file and added a new pattern to match the context where html like entries were valid (I look for label = <). The patterns I used for the captures aren't that good, but it works for fine for me. This give me the following, which I think is similar to what you are looking for.

Demo

I don't know anything about AngularJS, so I can't help you with anything specific to that, but it is certainly possible. Note that in the image below, the last <table></table> are just to show that highlighting doesn't occur there.

Edit:

Forgot to include this in the original post, but here is my updated tmLangauage file. That first pattern is what I added(link). I used PlistJsonConverter to go from JSON to plist, then saved the file as .tmLanguage. Hope this helps.

like image 200
skuroda Avatar answered Oct 19 '22 22:10

skuroda