lsp.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. local null_ls = require('null-ls')
  8. local null_ls_sources = {
  9. null_ls.builtins.code_actions.gitsigns,
  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. on_attach(client, bufnr)
  29. end,
  30. })
  31. end
  32. if vim.fn.executable("shellcheck") == 1 then
  33. table.insert(null_ls_sources, null_ls.builtins.diagnostics.shellcheck)
  34. table.insert(null_ls_sources, null_ls.builtins.code_actions.shellcheck)
  35. end
  36. if vim.fn.executable("deno") == 1 then
  37. nvim_lsp.denols.setup({
  38. capabilities = capabilities,
  39. on_attach = on_attach,
  40. });
  41. else
  42. require('typescript').setup({
  43. server = {
  44. init_options = {
  45. completionDisableFilterText = true,
  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. end
  67. }
  68. })
  69. end
  70. if vim.fn.executable("gopls") == 1 then
  71. nvim_lsp.gopls.setup({
  72. capabilities = capabilities,
  73. on_attach = on_attach,
  74. });
  75. elseif vim.fn.executable("gofmt") == 1 then
  76. table.insert(null_ls_sources, null_ls.builtins.formatting.gofmt)
  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. local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
  93. null_ls.setup({
  94. sources = null_ls_sources,
  95. on_attach = function(client, bufnr)
  96. -- format on save
  97. if client.server_capabilities.documentFormattingProvider then
  98. for key, cmd in pairs(vim.api.nvim_get_autocmds({ group = group, buffer = bufnr })) do
  99. vim.api.nvim_del_autocmd(cmd.id)
  100. end
  101. vim.api.nvim_create_autocmd('BufWritePre', {
  102. group = group,
  103. buffer = bufnr,
  104. callback = function()
  105. vim.lsp.buf.format()
  106. end,
  107. })
  108. end
  109. end,
  110. });
  111. -- custom LSP servers
  112. local configs = require('lspconfig.configs')
  113. if not configs.elvish then
  114. configs.elvish = {
  115. default_config = {
  116. cmd = {'elvish', '--lsp'},
  117. filetypes = {'elvish'},
  118. root_dir = nvim_lsp.util.root_pattern('*.elv'),
  119. settings = {},
  120. },
  121. }
  122. end
  123. nvim_lsp.elvish.setup({
  124. capabilities = capabilities,
  125. on_attach = on_attach,
  126. })
  127. -- show LSP progress bar
  128. require('fidget').setup()