lsp.lua 3.6 KB

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