8

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?

m0nhawk
  • 22,980
  • 9
  • 45
  • 73

1 Answers1

12

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!

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Greg Sadetsky
  • 4,863
  • 1
  • 38
  • 48
  • Could I use this to make the \section{text} headings bigger in Latex code like in Textmate 2? http://tex.stackexchange.com/questions/98574/textmate-2-how-can-increase-font-size-of-sections-in-the-markup-code – Jonathan Komar Feb 27 '13 at 00:05
  • 1
    @macmadness86 unfortunately, no. I've looked a bit at what I could find about the .tmTheme syntax, and it seems that it only supports a 'fontStyle' (of which Sublime supports 'bold' and 'italic') and 'foreground' and 'background' colors. I've tried to set a custom 'fontSize' setting to no avail. See here (especially in the comments): http://sublimetext.userecho.com/topic/20780-implement-fontstyle-in-tmtheme/ and http://stackoverflow.com/questions/9345222/syntax-specific-highlighting-with-sublime-text-2 as well – Greg Sadetsky Feb 27 '13 at 03:37