lsp.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. vim.diagnostic.config({
  2. -- only show virtual text for WARN and higher
  3. virtual_text = { severity = { min = vim.diagnostic.severity.WARN } },
  4. })
  5. local capabilities = require('cmp_nvim_lsp').default_capabilities()
  6. local nvim_lsp = require('lspconfig')
  7. nvim_lsp.bashls.setup({
  8. capabilities = capabilities,
  9. on_attach = on_attach,
  10. });
  11. if vim.fn.executable("node_modules/.bin/eslint") == 1 then
  12. local options = {}
  13. -- some projects have local rules, this isn't exactly generic but is good
  14. -- enough
  15. if vim.fn.isdirectory("lint-rules") == 1 then
  16. options.rulePaths = { "./lint-rules" }
  17. end
  18. nvim_lsp.eslint.setup({
  19. settings = {
  20. options = options,
  21. },
  22. handlers = {
  23. ["client/registerCapability"] = function(err, result, ctx, config)
  24. -- ignore as we can't handle any of this and it just spams the logs
  25. return vim.NIL
  26. end
  27. },
  28. on_init = function(client)
  29. -- add formatting capability, the language server registers this
  30. -- dynamically but neovim does not support that yet
  31. -- https://github.com/microsoft/vscode-eslint/pull/1307
  32. client.server_capabilities.documentFormattingProvider = true
  33. client.server_capabilities.documentRangeFormattingProvider = true
  34. end,
  35. -- mappings should have been attached by typescript and re-attaching can
  36. -- overwrite the typescript specific overrides
  37. })
  38. end
  39. if vim.fn.executable("dprint") == 1 then
  40. local version = vim.version.parse(vim.fn.system("dprint --version"))
  41. if vim.version.cmp(version, {0,45,0}) >= 0 then
  42. nvim_lsp.dprint.setup({})
  43. end
  44. end
  45. if vim.fn.executable("deno") == 1 then
  46. nvim_lsp.denols.setup({
  47. capabilities = capabilities,
  48. on_attach = on_attach,
  49. });
  50. else
  51. require('typescript').setup({
  52. server = {
  53. init_options = {
  54. completionDisableFilterText = true,
  55. preferences = {
  56. importModuleSpecifierPreference = 'non-relative',
  57. },
  58. },
  59. capabilities = capabilities,
  60. handlers = {
  61. ['$/typescriptVersion'] = function(err, result, ctx, config)
  62. vim.notify(string.format('Typescript %s', result.version))
  63. end
  64. },
  65. flags = {
  66. debounce_text_changes = 150,
  67. },
  68. on_init = function(client)
  69. -- mark tsserver as not having formatting available as we rely on
  70. -- eslint and dprint for that
  71. client.server_capabilities.documentFormattingProvider = false
  72. client.server_capabilities.documentRangeFormattingProvider = false
  73. end,
  74. on_attach = function(client, bufnr)
  75. on_attach(client, bufnr)
  76. -- override mappings for typescript
  77. local opts = { silent = true, buffer = bufnr }
  78. -- exclude import statements from reference search (may have false positives)
  79. vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
  80. vim.keymap.set('n', 'gD', '<cmd>Telescope lsp_definitions<CR>', opts)
  81. vim.keymap.set('n', 'gd', '<cmd>TypescriptGoToSourceDefinition<CR>', opts)
  82. end
  83. }
  84. })
  85. end
  86. if vim.fn.executable("gopls") == 1 then
  87. nvim_lsp.gopls.setup({
  88. capabilities = capabilities,
  89. on_attach = on_attach,
  90. });
  91. end
  92. if vim.fn.executable("solargraph") == 1 then
  93. nvim_lsp.solargraph.setup({
  94. capabilities = capabilities,
  95. on_attach = on_attach,
  96. init_options = {
  97. formatting = false,
  98. }
  99. })
  100. end
  101. if vim.fn.executable("nil") == 1 then
  102. nvim_lsp.nil_ls.setup({
  103. on_attach = on_attach,
  104. });
  105. end
  106. if vim.fn.executable("jdtls") == 1 then
  107. nvim_lsp.jdtls.setup({
  108. on_attach = on_attach,
  109. handlers = {
  110. ["$/progress"] = function()
  111. -- this is quite noisy so just disable it
  112. end
  113. },
  114. });
  115. end
  116. -- custom LSP servers
  117. local configs = require('lspconfig.configs')
  118. if not configs.elvish then
  119. configs.elvish = {
  120. default_config = {
  121. cmd = {'elvish', '--lsp'},
  122. filetypes = {'elvish'},
  123. root_dir = nvim_lsp.util.root_pattern('*.elv'),
  124. settings = {},
  125. },
  126. }
  127. end
  128. nvim_lsp.elvish.setup({
  129. capabilities = capabilities,
  130. on_attach = on_attach,
  131. })