Sublime Forum

"Go to Definition" not working on variables starting with an underscore

#1

Hello, I have been working on a project in which underscores are sometimes used at the start of variable strings. Go To Definition works great usually, but not on these variables that start with an underscore. Any way I can get Sublime Text 3.2.1 to index variables starting with an underscore?

0 Likes

#2

which syntax? what scope does the variable beginning with an underscore receive, and does it differ from those not beginning with an underscore?
Depending on the answer, you may need to tweak a sublime-syntax file, otherwise a tmPreferences file.

0 Likes

#3

z80 assembly syntax

I have labels in the exact same file that work and others that don’t, and the only discrepancy is they start with an underscore. When I remove the underscore from the label, it works. I don’t think it’s because of the syntax.

0 Likes

#4

According to z80 syntax definition, labels must start with ascii character a-z, but not with underscore.

see: https://github.com/mrcook/Z80Assembly/blob/5af8389787aefae725be8e23954afe58497c216c/Z80Assembly.tmLanguage.sublime-syntax#L15

0 Likes

#5

Hmm, can I modify it locally to allow underscore labels?

0 Likes

#6

Yes; you can install OverrideAudit and then use its OverrideAudit: Create Override button, and when prompted choose the Z80Assembly package and the Z80Assembly.tmLanguage.sublime-syntax file; navigate to line 15 and modify the regex so that the first capture group has an _ in it:

  label: '\b[a-zA-Z][a-zA-Z0-9_]*[a-zA-Z0-9]\b'

becomes:

  label: '\b[_a-zA-Z][a-zA-Z0-9_]*[a-zA-Z0-9]\b'

As soon as you save the file, the syntax will automagically recompile and and things will re-index as needed, and those labels should show up.

0 Likes

#7

Thanks! Rather than using this package which seems to need a newer version of sublime text, I just downloaded the Z80Assembly syntax file and created a new syntax locally, then pasted it in there and modified it to my needs. Now it works!

0 Likes

#8

This isn’t a bug in Sublime Text’s indexing; the z80 syntax definition is doing exactly what it’s supposed to do. The regex for labels says that _ cannot be the first character, so Go to Definition never sees those symbols as valid labels.

Your fix is correct: either override the z80 syntax or make a local copy of it, and make the label regex more flexible by letting leading underscores. Sublime’s symbol indexer picks it up right away once the scope is right. a clean solution that is much better than arguing with the editor.

0 Likes