init.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. require('nvim-treesitter.configs').setup {
  2. highlight = {
  3. enable = true,
  4. },
  5. }
  6. local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
  7. function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
  8. opts = opts or {}
  9. opts.border = opts.border or 'single'
  10. return orig_util_open_floating_preview(contents, syntax, opts, ...)
  11. end
  12. vim.diagnostic.config({
  13. virtual_text = { severity = { min = vim.diagnostic.severity.WARN } }
  14. })
  15. local opts = { noremap=true, silent=true }
  16. vim.api.nvim_set_keymap('n', '<space>e', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
  17. vim.api.nvim_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
  18. vim.api.nvim_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
  19. vim.api.nvim_set_keymap('n', '<space>q', '<cmd>lua vim.diagnostic.setqflist({ severity = { min = vim.diagnostic.severity.WARN } })<CR>', opts)
  20. function on_attach(client, bufnr)
  21. -- Enable completion triggered by <c-x><c-o>
  22. vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
  23. -- Mappings.
  24. -- See `:help vim.lsp.*` for documentation on any of the below functions
  25. vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  26. vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
  27. vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
  28. vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
  29. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
  30. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
  31. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
  32. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
  33. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
  34. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
  35. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
  36. vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
  37. vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
  38. end
  39. require'nvim-treesitter.configs'.setup {
  40. highlight = {
  41. enable = true,
  42. },
  43. }
  44. local nvim_lsp = require('lspconfig')
  45. local null_ls = require('null-ls')
  46. local null_ls_sources = {}
  47. if vim.fn.executable("node_modules/.bin/eslint") then
  48. table.insert(null_ls_sources, null_ls.builtins.formatting.eslint_d)
  49. table.insert(null_ls_sources, null_ls.builtins.diagnostics.eslint_d)
  50. table.insert(null_ls_sources, null_ls.builtins.code_actions.eslint_d)
  51. end
  52. if vim.fn.executable("gofmt") == 1 then
  53. table.insert(null_ls_sources, null_ls.builtins.formatting.gofmt)
  54. end
  55. if vim.fn.executable("shellcheck") == 1 then
  56. table.insert(null_ls_sources, null_ls.builtins.diagnostics.shellcheck)
  57. table.insert(null_ls_sources, null_ls.builtins.code_actions.shellcheck)
  58. end
  59. if vim.fn.executable("deno") == 1 then
  60. nvim_lsp.denols.setup({
  61. capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()),
  62. on_attach = on_attach,
  63. });
  64. table.insert(null_ls_sources, null_ls.builtins.formatting.deno_fmt);
  65. end
  66. if vim.fn.executable("node_modules/.bin/tsc") == 1 then
  67. local ts_utils = require("nvim-lsp-ts-utils")
  68. ts_utils.setup({
  69. debug = false,
  70. disable_commands = false,
  71. enable_import_on_completion = true,
  72. -- import all
  73. import_all_timeout = 5000, -- ms
  74. -- lower numbers = higher priority
  75. import_all_priorities = {
  76. same_file = 1, -- add to existing import statement
  77. local_files = 2, -- git files or files with relative path markers
  78. buffer_content = 3, -- loaded buffer content
  79. buffers = 4, -- loaded buffer names
  80. },
  81. import_all_scan_buffers = 100,
  82. import_all_select_source = false,
  83. -- filter diagnostics
  84. filter_out_diagnostics_by_severity = {},
  85. filter_out_diagnostics_by_code = {},
  86. -- inlay hints
  87. auto_inlay_hints = true,
  88. inlay_hints_highlight = "Comment",
  89. -- update imports on file move
  90. update_imports_on_move = false,
  91. require_confirmation_on_move = false,
  92. watch_dir = nil,
  93. })
  94. nvim_lsp.tsserver.setup({
  95. init_options = ts_utils.init_options,
  96. capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()),
  97. flags = {
  98. debounce_text_changes = 150,
  99. },
  100. on_attach = function(client, bufnr)
  101. client.resolved_capabilities.document_formatting = false
  102. client.resolved_capabilities.document_range_formatting = false
  103. on_attach(client, bufnr)
  104. -- required to fix code action ranges and filter diagnostics
  105. ts_utils.setup_client(client)
  106. end,
  107. })
  108. end
  109. null_ls.setup({
  110. sources = null_ls_sources,
  111. on_attach = function(client, bufnr)
  112. if client.resolved_capabilities.document_formatting then
  113. vim.cmd([[
  114. augroup LspFormatting
  115. autocmd! * <buffer>
  116. autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()
  117. augroup END
  118. ]])
  119. end
  120. on_attach(client, bufnr)
  121. end,
  122. });
  123. -- Setup nvim-cmp.
  124. local cmp = require'cmp'
  125. cmp.setup({
  126. completion = {
  127. autocomplete = false,
  128. },
  129. snippet = {
  130. expand = function(args)
  131. vim.fn["vsnip#anonymous"](args.body)
  132. end,
  133. },
  134. mapping = {
  135. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  136. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  137. ['<C-Space>'] = cmp.mapping.complete(),
  138. ['<C-e>'] = cmp.mapping.close(),
  139. ['<CR>'] = cmp.mapping.confirm({ select = true }),
  140. },
  141. sources = {
  142. { name = 'nvim_lsp' },
  143. { name = 'vsnip' },
  144. { name = 'buffer' },
  145. }
  146. })