Sublime Forum

In a syntax file, is there a way to eliminate a leading underscore from a function name?

#1

I am working on a project that is a mix of assembly and C. I have a custom syntax for my assembly language. I’m having a problem with the Goto Anything functionality because of a difference in convention between assembly and C.

If I have a function in C called “abc()”, the actual symbol exported to the linker is “_abc” (with a leading underscore). If I want to call that function directly from assembly, I need to jump to the label “_abc”, not “abc”.

The problem is that Goto Anything doesn’t recognize “_abc” and “abc” as the same symbol. I have tried changing the syntax file to make sure that just “abc” (and not the underscore) is scoped as variable.function (even though that makes the highlighting look weird), but Goto Anything seems to include the leading underscore as part of the symbol either way.

Is there a way to do this?

0 Likes

#2

You can’t do it with the syntax file but it should qui easy to write a plugin to do that:

Something like :

from Default import symbol as sublime_symbol

class GotoDefinitionAsmCommand(sublime_plugin.TextCommand):
    def run(self,edit):
        r = self.view.sel()[0]
        if r.empty() :
           r = self.view.word(r)
        symbol = self.view.substr(r)
        if symbol.startswith('_'):
           symbol = symbol[1:]

        locations = sublime_symbol.lookup_symbol(self.view.window(), symbol)
        sublime_symbol.navigate_to_symbol(self.view, symbol, locations)

Not tested, but it should give you a good starting point.
Then you can create a command or do a key binding to the function goto_definition_asm

0 Likes

#3

You would provide a tmPreferences file that specifies a symbol transformation. See http://docs.sublimetext.info/en/latest/reference/symbols.html.

1 Like

#4

Thanks! I haven’t written a plugin before, but I’ll give that a try - it sounds like it will do the right thing. I understand that if I write a keybinding with the right scope, I can have make the go-to-definition use my plugin. There is also the behavior that if you hover the mouse over a symbol (variable.funciton scope), it opens a pop-up with the location of the definition and the list of references. Is there a way to replace that with a plugin as well?

Regarding the symbol transformation in the tmPreferences, I have that, and I think it solves the problem in the other direction: if I define a function in assembly (where it has a leading underscore), I can use the symbol transformation so that the symbol does not have the underscore, and it can therefore be seen from C. As far as I can tell, though, the symbol transformation only works on the definition, not the use of the symbol, so I don’t think it would help me jump to a C definition from assembly. Is that correct?

I believe that I have to choose to have my symbols all have a leading underscore or all not have a leading underscore and use tmPreferences to force that convention. In one language, go-to-definition will just work, and in the other, I’ll have to do something like the plug-in.

0 Likes

#5

I’m using both suggestions together: a plugin to override goto-definition for assembly files (so they can find functions defined in C), and a tmPreferences file that does a symbol transformation (so the built-in goto-definition works from C to find functions defined in assembly). They work great. Thanks again!

To put a little icing on the cake, it would be great if the tooltip that pops up with the definition and references would also be able to strip the leading underscore. I don’t see any way to customize it, though, (just enable/disable it). Is there a way to do that?

1 Like

#6

You can use a syntax specific settings file to disable the setting you noticed and stop the default pop up from displaying and then implement your own version of it. The code that it’s using is in the Default package, so you can view it with the View Package File command and see how it works.

0 Likes

#7

Got it. It’s working well now. Thanks!

0 Likes