lsp.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. vim.diagnostic.config({
  2. -- only show virtual text for WARN and higher
  3. virtual_text = { severity = { min = vim.diagnostic.severity.WARN } },
  4. -- show diagnostic code in float if available
  5. float = {
  6. prefix = function(diagnostic, i)
  7. if diagnostic.code == nil then
  8. return string.format("[%s] ", vim.diagnostic.severity[diagnostic.severity])
  9. else
  10. return string.format("[%s] ", diagnostic.code)
  11. end
  12. end,
  13. },
  14. })
  15. local nvim_lsp = require('lspconfig')
  16. local null_ls = require('null-ls')
  17. local null_ls_sources = {
  18. null_ls.builtins.code_actions.gitsigns,
  19. }
  20. if vim.fn.executable("node_modules/.bin/eslint") == 1 then
  21. local options = {}
  22. -- some projects have local rules, this isn't exactly generic but is good
  23. -- enough
  24. if vim.fn.isdirectory("lint-rules") == 1 then
  25. options.rulePaths = { "./lint-rules" }
  26. end
  27. nvim_lsp.eslint.setup({
  28. settings = {
  29. options = options,
  30. },
  31. on_attach = function(client, bufnr)
  32. -- add formatting capability, the language server registers this
  33. -- dynamically but neovim does not support that yet
  34. -- https://github.com/microsoft/vscode-eslint/pull/1307
  35. client.server_capabilities.documentFormattingProvider = true
  36. client.server_capabilities.documentRangeFormattingProvider = true
  37. on_attach(client, bufnr)
  38. end,
  39. })
  40. end
  41. if vim.fn.executable("shellcheck") == 1 then
  42. table.insert(null_ls_sources, null_ls.builtins.diagnostics.shellcheck)
  43. table.insert(null_ls_sources, null_ls.builtins.code_actions.shellcheck)
  44. end
  45. if vim.fn.executable("deno") == 1 then
  46. nvim_lsp.denols.setup({
  47. on_attach = on_attach,
  48. });
  49. else
  50. require('typescript').setup({
  51. server = {
  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. on_attach = on_attach,
  72. });
  73. elseif vim.fn.executable("gofmt") == 1 then
  74. table.insert(null_ls_sources, null_ls.builtins.formatting.gofmt)
  75. end
  76. if vim.fn.executable("solargraph") == 1 then
  77. nvim_lsp.solargraph.setup({
  78. on_attach = on_attach,
  79. init_options = {
  80. formatting = false,
  81. }
  82. })
  83. end
  84. local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
  85. null_ls.setup({
  86. sources = null_ls_sources,
  87. on_attach = function(client, bufnr)
  88. -- format on save
  89. if client.server_capabilities.documentFormattingProvider then
  90. for key, cmd in pairs(vim.api.nvim_get_autocmds({ group = group, buffer = bufnr })) do
  91. vim.api.nvim_del_autocmd(cmd.id)
  92. end
  93. vim.api.nvim_create_autocmd('BufWritePre', {
  94. group = group,
  95. buffer = bufnr,
  96. callback = function()
  97. vim.lsp.buf.format()
  98. end,
  99. })
  100. end
  101. end,
  102. });
  103. -- custom LSP servers
  104. local configs = require('lspconfig.configs')
  105. if not configs.elvish then
  106. configs.elvish = {
  107. default_config = {
  108. cmd = {'elvish', '--lsp'},
  109. filetypes = {'elvish'},
  110. root_dir = nvim_lsp.util.root_pattern('*.elv'),
  111. settings = {},
  112. },
  113. }
  114. end
  115. nvim_lsp.elvish.setup({
  116. on_attach = on_attach,
  117. })
  118. -- show LSP progress bar
  119. require('fidget').setup()