forked from ironcladlou/GoTools
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgotools_guru.py
75 lines (64 loc) · 2.86 KB
/
gotools_guru.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
import sublime
import sublime_plugin
import os
import golangconfig
from .gotools_util import Buffers
from .gotools_util import GoBuffers
from .gotools_util import Logger
from .gotools_util import ToolRunner
class GotoolsGuruCommand(sublime_plugin.TextCommand):
def is_enabled(self):
return GoBuffers.is_go_source(self.view)
def run(self, edit, command=None):
if not command:
Logger.log("command is required")
return
filename, row, col, offset, offset_end = Buffers.location_at_cursor(self.view)
if command == "freevars":
pos = filename+":#"+str(offset)+","+"#"+str(offset_end)
else:
pos = filename+":#"+str(offset)
# Build up a package scope contaning all packages the user might have
# configured.
package_scope = []
project_package = golangconfig.setting_value("project_package", view=self.view)[0]
if project_package:
if not golangconfig.setting_value("build_packages")[0]:
package_scope.append(project_package)
else:
for p in golangconfig.setting_value("build_packages", view=self.view)[0]:
package_scope.append(os.path.join(project_package, p))
# add local package to guru scope
if golangconfig.setting_value("guru_use_current_package")[0]:
current_file_path = os.path.realpath(os.path.dirname(self.view.file_name()))
toolpath, env = golangconfig.subprocess_info('guru', ['GOPATH', 'PATH'], view=self.view)
GOPATH = os.path.realpath(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('\\', '/')
Logger.status("GOPATH: "+GOPATH)
Logger.status("local_package: "+local_package)
package_scope.append(local_package)
sublime.active_window().run_command("hide_panel", {"panel": "output.gotools_guru"})
self.do_plain_guru(command, pos, package_scope)
def do_plain_guru(self, mode, pos, package_scope=[], regex="^(.*):(\d+):(\d+):(.*)$"):
Logger.status("running guru "+mode+"...")
args = []
if len(package_scope) > 0:
args = ["-scope", ",".join(package_scope)]
args = args + [mode, pos]
output, err, rc = ToolRunner.run(self.view, "guru", args, timeout=60)
Logger.log("guru "+mode+" output: " + output.rstrip())
if rc != 0:
print("GoTools: Guru error:\n%s" % err)
Logger.status("guru call failed (" + str(rc) +")")
return
Logger.status("guru "+mode+" finished")
panel = self.view.window().create_output_panel('gotools_guru')
panel.set_scratch(True)
panel.settings().set("result_file_regex", regex)
panel.run_command("select_all")
panel.run_command("right_delete")
panel.run_command('append', {'characters': output})
self.view.window().run_command("show_panel", {"panel": "output.gotools_guru"})