Sublime Forum

Extend syntax highlighting

#1

What is the correct way to extend syntax highlighting? I have seen some posts requesting the ability to do so using user files, the way settings work. But this does not appear possible. I would like to add some syntax highlighting to a current language. Any ideas?

0 Likes

#2

You can do this using YAML Macros. Check out the JSX example.

This does require knowledge of Sublime’s syntax definitions.

What sort of changes do you want to make?

1 Like

#3

Did you have a look into tmLanguage files yet?

Example

I am not entirely sure what you like to achieve, maybe we could be of more help if you’d provide more information.

0 Likes

#4

.tmLanguage is the old syntax highlighting system. Most (all?) default packages use the new .sublime-syntax system, which is both more powerful and easier to write.

3 Likes

#5

Thanks for the replies. I write in Markdown, and I use Pandoc Academic which has a nice color theme. I would like to add critic markdown syntax highlighting (http://criticmarkup.com/), so changes are highlighted with an different color. For example

{++This is inserted text, which would be green. ++}
{–This is deleted text, which would be red. --}
{~~This is a substitution which should be orange~>This is the new text ~~},
{==this is highlighted (or selected) text, which would be yellow ==}
{>>and this is a comment, blue. <<}

0 Likes

#6

Shouldn’t Academic Markdown already supporting that. It says so in its description.

Have a look: https://packagecontrol.io/packages/AcademicMarkdown

Maybe you have to enable it.

0 Likes

#7

Yes, but there is no highlighting that I can see for any of the color schemes included in Pandoc academic. If I select Preferences - “Settings - Syntax Specific” it is empty.

0 Likes

#8

Note that it’s the job of the syntax definition (e.g. SomeLanguage.tmLanguage or SomeLanguage.sublime-syntax) to break the contents of the file into different parts based on their purpose in the language, and to apply one or more scopes that provide meta information on what each character is supposed to represent.

It’s the job of the color scheme (e.g. Monokai.tmTheme) to apply distinct foreground color, background color and font effects to various scopes (e.g. entity.name.tag shall be red).

It’s the job of syntax specific settings (e.g. SomeLanguage.sublime-settings) to set per-syntax editing settings, such as tab_size, color_scheme and so on. Apart from being able to have a color scheme set (from above) that is specific to a particular syntax, the settings have no interplay with syntax highlighting.

If the package mentioned above is providing a syntax for the language that is applying unique scopes to the constructs you mentioned above, but no color schemes are causing those constructs to be colored differently, then you need to modify/create a color scheme file which includes the extra scopes and the colors that they should use.

If instead the package mentioned above doesn’t provide a syntax for the language that applies the unique scopes that you want, then in addition to modifying the color scheme to know how to color the new scopes, you also need to modify the syntax specification to detect those constructs and apply scopes to them as well.

0 Likes

Mcnp code syntaxing
#9

Installing the above mentioned AcademicMarkdown package in a copy of Sublime 3126, I created a new empty file, put your sample code from above into it and changed the syntax to AcademicMarkdown, and I see this:

This is almost but not quite what you mentioned wanting above. Presumably the highlighted text is not yellow because yellow on white would be near invisible or something.

The AcademicMarkdown.sublime-settings that ships with the package includes the following:

	"color_scheme": "Packages/AcademicMarkdown/AcademicMarkdownEditor.tmTheme",
	// "color_scheme": "Packages/MarkdownEditing/MarkdownEditor-Dark.tmTheme",
	// "color_scheme": "Packages/MarkdownEditing/MarkdownEditor-Yellow.tmTheme",

The other options require you to install the MarkdownEditing package as well, but those schemes don’t supply any highlighting for the new scopes.

As such I would recommend making a modification to the color scheme that ships with AcademicMarkdown if you need it to be different (such as a darker background color or making the highlighted text yellow as you want).

From the looks of things, the scopes you want to target (in this or some other color scheme) are the following:

  • criticmarkup.addition
  • criticmarkup.deletion
  • criticmarkup.substitution
  • criticmarkup.highlight
  • criticmarkup.comment
3 Likes

#10

@ThomSmith Thanks! Indeed I did not know this. So much yet to learn about Sublime Text. :thumbsup:

0 Likes

#11

Those are pretty nonstandard scope names, at least according to Sublime’s scope naming document, and even going back to the referenced TextMate scope names. Not that there’s anything against using new scope names, but it’ll make compatibility with color schemes iffy. On the other hand, is this a growing scope name need for diff-type markup?

0 Likes

#12

You can use arbitrary scope names if you bundle your own color scheme for them, but because of the limited interchangeability it’s not recommended.

0 Likes

#13

OdatNurd, thanks, but for various reasons I use Pandoc Academic, not AcademicMarkdown. I was hoping to be able to add the criticmarkup stuff to pandoc academic, in a non-invasive way.

0 Likes

#14

So, is there no way to extend a syntax? I have edited the “Pandoc Academic” package to handle critic markup, but this is not a very good solution, since any package update will remove my edits.

0 Likes

#15

As far as I am aware, there is no way to extend a syntax without doing something similar to what you’ve done, no. Unlike some other sublime resource file types (e.g. sublime-settings, sublime-menu) that combine together, sublime-syntax files do not combine.

A possible solution to this might be to duplicate the existing file into your User package with a different name, and then while you have a file with the appropriate type open select View > Syntax > Open all with current extension as from the menu and select your custom syntax to override the default (you may also need set any syntax specific settings accordingly if you have any).

Indeed if you actually edited the contents of the sublime-package file, your changes will be unceremoniously clobbered away if/when the package gets updated. A more permanent solution would be to create a package override instead.

The easiest way to do so is to use PackageResourceViewer and use the PackageResourceViewer: Open Resource command to open the syntax file (from a pristine copy of the package), make your edits and then save.

That will unpack that single file into a folder in the Packages folder named for the package, and Sublime will use that version of the file in place of the one in the sublime-package file, so if the package updates your change is not lost.

If you do that, your version of the file will be used even if the package author changes the file, and you get no warning from Sublime, so you might miss syntax updates. The OverrideAudit package can help with that, telling you when the source file is newer than your local version, letting you see the differences between the two, etc.

0 Likes

#16

Why not open a pull request at Pandoc Academic with your additions? Pretty much all packages are open source for a reason.

1 Like

#17

That is indeed my plan. The goal of the question was to see what is possible with Sublime. But I will be making a pull request soon.

Thanks everyone for the input.

0 Likes