Skip to content

Commit 1262a21

Browse files
committed
[Vim Focus] Change python highlighter and each style to modify a dict of highlights instead of individual highlights
1 parent 8c5438a commit 1262a21

File tree

6 files changed

+90
-51
lines changed

6 files changed

+90
-51
lines changed

lib/vimade/highlighter.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,14 @@ def next(to_process):
287287

288288
result = []
289289
attrs_eval = []
290+
attrs_eval_ids = []
290291
base_keys = []
291292
for id in to_process:
292293
cache_key = str(id) + base_key_suffix
293294
if not cache_key in M.base_id_cache:
294295
M.base_id_cache[cache_key] = {}
295296
base_keys.append(cache_key)
297+
attrs_eval_ids.append(id)
296298
attrs_eval.append(hi_string % id)
297299

298300
if len(attrs_eval):
@@ -301,33 +303,45 @@ def next(to_process):
301303
# this occurs b/c another window might redo the highlight due to clear_color on the current window.
302304
# There is a performance gain but it's not worth maintaining the logic.
303305
attrs = IPC.eval_and_return('[' + ','.join(attrs_eval) + ']')
304-
attrs = _process_hl_results(attrs, to_process)
306+
attrs = _process_hl_results(attrs, attrs_eval_ids)
305307
for i, cache_key in enumerate(base_keys):
306308
base = M.base_id_cache.get(cache_key)
307309
base_hi = defaultHi(attrs[i], fg_wincolorhl)
308310
base['hi'] = base_hi
309311
base['base_key'] = str(base_hi['ctermfg']) + ',' + str(base_hi['ctermbg']) + ',' + str(base_hi['fg']) + ',' + str(base_hi['bg']) + ',' + str(base_hi['sp'])
310312
to_create = []
311313
style = win['style']
314+
highlights = {}
315+
hi_target = TYPE.shallow_copy(target)
316+
312317
for id in to_process:
313318
base = M.base_id_cache[str(id) + base_key_suffix]
314319
base_hi = base['hi']
320+
name = name_override if name_override else base_hi['name']
321+
if name in highlights:
322+
continue
315323
# selective copy (we cache other keys that should not be exposed)
316324
# copy the target for the style run
317325
hi_attrs = {
318-
'name': name_override if name_override else base_hi['name'], # base name
326+
'name': name,
319327
'ctermfg': base_hi['ctermfg'],
320328
'ctermbg': base_hi['ctermbg'],
321329
'fg': base_hi['fg'],
322330
'bg': base_hi['bg'],
323331
'sp': base_hi['sp'] }
324-
hi_target = TYPE.shallow_copy(target)
325332
### below logic prevents basebg from applying to VimadeWC (otherwise background is altered for Vim)
326-
if hi_attrs['name'] == 'Normal':
333+
if name == 'Normal':
327334
hi_attrs['ctermbg'] = None
328335
hi_attrs['bg'] = None
329-
for s in style:
330-
s.modify(hi_attrs, hi_target)
336+
highlights[name] = hi_attrs
337+
338+
for s in style:
339+
s.modify(highlights, hi_target)
340+
341+
for id in to_process:
342+
base = M.base_id_cache[str(id) + base_key_suffix]
343+
name = name_override if name_override else base['hi']['name']
344+
hi_attrs = highlights[name]
331345
cache_key = win['hi_key'] + ':' + base['base_key'] + ':' + str(hi_attrs['ctermfg']) + ',' + str(hi_attrs['ctermbg']) + ',' + str(hi_attrs['fg']) + ',' + str(hi_attrs['bg']) + ',' + str(hi_attrs['sp'])
332346
vimade_hi = M.vimade_id_cache.get(cache_key)
333347

lib/vimade/style/exclude.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,23 @@ def before(self, win, state):
5151
def key(self, win, state):
5252
if self.condition == False:
5353
return ''
54-
style_key = ','.join([s.key(win, state) for j, s in enumerate(self.style)])
55-
if len(style_key) == 0:
54+
style_keys = [s.key(win, state) for j, s in enumerate(self.style)]
55+
style_key = ','.join([s for s in style_keys if s])
56+
if not style_key:
5657
return ''
5758
return 'E-' + ','.join(self.exclude.values()) + '(' \
5859
+ style_key + ')'
59-
def modify(self, hl, to_hl):
60-
if self.condition == False or (hl['name'] in self.exclude):
60+
def modify(self, highlights, to_hl):
61+
if self.condition == False:
6162
return
6263
else:
64+
excluded_for_children = {}
65+
for name in self.exclude:
66+
if name in highlights:
67+
excluded_for_children[name] = highlights[name]
68+
del highlights[name]
6369
for s in self.style:
64-
s.modify(hl, to_hl)
70+
s.modify(highlights, to_hl)
71+
for (name, value) in excluded_for_children.items():
72+
highlights[name] = value
6573
self.attach = __Exclude

lib/vimade/style/fade.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,26 @@ def key(self, win, state):
3636
if self.condition == False:
3737
return ''
3838
return 'F-' + str(self.fade)
39-
def modify(self, hl, to_hl):
39+
def modify(self, highlights, to_hl):
4040
if self.condition == False:
4141
return
4242
fade = self.fade
43-
fg = hl['fg']
44-
bg = hl['bg']
45-
sp = hl['sp']
46-
ctermfg = hl['ctermfg']
47-
ctermbg = hl['ctermbg']
48-
if fg != None:
49-
hl['fg'] = COLOR_UTIL.interpolate24b(fg, to_hl['bg'], fade)
50-
if bg != None:
51-
hl['bg'] = COLOR_UTIL.interpolate24b(bg, to_hl['bg'], fade)
52-
if sp != None:
53-
hl['sp'] = COLOR_UTIL.interpolate24b(sp, to_hl['bg'], fade)
54-
if ctermfg != None:
55-
hl['ctermfg'] = COLOR_UTIL.interpolate256(ctermfg, to_hl['ctermbg'], fade)
56-
if ctermbg != None:
57-
hl['ctermbg'] = COLOR_UTIL.interpolate256(ctermbg, to_hl['ctermbg'], fade)
43+
for hl in highlights.values():
44+
fg = hl['fg']
45+
bg = hl['bg']
46+
sp = hl['sp']
47+
ctermfg = hl['ctermfg']
48+
ctermbg = hl['ctermbg']
49+
if fg != None:
50+
hl['fg'] = COLOR_UTIL.interpolate24b(fg, to_hl['bg'], fade)
51+
if bg != None:
52+
hl['bg'] = COLOR_UTIL.interpolate24b(bg, to_hl['bg'], fade)
53+
if sp != None:
54+
hl['sp'] = COLOR_UTIL.interpolate24b(sp, to_hl['bg'], fade)
55+
if ctermfg != None:
56+
hl['ctermfg'] = COLOR_UTIL.interpolate256(ctermfg, to_hl['ctermbg'], fade)
57+
if ctermbg != None:
58+
hl['ctermbg'] = COLOR_UTIL.interpolate256(ctermbg, to_hl['ctermbg'], fade)
5859
return
5960
parent.attach = __Fade
6061
def value(parent, replacement = None):

lib/vimade/style/include.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,21 @@ def before(self, win, state):
5151
def key(self, win, state):
5252
if self.condition == False:
5353
return ''
54-
style_key = ','.join([s.key(win, state) for j, s in enumerate(self.style)])
55-
if len(style_key) == 0:
54+
style_keys = [s.key(win, state) for j, s in enumerate(self.style)]
55+
style_key = ','.join([s for s in style_keys if s])
56+
if not style_key:
5657
return ''
5758
return 'I-' + ','.join(self.include.values()) + '(' \
5859
+ style_key + ')'
59-
def modify(self, hl, to_hl):
60+
def modify(self, highlights, to_hl):
6061
if self.condition == False:
6162
return
62-
# we don't need to foce set the bg,ctermbg here for Vim as targetting works slightly differently
63-
if hl['name'] in self.include:
64-
for s in self.style:
65-
s.modify(hl, to_hl)
63+
highlights_for_children = {}
64+
for name in self.include:
65+
if name in highlights:
66+
highlights_for_children[name] = highlights[name]
67+
for s in self.style:
68+
s.modify(highlights_for_children, to_hl)
69+
for (name, value) in highlights_for_children.items():
70+
highlights[name] = value
6671
self.attach = __Include

lib/vimade/style/invert.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,31 @@ def key(self, win, state):
4646
return ''
4747
# TODO shrink
4848
return 'INV-' + str(self.invert.get('fg',0)) + '-' + str(self.invert.get('bg',0)) + '-' + str(self.invert.get('sp',0))
49-
def modify(self, hl, to_hl):
49+
def modify(self, highlights, to_hl):
5050
if self.condition == False or not self.invert:
5151
return
5252
invert = self.invert
53-
for hi in (hl, to_hl):
53+
for key in ('fg', 'bg', 'sp'):
54+
color = to_hl.get(key)
55+
if color != None:
56+
to_hl[key] = COLOR_UTIL.interpolate24b(color, 0XFFFFFF - color, 1 - invert[key])
57+
for (key, i_key) in (('ctermfg', 'fg'), ('ctermbg', 'bg')):
58+
color = to_hl.get(key)
59+
if color != None:
60+
color = COLOR_UTIL.toRgb(color, True)
61+
target = [255 - color[0], 255 - color[1], 255 - color[2]]
62+
to_hl[key] = COLOR_UTIL.interpolate256(color, target, 1 - invert[i_key])
63+
for hl in highlights.values():
5464
for key in ('fg', 'bg', 'sp'):
55-
color = hi.get(key)
65+
color = hl.get(key)
5666
if color != None:
57-
hi[key] = COLOR_UTIL.interpolate24b(color, 0XFFFFFF - color, 1 - invert[key])
67+
hl[key] = COLOR_UTIL.interpolate24b(color, 0XFFFFFF - color, 1 - invert[key])
5868
for (key, i_key) in (('ctermfg', 'fg'), ('ctermbg', 'bg')):
59-
color = hi.get(key)
69+
color = hl.get(key)
6070
if color != None:
6171
color = COLOR_UTIL.toRgb(color, True)
6272
target = [255 - color[0], 255 - color[1], 255 - color[2]]
63-
hi[key] = COLOR_UTIL.interpolate256(color, target, 1 - invert[i_key])
73+
hl[key] = COLOR_UTIL.interpolate256(color, target, 1 - invert[i_key])
6474
parent.attach = __Invert
6575
def value(parent, replacement = None):
6676
if replacement != None:

lib/vimade/style/tint.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def key(self, win, state):
9393
+ str(to_hl['fg'] != None and (str(to_hl['fg'] or '') + ',' + (str(to_hl['ctermfg'][0])+'-'+str(to_hl['ctermfg'][1])+'-'+str(to_hl['ctermfg'][2])) + ',' + str(to_hl['fg_intensity'])) or '') + '|' \
9494
+ (to_hl['bg'] != None and (str(to_hl['bg'] or '') + ',' + (str(to_hl['ctermbg'][0])+'-'+str(to_hl['ctermbg'][1])+'-'+str(to_hl['ctermbg'][2])) + ',' + str(to_hl['bg_intensity'])) or '') + '|'
9595
+ (to_hl['sp'] != None and (str(to_hl['sp'] or '') + str(to_hl['sp_intensity'])) or '')
96-
def modify(self, hl, target):
96+
def modify(self, highlights, target):
9797
if self.condition == False:
9898
return
9999
to_hl = self.to_hl
@@ -103,16 +103,17 @@ def modify(self, hl, target):
103103
target['bg'] = COLOR_UTIL.interpolate24b(target['bg'], to_hl['bg'], to_hl['bg_intensity'])
104104
if target['ctermbg'] != None and to_hl['ctermbg'] != None:
105105
target['ctermbg'] = COLOR_UTIL.interpolate256(target['ctermbg'], to_hl['ctermbg'], to_hl['bg_intensity'])
106-
if hl['fg'] != None and to_hl['fg'] !=None:
107-
hl['fg'] = COLOR_UTIL.interpolate24b(hl['fg'], to_hl['fg'], to_hl['fg_intensity'])
108-
if hl['bg'] != None and to_hl['bg'] != None:
109-
hl['bg'] = COLOR_UTIL.interpolate24b(hl['bg'], to_hl['bg'], to_hl['bg_intensity'])
110-
if hl['sp'] != None and to_hl['sp'] != None:
111-
hl['sp'] = COLOR_UTIL.interpolate24b(hl['sp'], to_hl['sp'], to_hl['sp_intensity'])
112-
if hl['ctermfg'] != None and to_hl['ctermfg'] != None:
113-
hl['ctermfg'] = COLOR_UTIL.interpolate256(hl['ctermfg'], to_hl['ctermfg'], to_hl['fg_intensity'])
114-
if hl['ctermbg'] != None and to_hl['ctermbg'] != None:
115-
hl['ctermbg'] = COLOR_UTIL.interpolate256(hl['ctermbg'], to_hl['ctermbg'], to_hl['bg_intensity'])
106+
for hl in highlights.values():
107+
if hl['fg'] != None and to_hl['fg'] !=None:
108+
hl['fg'] = COLOR_UTIL.interpolate24b(hl['fg'], to_hl['fg'], to_hl['fg_intensity'])
109+
if hl['bg'] != None and to_hl['bg'] != None:
110+
hl['bg'] = COLOR_UTIL.interpolate24b(hl['bg'], to_hl['bg'], to_hl['bg_intensity'])
111+
if hl['sp'] != None and to_hl['sp'] != None:
112+
hl['sp'] = COLOR_UTIL.interpolate24b(hl['sp'], to_hl['sp'], to_hl['sp_intensity'])
113+
if hl['ctermfg'] != None and to_hl['ctermfg'] != None:
114+
hl['ctermfg'] = COLOR_UTIL.interpolate256(hl['ctermfg'], to_hl['ctermfg'], to_hl['fg_intensity'])
115+
if hl['ctermbg'] != None and to_hl['ctermbg'] != None:
116+
hl['ctermbg'] = COLOR_UTIL.interpolate256(hl['ctermbg'], to_hl['ctermbg'], to_hl['bg_intensity'])
116117
return
117118
parent.attach = __Tint
118119
def value(parent, replacement = None):

0 commit comments

Comments
 (0)