forked from kemayo/sublime-text-2-goto-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotodocumentation.py
172 lines (132 loc) · 6.4 KB
/
gotodocumentation.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
#!/usr/bin/python
import functools
import os
import re
import subprocess
import threading
import sublime
import sublime_plugin
def open_url(url):
sublime.active_window().run_command('open_url', {"url": url})
def main_thread(callback, *args, **kwargs):
# sublime.set_timeout gets used to send things onto the main thread
# most sublime.[something] calls need to be on the main thread
sublime.set_timeout(functools.partial(callback, *args, **kwargs), 0)
def _make_text_safeish(text, fallback_encoding, method='decode'):
# The unicode decode here is because sublime converts to unicode inside
# insert in such a way that unknown characters will cause errors, which is
# distinctly non-ideal... and there's no way to tell what's coming out of
# git in output. So...
try:
unitext = getattr(text, method)('utf-8')
except (UnicodeEncodeError, UnicodeDecodeError):
unitext = getattr(text, method)(fallback_encoding)
except AttributeError:
# strongly implies we're already unicode, but just in case let's cast
# to string
unitext = str(text)
return unitext
class CommandThread(threading.Thread):
def __init__(self, command, on_done, working_dir="", fallback_encoding=""):
threading.Thread.__init__(self)
self.command = command
self.on_done = on_done
self.working_dir = working_dir
self.fallback_encoding = fallback_encoding
def run(self):
try:
# Per http://bugs.python.org/issue8557 shell=True is required to
# get $PATH on Windows. Yay portable code.
shell = os.name == 'nt'
if self.working_dir != "":
os.chdir(self.working_dir)
proc = subprocess.Popen(self.command,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
shell=shell, universal_newlines=True)
output = proc.communicate()[0]
main_thread(self.on_done,
_make_text_safeish(output, self.fallback_encoding))
except subprocess.CalledProcessError as e:
main_thread(self.on_done, e.returncode)
class GotoDocumentationCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
word = self.view.word(region)
if not word.empty():
# scope: "text.html.basic source.php.embedded.block.html keyword.other.new.php"
scope = self.view.scope_name(word.begin()).strip()
extracted_scope = scope.rpartition('.')[2]
keyword = self.view.substr(word)
if "source.pde" in scope:
extracted_scope = "processing"
getattr(self, '%s_doc' % extracted_scope, self.unsupported)(keyword, scope)
def unsupported(self, keyword, scope):
sublime.status_message("This scope is not supported: %s" % scope.rpartition('.')[2])
def php_doc(self, keyword, scope):
open_url("http://php.net/%s" % keyword)
def ahk_doc(self, keyword, scope):
open_url("http://www.autohotkey.com/docs/commands/%s.htm" % keyword)
def processing_doc(self, keyword, scope):
open_url("http://www.processing.org/reference/%s_" % keyword + ".html")
def rails_doc(self, keyword, scope):
open_url("http://api.rubyonrails.org/?q=%s" % keyword)
def controller_doc(self, keyword, scope):
open_url("http://api.rubyonrails.org/?q=%s" % keyword)
def ruby_doc(self, keyword, scope):
open_url("http://ruby-doc.com/search.html?q=%s" % keyword)
def js_doc(self, keyword, scope):
open_url("https://developer.mozilla.org/en-US/search?q=%s" % keyword)
coffee_doc = js_doc
def python_doc(self, keyword, scope):
"""Not trying to be full on intellisense here, but want to make opening a
browser to a docs.python.org search a last resort
"""
# if not re.match(r'\s', keyword):
# self.run_command(["python", "-m", "pydoc", keyword])
# return
open_url("http://docs.python.org/search.html?q=%s" % keyword)
def clojure_doc(self, keyword, scope):
open_url("http://clojuredocs.org/search?x=0&y=0&q=%s" % keyword)
def go_doc(self, keyword, scope):
open_url("http://golang.org/search?q=%s" % keyword)
def smarty_doc(self, keyword, scope):
open_url('http://www.smarty.net/%s' % keyword)
def cmake_doc(self, keyword, scope):
open_url('http://cmake.org/cmake/help/v2.8.8/cmake.html#command:%s' % keyword.lower())
def perl_doc(self, keyword, scope):
open_url("http://perldoc.perl.org/search.html?q=%s" % keyword)
def cs_doc(self, keyword, scope):
open_url("http://social.msdn.microsoft.com/Search/?query=%s" % keyword)
def lua_doc(self, keyword, scope):
open_url('http://pgl.yoyo.org/luai/i/%s' % keyword)
def pgsql_doc(self, keyword, scope):
open_url('http://www.postgresql.org/search/?u=%%2Fdocs%%2Fcurrent%%2F&q=%s' % keyword)
def erlang_doc(self, keyword, scope):
otp_version = self.view.settings().get("otp_version", "R16B03")
url = 'http://erldocs.com/%(otp_version)s/?search=%(keyword)s' % {'otp_version': otp_version, 'keyword': keyword}
open_url(url)
def mel_doc(self, keyword, scope):
url = 'http://download.autodesk.com/global/docs/maya2014/en_us/Commands/{0}.html'.format(keyword)
open_url(url)
def run_command(self, command, callback=None, **kwargs):
if not callback:
callback = self.panel
thread = CommandThread(command, callback, **kwargs)
thread.start()
def panel(self, output, **kwargs):
active_window = sublime.active_window()
if not hasattr(self, 'output_view'):
self.output_view = active_window.get_output_panel("gotodocumentation")
self.output_view.set_read_only(False)
self.output_view.run_command('goto_documentation_output', {
'output': output,
'clear': True
})
self.output_view.set_read_only(True)
active_window.run_command("show_panel", {"panel": "output.gotodocumentation"})
class GotoDocumentationOutputCommand(sublime_plugin.TextCommand):
def run(self, edit, output = '', output_file = None, clear = False):
if clear:
region = sublime.Region(0, self.view.size())
self.view.erase(edit, region)
self.view.insert(edit, 0, output)