lsp.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. -- only show virtual text for WARN and higher
  2. vim.diagnostic.config({
  3. virtual_text = { severity = { min = vim.diagnostic.severity.WARN } }
  4. })
  5. local nvim_lsp = require('lspconfig')
  6. local null_ls = require('null-ls')
  7. local null_ls_sources = {
  8. null_ls.builtins.code_actions.gitsigns,
  9. }
  10. -- enable LS / null-ls sources based on executable presence
  11. if vim.fn.executable("node_modules/.bin/eslint") == 1 then
  12. table.insert(null_ls_sources, null_ls.builtins.formatting.eslint_d)
  13. table.insert(null_ls_sources, null_ls.builtins.diagnostics.eslint_d)
  14. table.insert(null_ls_sources, null_ls.builtins.code_actions.eslint_d)
  15. end
  16. if vim.fn.executable("shellcheck") == 1 then
  17. table.insert(null_ls_sources, null_ls.builtins.diagnostics.shellcheck)
  18. table.insert(null_ls_sources, null_ls.builtins.code_actions.shellcheck)
  19. end
  20. if vim.fn.executable("deno") == 1 then
  21. nvim_lsp.denols.setup({
  22. on_attach = on_attach,
  23. });
  24. end
  25. if vim.fn.executable("gopls") == 1 then
  26. nvim_lsp.gopls.setup({
  27. on_attach = on_attach,
  28. });
  29. elseif vim.fn.executable("gofmt") == 1 then
  30. table.insert(null_ls_sources, null_ls.builtins.formatting.gofmt)
  31. end
  32. if vim.fn.executable("node_modules/.bin/tsc") == 1 then
  33. require('typescript').setup({
  34. server = {
  35. flags = {
  36. debounce_text_changes = 150,
  37. },
  38. on_attach = function(client, bufnr)
  39. -- mark tsserver as not having formatting available as we rely on
  40. -- null-ls/eslint for that and having both available makes nvim ask us
  41. -- which LS to use everytime we format
  42. client.resolved_capabilities.document_formatting = false
  43. client.resolved_capabilities.document_range_formatting = false
  44. on_attach(client, bufnr)
  45. -- override mappings for typescript
  46. local opts = { silent = true, buffer = bufnr }
  47. -- exclude import statements from reference search (may have false positives)
  48. vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
  49. end
  50. }
  51. })
  52. end
  53. if vim.fn.executable("solargraph") == 1 then
  54. nvim_lsp.solargraph.setup({
  55. on_attach = on_attach,
  56. init_options = {
  57. formatting = false,
  58. }
  59. })
  60. end
  61. local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
  62. null_ls.setup({
  63. sources = null_ls_sources,
  64. on_attach = function(client, bufnr)
  65. -- format on save
  66. if client.resolved_capabilities.document_formatting then
  67. for key, cmd in pairs(vim.api.nvim_get_autocmds({ group = group, buffer = bufnr })) do
  68. vim.api.nvim_del_autocmd(cmd.id)
  69. end
  70. vim.api.nvim_create_autocmd('BufWritePre', {
  71. group = group,
  72. buffer = bufnr,
  73. callback = vim.lsp.buf.formatting_sync,
  74. })
  75. end
  76. end,
  77. });
  78. -- show LSP progress bar
  79. require('fidget').setup()