lsp.lua 3.8 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. -- 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. options = {}
  22. -- some projects have local rules, this isn't exactly generic but is good
  23. -- enough
  24. if vim.fn.isdirectory("lint-rules") 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. end
  50. if vim.fn.executable("gopls") == 1 then
  51. nvim_lsp.gopls.setup({
  52. on_attach = on_attach,
  53. });
  54. elseif vim.fn.executable("gofmt") == 1 then
  55. table.insert(null_ls_sources, null_ls.builtins.formatting.gofmt)
  56. end
  57. if vim.fn.executable("node_modules/.bin/tsc") == 1 then
  58. require('typescript').setup({
  59. server = {
  60. flags = {
  61. debounce_text_changes = 150,
  62. },
  63. on_attach = function(client, bufnr)
  64. -- mark tsserver as not having formatting available as we rely on
  65. -- eslint for that
  66. client.server_capabilities.documentFormattingProvider = false
  67. client.server_capabilities.documentRangeFormattingProvider = false
  68. on_attach(client, bufnr)
  69. -- override mappings for typescript
  70. local opts = { silent = true, buffer = bufnr }
  71. -- exclude import statements from reference search (may have false positives)
  72. vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references default_text=!import\\ <CR>', opts)
  73. end
  74. }
  75. })
  76. -- set compiler for :make
  77. vim.cmd("compiler! tsc")
  78. end
  79. if vim.fn.executable("solargraph") == 1 then
  80. nvim_lsp.solargraph.setup({
  81. on_attach = on_attach,
  82. init_options = {
  83. formatting = false,
  84. }
  85. })
  86. end
  87. local group = vim.api.nvim_create_augroup('LspFormatting', { clear = false })
  88. null_ls.setup({
  89. sources = null_ls_sources,
  90. on_attach = function(client, bufnr)
  91. -- format on save
  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. on_attach = on_attach,
  120. })
  121. -- show LSP progress bar
  122. require('fidget').setup()