ts-fmt-lint.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. -- Example configuations here: https://github.com/mattn/efm-langserver
  2. local M = {}
  3. M.setup = function()
  4. vim.cmd "let proj = FindRootDirectory()"
  5. local root_dir = vim.api.nvim_get_var "proj"
  6. local get_linter_instance = function()
  7. -- prioritize local instance over global
  8. local local_instance = root_dir .. "/node_modules/.bin/" .. O.lang.tsserver.linter
  9. if vim.fn.executable(local_instance) == 1 then
  10. return local_instance
  11. end
  12. return O.lang.tsserver.linter
  13. end
  14. local tsserver_args = {}
  15. local formattingSupported = false
  16. if O.lang.tsserver.linter == "eslint" or O.lang.tsserver.linter == "eslint_d" then
  17. local eslint = {
  18. lintCommand = get_linter_instance() .. " -f visualstudio --stdin --stdin-filename ${INPUT}",
  19. lintStdin = true,
  20. lintFormats = {
  21. "%f(%l,%c): %tarning %m",
  22. "%f(%l,%c): %trror %m",
  23. },
  24. lintSource = O.lang.tsserver.linter,
  25. lintIgnoreExitCode = true,
  26. }
  27. table.insert(tsserver_args, eslint)
  28. -- Only eslint_d supports --fix-to-stdout
  29. if string.find(get_linter_instance(), "eslint_d") then
  30. formattingSupported = true
  31. local eslint_fix = {
  32. formatCommand = get_linter_instance() .. " --fix-to-stdout --stdin --stdin-filename ${INPUT}",
  33. formatStdin = true,
  34. }
  35. table.insert(tsserver_args, eslint_fix)
  36. end
  37. end
  38. require("lspconfig").efm.setup {
  39. -- init_options = {initializationOptions},
  40. cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
  41. init_options = { documentFormatting = formattingSupported, codeAction = false },
  42. root_dir = require("lspconfig").util.root_pattern(".git/", "package.json"),
  43. filetypes = {
  44. "vue",
  45. "javascript",
  46. "javascriptreact",
  47. "typescript",
  48. "typescriptreact",
  49. "javascript.jsx",
  50. "typescript.tsx",
  51. },
  52. settings = {
  53. rootMarkers = { ".git/", "package.json" },
  54. languages = {
  55. vue = tsserver_args,
  56. javascript = tsserver_args,
  57. javascriptreact = tsserver_args,
  58. ["javascript.jsx"] = tsserver_args,
  59. typescript = tsserver_args,
  60. ["typescript.tsx"] = tsserver_args,
  61. typescriptreact = tsserver_args,
  62. },
  63. },
  64. }
  65. end
  66. return M