-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnippets.py
186 lines (142 loc) · 5.35 KB
/
snippets.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Keypirinha launcher (keypirinha.com)
import keypirinha as kp
import keypirinha_util as kpu
import keypirinha_net as kpnet
def get_path_as_list(path):
return path.split("/")
def get_path_from_section(section):
return section[9:]
class Snippets(kp.Plugin):
"""
Quickly copy user defined snippets to the clipboard.
"""
SNIPPETS = "snippets"
ITEMCAT_RESULT = kp.ItemCategory.USER_BASE + 1
structure = {}
def __init__(self):
super().__init__()
def on_start(self):
self._read_config()
def on_catalog(self):
catalog = []
for root_node in self.structure.keys():
if root_node != self.SNIPPETS:
catalog.append(
self.create_item(
category=kp.ItemCategory.REFERENCE,
label=root_node,
short_desc=root_node + " " + self.SNIPPETS,
target=root_node,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.NOARGS,
)
)
else:
catalog += self.structure[self.SNIPPETS][self.SNIPPETS]
self.set_catalog(catalog)
def on_suggest(self, user_input, items_chain):
if not items_chain:
return
if items_chain and (
items_chain[0].category() != kp.ItemCategory.REFERENCE
or items_chain[0].target() not in self.structure.keys()
):
return
path = [node.label() for node in items_chain]
structure_ref = self.get_node_from_structure(path)
nodes = structure_ref.keys()
suggestions = []
for node in nodes:
if node != self.SNIPPETS:
suggestions.append(
self.create_item(
category=kp.ItemCategory.REFERENCE,
label=node,
short_desc=node + " " + self.SNIPPETS,
target=node,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.NOARGS,
)
)
if self.SNIPPETS in nodes:
suggestions += structure_ref[self.SNIPPETS]
user_input = user_input.strip()
self.set_suggestions(
suggestions,
kp.Match.ANY if not user_input else kp.Match.FUZZY,
kp.Sort.NONE if not user_input else kp.Sort.SCORE_DESC,
)
def on_execute(self, item, action):
if item and item.category() == self.ITEMCAT_RESULT:
kpu.set_clipboard(item.target())
def on_activated(self):
pass
def on_deactivated(self):
pass
def on_events(self, flags):
if flags & kp.Events.PACKCONFIG:
self._read_config()
self.on_catalog()
def _read_config(self):
settings = self.load_settings()
sections = [
section
for section in settings.sections()
if section.lower().startswith(self.SNIPPETS)
]
data = {}
for section in sections:
path = get_path_from_section(section)
data[path] = {}
snippets = settings.keys(section)
for snippet_key in snippets:
snippet_string = settings.get(snippet_key, section=section)
if not len(snippet_string):
self.warn(
'Snippet "{}" does not have "string" value (or is empty). Ignored.'.format(
snippet_key
)
)
continue
data[path][snippet_key] = snippet_string
self.generate_folder_structure(data)
def get_node_from_structure(self, path):
structure_ref = self.structure
for node in path:
structure_ref = structure_ref[node]
return structure_ref
def set_node_in_structure(self, path, value):
structure_ref = self.structure
for node in path[:-1]:
structure_ref = structure_ref.setdefault(node, {})
structure_ref[path[-1]] = value
def generate_folder_structure(self, data):
self.structure.clear()
self.add_path_to_structure(get_path_as_list(path) for path in data.keys())
for path, snippets in data.items():
path = get_path_as_list(path)
self.set_node_in_structure(
path, {self.SNIPPETS: self.create_result_set(snippets)}
)
def add_path_to_structure(self, paths):
for path in paths:
structure_ref = self.structure
for node in path:
if node not in structure_ref:
structure_ref[node] = {}
structure_ref = structure_ref[node]
def create_result_set(self, snippets):
results = []
for label, snippet in snippets.items():
short_description = snippet.replace("\n", "↵")
results.append(
self.create_item(
category=self.ITEMCAT_RESULT,
label=label,
short_desc=short_description,
target=snippet,
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.IGNORE,
)
)
return results