From 1668e9131020fdda0125e14600cd4983c3a381a3 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 30 Apr 2016 16:21:08 -0400 Subject: [PATCH] Add linter plugin to easily make linters --- cmd/micro/plugin.go | 1 + runtime/plugins/go/go.lua | 37 +++---------------------------- runtime/plugins/linter/linter.lua | 32 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 runtime/plugins/linter/linter.lua diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index 242a49f7..4138e3a0 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -9,6 +9,7 @@ var loadedPlugins []string var preInstalledPlugins = []string{ "go", + "linter", } // Call calls the lua function 'function' diff --git a/runtime/plugins/go/go.lua b/runtime/plugins/go/go.lua index 73608160..f6667304 100644 --- a/runtime/plugins/go/go.lua +++ b/runtime/plugins/go/go.lua @@ -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"), ":") diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua new file mode 100644 index 00000000..a8e2da85 --- /dev/null +++ b/runtime/plugins/linter/linter.lua @@ -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