abusalimov/SublimeCImproved

Can we do something about macros? [feature req]

kgizdov opened this issue · 3 comments

Hi,

Thanks for a great Sublime package!

I wanted to basically ask the question from the title. I miss this feature in many C IDE and I have only seen it in Visual Studio. I am not sure how would it work, because Sublime doesn't seem to identify it as a syntax element inline, only at definition. If it's possible and you're willing to do it, could you also think about macros defined in a different file?

Just wanted to get the conversation started. Thanks.

This would be great to implement, if I knew how. :)

In fact, it is possible to achieve this in ST3 for symbols defined in an open file. The following snippet highlights (underlines) all calls recognized as calls to macros. I have only tried some pieces of this code, but it think you'll get the idea:

w = sublime.active_window()
v = w.active_view()

macro_calls = []
for r in v.find_by_selector('meta.function-call support.function'):
    s = v.substr(r)
    for path, rel_path, (row, col) in w.lookup_symbol_in_index(s.strip()):
        sym_view = w.find_open_file(path)
        sym_point = sym_view.text_point(row-1, col-1)
        if sym_view and sym_view.score_selector(sym_point,
                'entity.name.function.preprocessor'):
            macro_calls.append(r)

v.add_regions('macro-call', macro_calls, 'support.function', '',
              sublime.DRAW_NO_FILL |
              sublime.DRAW_NO_OUTLINE |
              sublime.DRAW_STIPPLED_UNDERLINE)

Konstantin, what do you think?

To be honest, I am not really good with python. I can follow the snippet and looks like it should do the job. If it works for open files, that's really what one needs after all. Also if you identify the files by extension you could narrow the search to lib and source files. I believe most people will have those properly labelled with extensions.

Otherwise, I think there is a way of going through the files in the current open directory in Sublime. So on top of going through open file, you could try also going through files the the current Sublime windows has access to.

And if you really really want to get into it (but I think that may be overkill) you could look for library definitions in the current and look for macros in those linked files.

I am not sure what the best option(s) will be, but if you're into it - have fun. I'm sure it would be pretty nice to see it working.

Closing it for now. Hope one day I could add this nice feature, but not in a foreseeable future...