Skip to content

Commit f49e33a

Browse files
committed
first workable version
1 parent e2885a3 commit f49e33a

4 files changed

+158
-28
lines changed

Default (Linux).sublime-keymap

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
[
22
{
3-
"keys": ["ctrl+shift+g"],
4-
"command": "go_to_doc",
3+
"keys": ["ctrl+alt+e"],
4+
"command": "go_to_doc_exact" ,
5+
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
6+
},
7+
{
8+
"keys": ["ctrl+alt+l"],
9+
"command": "go_to_doc_lucky" ,
510
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
611
}
712
]

Default (OSX).sublime-keymap

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
[
22
{
3-
"keys": ["super+g"],
4-
"command": "go_to_doc" ,
3+
"keys": ["super+ctrl+e"],
4+
"command": "go_to_doc_exact" ,
5+
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
6+
},
7+
{
8+
"keys": ["super+ctrl+l"],
9+
"command": "go_to_doc_lucky" ,
510
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
611
}
712
]

Default (Windows).sublime-keymap

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
[
22
{
3-
"keys": ["ctrl+shift+g"],
4-
"command": "go_to_doc",
3+
"keys": ["ctrl+alt+e"],
4+
"command": "go_to_doc_exact" ,
5+
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
6+
},
7+
{
8+
"keys": ["ctrl+alt+l"],
9+
"command": "go_to_doc_lucky" ,
510
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
611
}
712
]

GoToDoc.py

+137-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,137 @@
1-
import sublime, sublime_plugin
2-
import webbrowser
3-
4-
# 1. go to only package document at http://golang.org/pkg/
5-
# 2. google search on golang.org and go to the first result like "I'm Feeling Lucky"
6-
# 3.
7-
8-
class GoToDocCommand(sublime_plugin.TextCommand):
9-
10-
def description(self):
11-
return 'A Sublime Text 2 plugin to quickly open go package document base on the selected text'
12-
13-
def get_full_pkg_name(self, edit):
14-
'''Try to find the full package name in go import statement'''
15-
pass
16-
17-
def run(self, edit):
18-
#self.view.insert(edit, 0, "Hello, World!")
19-
sels = self.view.sel()
20-
for sel in sels:
21-
#print(self.view.substr(sel))
22-
webbrowser.open_new_tab('http://golang.org/pkg/' + self.view.substr(sel))
1+
import sublime, sublime_plugin
2+
import webbrowser
3+
import re
4+
5+
GO_KEYWORDS = {
6+
'if':'If_statements', 'break':'Break_statements',
7+
'default':'Switch_statements', 'func':'Function_declarations',
8+
'interface':'Interface_types', 'select':'Select_statements',
9+
'case':'Switch_statements', 'defer':'Defer_statements',
10+
'go':'Go_statements', 'map':'Map_types',
11+
'struct':'Struct_types', 'chan':'Channel_types',
12+
'else':'If_statements', 'goto':'Goto_statements',
13+
'package':'Packages', 'switch':'Switch_statements',
14+
'const':'Constant_expressions', 'fallthrough':'Fallthrough_statements',
15+
'type':'Type_declarations', 'continue':'Continue_statements',
16+
'for':'For_statements', 'import':'Import_declarations',
17+
'return':'Return_statements', 'var':'Variable_declarations',
18+
'range':'RangeClause'
19+
}
20+
21+
GO_BUILTINS = ['append','close','delete','panic','recover','ComplexType','complex',
22+
'FloatType','imag','real','IntegerType','Type','make','new','Type1',
23+
'bool','byte','complex128','complex64','error','float32','float64',
24+
'int','cap','copy','len','int8','int16','int32','int64','rune','string',
25+
'uintptr','uint','uint8','uint16','uint32','uint64']
26+
27+
28+
def get_imports(src):
29+
''''build a list of imported packages from src, each tuple is (pkg_alias, pkg_path)
30+
[('', 'compress/bzip2'), ('E', 'errors'), ('.', 'archive/tar'), ('_', 'database/sql/driver')]
31+
'''
32+
single_import_pattern = ''' import \s+ (\w|\.){0,1} \s* "(.*?)" '''
33+
single_imports = re.findall(single_import_pattern, src, re.M | re.X | re.S)
34+
35+
multi_import_pattern0 = '''import \s* \( (.*?) \)'''
36+
multi_imports0 = re.findall(multi_import_pattern0, src, re.M | re.X | re.S)
37+
38+
multi_import_pattern = ''' (\w|\.){0,1} \s* "(.*?)" '''
39+
multi_imports = re.findall(multi_import_pattern, ''.join(multi_imports0), re.M | re.X | re.S)
40+
41+
return single_imports + multi_imports
42+
43+
44+
def get_full_pkg(imports, pkg):
45+
'''get full pkg path like "encoding/json" for json, pkg could be an alias'''
46+
for alias, fpkg in imports:
47+
#print alias, fpkg
48+
if pkg in (alias, fpkg, fpkg.split('/')[-1]):
49+
return fpkg
50+
51+
return ''
52+
53+
54+
def get_pkg_doc_path(view, sel):
55+
'''Find the full import path for the selected obj in src'''
56+
region = sublime.Region(0, view.size())
57+
src = view.substr(region)
58+
imports = get_imports(src)
59+
#print imports
60+
61+
selected = view.substr(sel)
62+
pkg,typ = '', ''
63+
if selected[0].isupper() and view.substr(sel.begin()-1) == '.':
64+
pkg = view.substr(view.word(sel.begin()-1))
65+
typ = selected
66+
else:
67+
pkg = selected
68+
69+
#pkg could be an alias,
70+
fpkg = get_full_pkg(imports, pkg)
71+
#print 'pkg:', pkg, 'typ:', typ, 'fpkg:', fpkg
72+
73+
if typ:
74+
return fpkg + '/#' + typ
75+
else:
76+
return fpkg
77+
78+
79+
80+
class GoToDocExactCommand(sublime_plugin.TextCommand):
81+
82+
def description(self):
83+
return 'Open go document exactly base on selected text'
84+
85+
def run(self, edit):
86+
sels = self.view.sel()
87+
for sel in sels:
88+
sel_txt = self.view.substr(sel).strip()
89+
if sel_txt == '': continue
90+
91+
if GO_KEYWORDS.has_key(sel_txt):
92+
doc_url = 'http://golang.org/ref/spec#' + GO_KEYWORDS[sel_txt]
93+
elif sel_txt in GO_BUILTINS:
94+
doc_url = 'http://golang.org/pkg/builtin/#' + sel_txt
95+
else:
96+
doc_url = 'http://golang.org/pkg/' + get_pkg_doc_path(self.view, sel)
97+
98+
webbrowser.open_new_tab(doc_url)
99+
100+
101+
class GoToDocLuckyCommand(sublime_plugin.TextCommand):
102+
103+
LUCKY_SEARCH_URL="http://google.com/search?btnI=1&q=site:golang.org+"
104+
105+
def description(self):
106+
return "Search the selected text on golang.org using Google - I'm Feeling Lucky"
107+
108+
def run(self, edit):
109+
sels = self.view.sel()
110+
for sel in sels:
111+
#print(self.view.substr(sel))
112+
kw = self.view.substr(sel).strip()
113+
if kw:
114+
webbrowser.open_new_tab(self.LUCKY_SEARCH_URL + kw)
115+
116+
117+
class GoToDocTestCommand(sublime_plugin.TextCommand):
118+
119+
120+
def run(self, edit):
121+
sels = self.view.sel()
122+
for sel in sels:
123+
#print self.view.substr(sel.begin())
124+
#print self.view.substr(sel.end())
125+
#print self.view.substr(self.view.word(sel.begin()))
126+
#print self.view.substr(self.view.word(sel.end()))
127+
selected = self.view.substr(sel)
128+
pkg,typ = None, None
129+
if selected[0].isupper() and self.view.substr(sel.begin()-1) == '.':
130+
pkg = self.view.substr(self.view.word(sel.begin()-1))
131+
typ = selected
132+
else:
133+
pkg = selected
134+
135+
#print pkg,typ
136+
137+

0 commit comments

Comments
 (0)