-
Notifications
You must be signed in to change notification settings - Fork 2
/
subflow.py
44 lines (34 loc) · 1.24 KB
/
subflow.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
import sublime
import sublime_plugin
import subprocess
class SubFlowCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
line = self.view.line(region)
cont = self.view.substr(line)
if (len(cont) < 2):
pass
else:
lang = " in " + self.get_syntax()
cont = cont.strip()
cont = cont + lang
p = subprocess.Popen("howdoi " + cont,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
output, errors = p.communicate()
# Decode binary data for python 3
output = output.decode('utf-8')
# Remove CR for windows.
if sublime.platform() == 'windows':
output = output.replace('\r', '')
self.view.replace(edit, line, output)
def get_syntax(self):
syntax_file = self.view.settings().get('syntax')
start = syntax_file.rfind('/')
if start == -1:
start = 0
end = syntax_file.rfind('.')
if end == -1:
end = len(syntax_file)
return syntax_file[start+1:end].lower()