You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

  1. return {
  2. "mfussenegger/nvim-lint",
  3. event = { "BufReadPre", "BufNewFile" },
  4. config = function()
  5. local lint = require("lint")
  6. local severities = {
  7. ERROR = vim.diagnostic.severity.ERROR,
  8. WARNING = vim.diagnostic.severity.WARN,
  9. }
  10. lint.linters.phpcs = {
  11. name = "phpcs",
  12. cmd = "phpcs",
  13. stdin = true,
  14. args = {
  15. "-q",
  16. "--report=json",
  17. "--standard=~/.config/phpcs.xml",
  18. "-", -- need `-` at the end for stdin support
  19. },
  20. ignore_exitcode = true,
  21. parser = function(output, _)
  22. if vim.trim(output) == "" or output == nil then
  23. return {}
  24. end
  25. if not vim.startswith(output, "{") then
  26. vim.notify(output)
  27. return {}
  28. end
  29. local decoded = vim.json.decode(output)
  30. local diagnostics = {}
  31. local messages = decoded["files"]["STDIN"]["messages"]
  32. for _, msg in ipairs(messages or {}) do
  33. table.insert(diagnostics, {
  34. lnum = msg.line - 1,
  35. end_lnum = msg.line - 1,
  36. col = msg.column - 1,
  37. end_col = msg.column - 1,
  38. message = msg.message,
  39. code = msg.source,
  40. source = "phpcs",
  41. severity = assert(severities[msg.type], "missing mapping for severity " .. msg.type),
  42. })
  43. end
  44. return diagnostics
  45. end,
  46. }
  47. lint.linters_by_ft = {
  48. javascript = { "eslint" },
  49. typescript = { "eslint" },
  50. vue = { "eslint" },
  51. json = { "jsonlint" },
  52. markdown = { "markdownlint" },
  53. php = { "phpcs" },
  54. golang = { "gospell", "golangci-lint" },
  55. python = { "pylint" },
  56. dockerfile = { "hadolint" },
  57. blade = { "phpcs" },
  58. html = { "curlylint" },
  59. }
  60. local lint_augroup = vim.api.nvim_create_augroup("lint", {
  61. clear = true,
  62. })
  63. local function find_nearest_node_modules_dir()
  64. -- current buffer dir
  65. local current_dir = vim.fn.expand('%:p:h') .. "./frontend"
  66. while current_dir ~= "/" do
  67. if vim.fn.isdirectory(current_dir .. "/node_modules") == 1 then
  68. return current_dir
  69. end
  70. current_dir = vim.fn.fnamemodify(current_dir, ":h")
  71. end
  72. return nil
  73. end
  74. vim.api.nvim_create_autocmd({
  75. "BufEnter",
  76. "BufWritePost",
  77. "InsertLeave",
  78. }, {
  79. group = lint_augroup,
  80. callback = function()
  81. local ft = vim.bo.filetype
  82. local js_types = { "javascript", "typescript", "javascriptreact", "typescriptreact", "vue" }
  83. if not vim.tbl_contains(js_types, ft) then
  84. lint.try_lint()
  85. return
  86. end
  87. local original_cwd = vim.fn.getcwd()
  88. local node_modules_dir = find_nearest_node_modules_dir()
  89. if node_modules_dir then
  90. vim.cmd("cd " .. node_modules_dir)
  91. end
  92. lint.try_lint()
  93. vim.cmd("cd " .. original_cwd)
  94. end,
  95. })
  96. -- vim.keymap.set("n", "<leader>ll", function()
  97. -- lint.try_lint()
  98. -- end)
  99. end,
  100. }