ts-fmt-lint.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -- Example configuations here: https://github.com/mattn/efm-langserver
  2. -- You can look for project scope Prettier and Eslint with e.g. vim.fn.glob("node_modules/.bin/prettier") etc. If it is not found revert to global Prettier where needed.
  3. local M = {}
  4. M.setup = function()
  5. local tsserver_args = {}
  6. local prettier = {
  7. formatCommand = "prettier --stdin-filepath ${INPUT}",
  8. formatStdin = true
  9. }
  10. if vim.fn.glob("node_modules/.bin/prettier") then
  11. prettier = {
  12. formatCommand = "./node_modules/.bin/prettier --stdin-filepath ${INPUT}",
  13. formatStdin = true
  14. }
  15. end
  16. -- TODO global eslint?
  17. local eslint = {
  18. lintCommand = "./node_modules/.bin/eslint -f unix --stdin --stdin-filename ${INPUT}",
  19. lintIgnoreExitCode = true,
  20. lintStdin = true,
  21. lintFormats = {"%f:%l:%c: %m"},
  22. -- formatCommand = "./node_modules/.bin/eslint -f unix --fix --stdin-filename ${INPUT}", -- TODO check if eslint is the formatter then add this
  23. formatStdin = true
  24. }
  25. if O.lang.tsserver.formatter == 'prettier' then
  26. table.insert(tsserver_args, prettier)
  27. end
  28. if O.lang.tsserver.linter == 'eslint' then
  29. table.insert(tsserver_args, eslint)
  30. end
  31. require"lspconfig".efm.setup {
  32. -- init_options = {initializationOptions},
  33. cmd = {DATA_PATH .. "/lspinstall/efm/efm-langserver"},
  34. init_options = {documentFormatting = true, codeAction = false},
  35. filetypes = {
  36. "javascriptreact", "javascript", "typescript", "typescriptreact",
  37. "html", "css", "yaml", "vue"
  38. },
  39. settings = {
  40. rootMarkers = {".git/", "package.json"},
  41. languages = {
  42. javascript = tsserver_args,
  43. javascriptreact = tsserver_args,
  44. typescript = tsserver_args,
  45. typescriptreact = tsserver_args,
  46. html = {prettier},
  47. css = {prettier},
  48. json = {prettier},
  49. yaml = {prettier}
  50. -- javascriptreact = {prettier, eslint},
  51. -- javascript = {prettier, eslint},
  52. -- markdown = {markdownPandocFormat, markdownlint},
  53. }
  54. }
  55. }
  56. end
  57. return M