1
0

lsp.lua 4.2 KB

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