Lua prompt support and plugin improvements

This commit is contained in:
Zachary Yedidia 2019-12-24 17:17:44 -05:00
parent 3b306c1d3b
commit 94ff79e7b2
8 changed files with 87 additions and 25 deletions

View File

@ -68,6 +68,10 @@ func BufMapKey(k Event, action string) {
} else if strings.HasPrefix(a, "lua:") {
a = strings.SplitN(a, ":", 2)[1]
afn = LuaAction(a)
if afn == nil {
screen.TermMessage("Lua Error:", action, "does not exist")
continue
}
} else if f, ok := BufKeyActions[a]; ok {
afn = f
} else {

View File

@ -89,11 +89,7 @@ func LuaFunctionCommand(fn string) func(*BufPane, []string) {
return nil
}
return func(bp *BufPane, args []string) {
var luaArgs []lua.LValue
luaArgs = append(luaArgs, luar.New(ulua.L, bp))
for _, v := range args {
luaArgs = append(luaArgs, luar.New(ulua.L, v))
}
luaArgs := []lua.LValue{luar.New(ulua.L, bp), luar.New(ulua.L, args)}
_, err := pl.Call(plFn, luaArgs...)
if err != nil {
screen.TermMessage(err)

File diff suppressed because one or more lines are too long

View File

@ -2,8 +2,14 @@ package info
import (
"fmt"
"strings"
"github.com/zyedidia/micro/internal/buffer"
luar "layeh.com/gopher-luar"
"github.com/zyedidia/micro/internal/config"
ulua "github.com/zyedidia/micro/internal/lua"
"github.com/zyedidia/micro/internal/screen"
)
// The InfoBuf displays messages and other info at the bottom of the screen.
@ -113,6 +119,8 @@ func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(s
i.Buffer.Insert(i.Buffer.Start(), msg)
}
// YNPrompt creates a yes or no prompt, and the callback returns the yes/no result and whether
// the prompt was canceled
func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
if i.HasPrompt {
i.DonePrompt(true)
@ -126,6 +134,63 @@ func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
i.YNCallback = donecb
}
// PlugPrompt provides a plugin interface for calling "Prompt" with the appropriate Lua callbacks
func (i *InfoBuf) PlugPrompt(prompt string, msg string, ptype string, eventcb string, donecb string) {
eventLuaFn := strings.Split(eventcb, ".")
doneLuaFn := strings.Split(donecb, ".")
var luaEventcb func(string)
var luaDonecb func(string, bool)
if len(eventLuaFn) == 2 {
plName, plFn := doneLuaFn[0], doneLuaFn[1]
pl := config.FindPlugin(plName)
if pl != nil {
luaEventcb = func(resp string) {
_, err := pl.Call(plFn, luar.New(ulua.L, resp))
if err != nil && err != config.ErrNoSuchFunction {
screen.TermMessage(err)
}
}
}
}
if len(doneLuaFn) == 2 {
plName, plFn := doneLuaFn[0], doneLuaFn[1]
pl := config.FindPlugin(plName)
if pl != nil {
luaDonecb = func(resp string, canceled bool) {
_, err := pl.Call(plFn, luar.New(ulua.L, resp), luar.New(ulua.L, canceled))
if err != nil && err != config.ErrNoSuchFunction {
screen.TermMessage(err)
}
}
}
}
i.Prompt(prompt, msg, ptype, luaEventcb, luaDonecb)
}
// PlugYNPrompt provides a plugin interface for calling "YNPrompt" with the appropriate Lua callbacks
func (i *InfoBuf) PlugYNPrompt(prompt string, donecb string) {
doneLuaFn := strings.Split(donecb, ".")
var luaDonecb func(bool, bool)
if len(doneLuaFn) == 2 {
plName, plFn := doneLuaFn[0], doneLuaFn[1]
pl := config.FindPlugin(plName)
if pl != nil {
luaDonecb = func(resp bool, canceled bool) {
_, err := pl.Call(plFn, luar.New(ulua.L, resp), luar.New(ulua.L, canceled))
if err != nil && err != config.ErrNoSuchFunction {
screen.TermMessage(err)
}
}
}
}
i.YNPrompt(prompt, luaDonecb)
}
// DonePrompt finishes the current prompt and indicates whether or not it was canceled
func (i *InfoBuf) DonePrompt(canceled bool) {
hadYN := i.HasYN

View File

@ -115,11 +115,7 @@ func luaFunctionJob(fn string) func(string, ...interface{}) {
return nil
}
return func(output string, args ...interface{}) {
var luaArgs []lua.LValue
luaArgs = append(luaArgs, luar.New(ulua.L, output))
for _, v := range args {
luaArgs = append(luaArgs, luar.New(ulua.L, v))
}
luaArgs := []lua.LValue{luar.New(ulua.L, output), luar.New(ulua.L, args)}
_, err := pl.Call(plFn, luaArgs...)
if err != nil && err != config.ErrNoSuchFunction {
screen.TermMessage(err)

View File

@ -102,11 +102,7 @@ func (t *Terminal) Start(execCmd []string, getOutput bool, wait bool, callback s
pl := config.FindPlugin(plName)
if pl != nil {
t.callback = func(out string) {
var luaArgs []lua.LValue
luaArgs = append(luaArgs, luar.New(ulua.L, out))
for _, v := range userargs {
luaArgs = append(luaArgs, luar.New(ulua.L, v))
}
luaArgs := []lua.LValue{luar.New(ulua.L, out), luar.New(ulua.L, userargs)}
_, err := pl.Call(plFn, luaArgs...)
if err != nil {
screen.TermMessage(err)

View File

@ -74,7 +74,7 @@ function commentSelection(bp, startLine, endLine)
end
end
function comment(bp)
function comment(bp, args)
if bp.Cursor:HasSelection() then
if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
local endLine = bp.Cursor.CurSelection[1].Y

View File

@ -1,3 +1,4 @@
local micro = import("micro")
local runtime = import("runtime")
local filepath = import("path/filepath")
local shell = import("micro/shell")
@ -59,8 +60,8 @@ function init()
makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
makeLinter("gcc", "c++", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
makeLinter("dmd", "d", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", "%f"}, "%f%(%l%):.+: %m")
makeLinter("gobuild", "go", "go", {"build", "-o", devnull}, "%f:%l: %m")
makeLinter("golint", "go", "golint", {"%f"}, "%f:%l:%c: %m")
makeLinter("gobuild", "go", "go", {"build", "-o", devnull}, "%f:%l:%c:? %m")
-- makeLinter("golint", "go", "golint", {"%f"}, "%f:%l:%c: %m")
makeLinter("javac", "java", "javac", {"-d", "%d", "%f"}, "%f:%l: error: %m")
makeLinter("jshint", "javascript", "jshint", {"%f"}, "%f: line %l,.+, %m")
makeLinter("literate", "literate", "lit", {"-c", "%f"}, "%f:%l:%m", {}, false, true)
@ -78,7 +79,7 @@ function init()
config.MakeCommand("lint", "linter.lintCmd", config.NoComplete)
end
function lintCmd(bp)
function lintCmd(bp, args)
bp:Save()
runLinter(bp.Buf)
end
@ -128,12 +129,13 @@ function onSave(bp)
end
function lint(buf, linter, cmd, args, errorformat, loff, coff)
buf:ClearMessages("linter")
buf:ClearMessages(linter)
shell.JobSpawn(cmd, args, "", "", "linter.onExit", buf, linter, errorformat, loff, coff)
end
function onExit(output, buf, linter, errorformat, loff, coff)
function onExit(output, args)
local buf, linter, errorformat, loff, coff = args[1], args[2], args[3], args[4], args[5]
local lines = split(output, "\n")
local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%c", "(%d+)"):gsub("%%m", "(.+)")
@ -146,15 +148,18 @@ function onExit(output, buf, linter, errorformat, loff, coff)
if not string.find(errorformat, "%%c") then
hascol = false
msg = col
elseif col == nil then
hascol = false
end
micro.Log(basename(buf.Path), basename(file))
if basename(buf.Path) == basename(file) then
local bmsg = nil
if hascol then
local mstart = buffer.Loc(tonumber(col-1+coff), tonumber(line-1+loff))
local mend = buffer.Loc(tonumber(col+coff), tonumber(line-1+loff))
bmsg = buffer.NewMessage("linter", msg, mstart, mend, buffer.MTError)
bmsg = buffer.NewMessage(linter, msg, mstart, mend, buffer.MTError)
else
bmsg = buffer.NewMessageAtLine("linter", msg, tonumber(line+loff), buffer.MTError)
bmsg = buffer.NewMessageAtLine(linter, msg, tonumber(line+loff), buffer.MTError)
end
buf:AddMessage(bmsg)
end