Make linter a default plugin once again

This commit is contained in:
Zachary Yedidia
2016-10-06 20:28:10 -04:00
parent 6a5879cc15
commit e563211790
5 changed files with 113 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@@ -64,7 +64,3 @@ Here are the possible commands that you can use.
The following commands are provided by the default plugins:
* `lint`: Lint the current file for errors.
* `gofmt`: Run gofmt on the current file.
* `goimports`: Run goimports on the current file.

View File

@@ -121,6 +121,10 @@ Default plugin options:
default value: `on`
* `linter`: Automatically lint when the file is saved. Provided by the `linter` plugin
default value: `on`
Any option you set in the editor will be saved to the file
~/.config/micro/settings.json so, in effect, your configuration file will be
created for you. If you'd like to take your configuration with you to another

View File

@@ -162,7 +162,8 @@ MakeCommand("foo", "example.foo", MakeCompletion("example.complete"))
# Default plugins
For examples of plugins, see the default `autoclose` plugin (stored in the normal micro core repo under `runtime/plugins/autoclose`) as well as
For examples of plugins, see the default `autoclose` and `linter` plugins
(stored in the normal micro core repo under `runtime/plugins`) as well as
any plugins that are stored in the official channel [here](https://github.com/micro-editor/plugin-channel).
# Plugin Manager

View File

@@ -0,0 +1,79 @@
if GetOption("linter") == nil then
AddOption("linter", true)
end
MakeCommand("lint", "linter.lintCommand", 0)
function lintCommand()
CurView():Save(false)
runLinter()
end
function runLinter()
local ft = CurView().Buf:FileType()
local file = CurView().Buf.Path
local devnull = "/dev/null"
if OS == "windows" then
devnull = "NUL"
end
if ft == "go" then
lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
lint("golint", "golint " .. CurView().Buf.Path, "%f:%l:%d+: %m")
elseif ft == "lua" then
lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
elseif ft == "python" then
lint("pyflakes", "pyflakes " .. file, "%f:%l:.-:? %m")
elseif ft == "c" then
lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
elseif ft == "d" then
lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
elseif ft == "java" then
lint("javac", "javac " .. file, "%f:%l: error: %m")
elseif ft == "javascript" then
lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
end
end
function onSave(view)
if GetOption("linter") then
runLinter()
else
CurView():ClearAllGutterMessages()
end
end
function lint(linter, cmd, errorformat)
CurView():ClearGutterMessages(linter)
JobStart(cmd, "", "", "linter.onExit", linter, errorformat)
end
function onExit(output, linter, errorformat)
local lines = split(output, "\n")
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 basename(CurView().Buf.Path) == basename(file) then
CurView():GutterMessage(linter, tonumber(line), msg, 2)
end
end
end
end
function 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 basename(file)
local name = string.gsub(file, "(.*/)(.*)", "%2")
return name
end