| @ -1,86 +1,144 @@ | |||||
| return { | return { | ||||
| "mfussenegger/nvim-lint", | |||||
| event = { "BufReadPre", "BufNewFile" }, | |||||
| config = function() | |||||
| local lint = require("lint") | |||||
| "mfussenegger/nvim-lint", | |||||
| event = { "BufReadPre", "BufNewFile" }, | |||||
| config = function() | |||||
| local lint = require("lint") | |||||
| local severities = { | |||||
| ERROR = vim.diagnostic.severity.ERROR, | |||||
| WARNING = vim.diagnostic.severity.WARN, | |||||
| } | |||||
| local severities = { | |||||
| ERROR = vim.diagnostic.severity.ERROR, | |||||
| WARNING = vim.diagnostic.severity.WARN, | |||||
| } | |||||
| lint.linters.phpcs = { | |||||
| name = "phpcs", | |||||
| cmd = "phpcs", | |||||
| stdin = true, | |||||
| args = { | |||||
| "-q", | |||||
| "--report=json", | |||||
| "--standard=~/.config/phpcs.xml", | |||||
| "-", -- need `-` at the end for stdin support | |||||
| }, | |||||
| ignore_exitcode = true, | |||||
| parser = function(output, _) | |||||
| if vim.trim(output) == "" or output == nil then | |||||
| return {} | |||||
| end | |||||
| lint.linters.phpcs = { | |||||
| name = "phpcs", | |||||
| cmd = "phpcs", | |||||
| stdin = true, | |||||
| args = { | |||||
| "-q", | |||||
| "--report=json", | |||||
| "--standard=~/.config/phpcs.xml", | |||||
| "-", -- need `-` at the end for stdin support | |||||
| }, | |||||
| ignore_exitcode = true, | |||||
| parser = function(output, _) | |||||
| if vim.trim(output) == "" or output == nil then | |||||
| return {} | |||||
| end | |||||
| if not vim.startswith(output, "{") then | |||||
| vim.notify(output) | |||||
| return {} | |||||
| end | |||||
| if not vim.startswith(output, "{") then | |||||
| vim.notify(output) | |||||
| return {} | |||||
| end | |||||
| local decoded = vim.json.decode(output) | |||||
| local diagnostics = {} | |||||
| local messages = decoded["files"]["STDIN"]["messages"] | |||||
| local decoded = vim.json.decode(output) | |||||
| local diagnostics = {} | |||||
| local messages = decoded["files"]["STDIN"]["messages"] | |||||
| for _, msg in ipairs(messages or {}) do | |||||
| table.insert(diagnostics, { | |||||
| lnum = msg.line - 1, | |||||
| end_lnum = msg.line - 1, | |||||
| col = msg.column - 1, | |||||
| end_col = msg.column - 1, | |||||
| message = msg.message, | |||||
| code = msg.source, | |||||
| source = "phpcs", | |||||
| severity = assert(severities[msg.type], "missing mapping for severity " .. msg.type), | |||||
| }) | |||||
| end | |||||
| for _, msg in ipairs(messages or {}) do | |||||
| table.insert(diagnostics, { | |||||
| lnum = msg.line - 1, | |||||
| end_lnum = msg.line - 1, | |||||
| col = msg.column - 1, | |||||
| end_col = msg.column - 1, | |||||
| message = msg.message, | |||||
| code = msg.source, | |||||
| source = "phpcs", | |||||
| severity = assert(severities[msg.type], "missing mapping for severity " .. msg.type), | |||||
| }) | |||||
| end | |||||
| return diagnostics | |||||
| end, | |||||
| } | |||||
| return diagnostics | |||||
| end, | |||||
| } | |||||
| lint.linters_by_ft = { | |||||
| javascript = { "eslint" }, | |||||
| typescript = { "eslint" }, | |||||
| vue = { "eslint" }, | |||||
| json = { "jsonlint" }, | |||||
| markdown = { "markdownlint" }, | |||||
| php = { "phpcs" }, | |||||
| golang = { "gospell", "golangci-lint" }, | |||||
| python = { "pylint" }, | |||||
| dockerfile = { "hadolint" }, | |||||
| blade = { "phpcs" }, | |||||
| } | |||||
| -- lint.linters.eslint_d = { | |||||
| -- name = "eslint_d", | |||||
| -- cmd = "eslint_d", | |||||
| -- args = { | |||||
| -- '--config=./frontend/.eslintrc.js', | |||||
| -- '--no-warn-ignored', | |||||
| -- '--format', | |||||
| -- 'json', | |||||
| -- '--stdin', | |||||
| -- '--stdin-filename', | |||||
| -- function() | |||||
| -- return vim.api.nvim_buf_get_name(0) | |||||
| -- end, | |||||
| -- }, | |||||
| -- parser = function(output, _) | |||||
| -- local diagnostics = {} | |||||
| -- local decoded = vim.json.decode(output) | |||||
| -- for _, message in ipairs(decoded[1].messages or {}) do | |||||
| -- table.insert(diagnostics, { | |||||
| -- lnum = message.line - 1, | |||||
| -- end_lnum = message.endLine and message.endLine - 1 or nil, | |||||
| -- col = message.column - 1, | |||||
| -- end_col = message.endColumn and message.endColumn - 1 or nil, | |||||
| -- message = message.message, | |||||
| -- code = message.ruleId, | |||||
| -- source = "eslint_d", | |||||
| -- severity = severities[message.severity], | |||||
| -- }) | |||||
| -- end | |||||
| -- return diagnostics | |||||
| -- end, | |||||
| -- } | |||||
| local lint_augroup = vim.api.nvim_create_augroup("lint", { | |||||
| clear = true, | |||||
| }) | |||||
| lint.linters_by_ft = { | |||||
| javascript = { "eslint" }, | |||||
| typescript = { "eslint" }, | |||||
| vue = { "eslint" }, | |||||
| json = { "jsonlint" }, | |||||
| markdown = { "markdownlint" }, | |||||
| php = { "phpcs" }, | |||||
| golang = { "gospell", "golangci-lint" }, | |||||
| python = { "pylint" }, | |||||
| dockerfile = { "hadolint" }, | |||||
| blade = { "phpcs" }, | |||||
| } | |||||
| vim.api.nvim_create_autocmd({ | |||||
| "BufEnter", | |||||
| "BufWritePost", | |||||
| "InsertLeave", | |||||
| }, { | |||||
| group = lint_augroup, | |||||
| callback = function() | |||||
| lint.try_lint() | |||||
| end, | |||||
| }) | |||||
| local lint_augroup = vim.api.nvim_create_augroup("lint", { | |||||
| clear = true, | |||||
| }) | |||||
| -- vim.keymap.set("n", "<leader>ll", function() | |||||
| -- lint.try_lint() | |||||
| -- end) | |||||
| end, | |||||
| local function find_nearest_node_modules_dir() | |||||
| -- current buffer dir | |||||
| local current_dir = vim.fn.expand('%:p:h') .. "./frontend" | |||||
| while current_dir ~= "/" do | |||||
| if vim.fn.isdirectory(current_dir .. "/node_modules") == 1 then | |||||
| return current_dir | |||||
| end | |||||
| current_dir = vim.fn.fnamemodify(current_dir, ":h") | |||||
| end | |||||
| return nil | |||||
| end | |||||
| vim.api.nvim_create_autocmd({ | |||||
| "BufEnter", | |||||
| "BufWritePost", | |||||
| "InsertLeave", | |||||
| }, { | |||||
| group = lint_augroup, | |||||
| callback = function() | |||||
| local ft = vim.bo.filetype | |||||
| local js_types = { "javascript", "typescript", "javascriptreact", "typescriptreact", "vue" } | |||||
| if not vim.tbl_contains(js_types, ft) then | |||||
| lint.try_lint() | |||||
| return | |||||
| end | |||||
| local original_cwd = vim.fn.getcwd() | |||||
| local node_modules_dir = find_nearest_node_modules_dir() | |||||
| if node_modules_dir then | |||||
| vim.cmd("cd " .. node_modules_dir) | |||||
| end | |||||
| lint.try_lint() | |||||
| vim.cmd("cd " .. original_cwd) | |||||
| end, | |||||
| }) | |||||
| -- vim.keymap.set("n", "<leader>ll", function() | |||||
| -- lint.try_lint() | |||||
| -- end) | |||||
| end, | |||||
| } | } | ||||