Highlight code between #if .... #endif
KrisAbildgaard opened this issue · comments
I love the simpleifdef, but it would be even better if I was able to highlight entire code between #if and #endif (or #else).
Would that be possible to make?
Thanks
Kris
Did you have in mind something like this ?
I'm not convinced to add this feature, because I'd need a configuration option to enable it, and that would make this plugin not so simple. But you could modify simpleifdef.py to make it work
def on_selection_modified(self, view):
# NOTE: do I need to erase on each update?
view.erase_regions('ifdef')
view.erase_regions('inbetween')
try:
cursor = view.sel()[0].a
except IndexError:
return
for r in self._regions:
if r.contains(cursor):
rtup = r.a, r.b
if rtup in self._groups:
view.add_regions('ifdef',self._groups[rtup],
'keyword','',sublime.HIDE_ON_MINIMAP)
g = sorted(self._groups[rtup])
if len(g) < 2:
continue
inbetween = []
hl = view.lines(g[0].cover(g[-1]))
hlines = []
for l in hl:
h = True
for r2 in g:
if r2.intersects(l):
h = False
break
if h:
hlines.append(l)
view.add_regions('inbetween',hlines,'meta.block','',
sublime.HIDE_ON_MINIMAP)
break
Hi Joseph
Thanks for very fast response :-)
To answer your question:
No, it would be way too "flashy" if the highlighting happened automatically.
In my old editor I could press Ctrl+something and then it highlighted all code in between #if - #endif (or #else)
That would be really nice, when the #if - #endif spans over more pages (screens).
Regards
Kris
If the highlighted region spans over several pages, you would need to move the text cursor with the keyboard, and that would clear the highlight. Or you could scroll with the mouse wheel, that would probably work.
The way you see it, you press the Ctrl+something shortcut, and the block of code is highlighted. Will this change persist? If so, there is a need for another hotkey to clear it.
Such functionality is maybe outside the scope of my plugin. I try to keep it simple.
IMO there is a need for a separate ST plugin, that could highlight the block(s) of code on demand. And it could work not only with #ifdef - #else - #endif , but with if - else, or even Pascal constructs begin - end, if - then - else etc.
If you have a need for such thing, I encourage you to try and write it. It is fun, and the documentation is better than in 2018, when I was writing simpleifdef.
OK.. Thanks.
Maybe I'l try to have a look at it