You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.6 KiB

  1. local lsp = require('lsp-zero')
  2. lsp.preset('recommended')
  3. local luasnip = require("luasnip")
  4. lsp.ensure_installed({
  5. 'tsserver',
  6. 'eslint',
  7. 'gopls'
  8. })
  9. local has_words_before = function()
  10. local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  11. return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil
  12. end
  13. local cmp = require('cmp')
  14. -- Use buffer source for `/`
  15. cmp.setup.cmdline("/", {
  16. enabled = false
  17. })
  18. -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
  19. cmp.setup.cmdline(':', {
  20. enabled = false,
  21. })
  22. -- local cmp_select = {behavior = cmp.SelectBehavior.Select}
  23. local cmp_mappings = lsp.defaults.cmp_mappings({
  24. ["<C-k>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
  25. ["<C-j>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
  26. ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
  27. ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
  28. ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
  29. ["<C-e>"] = cmp.mapping { i = cmp.mapping.close(), c = cmp.mapping.close() },
  30. ["<CR>"] = cmp.mapping {
  31. i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false },
  32. c = function(fallback)
  33. if cmp.visible() then
  34. cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
  35. else
  36. fallback()
  37. end
  38. end,
  39. },
  40. ["<Tab>"] = cmp.mapping(
  41. function(fallback)
  42. if cmp.visible() then
  43. cmp.select_next_item()
  44. elseif luasnip.expand_or_jumpable() then
  45. luasnip.expand_or_jump()
  46. elseif has_words_before() then
  47. cmp.complete()
  48. else
  49. fallback()
  50. end
  51. end,
  52. {
  53. "i",
  54. "s",
  55. }),
  56. ["<S-Tab>"] = cmp.mapping(function(fallback)
  57. if cmp.visible() then
  58. cmp.select_prev_item()
  59. elseif luasnip.jumpable(-1) then
  60. luasnip.jump(-1)
  61. else
  62. fallback()
  63. end
  64. end,
  65. {
  66. "i",
  67. "s",
  68. }),
  69. })
  70. lsp.setup_nvim_cmp({
  71. mapping = cmp_mappings,
  72. window = {
  73. documentation = {
  74. border = { "", "", "", "", "", "", "", "" },
  75. winhighlight = "NormalFloat:NormalFloat,FloatBorder:TelescopeBorder",
  76. },
  77. },
  78. })
  79. lsp.on_attach(function(client, bufnr)
  80. local opts = {buffer = bufnr, remap = false}
  81. -- Key mappings
  82. vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
  83. vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
  84. vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
  85. vim.keymap.set("n", "[e", function () vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR}) end, opts)
  86. vim.keymap.set("n", "]e", function () vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR}) end, opts)
  87. vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts)
  88. vim.keymap.set("n", "<leader>ka", vim.lsp.buf.code_action, opts)
  89. vim.keymap.set("n", "<leader>of", vim.diagnostic.open_float, opts)
  90. vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
  91. vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
  92. -- vim.keymap.set("n", "gr", function() vim.lsp.buf.references({ includeDeclaration = false }) end, opts)
  93. vim.keymap.set("n", "gh", vim.lsp.buf.signature_help, opts)
  94. vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
  95. vim.keymap.set("n", "gT", vim.lsp.buf.type_definition, opts)
  96. end)
  97. lsp.setup()