forked from alvarolm/GoGuru
/
goGuru.py
370 lines (291 loc) · 12.2 KB
/
goGuru.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Copyright (c) 2014 Jesse Meek <https://github.com/waigani>
# Copyright (c) 2016 Alvaro Leiva <https://github.com/alvarolm>
# This program is Free Software see LICENSE file for details.
"""
GoGuru is a Go guru plugin for Sublime Text 3.
It depends on the guru tool being installed:
go get golang.org/x/tools/cmd/guru
"""
import sublime, sublime_plugin, subprocess, time, re, os, subprocess, sys
DEBUG = False
VERSION = ''
PluginPath = ''
use_golangconfig = False
def log(*msg):
print("GoGuru:", msg[0:])
def debug(*msg):
if DEBUG:
print("GoGuru [DEBUG]:", msg[0:])
def error(*msg):
print("GoGuru [ERROR]:", msg[0:])
def plugin_loaded():
global DEBUG
global VERSION
global PluginPath
global use_golangconfig
DEBUG = get_setting("debug", False)
PluginPath = sublime.packages_path()+'/GoGuru/'
use_golangconfig = get_setting("use_golangconfig", False)
# load shellenv
def load_shellenv():
sys.path.append(PluginPath+"/dep/")
global shellenv
import shellenv
# try golangconfig
if use_golangconfig:
try:
global golangconfig
import golangconfig
except:
error("couldn't import golangconfig:", sys.exc_info()[0])
log("using shellenv instead of golangconfig")
use_golangconfig = False
load_shellenv()
else:
load_shellenv()
log("debug:", DEBUG)
log("use_golangconfig", use_golangconfig)
# keep track of the version if possible
try:
p = subprocess.Popen(["git", "describe", "master", "--tags"], stdout=subprocess.PIPE, cwd=PluginPath)
GITVERSION = p.communicate()[0].decode("utf-8").rstrip()
if p.returncode != 0:
debug("git return code", p.returncode)
f = open(PluginPath+'VERSION', 'w')
f.write(GITVERSION)
f.close()
except:
debug("couldn't get git tag:", sys.exc_info()[0])
# read version
f = open(PluginPath+'VERSION', 'r')
VERSION = f.read().rstrip()
f.close()
log("version:", VERSION)
class GoGuruCommand(sublime_plugin.TextCommand):
def __init__(self, view):
self.view = view
self.mode = 'None'
def run(self, edit, mode=None):
region = self.view.sel()[0]
text = self.view.substr(sublime.Region(0, region.end()))
cb_map = self.get_map(text)
byte_end = cb_map[sorted(cb_map.keys())[-1]]
byte_begin = None
if not region.empty():
byte_begin = cb_map[region.begin()-1]
if mode:
self.write_running(mode)
self.guru(byte_end, begin_offset=byte_begin, mode=mode, callback=self.guru_complete)
return
# Get the guru mode from the user.
modes = ["callees","callers","callstack","definition","describe","freevars","implements","peers","pointsto","referrers","what","whicherrs"]
descriptions = [
"callees show possible targets of selected function call",
"callers show possible callers of selected function",
"callstack show path from callgraph root to selected function",
"definition show declaration of selected identifier",
"describe describe selected syntax: definition, methods, etc",
"freevars show free variables of selection",
"implements show 'implements' relation for selected type or method",
"peers show send/receive corresponding to selected channel op",
"pointsto show variables the selected pointer may point to",
"referrers show all refs to entity denoted by selected identifier",
"what show basic information about the selected syntax node",
"whicherrs show possible values of the selected error variable"]
# Call guru cmd with the given mode.
def on_done(i):
if i >= 0 :
self.write_running(modes[i])
self.guru(byte_end, begin_offset=byte_begin, mode=modes[i], callback=self.guru_complete)
self.view.window().show_quick_panel(descriptions, on_done, sublime.MONOSPACE_FONT)
def guru_complete(self, out, err):
self.write_out(out, err)
def write_running(self, mode):
""" Write the "Running..." header to a new file and focus it to get results
"""
# remember mode for future actions
self.mode = mode
window = self.view.window()
view = get_output_view(window)
# Run a new command to use the edit object for this view.
view.run_command('go_guru_write_running', {'mode': mode})
if get_setting("output", "buffer") == "output_panel":
window.run_command('show_panel', {'panel': "output." + view.name() })
else:
window.focus_view(view)
def write_out(self, result, err):
""" Write the guru output to a new file.
"""
window = self.view.window()
view = get_output_view(window)
# Run a new command to use the edit object for this view.
view.run_command('go_guru_write_results', {
'result': result,
'err': err})
if get_setting("output", "buffer") == "output_panel":
window.run_command('show_panel', {'panel': "output." + view.name() })
else:
window.focus_view(view)
# jump to definition if is set
if self.mode == 'definition':
if get_setting("jumpto_definition", False):
if result:
coordinates = result.split(':')[:3]
new_view = window.open_file(':'.join(coordinates), sublime.ENCODED_POSITION)
group, _ = window.get_view_index(new_view)
if group != -1:
window.focus_group(group)
def get_map(self, chars):
""" Generate a map of character offset to byte offset for the given string 'chars'.
"""
byte_offset = 0
cb_map = {}
for char_offset, char in enumerate(chars):
cb_map[char_offset] = byte_offset
byte_offset += len(char.encode('utf-8'))
if char == '\n' and self.view.line_endings() == "Windows":
byte_offset += 1
return cb_map
def guru(self, end_offset, begin_offset=None, mode="describe", callback=None):
""" Builds the guru shell command and calls it, returning it's output as a string.
"""
pos = "#" + str(end_offset)
if begin_offset is not None:
pos = "#%i,#%i" %(begin_offset, end_offset)
file_path = self.view.file_name()
# golang config or shellenv ?
cmd_env = ''
if use_golangconfig:
try:
toolpath, cmd_env = golangconfig.subprocess_info('guru', ['GOPATH', 'PATH'], view=self.view)
toolpath = os.path.realpath(toolpath)
except:
error("golangconfig:", sys.exc_info())
return
else:
toolpath = 'guru'
cmd_env = shellenv.get_env(for_subprocess=True)[1]
cmd_env.update(get_setting("env", {}))
debug("env", cmd_env)
guru_scope = ",".join(get_setting("guru_scope", ""))
# add local package to guru scope
if get_setting("use_current_package", True) :
current_file_path = os.path.realpath(os.path.dirname(file_path))
GOPATH = os.path.realpath(cmd_env["GOPATH"])
GOPATH = os.path.join(GOPATH,"src")
local_package = os.path.relpath(current_file_path, GOPATH)
if sublime.platform() == 'windows':
local_package = local_package.replace('\\', '/')
debug("GOPATH", GOPATH)
debug("local_package", local_package)
guru_scope = guru_scope+','+local_package
guru_scope = guru_scope.strip()
debug("guru_scope", guru_scope)
if len(guru_scope) > 0:
guru_scope = "-scope "+guru_scope
guru_json = ""
if get_setting("guru_json", False):
guru_json = "-json"
# Build guru cmd.
cmd = "%(toolpath)s %(scope)s %(guru_json)s %(mode)s %(file_path)s:%(pos)s" % {
"toolpath": toolpath,
"file_path": file_path,
"pos": pos,
"guru_json": guru_json,
"mode": mode,
"scope": guru_scope}
debug("cmd", cmd)
sublime.set_timeout_async(lambda: self.runInThread(cmd, callback, cmd_env), 0)
def runInThread(self, cmd, callback, env):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True, env=env)
out, err = proc.communicate()
callback(out.decode('utf-8'), err.decode('utf-8'))
class GoGuruWriteResultsCommand(sublime_plugin.TextCommand):
""" Writes the guru output to the current view.
"""
def run(self, edit, result, err):
view = self.view
view.insert(edit, view.size(), "\n")
if result:
view.insert(edit, view.size(), result)
if err:
view.insert(edit, view.size(), err)
view.insert(edit, view.size(), "\n\n\n")
class GoGuruWriteRunningCommand(sublime_plugin.TextCommand):
""" Writes the guru output to the current view.
"""
def run(self, edit, mode):
view = self.view
content = "Running guru " + mode + " command...\n"
view.set_viewport_position(view.text_to_layout(view.size() - 1))
view.insert(edit, view.size(), content)
class GoGuruShowResultsCommand(sublime_plugin.TextCommand):
def run(self, edit):
if get_setting("output", "buffer") == "output_panel":
self.view.window().run_command('show_panel', {'panel': "output.Oracle Output" })
else:
output_view = get_output_view(self.view.window())
self.view.window().focus_view(output_view)
class GoGuruOpenResultCommand(sublime_plugin.EventListener):
def on_selection_modified(self, view):
if view.name() == "Oracle Output":
if len(view.sel()) != 1:
return
if view.sel()[0].size() == 0:
return
lines = view.lines(view.sel()[0])
if len(lines) != 1:
return
line = view.full_line(lines[0])
text = view.substr(line)
format = get_setting("guru_format")
# "filename:line:col" pattern for json
m = re.search("\"([^\"]+):([0-9]+):([0-9]+)\"", text)
# >filename:line:col< pattern for xml
if m == None:
m = re.search(">([^<]+):([0-9]+):([0-9]+)<", text)
# filename:line.col-line.col: pattern for plain
if m == None:
m = re.search("^(.+\.go):([0-9]+).([0-9]+)[-: ]", text)
if m:
w = view.window()
new_view = w.open_file(m.group(1) + ':' + m.group(2) + ':' + m.group(3), sublime.ENCODED_POSITION)
group, index = w.get_view_index(new_view)
if group != -1:
w.focus_group(group)
def get_output_view(window):
view = None
buff_name = 'Oracle Output'
if get_setting("output", "buffer") == "output_panel":
view = window.create_output_panel(buff_name)
else:
# If the output file is already open, use that.
for v in window.views():
if v.name() == buff_name:
view = v
break
# Otherwise, create a new one.
if view is None:
view = window.new_file()
view.set_name(buff_name)
view.set_scratch(True)
view_settings = view.settings()
view_settings.set('line_numbers', False)
view.set_syntax_file('Packages/GoGuru/GoGuruResults.tmLanguage')
return view
def get_setting(key, default=None):
""" Returns the setting in the following hierarchy: project setting, user setting,
default setting. If none are set the 'default' value passed in is returned.
"""
val = None
try:
val = sublime.active_window().active_view().settings().get('GoGuru', {}).get(key)
except AttributeError:
pass
if not val:
val = sublime.load_settings("User.sublime-settings").get(key)
if not val:
val = sublime.load_settings("Default.sublime-settings").get(key)
if not val:
val = default
return val