lsp.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. local function make_settings()
  17. -- we disable formatting but these are still used when performing some code
  18. -- actions
  19. local format = {
  20. indentSize = vim.bo.shiftwidth,
  21. convertTabsToSpaces = vim.o.expandtab,
  22. }
  23. return {
  24. javascript = { format = format },
  25. typescript = { format = format },
  26. }
  27. end
  28. vim.lsp.config("ts_ls", {
  29. init_options = {
  30. completionDisableFilterText = true,
  31. preferences = {
  32. importModuleSpecifierPreference = 'non-relative',
  33. -- this prevents renames from aliasing when destructuring
  34. providePrefixAndSuffixTextForRename = false,
  35. },
  36. },
  37. settings = make_settings(),
  38. handlers = {
  39. ['$/typescriptVersion'] = function(err, result, ctx, config)
  40. vim.notify(string.format('Typescript %s', result.version))
  41. end
  42. },
  43. flags = {
  44. debounce_text_changes = 150,
  45. },
  46. on_init = function(client)
  47. -- mark tsserver as not having formatting available as we rely on
  48. -- eslint and dprint for that
  49. client.server_capabilities.documentFormattingProvider = false
  50. client.server_capabilities.documentRangeFormattingProvider = false
  51. -- we only really know our settings once we've opened a file so report
  52. -- the new formatting settings
  53. client:notify(vim.lsp.protocol.Methods.workspace_didChangeConfiguration, {
  54. settings = make_settings(),
  55. })
  56. end,
  57. on_attach = function(client)
  58. -- add custom commands, we follow upstream's Lsp* prefix convention
  59. -- setup custom source actions, unlike code actions these apply to the
  60. -- whole file rather than a particular line.
  61. local function source_action(usercmd_name, command_name)
  62. vim.api.nvim_buf_create_user_command(0, usercmd_name, function()
  63. vim.lsp.buf.code_action({
  64. context = { only = { command_name } },
  65. apply = true,
  66. })
  67. end, {})
  68. end
  69. source_action("LspAddMissingImports", "source.addMissingImports.ts")
  70. source_action("LspOrganizeImports", "source.organizeImports.ts")
  71. source_action("LspRemoveUnusedImports", "source.removeUnusedImports.ts")
  72. -- rename file with import renaming, this does no error checking
  73. vim.api.nvim_buf_create_user_command(0, "LspRenameFile", function()
  74. local source = vim.api.nvim_buf_get_name(0)
  75. local function do_rename(target)
  76. if target == nil then
  77. return
  78. end
  79. -- rename the buffer
  80. vim.lsp.util.rename(source, target)
  81. -- ask LSP to rename imports
  82. client:exec_cmd({
  83. command = "_typescript.applyRenameFile",
  84. arguments = {
  85. {
  86. sourceUri = vim.uri_from_fname(source),
  87. targetUri = vim.uri_from_fname(target),
  88. }
  89. }
  90. })
  91. end
  92. vim.ui.input({
  93. prompt = "New Filename: ",
  94. completion = "file",
  95. default = source,
  96. }, do_rename)
  97. end, {})
  98. end,
  99. })
  100. vim.lsp.enable("ts_ls")
  101. end
  102. if vim.fn.executable("gopls") == 1 then
  103. vim.lsp.enable("gopls")
  104. end
  105. if vim.fn.executable("ruby-lsp") == 1 then
  106. vim.lsp.config("ruby_lsp", {
  107. init_options = {
  108. enabledFeatures = {
  109. formatting = false,
  110. },
  111. }
  112. })
  113. vim.lsp.enable("ruby_lsp")
  114. end
  115. if vim.fn.executable("nil") == 1 then
  116. vim.lsp.enable("nil_ls")
  117. end
  118. if vim.fn.executable("jdtls") == 1 then
  119. vim.lsp.config("jdtls", {
  120. handlers = {
  121. ["$/progress"] = function()
  122. -- this is quite noisy so just disable it
  123. end
  124. },
  125. })
  126. vim.lsp.enable("jdtls")
  127. end
  128. -- custom LSP servers
  129. vim.lsp.config("elvish", {
  130. cmd = {'elvish', '--lsp'},
  131. filetypes = {'elvish'},
  132. settings = {},
  133. })
  134. vim.lsp.enable("elvish")