Sublime text 4 GoFmter

这几天在学习Go语言,又心血来潮向回去用用Sublime Text,发现已经Sublime Text 4了 = =,安装gofmt又一直不成功,就自己写了小插件,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import sublime
import sublime_plugin
import subprocess

class GofmterCommand(sublime_plugin.TextCommand):
def run(self, edit):
fn = self.view.file_name()
if not fn or not fn.endswith('.go'):
print('not go file')
else:
print('find go file')
print('ok')
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
output = subprocess.check_output(['gofmt.exe', '%s' % (fn)], startupinfo=si)
print(output)
r = sublime.Region(0, self.view.size())
self.view.replace(edit, r, output.decode('utf-8'))

可以添加快捷键,将command直接设为gofmter就行了。