mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-16 05:47:06 +09:00
Add default plugins, and install go plugin by default
This commit is contained in:
@@ -2,10 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -615,10 +613,6 @@ func (v *View) Save() bool {
|
|||||||
messenger.Error(err.Error())
|
messenger.Error(err.Error())
|
||||||
} else {
|
} else {
|
||||||
messenger.Message("Saved " + v.Buf.Path)
|
messenger.Message("Saved " + v.Buf.Path)
|
||||||
switch v.Buf.Filetype {
|
|
||||||
case "Go":
|
|
||||||
v.GoSave()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for _, pl := range loadedPlugins {
|
for _, pl := range loadedPlugins {
|
||||||
if err := L.CallByParam(lua.P{
|
if err := L.CallByParam(lua.P{
|
||||||
@@ -633,33 +627,6 @@ func (v *View) Save() bool {
|
|||||||
return true
|
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
|
// Find opens a prompt and searches forward for the input
|
||||||
func (v *View) Find() bool {
|
func (v *View) Find() bool {
|
||||||
if v.Cursor.HasSelection() {
|
if v.Cursor.HasSelection() {
|
||||||
@@ -889,25 +856,3 @@ func (v *View) JumpLine() bool {
|
|||||||
func None() bool {
|
func None() bool {
|
||||||
return false
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,6 +6,11 @@ import (
|
|||||||
|
|
||||||
var loadedPlugins []string
|
var loadedPlugins []string
|
||||||
|
|
||||||
|
var preInstalledPlugins = []string{
|
||||||
|
"go",
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
|
||||||
func LoadPlugins() {
|
func LoadPlugins() {
|
||||||
files, _ := ioutil.ReadDir(configDir + "/plugins")
|
files, _ := ioutil.ReadDir(configDir + "/plugins")
|
||||||
for _, plugin := range files {
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -372,6 +372,7 @@ func (v *View) HandleEvent(event tcell.Event) {
|
|||||||
|
|
||||||
// GutterMessage creates a message in this view's gutter
|
// GutterMessage creates a message in this view's gutter
|
||||||
func (v *View) GutterMessage(lineN int, msg string, kind int) {
|
func (v *View) GutterMessage(lineN int, msg string, kind int) {
|
||||||
|
lineN--
|
||||||
gutterMsg := GutterMessage{
|
gutterMsg := GutterMessage{
|
||||||
lineNum: lineN,
|
lineNum: lineN,
|
||||||
msg: msg,
|
msg: msg,
|
||||||
|
|||||||
49
runtime/plugins/go/go.lua
Normal file
49
runtime/plugins/go/go.lua
Normal 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
|
||||||
Reference in New Issue
Block a user