lsp.lua 3.8 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. 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("deno") == 1 then
  33. nvim_lsp.denols.setup({
  34. capabilities = capabilities,
  35. on_attach = on_attach,
  36. });
  37. else
  38. require('typescript').setup({
  39. server = {
  40. init_options = {
  41. completionDisableFilterText = true,
  42. preferences = {
  43. importModuleSpecifierPreference = 'non-relative',
  44. },
  45. },
  46. capabilities = capabilities,
  47. handlers = {
  48. ['$/typescriptVersion'] = function(err, result, ctx, config)
  49. vim.notify(string.format('Typescript %s', result.version))
  50. end
  51. },
  52. flags = {
  53. debounce_text_changes = 150,
  54. },
  55. on_attach = function(client, bufnr)
  56. -- mark tsserver as not having formatting available as we rely on
  57. -- eslint for that
  58. client.server_capabilities.documentFormattingProvider = false
  59. client.server_capabilities.documentRangeFormattingProvider = false
  60. on_attach(client, bufnr)
  61. -- override mappings for typescript
  62. local opts = { silent = true, buffer = bufnr }
  63. -- exclude import statements from reference search (may have false positives)
  64. vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
  65. end
  66. }
  67. })
  68. end
  69. if vim.fn.executable("gopls") == 1 then
  70. nvim_lsp.gopls.setup({
  71. capabilities = capabilities,
  72. on_attach = on_attach,
  73. });
  74. end
  75. if vim.fn.executable("solargraph") == 1 then
  76. nvim_lsp.solargraph.setup({
  77. capabilities = capabilities,
  78. on_attach = on_attach,
  79. init_options = {
  80. formatting = false,
  81. }
  82. })
  83. end
  84. if vim.fn.executable("nil") == 1 then
  85. nvim_lsp.nil_ls.setup({
  86. on_attach = on_attach,
  87. });
  88. end
  89. -- format on save
  90. local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
  91. vim.api.nvim_create_autocmd('LspAttach', {
  92. callback = function(args)
  93. local bufnr = args.buf
  94. local client = vim.lsp.get_client_by_id(args.data.client_id)
  95. if client.server_capabilities.documentFormattingProvider then
  96. for key, cmd in pairs(vim.api.nvim_get_autocmds({ group = group, buffer = bufnr })) do
  97. vim.api.nvim_del_autocmd(cmd.id)
  98. end
  99. vim.api.nvim_create_autocmd('BufWritePre', {
  100. group = group,
  101. buffer = bufnr,
  102. callback = function()
  103. vim.lsp.buf.format()
  104. end,
  105. })
  106. end
  107. end,
  108. })
  109. -- custom LSP servers
  110. local configs = require('lspconfig.configs')
  111. if not configs.elvish then
  112. configs.elvish = {
  113. default_config = {
  114. cmd = {'elvish', '--lsp'},
  115. filetypes = {'elvish'},
  116. root_dir = nvim_lsp.util.root_pattern('*.elv'),
  117. settings = {},
  118. },
  119. }
  120. end
  121. nvim_lsp.elvish.setup({
  122. capabilities = capabilities,
  123. on_attach = on_attach,
  124. })
  125. -- show LSP progress bar
  126. require('fidget').setup()