lsp.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. vim.diagnostic.config({
  2. -- only show virtual text for WARN and higher
  3. virtual_text = { severity = { min = vim.diagnostic.severity.WARN } },
  4. })
  5. vim.lsp.enable("bashls");
  6. vim.lsp.enable("eslint");
  7. if vim.fn.executable("dprint") == 1 then
  8. local version = vim.version.parse(vim.fn.system("dprint --version"))
  9. if vim.version.cmp(version, {0,45,0}) >= 0 then
  10. vim.lsp.enable("dprint")
  11. end
  12. end
  13. if vim.fn.executable("deno") == 1 then
  14. vim.lsp.enable("deno");
  15. else
  16. vim.lsp.config("ts_ls", {
  17. init_options = {
  18. completionDisableFilterText = true,
  19. preferences = {
  20. importModuleSpecifierPreference = 'non-relative',
  21. -- this prevents renames from aliasing when destructuring
  22. providePrefixAndSuffixTextForRename = false,
  23. },
  24. },
  25. handlers = {
  26. ['$/typescriptVersion'] = function(err, result, ctx, config)
  27. vim.notify(string.format('Typescript %s', result.version))
  28. end
  29. },
  30. flags = {
  31. debounce_text_changes = 150,
  32. },
  33. on_init = function(client)
  34. -- mark tsserver as not having formatting available as we rely on
  35. -- eslint and dprint for that
  36. client.server_capabilities.documentFormattingProvider = false
  37. client.server_capabilities.documentRangeFormattingProvider = false
  38. end,
  39. on_attach = function(client)
  40. -- add custom commands, we follow upstream's Lsp* prefix convention
  41. -- setup custom source actions, unlike code actions these apply to the
  42. -- whole file rather than a particular line.
  43. local function source_action(usercmd_name, command_name)
  44. vim.api.nvim_buf_create_user_command(0, usercmd_name, function()
  45. vim.lsp.buf.code_action({
  46. context = { only = { command_name } },
  47. apply = true,
  48. })
  49. end, {})
  50. end
  51. source_action("LspAddMissingImports", "source.addMissingImports.ts")
  52. source_action("LspOrganizeImports", "source.organizeImports.ts")
  53. source_action("LspRemoveUnusedImports", "source.removeUnusedImports.ts")
  54. -- rename file with import renaming, this does no error checking
  55. vim.api.nvim_buf_create_user_command(0, "LspRenameFile", function()
  56. local source = vim.api.nvim_buf_get_name(0)
  57. local function do_rename(target)
  58. if target == nil then
  59. return
  60. end
  61. -- rename the buffer
  62. vim.lsp.util.rename(source, target)
  63. -- ask LSP to rename imports
  64. client:exec_cmd({
  65. command = "_typescript.applyRenameFile",
  66. arguments = {
  67. {
  68. sourceUri = vim.uri_from_fname(source),
  69. targetUri = vim.uri_from_fname(target),
  70. }
  71. }
  72. })
  73. end
  74. vim.ui.input({
  75. prompt = "New Filename: ",
  76. completion = "file",
  77. default = source,
  78. }, do_rename)
  79. end, {})
  80. end,
  81. })
  82. vim.lsp.enable("ts_ls")
  83. end
  84. if vim.fn.executable("gopls") == 1 then
  85. vim.lsp.enable("gopls")
  86. end
  87. if vim.fn.executable("ruby-lsp") == 1 then
  88. vim.lsp.config("ruby_lsp", {
  89. init_options = {
  90. enabledFeatures = {
  91. formatting = false,
  92. },
  93. }
  94. })
  95. vim.lsp.enable("ruby_lsp")
  96. end
  97. if vim.fn.executable("nil") == 1 then
  98. vim.lsp.enable("nil_ls")
  99. end
  100. if vim.fn.executable("jdtls") == 1 then
  101. vim.lsp.config("jdtls", {
  102. handlers = {
  103. ["$/progress"] = function()
  104. -- this is quite noisy so just disable it
  105. end
  106. },
  107. })
  108. vim.lsp.enable("jdtls")
  109. end
  110. -- custom LSP servers
  111. vim.lsp.config("elvish", {
  112. cmd = {'elvish', '--lsp'},
  113. filetypes = {'elvish'},
  114. settings = {},
  115. })
  116. vim.lsp.enable("elvish")