Sublime Forum

Creating Syntax Definitions Tutorial

#1

Hello, I was hoping I could have a few questions asnwered that hopefully will help me understand creating syntax definitions.

I started by following the docs for syntax definitions: http://docs.sublimetext.info/en/latest/extensibility/syntaxdefs.html

which have you install PackageDev which I did.

Then later it says go to: Tools | Packages | Package Development | New Syntax Definition

But that doesn’t exist for me, so I['m not sure I’m on the right track.

I did go to: Tools > Developer > New Syntax

This opened a new file with what appears to be a YAML template.

It seemed to be similar to what they are using in the doc descriptions so a I saved it with user name with the .yaml extension in the file folder indicated by the docs:

…/AppDate/Roaming/Sublime Text 3/{ackages/User

Just to see if I was on track, as per the docs, I added the line ( it wasn’t there in the template ) just before the “scope:…” line, a line with “name:”

After “name:” I typed “vex” which is going to be the file extension name I am using for my new definition.

After the line with “name: vex” I added another line with “file_extensions: [vex]”

I saved the file and closed Sublime.

When I re-opened Sublime and opened a new file I went to View > Syntax but I don’t see the listed language “vex” which is what I assumed that the line in my yaml file would create as an option with “name: vex”

I guess what is happening is that in View > Syntax - under the YAML selection it will look at all the different yaml files you have and just automatically choose the definition based on lines that have "file_extensions: [your new file extension] in those yaml files.?

In other words its just your ‘job’ to remember if you have a yaml file that has a header and definition related to the unique file extension name you want it applie too? That you can’t just create a new selection in the " View > Syntax " menu?

Also, when I looked in the folder location for yaml file/s I noticed the other languages aren’t there like for c or python definitions.

I was hoping to find the c definition and simply copy and paste those definitions in my new vex - yaml file and simply make some minor changes. But I can’t find those ‘c’ definitions.

I guess those definitions are “built-in” Sublime? and that we access them through the “scope:” line of the yaml file. In my desired case - scope: source.c ? and then after that just add or make changes in that yaml file?

If anyone could give me some pointers so I understand this a bit better it’s much appreciated.

Thank you

0 Likes

Create my own syntax theme
Cannot find my New Syntax definition file in ST3
HTML code Monokai color scheme - can I make <!--#include tags look different to plain comments?
#2

I am by no means an expert in sublime syntax stuff (I’m dabbling with it currently), but I can offer a bit of assistance.

I would think that this should work based on my own experiments. Whenever things aren’t going as you expect, it can be helpful to open the sublime console (View > Show Console or Ctrl+`) and see if it’s unhappy about something.

Indeed the idea is that Sublime will choose the syntax based on the extension of the file when you open it. You can override this by opening a file and then using View > Syntax > Open all with current extension as... to select a different syntax. You could use this if (for example) you had multiple syntaxes defined that apply to the same file extension.

In addition, plugins can tweak this. A good example is the ApplySyntax package.

No, it should select it (assuming it has a unique extension, otherwise see what I mentioned above). If it’s not doing that (or appearing in the menu) then it’s probably unhappy with the syntax definition file, which is why it may not be appearing and working for you.

They’re just regular packges, but they’re not stored in the location you’re looking at because that’s for the plugins you’ve manually installed and not the ones that come bundled.

In my opinion, your best bet here is to install PackageResourceViewer, which is an invaluable resource for seeing how other packages are doing what you’re trying to do.

With this installed, you can open the command palette with Shift+Ctrl+P, select Package Resource Viewer: Open Resource by narrowing the selected items with prv, and then select the C++ package, followed by C.sublime-syntax.

That will open the syntax file that is being used for C, so you can see how it works.

As an unrelated side note, are you working with RobotC on a Vex robotics kit by any chance?

1 Like

#3

Thanks very much Odat for the info.

I’ll work on those suggestions and see what I can do.

The “vex” I’m working on is one of the “scripting” languages that Houdini software uses for animation/special effects.

It’s very close to the c language just a few things I would like to change to make my code more readable.

Thanks again.

0 Likes

#4

Thanks for that help Odat.

So I tried your suggestions and opened up the C.sublime-syntax.

In a copy of that file I changed the name in the header and made the change I wanted to and saved it to where the tutorial said it should go.

I noticed that it saved the file the the extention *.sublime-syntax instead of *.yaml which is what I was doing and what was the problem.

So it now shows up in View > Syntax and the changes I made work when opening my *.vex files.

Thanks again.

1 Like

#5

Note that the syntax docs at http://docs.sublimetext.info/en/latest/extensibility/syntaxdefs.html are for the old .tmLanguage syntax definition format, which is superseded by .sublime-syntax, as you probably noticed.

Documentation for the new format is available at the official docs page: http://www.sublimetext.com/docs/3/syntax.html

0 Likes