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.

144 lines
4.9 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.eslint_d = {
  48. -- name = "eslint_d",
  49. -- cmd = "eslint_d",
  50. -- args = {
  51. -- '--config=./frontend/.eslintrc.js',
  52. -- '--no-warn-ignored',
  53. -- '--format',
  54. -- 'json',
  55. -- '--stdin',
  56. -- '--stdin-filename',
  57. -- function()
  58. -- return vim.api.nvim_buf_get_name(0)
  59. -- end,
  60. -- },
  61. -- parser = function(output, _)
  62. -- local diagnostics = {}
  63. -- local decoded = vim.json.decode(output)
  64. -- for _, message in ipairs(decoded[1].messages or {}) do
  65. -- table.insert(diagnostics, {
  66. -- lnum = message.line - 1,
  67. -- end_lnum = message.endLine and message.endLine - 1 or nil,
  68. -- col = message.column - 1,
  69. -- end_col = message.endColumn and message.endColumn - 1 or nil,
  70. -- message = message.message,
  71. -- code = message.ruleId,
  72. -- source = "eslint_d",
  73. -- severity = severities[message.severity],
  74. -- })
  75. -- end
  76. -- return diagnostics
  77. -- end,
  78. -- }
  79. lint.linters_by_ft = {
  80. javascript = { "eslint" },
  81. typescript = { "eslint" },
  82. vue = { "eslint" },
  83. json = { "jsonlint" },
  84. markdown = { "markdownlint" },
  85. php = { "phpcs" },
  86. golang = { "gospell", "golangci-lint" },
  87. python = { "pylint" },
  88. dockerfile = { "hadolint" },
  89. blade = { "phpcs" },
  90. }
  91. local lint_augroup = vim.api.nvim_create_augroup("lint", {
  92. clear = true,
  93. })
  94. local function find_nearest_node_modules_dir()
  95. -- current buffer dir
  96. local current_dir = vim.fn.expand('%:p:h') .. "./frontend"
  97. while current_dir ~= "/" do
  98. if vim.fn.isdirectory(current_dir .. "/node_modules") == 1 then
  99. return current_dir
  100. end
  101. current_dir = vim.fn.fnamemodify(current_dir, ":h")
  102. end
  103. return nil
  104. end
  105. vim.api.nvim_create_autocmd({
  106. "BufEnter",
  107. "BufWritePost",
  108. "InsertLeave",
  109. }, {
  110. group = lint_augroup,
  111. callback = function()
  112. local ft = vim.bo.filetype
  113. local js_types = { "javascript", "typescript", "javascriptreact", "typescriptreact", "vue" }
  114. if not vim.tbl_contains(js_types, ft) then
  115. lint.try_lint()
  116. return
  117. end
  118. local original_cwd = vim.fn.getcwd()
  119. local node_modules_dir = find_nearest_node_modules_dir()
  120. if node_modules_dir then
  121. vim.cmd("cd " .. node_modules_dir)
  122. end
  123. lint.try_lint()
  124. vim.cmd("cd " .. original_cwd)
  125. end,
  126. })
  127. -- vim.keymap.set("n", "<leader>ll", function()
  128. -- lint.try_lint()
  129. -- end)
  130. end,
  131. }