From: Zachary Yedidia Date: Wed, 27 Apr 2016 18:12:32 +0000 (-0400) Subject: Add default plugins, and install go plugin by default X-Git-Tag: 1.0rc1~195^2~11 X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=a333f0ade2b8127e19a9898fbce90501289101d1;p=micro.git Add default plugins, and install go plugin by default --- diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index d02aab82..dbac7dab 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -2,10 +2,8 @@ package main import ( "encoding/json" - "errors" "io/ioutil" "os" - "os/exec" "strconv" "strings" "time" @@ -615,10 +613,6 @@ func (v *View) Save() bool { messenger.Error(err.Error()) } else { messenger.Message("Saved " + v.Buf.Path) - switch v.Buf.Filetype { - case "Go": - v.GoSave() - } } for _, pl := range loadedPlugins { if err := L.CallByParam(lua.P{ @@ -633,33 +627,6 @@ func (v *View) Save() bool { return true } -// GoSave saves the current file (must be a go file) and runs goimports or gofmt -// depending on the user's configuration -func (v *View) GoSave() { - if settings["goimports"] == true { - messenger.Message("Running goimports...") - err := goimports(v.Buf.Path) - if err != nil { - messenger.Error(err) - } else { - messenger.Message("Saved " + v.Buf.Path) - } - v.ReOpen() - } else if settings["gofmt"] == true { - messenger.Message("Running gofmt...") - err := gofmt(v.Buf.Path) - if err != nil { - messenger.Error(err) - } else { - messenger.Message("Saved " + v.Buf.Path) - } - v.ReOpen() - return - } - - return -} - // Find opens a prompt and searches forward for the input func (v *View) Find() bool { if v.Cursor.HasSelection() { @@ -889,25 +856,3 @@ func (v *View) JumpLine() bool { func None() bool { return false } - -// gofmt runs gofmt on a file -func gofmt(file string) error { - cmd := exec.Command("gofmt", "-w", file) - cmd.Start() - err := cmd.Wait() - if err != nil { - return errors.New("Check syntax ") //TODO: highlight or display locations - } - return nil -} - -// goimports runs goimports on a file -func goimports(file string) error { - cmd := exec.Command("goimports", "-w", file) - cmd.Start() - err := cmd.Wait() - if err != nil { - return errors.New("Check syntax ") //TODO: highlight or display locations - } - return nil -} diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index add2476a..667dac2a 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -6,6 +6,11 @@ import ( var loadedPlugins []string +var preInstalledPlugins = []string{ + "go", +} + +// LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins func LoadPlugins() { files, _ := ioutil.ReadDir(configDir + "/plugins") for _, plugin := range files { @@ -23,4 +28,18 @@ func LoadPlugins() { } } } + + for _, pluginName := range preInstalledPlugins { + plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua" + data, err := Asset(plugin) + if err != nil { + TermMessage("Error loading pre-installed plugin: " + pluginName) + continue + } + if err := L.DoString(string(data)); err != nil { + TermMessage(err) + continue + } + loadedPlugins = append(loadedPlugins, pluginName) + } } diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 576bf660..bf1468fd 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -372,6 +372,7 @@ func (v *View) HandleEvent(event tcell.Event) { // GutterMessage creates a message in this view's gutter func (v *View) GutterMessage(lineN int, msg string, kind int) { + lineN-- gutterMsg := GutterMessage{ lineNum: lineN, msg: msg, diff --git a/runtime/plugins/go/go.lua b/runtime/plugins/go/go.lua new file mode 100644 index 00000000..b08ddac4 --- /dev/null +++ b/runtime/plugins/go/go.lua @@ -0,0 +1,49 @@ +function go_onSave() + if view.Buf.Filetype == "Go" then + if settings.GoImports then + go_goimports() + elseif settings.GoFmt then + go_gofmt() + end + go_golint() + end +end + +function go_gofmt() + local handle = io.popen("gofmt -w " .. view.Buf.Path) + local result = handle:read("*a") + handle:close() + + view:ReOpen() +end + +function go_golint() + local handle = io.popen("golint " .. view.Buf.Path) + local result = go_split(handle:read("*a"), ":") + handle:close() + + local file = result[1] + local line = tonumber(result[2]) + local col = tonumber(result[3]) + local msg = result[4] + + view:ReOpen() + view:GutterMessage(line, msg, 2) +end + +function go_goimports() + local handle = io.popen("goimports -w " .. view.Buf.Path) + local result = go_split(handle:read("*a"), ":") + handle:close() + + view:ReOpen() +end + +function go_split(str, sep) + local result = {} + local regex = ("([^%s]+)"):format(sep) + for each in str:gmatch(regex) do + table.insert(result, each) + end + return result +end