Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to make go imports work as separated cmd. #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions GoSublime.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"caption": "GoSublime: Fmt the current file (without saving it)",
"command": "gs_fmt"
},
{
"caption": "GoSublime: Help you to make your imports clean",
"command": "gs_imports"
},
{
"caption": "GoSublime: New Go File",
"command": "gs_new_go_file"
Expand Down
1 change: 1 addition & 0 deletions GoSublime.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
// the command will be passed, to its stdin, the contents of the file
// it must output the new file contents
"fmt_cmd": [],
"goimports_cmd": ["goimports"],

// Whether or not gslint is enabled
"gslint_enabled": true,
Expand Down
1 change: 1 addition & 0 deletions gosubl/gs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"fmt_tab_indent": True,
"fmt_tab_width": 8,
"fmt_cmd": [],
"goimports_cmd": ["goimports"],
"gslint_enabled": False,
"comp_lint_enabled": False,
"comp_lint_commands": [],
Expand Down
22 changes: 22 additions & 0 deletions gosubl/mg9.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,28 @@ def fmt(fn, src):
})
return res.get('src', ''), err

def goimports(fn, src):
st = gs.settings_dict()
x = st.get('goimports_cmd')
if x:
res, err = bcall('sh', {
'Env': sh.env(),
'Cmd': {
'Name': x[0],
'Args': x[1:],
'Input': src or '',
},
})
return res.get('out', ''), (err or res.get('err', ''))

res, err = bcall('goimports', {
'Fn': fn or '',
'Src': src or '',
'TabIndent': st.get('fmt_tab_indent'),
'TabWidth': st.get('fmt_tab_width'),
})
return res.get('src', ''), err

def import_paths(fn, src, f):
tid = gs.begin(DOMAIN, 'Fetching import paths')
def cb(res, err):
Expand Down
31 changes: 31 additions & 0 deletions gscommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ def run(self, edit):
self.view.run_command("run_macro_file", {"file": "Packages/Default/Add Line.sublime-macro"})
self.view.run_command("toggle_comment", {"block": False})

class GsImportsCommand(sublime_plugin.TextCommand):
def is_enabled(self):
fn = self.view.file_name()
if fn:
scope_ok = fn.lower().endswith('.go')
else:
scope_ok = gs.is_go_source_view(self.view)

return scope_ok

def run(self, edit):
vsize = self.view.size()
src = self.view.substr(sublime.Region(0, vsize))
if not src.strip():
return

src, err = mg9.goimports(self.view.file_name(), src)
if err:
gs.println(DOMAIN, "cannot goimports file. error: `%s'" % err)
return

if not src.strip():
gs.println(DOMAIN, "cannot goimports file. it appears to be empty")
return

_, err = gspatch.merge(self.view, vsize, src, edit)
if err:
msg = 'PANIC: Cannot goimports file. Check your source for errors (and maybe undo any changes).'
sublime.error_message("%s: %s: Merge failure: `%s'" % (DOMAIN, msg, err))

class GsFmtCommand(sublime_plugin.TextCommand):
def is_enabled(self):
fn = self.view.file_name()
Expand Down Expand Up @@ -64,6 +94,7 @@ def run(self, edit):
self.view.run_command("gs_fmt")
sublime.set_timeout(lambda: self.view.run_command("prompt_save_as"), 0)


class GsGotoRowColCommand(sublime_plugin.TextCommand):
def run(self, edit, row, col=0):
pt = self.view.text_point(row, col)
Expand Down
32 changes: 32 additions & 0 deletions src/gosubli.me/margo/m_goimports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"go/ast"
"go/parser"
)

type mGoImports struct {
Fn string
Src string
TabIndent bool
TabWidth int
}

func (m *mGoImports) Call() (interface{}, string) {
res := M{}
fset, af, err := parseAstFile(m.Fn, m.Src, parser.ParseComments)
if err == nil {
ast.SortImports(fset, af)
res["src"], err = printSrc(fset, af, m.TabIndent, m.TabWidth)
}
return res, errStr(err)
}

func init() {
registry.Register("goimports", func(b *Broker) Caller {
return &mGoImports{
TabIndent: true,
TabWidth: 8,
}
})
}