]> git.lizzy.rs Git - micro.git/commitdiff
Add default plugins, and install go plugin by default
authorZachary Yedidia <zyedidia@gmail.com>
Wed, 27 Apr 2016 18:12:32 +0000 (14:12 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Thu, 5 May 2016 16:53:26 +0000 (12:53 -0400)
cmd/micro/bindings.go
cmd/micro/plugin.go
cmd/micro/view.go
runtime/plugins/go/go.lua [new file with mode: 0644]

index d02aab8295451d8cd32fdbf44bcefd0424cdb117..dbac7dabe0d79932afed6d4bd8f493026557b28a 100644 (file)
@@ -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
-}
index add2476a6060b92473846b90d039e7720ed42811..667dac2ae5f71e7cfe8fcc767d3a977a8052f2f0 100644 (file)
@@ -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)
+       }
 }
index 576bf66062ebe6bf9319f9497646ac6f9577b683..bf1468fd86b0385c3d5ae17ddfa565406bc853f5 100644 (file)
@@ -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 (file)
index 0000000..b08ddac
--- /dev/null
@@ -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