import (
"encoding/json"
- "errors"
"io/ioutil"
"os"
- "os/exec"
"strconv"
"strings"
"time"
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{
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() {
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
-}
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 {
}
}
}
+
+ 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)
+ }
}
// 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,
--- /dev/null
+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