-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSublimeHowDoI.py
executable file
·135 lines (110 loc) · 4.54 KB
/
SublimeHowDoI.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
# Jeff Langston
#
# Very simple proof of concept utilizing https://github.com/gleitz/howdoi
# to run within the editor
import sublime
import sublime_plugin
import threading
import subprocess
import functools
def get_settings(key, default=None, view=None):
# sublime.load_settings is silent in startup period
# this lazy loader is to overcome this problem
try:
if view is None:
view = sublime.active_window().active_view()
s = view.settings()
if s.has(key):
return s.get(key)
except:
pass
return sublime.load_settings("SublimeHowDoI.sublime-settings").get(key, default)
class CommandThread(threading.Thread):
# Seperate thread to run howdoi query in background
def __init__(self, command, on_done, syntaxes, query):
threading.Thread.__init__(self)
self.command = command
self.on_done = on_done
self._syntaxes = syntaxes
self._query = query
def run(self):
query = self._query
syntaxes_lc = [x.lower() for x in self._syntaxes]
searchTermSyntax = ""
for searchWord in query.split():
if searchWord.lower() in syntaxes_lc:
searchTermSyntax = self._syntaxes[
syntaxes_lc.index(searchWord.lower())]
break
try:
exec_path = get_settings("exec_path") or ""
p = subprocess.Popen(exec_path + self.command + ' ' + query, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
output = p.communicate()[0]
output = output.decode('utf-8')
output = output.replace("\r", "")
sublime.set_timeout(
functools.partial(self.on_done, output,
searchTermSyntax, query), 0)
except subprocess.CalledProcessError as e:
sublime.set_timeout(
functools.partial(self.on_done, e.returncode), 0)
class HowdoiCommand(sublime_plugin.TextCommand):
def run(self, edit):
# TODO: google autocomplete in input panel
# Get the search term
active_window = sublime.active_window()
active_window.show_input_panel('How do I:', '',
self.on_done, self.on_change, self.on_cancel)
def on_done(self, query):
command = 'howdoi'
options = get_settings("options")
if options is not None:
for k, v in options.items():
command += ' '
# options without store
if v == "":
command += '--' + k
else:
command += '--' + k + ' ' + str(v)
thread = CommandThread(
command, self.panel, get_settings("syntaxes"), query)
thread.start()
sublime.status_message("Retrieving answer ...")
def on_change(self, query):
pass
def on_cancel(self):
pass
def panel(self, output, searchTermSyntax, query):
active_window = sublime.active_window()
useBuffer = get_settings("useBuffer")
if not hasattr(self, 'output_view'):
if useBuffer:
self.output_view = active_window.new_file()
self.output_view.set_name(query)
else:
self.output_view = active_window.create_output_panel("howdoi_answer")
self.output_view.settings().set("word_wrap", True)
self.output_view.settings().set("line_numbers", True)
self.output_view.settings().set("gutter", True)
self.output_view.settings().set("scroll_past_end", False)
self.output_view.settings().set("draw_white_space", "none")
self.output_view.set_read_only(False)
if searchTermSyntax != '':
syntax = "Packages/" + searchTermSyntax + "/" + searchTermSyntax + ".tmLanguage"
else:
syntax = active_window.active_view().settings().get('syntax')
self.output_view.run_command('howdoi_show', {
'output': output,
'searchTermSyntax': syntax,
'clear': True
})
self.output_view.set_read_only(True)
if not useBuffer:
active_window.run_command('show_panel', {"panel": 'output.howdoi_answer'})
class HowdoiShowCommand(sublime_plugin.TextCommand):
def run(self, edit, searchTermSyntax, output='', clear=False):
if clear:
region = sublime.Region(0, self.view.size())
self.view.erase(edit, region)
self.view.insert(edit, 0, output)
self.view.set_syntax_file(searchTermSyntax)