forked from MarioRicalde/livecss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
101 lines (78 loc) · 2.99 KB
/
state.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
90
91
92
93
94
95
96
97
98
99
100
101
"""
livecss.state
~~~~~~~~~
This module implements state object and function to associate state with view
"""
from .theme import is_colorized
from os.path import exists
class State(object):
def __init__(self, view):
self.count = 0
self.theme_path = ""
self.saved_colors = []
self.saved_regions = []
self.view = view
@property
def is_dirty(self):
"""Indicates if state was changed"""
highlighted_regions = get_highlighted_regions(self.view, self.count)
# check if regions have changed
if highlighted_regions == self.regions or (not self.regions and not highlighted_regions):
is_dirty = False
else:
is_dirty = True
# check if lengths of regions changed
if not is_dirty:
for reg_pair in zip(self.saved_regions, self.regions):
if abs(reg_pair[0].a - reg_pair[0].b) != abs(reg_pair[1].a - reg_pair[1].b):
is_dirty = True
# save current regions
self.saved_regions = self.regions
return is_dirty
@property
def need_generate_theme_file(self):
"""Indicates if new color definition appeared in current file"""
# check if there are new colors in view
if not self.saved_colors or set(self.colors) - set(self.saved_colors):
need_generate = True
else:
need_generate = False
# save current colors
self.saved_colors = self.colors
# check if theme_path is "valid"
return need_generate or not is_colorized(self.theme_path) or not exists(self.theme_path)
def get_highlighted_regions(view, last_highlighted_region):
"""Return currently highlighted regions for this file."""
if not last_highlighted_region:
return
regions = []
for i in range(last_highlighted_region):
region = view.get_regions('css_color_%d' % i)
if region:
regions.append(region[0])
return regions
# dict to hold states
states = dict()
class state_for(object):
"""Store and retrieve state"""
def __init__(self, view):
"""Create or retrieve state for given view
self.focused = unique attribute for all states
"""
if view.buffer_id() not in states:
states[view.buffer_id()] = State(view)
self.view = view
def __getattribute__(self, attr):
if attr == 'focused':
bid, is_focused = states['focused']
if bid == self.view.buffer_id():
return True
elif attr == 'view' or attr.startswith('__'):
return object.__getattribute__(self, attr)
return getattr(states[self.view.buffer_id()], attr)
def __setattr__(self, attr, value):
object.__setattr__(self, attr, value)
if attr == 'focused':
states['focused'] = [self.view.buffer_id(), value]
elif attr != 'view' and not attr.startswith('__'):
setattr(states[self.view.buffer_id()], attr, value)