-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
gspalette.py
284 lines (241 loc) · 7.03 KB
/
gspalette.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from .gosubl import gs
from .gosubl import gspatch
from .gosubl import mg9
from os.path import dirname, basename, relpath
from . import gslint
import re
import sublime
import sublime_plugin
DOMAIN = 'GsPalette'
class Loc(object):
def __init__(self, fn, row, col=0):
self.fn = fn
self.row = row
self.col = col
class GsPaletteCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return bool(gs.active_valid_go_view(self.window))
def run(self, palette='auto', direct=False):
if not hasattr(self, 'items'):
self.items = []
self.bookmarks = []
self.last_activate_palette = ''
self.requires_margo = ['declarations', 'imports']
self.palettes = {
'declarations': self.palette_declarations,
'imports': self.palette_imports,
'errors': self.palette_errors,
}
if palette == 'jump_back':
self.jump_back()
elif palette == 'jump_to_imports':
self.jump_to_imports()
else:
self.show_palette(palette, direct)
def show_palette(self, palette, direct=False):
view = gs.active_valid_go_view(self.window)
if not view:
return
palette = palette.lower().strip()
if palette == 'auto':
palette = self.last_activate_palette
elif palette == 'main':
palette = ''
pcb = None
if palette:
pcb = self.palettes.get(palette)
if pcb:
self.last_activate_palette = palette
else:
gs.notice(DOMAIN, 'Invalid palette `%s`' % palette)
palette = ''
if not direct and len(self.bookmarks) > 0:
loc = self.bookmarks[-1]
line = 'line %d' % (loc.row + 1)
if view.file_name() == loc.fn:
fn = ''
else:
fn = relpath(loc.fn, dirname(loc.fn))
if fn.startswith('..'):
fn = loc.fn
fn = '%s ' % fn
self.add_item(u'\u2190 Go Back (%s%s)' % (fn, line), self.jump_back, None)
if not direct and palette:
self.add_item(u'@%s \u21B5' % palette.title(), self.show_palette, 'main')
li1 = len(self.items)
if pcb:
pcb(view, direct)
if not direct:
for k in sorted(self.palettes.keys()):
if k:
if k != palette:
ttl = '@' + k.title()
if k == 'errors':
fr = gslint.ref(view.file_name())
if not fr or len(fr.reports) == 0:
continue
ttl = '%s (%d)' % (ttl, len(fr.reports))
itm = ttl
self.add_item(itm, self.show_palette, k)
def do_show_panel(self):
# todo cleanup this file and get rid of the old gspalette
items = []
actions = {}
for tup in self.items:
item, action, args = tup
actions[len(items)] = (action, args)
items.append(item)
self.items = []
def on_done(i, win):
action, args = actions.get(i, (None, None))
if i >= 0 and action:
action(args)
gs.show_quick_panel(items, on_done)
def add_item(self, item, action=None, args=None):
self.items.append((item, action, args))
def log_bookmark(self, view, loc):
bks = self.bookmarks
if len(bks) == 0 or (bks[-1].row != loc.row and bks[-1].fn != view.file_name()):
bks.append(loc)
def goto(self, loc):
gs.focus(loc.fn, loc.row, loc.col)
def jump_to_imports(self):
view = gs.active_valid_go_view()
if not view:
return
last_import = gs.attr('last_import_path.%s' % gs.view_fn(view), '')
r = None
if last_import:
offset = len(last_import) + 2
last_import = re.escape(last_import)
pat = '(?s)import.*?(?:"%s"|`%s`)' % (last_import, last_import)
r = view.find(pat, 0)
if not r:
offset = 1
pat = '(?s)import.*?["`]'
r = view.find(pat, 0)
if not r:
gs.notice(DOMAIN, "cannot find import declarations")
return
pt = r.end() - offset
row, col = view.rowcol(pt)
loc = Loc(view.file_name(), row, col)
self.jump_to((view, loc))
def jump_back(self, _=None):
if len(self.bookmarks) > 0:
self.goto(self.bookmarks.pop())
def palette_errors(self, view, direct=False):
indent = '' if direct else ' '
reps = {}
fr = gslint.ref(view.file_name())
if fr:
reps = fr.reports.copy()
keys = sorted(reps.keys())
if keys:
for k in keys:
r = reps[k]
loc = Loc(view.file_name(), r.row, r.col)
m = []
m.append("%sline %d:" % (indent, r.row+1))
lc = 0
for ln in r.msg.split('\n'):
if ln:
lc += 1
if len(ln) > 50:
m.append('\t%d: %s -' % (lc, ln[:50]))
m.append('\t %s' % ln[50:])
else:
m.append('\t%d: %s' % (lc, ln))
self.add_item(m, self.jump_to, (view, loc))
else:
self.add_item(['', 'No errors to report'])
self.do_show_panel()
def palette_imports(self, view, direct=False):
indent = '' if direct else ' '
src = view.substr(sublime.Region(0, view.size()))
def f(im, err):
if err:
gs.notice(DOMAIN, err)
return
delete_imports = []
add_imports = []
paths = im.get('paths', {})
use_named = gs.setting('use_named_imports') is True
for path in paths:
skipAdd = False
for i in im.get('imports', []):
if i.get('path') == path:
skipAdd = True
name = i.get('name', '')
if not name:
name = basename(path)
if name == path:
delete_imports.append(('%sdelete: %s' % (indent, name), i))
else:
delete_imports.append(('%sdelete: %s ( %s )' % (indent, name, path), i))
if not skipAdd:
title = paths[path]
s = '%s%s' % (indent, path)
m = {
'path': path,
'add': True,
}
name = title.split()[0] if title else ''
if name and name != path and not path.endswith('/%s' % name):
s = '%s (%s)' % (s, name)
if use_named:
m['name'] = name
if '[vendored]' in title:
s = s + ' [vendored]'
add_imports.append((s, m))
for i in sorted(delete_imports):
self.add_item(i[0], self.toggle_import, (view, i[1]))
if len(delete_imports) > 0:
self.add_item(' ', self.show_palette, 'imports')
for i in sorted(add_imports):
self.add_item(i[0], self.toggle_import, (view, i[1]))
self.do_show_panel()
mg9.import_paths(view.file_name(), src, f)
def toggle_import(self, a):
view, decl = a
im, err = mg9.imports(
view.file_name(),
view.substr(sublime.Region(0, view.size())),
[decl]
)
if err:
gs.notice(DOMAIN, err)
else:
src = im.get('src', '')
line_ref = im.get('lineRef', 0)
r = view.full_line(view.text_point(max(0, line_ref-1), 0))
if not src or line_ref < 1 or not r:
return
view.run_command('gs_patch_imports', {
'pos': r.end(),
'content': src,
'added_path': (decl.get('path') if decl.get('add') else '')
})
def jump_to(self, a):
view, loc = a
row, col = gs.rowcol(view)
if loc.row != row:
self.log_bookmark(view, Loc(view.file_name(), row, col))
self.goto(loc)
def palette_declarations(self, view, direct=False):
def f(res, err):
if err:
gs.notify('GsDeclarations', err)
else:
decls = res.get('file_decls', [])
decls.sort(key=lambda v: v.get('row', 0))
added = 0
for i, v in enumerate(decls):
loc = Loc(v['fn'], v['row'], v['col'])
s = '%s %s' % (v['kind'], (v['repr'] or v['name']))
self.add_item(s, self.jump_to, (view, loc))
added += 1
if added < 1:
self.add_item(['', 'No declarations found'])
self.do_show_panel()
mg9.declarations(gs.view_fn(view), gs.view_src(view), '', f)