lsp.lua 3.7 KB

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