greplytix / Hamlbars

Sublime Text 3 Syntax highlighting for .hamlbars files which is a combination of both Haml and Handlebars
MIT License
2 stars 0 forks source link

How is syntax highlighting accomplished in Sublime Text 3? #9

Closed ghost closed 9 years ago

ghost commented 10 years ago

Can the highlighting be extended in ST3? If so, what is the general means of doing so? What is the process of distributing such extensions? What is the best language for the job? Do regular expressions play a pivotal role here? Are there existing open source extensions like this that can be leveraged for this purpose?

ghost commented 9 years ago

Syntax Definitions in Sublime Text 3

Introduction

Syntax highlighting can most certainly be extended in Sublime Text 3. Sublime Text extensions are distributed as packages. They are distributed through a package manager called Package Control which may be installed by following the directions here. Our project can be deployed as such a package, and we might leverage suitably licensed packages in creating ours, but I'll come back to that later.

A pretty comprehensive tutorial on implementing syntax highlighting comes from the Sublime Text Unofficial Documentation page on syntax definitions (an umbrella term for syntax highlighting). As discussed in the tutorial, Sublime Text uses property lists to specify definitions. Property lists (Plist) are XML files, which many developers, including myself, consider to be too verbose as opposed to alternatives such as JSON.

Serialization Options

YAML to Plist

Cue the tutorial's espousal of compiling JSON to Plist by use of the AAAPackageDev extension. Please note that AAAPackageDev actually compiles YAML to Plist as of its current release, not JSON per se. JSON is a subset of YAML, so perhaps somewhere along the line, the AAAPackageDev developers decided that they wanted to account for all of YAML's features (which would encompass JSON), such as commenting. Thus, the tutorial should be considered outdated for this reason, but it's seemingly just as useful as it originally was.

CoffeeScript to Plist

Anyway, another alternative is compiling CoffeeScript objects to Plist by use of the Aroma Node.js module. Aroma allows for logic and variables, which might reduce the amount of code we will write.

A Note on Filenames

Sublime Text 3 uses the ".tmTheme" filename extension, not ".plist," meaning that Aroma-compiled files will need to be renamed, or we could just pass the "-e" argument to change the extension from the default ".plist" before compilation.

Scopes and Syntax Definitions

Tying It All Together

Now that serialization options are clear, we should consider scopes. Scopes are "named text regions in a buffer," according to the tutorial. The named part and associated code can be nested in a way comparable to CSS selectors. They are truly the key to syntax definitions in Sublime Text, and are derivative of scope as implemented in TextMate. Essentially Sublime Text seeks the buffer of scopes and applies highlighting based on matching regular expressions. (When a scope name is accompanied by a regular expression, it's called a syntax definition.)

Implementing a syntax definition, syntax highlighting, is almost as simple as the object notation used, such as JSON. That's where the scope name, matching regular expressions, and other metadata are kept - in an object notation file that will be serialized to Plist.

Conclusion

We need to choose a serialization pipeline. I will prompt discussion on this soon.

Regardless of the serialization pipeline, we might employ the ScopeHunter package in our development process. It essentially echoes the scope of whatever text is under the cursor at any given time. This sounds very useful.

Also, the creator of ScopeHunter also made the BracketHighlighter package. It looks like it could help specifically regarding Handlebars. @abandimh01 might look into this since he's been assigned #11.