Browse Source

[Feature] add linter support (#982)

Luc Sinet 4 years ago
parent
commit
836286798e

+ 33 - 0
lua/core/linter.lua

@@ -0,0 +1,33 @@
+local M = {}
+
+M.setup = function()
+  if O.lint_on_save then
+    require("lv-utils").define_augroups {
+      autolint = {
+        {
+          "BufWritePost",
+          "<buffer>",
+          ":silent lua require('lint').try_lint()",
+        },
+        {
+          "BufEnter",
+          "<buffer>",
+          ":silent lua require('lint').try_lint()",
+        },
+      },
+    }
+  end
+end
+
+local status_ok, linter = pcall(require, "lint")
+if not status_ok then
+  return
+end
+
+if not O.lint_on_save then
+  vim.cmd [[if exists('#autolint#BufWritePost')
+	:autocmd! autolint
+	endif]]
+end
+
+return M

+ 2 - 1
lua/core/which-key.lua

@@ -127,9 +127,10 @@ M.config = function()
           "<cmd>lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = O.lsp.popup_border}})<cr>",
           "Prev Diagnostic",
         },
+        l = { "<cmd>silent lua require('lint').try_lint()<cr>", "Lint" },
         q = { "<cmd>Telescope quickfix<cr>", "Quickfix" },
         r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename" },
-        s = { "<cmd> Telescope lsp_document_symbols<cr>", "Document Symbols" },
+        s = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" },
         S = {
           "<cmd>Telescope lsp_dynamic_workspace_symbols<cr>",
           "Workspace Symbols",

+ 1 - 0
lua/default-config.lua

@@ -10,6 +10,7 @@ O = {
   line_wrap_cursor_movement = true,
   transparent_window = false,
   format_on_save = true,
+  lint_on_save = true,
   vsnip_dir = vim.fn.stdpath "config" .. "/snippets",
 
   default_options = {

+ 8 - 2
lua/lang/clang.lua

@@ -14,6 +14,10 @@ M.config = function()
       exe = "clang-format",
       args = {},
     },
+    linters = {
+      "cppcheck",
+      "clangtidy",
+    },
     debug = {
       adapter = {
         command = "/usr/bin/lldb-vscode",
@@ -45,8 +49,10 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    c = O.lang.clang.linters,
+    cpp = O.lang.clang.linters,
+  }
 end
 
 M.lsp = function()

+ 7 - 2
lua/lang/go.lua

@@ -6,6 +6,10 @@ M.config = function()
       exe = "gofmt",
       args = {},
     },
+    linters = {
+      "golangcilint",
+      "revive",
+    },
   }
 end
 
@@ -27,8 +31,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    go = O.lang.go.linters,
+  }
 end
 
 M.lsp = function()

+ 10 - 3
lua/lang/html.lua

@@ -1,7 +1,13 @@
 local M = {}
 
 M.config = function()
-  O.lang.html = {}
+  O.lang.html = {
+    linters = {
+      "tidy",
+      -- https://docs.errata.ai/vale/scoping#html
+      "vale",
+    },
+  }
 end
 
 M.format = function()
@@ -10,8 +16,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    html = O.lang.html.linters,
+  }
 end
 
 M.lsp = function()

+ 4 - 2
lua/lang/lua.lua

@@ -12,6 +12,7 @@ M.config = function()
       args = {},
       stdin = false,
     },
+    linters = { "luacheck" },
   }
 end
 
@@ -34,8 +35,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    lua = O.lang.lua.linters,
+  }
 end
 
 M.lsp = function()

+ 7 - 34
lua/lang/python.lua

@@ -19,6 +19,11 @@ M.config = function()
       exe = "yapf",
       args = {},
     },
+    linters = {
+      "flake8",
+      "pylint",
+      "mypy",
+    },
   }
 end
 
@@ -40,41 +45,9 @@ M.format = function()
 end
 
 M.lint = function()
-  if require("lv-utils").check_lsp_client_active "efm" then
-    return
-  end
-  local python_arguments = {}
-
-  local flake8 = {
-    LintCommand = "flake8 --ignore=E501 --stdin-display-name ${INPUT} -",
-    lintStdin = true,
-    lintFormats = { "%f:%l:%c: %m" },
+  require("lint").linters_by_ft = {
+    python = O.lang.python.linters,
   }
-
-  local isort = { formatCommand = "isort --quiet -", formatStdin = true }
-
-  if O.lang.python.linter == "flake8" then
-    table.insert(python_arguments, flake8)
-  end
-
-  if O.lang.python.isort then
-    table.insert(python_arguments, isort)
-  end
-
-  if not require("lv-utils").check_lsp_client_active "efm" then
-    require("lspconfig").efm.setup {
-      cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
-      init_options = { documentFormatting = true, codeAction = false },
-      root_dir = require("lspconfig").util.root_pattern(".git/", "requirements.txt"),
-      filetypes = { "python" },
-      settings = {
-        rootMarkers = { ".git/", "requirements.txt" },
-        languages = {
-          python = python_arguments,
-        },
-      },
-    }
-  end
 end
 
 M.lsp = function()

+ 4 - 2
lua/lang/ruby.lua

@@ -12,6 +12,7 @@ M.config = function()
       exe = "rufo",
       args = { "-x" },
     },
+    linters = { "ruby" },
   }
 end
 
@@ -33,8 +34,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    ruby = O.lang.ruby.linters,
+  }
 end
 
 M.lsp = function()

+ 3 - 29
lua/lang/sh.lua

@@ -15,6 +15,7 @@ M.config = function()
       args = { "-w" },
       stdin = false,
     },
+    linters = { "shellcheck" },
   }
 end
 
@@ -37,36 +38,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- sh
-  local sh_arguments = {}
-
-  local shfmt = { formatCommand = "shfmt -ci -s -bn", formatStdin = true }
-
-  local shellcheck = {
-    LintCommand = "shellcheck -f gcc -x",
-    lintFormats = { "%f:%l:%c: %trror: %m", "%f:%l:%c: %tarning: %m", "%f:%l:%c: %tote: %m" },
+  require("lint").linters_by_ft = {
+    sh = O.lang.sh.linters,
   }
-
-  if O.lang.sh.linter == "shellcheck" then
-    table.insert(sh_arguments, shellcheck)
-  end
-
-  if not require("lv-utils").check_lsp_client_active "efm" then
-    require("lspconfig").efm.setup {
-      -- init_options = {initializationOptions},
-      cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
-      on_attach = require("lsp").common_on_attach,
-      init_options = { documentFormatting = true, codeAction = false },
-      root_dir = require("lspconfig").util.root_pattern ".git/",
-      filetypes = { "sh" },
-      settings = {
-        rootMarkers = { ".git/" },
-        languages = {
-          sh = sh_arguments,
-        },
-      },
-    }
-  end
 end
 
 M.lsp = function()

+ 4 - 2
lua/lang/tex.lua

@@ -31,6 +31,7 @@ M.config = function()
       signs = true,
       underline = true,
     },
+    linters = { "chktex" },
     auto_save = false,
     ignore_errors = {},
   }
@@ -42,8 +43,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    tex = O.lang.latex.linters,
+  }
 end
 
 M.lsp = function()

+ 6 - 3
lua/lang/vim.lua

@@ -1,7 +1,9 @@
 local M = {}
 
 M.config = function()
-  O.lang.vim = {}
+  O.lang.vim = {
+    linters = { "vint" },
+  }
 end
 
 M.format = function()
@@ -10,8 +12,9 @@ M.format = function()
 end
 
 M.lint = function()
-  -- TODO: implement linters (if applicable)
-  return "No linters configured!"
+  require("lint").linters_by_ft = {
+    vim = O.lang.vim.linters,
+  }
 end
 
 M.lsp = function()

+ 6 - 0
lua/plugins.lua

@@ -86,6 +86,12 @@ return require("packer").startup(function(use)
     end,
   }
 
+  -- Linter
+  use {
+    "mfussenegger/nvim-lint",
+    config = require("core.linter").setup,
+  }
+
   -- NvimTree
   use {
     "kyazdani42/nvim-tree.lua",

+ 6 - 2
utils/installer/lv-config.example-no-ts.lua

@@ -11,6 +11,7 @@ an executable
 -- general
 
 O.format_on_save = true
+O.lint_on_save = true
 O.completion.autocomplete = true
 O.colorscheme = "spacegray"
 O.auto_close_tree = 0
@@ -35,12 +36,15 @@ O.treesitter.highlight.enabled = true
 O.lang.python.isort = true
 O.lang.python.diagnostics.virtual_text = true
 O.lang.python.analysis.use_library_code_types = true
--- to change default formatter from yapf to black
+-- To change default formatter from yapf to black
 -- O.lang.python.formatter.exe = "black"
 -- O.lang.python.formatter.args = {"-"}
+-- To change enabled linters
+-- https://github.com/mfussenegger/nvim-lint#available-linters
+-- O.lang.python.linters = { "flake8", "pylint", "mypy", ... }
 
 -- go
--- to change default formatter from gofmt to goimports
+-- To change default formatter from gofmt to goimports
 -- O.lang.formatter.go.exe = "goimports"
 
 -- javascript

+ 6 - 2
utils/installer/lv-config.example.lua

@@ -11,6 +11,7 @@ an executable
 -- general
 
 O.format_on_save = true
+O.lint_on_save = true
 O.completion.autocomplete = true
 O.colorscheme = "spacegray"
 O.auto_close_tree = 0
@@ -35,12 +36,15 @@ O.treesitter.highlight.enabled = true
 O.lang.python.isort = true
 O.lang.python.diagnostics.virtual_text = true
 O.lang.python.analysis.use_library_code_types = true
--- to change default formatter from yapf to black
+-- To change default formatter from yapf to black
 -- O.lang.python.formatter.exe = "black"
 -- O.lang.python.formatter.args = {"-"}
+-- To change enabled linters
+-- https://github.com/mfussenegger/nvim-lint#available-linters
+-- O.lang.python.linters = { "flake8", "pylint", "mypy", ... }
 
 -- go
--- to change default formatter from gofmt to goimports
+-- To change default formatter from gofmt to goimports
 -- O.lang.formatter.go.exe = "goimports"
 
 -- javascript