mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-04 22:20:20 +09:00
Add automatic linting for Go, Python, C, D, Java, Javascript, Lua
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/layeh/gopher-luar"
|
||||
@@ -215,6 +216,7 @@ func main() {
|
||||
messenger = new(Messenger)
|
||||
view := NewView(buf)
|
||||
|
||||
L.SetGlobal("OS", luar.New(L, runtime.GOOS))
|
||||
L.SetGlobal("view", luar.New(L, view))
|
||||
L.SetGlobal("settings", luar.New(L, &settings))
|
||||
L.SetGlobal("messenger", luar.New(L, messenger))
|
||||
|
||||
@@ -1,13 +1,39 @@
|
||||
function linter_onSave()
|
||||
local ft = view.Buf.Filetype
|
||||
local file = view.Buf.Path
|
||||
local devnull = "/dev/null"
|
||||
if OS == "windows" then
|
||||
devnull = "NUL"
|
||||
end
|
||||
if ft == "Go" then
|
||||
linter_lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
|
||||
linter_lint("golint", "golint " .. view.Buf.Path, "%f:%l:%d+: %m")
|
||||
elseif ft == "Lua" then
|
||||
linter_lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
|
||||
elseif ft == "Python" then
|
||||
linter_lint("pyflakes", "pyflakes " .. file, "%f:%l: %m")
|
||||
elseif ft == "C" then
|
||||
linter_lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
|
||||
elseif ft == "D" then
|
||||
linter_lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
|
||||
elseif ft == "Java" then
|
||||
linter_lint("javac", "javac " .. file, "%f:%l: error: %m")
|
||||
elseif ft == "JavaScript" then
|
||||
linter_lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
|
||||
end
|
||||
end
|
||||
|
||||
function linter_lint(linter, cmd, errorformat)
|
||||
view:ClearGutterMessages(linter)
|
||||
|
||||
local handle = io.popen(cmd)
|
||||
local handle = io.popen("(" .. cmd .. ")" .. " 2>&1")
|
||||
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
|
||||
-- Trim whitespace
|
||||
line = line:match("^%s*(.+)%s*$")
|
||||
if string.find(line, regex) then
|
||||
local file, line, msg = string.match(line, regex)
|
||||
if linter_basename(view.Buf.Path) == linter_basename(file) then
|
||||
|
||||
Reference in New Issue
Block a user