Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2: build system custom selector

How can I define a selector in the build system with the custom extension (like *.ltx, *.cmake etc) for which there is no available selector (like text.tex.latex, source.c++ etc)?

Is it possible? If yes - how?

like image 638
m0nhawk Avatar asked Jan 03 '13 09:01

m0nhawk


1 Answers

It's possible, if you define a new syntax definition (i.e., a new .tmLanguage file). Syntax definitions can declare new 'scope names' which you can then use in your new, custom build systems.

The new syntax definition file doesn't actually have to define/match the file's syntax, as you can simply match by file extension...!

Take a look here at the .tmLanguage file syntax. The "scopeName" item allows you to name your new scope (i.e., "text.tex.latex", etc.). I'll go through an example below.


I created a new syntax which defined a new scope -- it was quite easy (like most things in Sublime):

  • In the Command Palette, select 'Package Control: Install Package'
  • In the list of packages, select 'PackageDev'
  • Create a new syntax definition by selecting Tools > Packages > Package Development > New Syntax Definition
  • Your new syntax definition will look like this:
{ "name": "Syntax Name",
  "scopeName": "source.syntax_name",
  "fileTypes": [""],
  "patterns": [
  ],
  "uuid": "..."
}

... replace "Syntax Name" with a descriptive name, "source.syntax_name" with your new scope name, and fill in "fileTypes" to contain one or more file extensions. For instance:

"fileTypes": ["tex", "ltx"]

  • Save the file using an ".JSON-tmLanguage" extension under Packages/User
  • Select Tools > Build System > Select Json to tmLanguage
  • Select Tools > Build

You're done! Any new files which happen to have one of the extensions defined in "fileTypes" will activate the "scopeName" scope.

You can now use this scope in a new Build System file (Tools > Build System > New Build System...)

Cheers!

like image 88
Greg Sadetsky Avatar answered Oct 25 '22 17:10

Greg Sadetsky