Add default plugins, and install go plugin by default

This commit is contained in:
Zachary Yedidia
2016-04-27 14:12:32 -04:00
parent 603cec9d81
commit a333f0ade2
4 changed files with 69 additions and 55 deletions

View 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
}

View 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)
}
}

View 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,

49
runtime/plugins/go/go.lua Normal file
View File

@@ -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