mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-16 13:57:07 +09:00
Add linter plugin to easily make linters
This commit is contained in:
@@ -9,6 +9,7 @@ var loadedPlugins []string
|
||||
|
||||
var preInstalledPlugins = []string{
|
||||
"go",
|
||||
"linter",
|
||||
}
|
||||
|
||||
// Call calls the lua function 'function'
|
||||
|
||||
@@ -12,8 +12,9 @@ function go_onSave()
|
||||
elseif GetOption("gofmt") then
|
||||
go_gofmt()
|
||||
end
|
||||
go_build()
|
||||
go_golint()
|
||||
|
||||
linter_lint("go build", "go build -o /dev/null 2>&1", "%f:%l: %m")
|
||||
linter_lint("go lint", "golint " .. view.Buf.Path, "%f:%l:%d+: %m")
|
||||
|
||||
view:ReOpen()
|
||||
end
|
||||
@@ -25,38 +26,6 @@ function go_gofmt()
|
||||
handle:close()
|
||||
end
|
||||
|
||||
function go_golint()
|
||||
view:ClearGutterMessages("go-lint")
|
||||
|
||||
local handle = io.popen("golint " .. view.Buf.Path)
|
||||
local lines = go_split(handle:read("*a"), "\n")
|
||||
handle:close()
|
||||
|
||||
for _,line in ipairs(lines) do
|
||||
local result = go_split(line, ":")
|
||||
local line = tonumber(result[2])
|
||||
local msg = result[4]
|
||||
|
||||
view:GutterMessage("go-lint", line, msg, 1)
|
||||
end
|
||||
end
|
||||
|
||||
function go_build()
|
||||
view:ClearGutterMessages("go-build")
|
||||
|
||||
local handle = io.popen("go build -o /dev/null 2>&1")
|
||||
local lines = go_split(handle:read("*a"), "\n")
|
||||
handle:close()
|
||||
|
||||
messenger:Message(view.Buf.Path)
|
||||
for _,line in ipairs(lines) do
|
||||
if string.find(line, ".+:(%d+):(.+)") then
|
||||
local line, msg = string.match(line, ".+:(%d+):(.+)")
|
||||
view:GutterMessage("go-build", tonumber(line), msg, 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function go_goimports()
|
||||
local handle = io.popen("goimports -w " .. view.Buf.Path)
|
||||
local result = go_split(handle:read("*a"), ":")
|
||||
|
||||
32
runtime/plugins/linter/linter.lua
Normal file
32
runtime/plugins/linter/linter.lua
Normal file
@@ -0,0 +1,32 @@
|
||||
function linter_lint(linter, cmd, errorformat)
|
||||
view:ClearGutterMessages(linter)
|
||||
|
||||
local handle = io.popen(cmd)
|
||||
local lines = linter_split(handle:read("*a"), "\n")
|
||||
handle:close()
|
||||
|
||||
messenger:Message(view.Buf.Path)
|
||||
local regex = errorformat:gsub("%%f", "(.+)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
|
||||
for _,line in ipairs(lines) do
|
||||
if string.find(line, regex) then
|
||||
local file, line, msg = string.match(line, regex)
|
||||
if linter_basename(view.Buf.Path) == linter_basename(file) then
|
||||
view:GutterMessage(linter, tonumber(line), msg, 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function linter_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
|
||||
|
||||
function linter_basename(file)
|
||||
local name = string.gsub(file, "(.*/)(.*)", "%2")
|
||||
return name
|
||||
end
|
||||
Reference in New Issue
Block a user