Sublime Forum

Ctrl+r, jack, and syntax files (maybe a tree too)

#1

I’m programming in the jack language. There is a Jack.sublime-syntax file, but ctrl+r doesn’t recognise any functions, methods etc, i.e ctrl+r doesn’t do the same for a .jack file as a .cpp or .py file (C++, python).

Does ctrl+r use the syntax file ?
If yes, what mods do I have to make to the syntax file for ctrl+r to work for a Jack program…
Or if no, how do i get started if I’m barking up the wrong tree.

I’ve had a quick look on this forum and the interweb at large (say 20min) but no joy as yet.

Instructions or correct tree to bark up, much appreciated :evergreen_tree::poodle:
Thanks in advance, Lozminda

0 Likes

Code Colouring - Language Aware?
#2

The symbol list is populated by a combination of the syntax definition and a separate metadata file that tells Sublime what parts of the language are symbols worth looking at.

If the syntax definition can distinctly recognize the items that you want in the symbol list (that’s generally the case if the color scheme can apply a style to just that particular text), then you’re probably just missing out on the metadata information. You should only need to modify the syntax definition if it doesn’t already match what you’re trying to capture.

As far as the metadata information goes, that requires a tmPreferences file that associates a scope selector with a setting that tells Sublime that the symbols that match the selector should appear in the symbol list.

As a simple example, you can use View Package File from the command palette to open Python/Symbol List.tmPreferences; the content of that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>Symbol List</string>
	<key>scope</key>
	<string>source.python meta.function - meta.function.inline, source.python meta.class</string>
	<key>settings</key>
	<dict>
		<key>showInSymbolList</key>
		<integer>1</integer>
		<key>symbolTransformation</key>
		<string>
		s/class\s+([[:alpha:]_][[:alnum:]_]*.+?\)?)(\:|$)/$1/g;
		s/(?:async\s+)?def\s+([[:alpha:]_][[:alnum:]_]*).*/$1\(…\))/g;
		</string>
	</dict>
</dict>
</plist>

This metadata tells Sublime that any text that matches the scope selector provided (non-inline functions as well as classes) should appear in the symbol list. By default the symbol list will show you the text as it appears in the file; the optional symbolTransformation can be used to modify the text that appears in the symbol list.

The name of the file doesn’t matter (so long as the extension is correct) but it’s common practice to name it something like the above so that you can tell what it’s doing easier. For more examples of what’s possible you can look at some of the other metadata files the same way.

The Package of the file doesn’t matter, nor does the name of the file (so long as it has the correct extension), so you could create a file in your User package (or perhaps submit a PR upstream to the package author) that has the appropriate content to get things working.

1 Like

#4

OK made some mistakes, don’t know if you can see the withdrawn post or no.

So I copied and pasted the above Symbol List.tmPreferences changing the source.python to source.jack. Saved it as jackSymbol Index.tmPreferences in the same folder as the jack syntax file and also my User folder.

No result crtl+r still only shows an ‘@’.

Obvs I’m doing something wrong/not understanding your previous message.

Thanks so far for your help, you seem to be single handedly being my sublime coach ! Cheers, Lozminda

0 Likes

#5

The scopes may be different in the syntax that you’re using from what they appear as in Python, so that may be the issue. Can you provide a link to where the Syntax definition that you’re using is?

0 Likes

#6

I downloaded it using Package Manager, just typing in “jack”.
So I copied it, and it follows. It does look very different to all the other syntax files i’ve looked at (for C++, haskell etc)

http://www.sublimetext.com/docs/3/syntax.html

name: Jack
file_extensions:

  • jack
    scope: source.jack
    contexts:
    main:
    • match: \b(class|int|char|boolean|void)\b
      scope: storage.type.jack
    • match: \b(constructor|function|method)\b
      scope: storage.modifier.jack
    • match: \b(field|static|var)\b
      scope: storage.modifier.jack
    • match: \b(let |if |else |while |do |return)\b
      scope: keyword.control.jack
    • match: ‘"’
      push:
      • meta_scope: string.quoted.double.jack
      • match: ‘"’
        pop: true
    • match: /*
      push:
      • meta_scope: comment.block.jack
      • match: */
        pop: true
    • match: //
      push:
      • meta_scope: comment.line.double-slash.jack
      • match: $\n?
        pop: true
    • match: ‘\b[0-9]+\b’
      scope: constant.numeric.jack
    • match: \b(true|false|null|this)\b
      scope: constant.language.jack
0 Likes

#7

Hmm… the package in Packge Control includes a tmLanguage version of the syntax (although it does convert to the sublime-syntax version that you outlined above.

This is a very simple syntax definition; it’s providing highlighting for keywords, but it doesn’t capture the sort of information that you need to do what you want; For example it matches the words constructor, function and method and assigns them the scope storage.modifier.jack, but it doesn’t capture the actual name of those things and apply a scope to them. Without a scope being applied to the symbols that you want in the symbol list, there’s not a way to extract them to put them into the symbol list.

0 Likes

#8

What would be the simplest modification i can do to the jack syntax file (i.e put in the appropriate scopes) so that the symbol index works? Or is it going to be a lot more complicated? Unfortunately C++, C, and jack (some forgotten Fortran/Pascal/Assembled Basic) I don’t think are going to help with this problem much. I can understand in the vaguist of terms what’s going on but I can’t be of any real help…sorry.

It is, in the grand scheme of things, in no way urgent. Not remotely. I have more important work to do, as I’m sure you do… But it would be quite cool, coz crtl+r is very handy in other languages, some one has gone to the effort of writing a jack syntax and peeps are always signing up to the Elements of Computer Systems course ! (Nand2Tetris, from which comes the jack language.)
Perhaps when we’re both retired and have more spare time… ?
Maybe someone else can help ?
I’ve got to crack on with my chores, thanks again !

0 Likes

#9

If you can provide some sample code that shows how constructors, functions and methods are defined (I searched a bit but couldn’t find any examples of code in this language) it might be a relatively simple manner to alter the syntax enough to get this working.

0 Likes

#10

This is an example of a jack program
The closest language (according to the authors of the book Nand2Tetris) is java.

It’s a really simple OOP.

If you need any help decoding give ,give me a shout, but I won’t be back on till the w.e. (work).

Ps

0 Likes

#11

pps I’ve got some stuff I’m writing in jack, but don’t yet have a github account so I’ll just have to past it in (from sublime), and it gets a bit mangled (I guess) going to a html format (??)

:crazy_face:

0 Likes

#12

Ppps https://github.com/swarn/sublime-jack (Credit where credit’s due)

0 Likes

#13

That is indeed extremely java-like. Those are the sorts of non-trivial(ish) changes which are unfortunately outside of my bailiwick. I get my hands dirty with a lot of package development in Sublime, but syntax definitions are not my strong suit. There are some other forum members that are more versed though, so someone may be able to help.

As a matter of curiosity, have you tried setting the syntax to Java for these files while you’re editing them? I’m not sure how close the full syntax is, and it’s not totally viable because the scopes are wrong. If it seems “good enough” it’s relatively simple to make a version of the Java syntax that associates itself with Jack files and has an appropriate Jack scope.

The potential problem there is in exactly how compatible the two languages seem to be; it may work really well (and possibly highlight some things that it shouldn’t, but otherwise be better) or it may be complete chaos…

0 Likes

#14

Cheers ! Sorry for the late ackknowledgement

0 Likes