forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsdoc.py
58 lines (49 loc) · 1.43 KB
/
gsdoc.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
import gscommon as gs, margo
import sublime, sublime_plugin
DOMAIN = 'GsDoc'
class GsDocCommand(sublime_plugin.TextCommand):
def is_enabled(self):
return gs.is_go_source_view(self.view)
def show_output(self, s):
gs.show_output(DOMAIN+'-output', s, False, 'Packages/Go/Go.tmLanguage')
def run(self, _, mode=''):
view = self.view
if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint']):
return
pt = view.sel()[0].begin()
src = view.substr(sublime.Region(0, view.size()))
docs, err = margo.doc(view.file_name(), src, pt)
if err:
self.show_output('// Error: %s' % err)
elif docs:
if mode == "goto":
fn = ''
flags = 0
for d in docs:
fn = d.get('fn', '')
row = d.get('row', 0)
col = d.get('col', 0)
if row > 0:
flags = sublime.ENCODED_POSITION
fn = '%s:%d:%d' % (fn, row+1, col+1)
if fn:
view.window().open_file(fn, flags)
else:
self.show_output("%s: cannot find definition" % DOMAIN)
elif mode == "hint":
s = []
for d in docs:
decl = d.get('decl', '').strip()
doc = d.get('doc', '').strip()
src = d.get('src', '').strip()
if doc:
doc = '// %s' % doc.replace('\n', '\n// ')
if decl:
doc = '%s\n\n%s' % (decl, doc)
if src:
doc = src
s.append(doc.strip() or ('// %s no docs' % DOMAIN))
s = '\n\n\n\n'.join(s)
self.show_output(s)
else:
self.show_output("%s: no docs found" % DOMAIN)