-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleifdef.py
89 lines (74 loc) · 2.12 KB
/
simpleifdef.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Sublime Text 3 plugin for highlighting #ifdef-#else-#endif
# in source code.
# Author: Joseph Botosh <[email protected]>
# License: GPL
import sublime
import sublime_plugin
class IfdefHighlighter(sublime_plugin.EventListener):
'''
Scans whole text and searches for matching
#ifdef-#else-#endif. Remembers their position.
Then, when the cursor is on #ifdef/#else/#endif,
all corresponding keywords are highlighted.
Supports simple error checking
'''
def __init__(self, *args, **kw):
super().__init__(*args,**kw)
self._groups = dict()
self._regions = []
def _rescan(self, view):
if not view.match_selector(0, "source.c | source.c++ | source.objc | source.objc++"):
return
self._groups.clear()
self._regions.clear()
stack = []
errors = []
regions = view.find_by_selector("meta.preprocessor keyword.control.import")
for r in regions:
s = view.substr(r)
if s.startswith('# '):
s = s.replace(' ', '')
if s.startswith('#if'):
self._regions.append(r)
stack.append({(r.a, r.b)}) # Region is not hashable
elif s in ('#elif', '#else'):
if len(stack) > 0:
self._regions.append(r)
stack[-1].add((r.a, r.b))
else:
errors.append(r)
elif s == '#endif':
if len(stack) > 0:
self._regions.append(r)
pset = stack.pop()
pset.add((r.a, r.b))
rlist = []
for a, b in pset:
rlist.append(sublime.Region(a, b))
for p in pset:
self._groups[p] = rlist
else:
errors.append(r)
for p in stack:
errors.extend(sublime.Region(t[0], t[1]) for t in p)
view.erase_regions('ifdeferror')
if errors:
view.add_regions('ifdeferror', errors, 'invalid', '', 0)
def on_modified(self, view):
self._rescan(view)
def on_activated(self, view):
self._rescan(view)
def on_selection_modified(self, view):
# NOTE: do I need to erase on each update?
view.erase_regions('ifdef')
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)
break