/
color_scheme.py
49 lines (33 loc) · 1.55 KB
/
color_scheme.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
import plistlib
from sublime import load_resource
from sublime import score_selector
def load_color_scheme_resource(color_scheme):
return plistlib.readPlistFromBytes(bytes(load_resource(color_scheme), 'UTF-8'))
class ViewStyle():
def __init__(self, view):
self.view = view
self.scope_style_cache = {}
color_scheme = self.view.settings().get('color_scheme')
self.plist = load_color_scheme_resource(color_scheme)
self.default_styles = {}
for plist_settings_dict in self.plist['settings']:
if 'scope' not in plist_settings_dict:
self.default_styles.update(plist_settings_dict['settings'])
def at_point(self, point):
# scope_name() needs to striped due to a bug in ST:
# See https://github.com/SublimeTextIssues/Core/issues/657.
scope = self.view.scope_name(point).strip()
if scope in self.scope_style_cache:
return self.scope_style_cache[scope]
style = self.default_styles.copy()
scored_styles = []
for color_scheme_definition in self.plist['settings']:
if 'scope' in color_scheme_definition:
score = score_selector(scope, color_scheme_definition['scope'])
if score:
color_scheme_definition.update({'score': score})
scored_styles.append(color_scheme_definition)
for s in sorted(scored_styles, key=lambda k: k['score']):
style.update(s['settings'])
self.scope_style_cache[scope] = style
return style