-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGoImports.py
48 lines (40 loc) · 1.59 KB
/
GoImports.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
# Sublime Text 3 Plugin that integrates goimports with your favorite editor
#
# Author: Lukas Zilka ([email protected])
#
import sublime
import sublime_plugin
import subprocess
import io
import os
MY_PATH = os.path.dirname(os.path.realpath(__file__))
def install():
script = [
"GOPATH='%s' go get github.com/bradfitz/goimports" % MY_PATH
]
for ln in script:
p = subprocess.Popen(ln, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.wait():
print("Error when installing goimports:")
print(p.stdout.read())
print(p.stderr.read())
if not os.path.exists(os.path.join(MY_PATH, "bin/goimports")):
install()
s = sublime.load_settings("GoImports.sublime-settings")
class GoImportsCommand(sublime_plugin.TextCommand):
def run(self, edit, saving=False):
# Get the content of the current window from the text editor.
selection = sublime.Region(0, self.view.size())
content = self.view.substr(selection)
# Shove that content down goimports process's throat.
process = subprocess.Popen([os.path.join(MY_PATH, "bin/goimports")],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write(bytes(content, 'utf8'))
process.stdin.close()
process.wait()
# Check and see if we got an error
error = process.stderr.read().decode('utf8')
if error:
print("error: " + error)
else:
self.view.replace(edit, selection, process.stdout.read().decode('utf8'))