Sublime Forum

[SOLVED] Help editing default C++ sublime-syntax

#1

In our code base, we have macros defined that expand out into functions when compiled, something like this:

MACRO_NAME( function_name, parameter )
{
    // Function things here
    return;
}

I would like to make Sublime Text recognize these macros as functions when using Goto Symbol, but I’m feeling hopelessly lost looking at the sublime-syntax file for C++. Any pointers or tips would be appreciated.

0 Likes

#2

Just under line 1948 of the Packages/C++.sublime-syntax file, you can add the following, indicated by the leading plus-signs:

 C++/C++.sublime-syntax | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/C++/C++.sublime-syntax b/C++/C++.sublime-syntax
index ead8a51..dc94d9b 100644
--- a/C++/C++.sublime-syntax
+++ b/C++/C++.sublime-syntax
@@ -1946,4 +1946,6 @@ contexts:
             - match: '>'
               scope: punctuation.definition.string.end.c++
               pop: true
+    - match: (?={{macro_identifier}}\s*\()
+      set: global-function-identifier
     - include: scope:source.c#preprocessor-practical-workarounds

This should scope macros ending with an opening parenthesis as entity.name.function, although I haven’t tested this much in context with other code.

1 Like

#3

This is so close to what I need, the problem is it shows up in Goto Symbol as MACRO_NAME, is it possible to tease out the function name so it could look like MACRO_NAME::function_name ?

Thanks for the assist!

Edit:

I think this gives me enough of an idea to proceed on my own, it is still confusing but I’m starting to grasp how this works, I will see if I can figure this out.

Edit x2:

Getting closer, this gives me the entire line in Goto Symbol, which is better than what I was getting:

- match: (?=^{{macro_identifier}}\s*\()
  push:
    - match: '.*'
      scope: entity.name.function.c++
      pop: true

Edit x3

Even closer now, this makes the MACRO_COMMAND lose it’s macro scope, but otherwise works.

- match: (?=(^{{macro_identifier}})\s*\(.*$)
  captures:
    1: meta.preprocessor.macro.c++
  push:
    - match: '\(\s*(.*),(.*)\)'
      captures:
        1: entity.name.function.c++
        2: variable.parameter.c++
      pop: true
0 Likes

#4

Final post and marking this solved, this is how I finally accomplished this. Note that I later discovered not only do we have things like:

MACRO_NAME( function_name, parameter )

but also:

MACRO_NAME( function_name )

and this gets both of those

- match: (?=((^{{macro_identifier}}))\s*\(.*$)
  push:
    - meta_scope: meta.assumed-macro.c++
    - match: '{{macro_identifier}}'
      scope: variable.function.c++
    - match: '\(\s*(.*),(.*)\)'
      captures:
        1: entity.name.function.c++
        2: variable.parameter.c++
      pop: true
    - match: '\((.*)\)'
      captures:
        1: entity.name.function.c++
      pop: true
1 Like