lsp.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. on_attach = function(client, bufnr)
  23. -- add formatting capability, the language server registers this
  24. -- dynamically but neovim does not support that yet
  25. -- https://github.com/microsoft/vscode-eslint/pull/1307
  26. client.server_capabilities.documentFormattingProvider = true
  27. client.server_capabilities.documentRangeFormattingProvider = true
  28. -- mappings should have been attached by typescript and re-attaching can
  29. -- overwrite the typescript specific overrides
  30. end,
  31. })
  32. end
  33. if vim.fn.executable("deno") == 1 then
  34. nvim_lsp.denols.setup({
  35. capabilities = capabilities,
  36. on_attach = on_attach,
  37. });
  38. else
  39. require('typescript').setup({
  40. server = {
  41. init_options = {
  42. completionDisableFilterText = true,
  43. preferences = {
  44. importModuleSpecifierPreference = 'non-relative',
  45. },
  46. },
  47. capabilities = capabilities,
  48. handlers = {
  49. ['$/typescriptVersion'] = function(err, result, ctx, config)
  50. vim.notify(string.format('Typescript %s', result.version))
  51. end
  52. },
  53. flags = {
  54. debounce_text_changes = 150,
  55. },
  56. on_attach = function(client, bufnr)
  57. -- mark tsserver as not having formatting available as we rely on
  58. -- eslint for that
  59. client.server_capabilities.documentFormattingProvider = false
  60. client.server_capabilities.documentRangeFormattingProvider = false
  61. on_attach(client, bufnr)
  62. -- override mappings for typescript
  63. local opts = { silent = true, buffer = bufnr }
  64. -- exclude import statements from reference search (may have false positives)
  65. vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
  66. vim.keymap.set('n', 'gd', '<cmd>TypescriptGoToSourceDefinition<CR>', opts)
  67. vim.keymap.set('n', 'gD', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
  68. end
  69. }
  70. })
  71. end
  72. if vim.fn.executable("gopls") == 1 then
  73. nvim_lsp.gopls.setup({
  74. capabilities = capabilities,
  75. on_attach = on_attach,
  76. });
  77. end
  78. if vim.fn.executable("solargraph") == 1 then
  79. nvim_lsp.solargraph.setup({
  80. capabilities = capabilities,
  81. on_attach = on_attach,
  82. init_options = {
  83. formatting = false,
  84. }
  85. })
  86. end
  87. if vim.fn.executable("nil") == 1 then
  88. nvim_lsp.nil_ls.setup({
  89. on_attach = on_attach,
  90. });
  91. end
  92. -- custom LSP servers
  93. local configs = require('lspconfig.configs')
  94. if not configs.elvish then
  95. configs.elvish = {
  96. default_config = {
  97. cmd = {'elvish', '--lsp'},
  98. filetypes = {'elvish'},
  99. root_dir = nvim_lsp.util.root_pattern('*.elv'),
  100. settings = {},
  101. },
  102. }
  103. end
  104. nvim_lsp.elvish.setup({
  105. capabilities = capabilities,
  106. on_attach = on_attach,
  107. })