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.

98 lines
3.3 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. -- local cmp_select = {behavior = cmp.SelectBehavior.Select}
  15. local cmp_mappings = lsp.defaults.cmp_mappings({
  16. ["<C-k>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }),
  17. ["<C-j>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }),
  18. ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
  19. ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
  20. ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
  21. ["<C-e>"] = cmp.mapping { i = cmp.mapping.close(), c = cmp.mapping.close() },
  22. ["<CR>"] = cmp.mapping {
  23. i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false },
  24. c = function(fallback)
  25. if cmp.visible() then
  26. cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
  27. else
  28. fallback()
  29. end
  30. end,
  31. },
  32. ["<Tab>"] = cmp.mapping(
  33. function(fallback)
  34. if cmp.visible() then
  35. cmp.select_next_item()
  36. elseif luasnip.expand_or_jumpable() then
  37. luasnip.expand_or_jump()
  38. elseif has_words_before() then
  39. cmp.complete()
  40. else
  41. fallback()
  42. end
  43. end,
  44. {
  45. "i",
  46. "s",
  47. }),
  48. ["<S-Tab>"] = cmp.mapping(function(fallback)
  49. if cmp.visible() then
  50. cmp.select_prev_item()
  51. elseif luasnip.jumpable(-1) then
  52. luasnip.jump(-1)
  53. else
  54. fallback()
  55. end
  56. end,
  57. {
  58. "i",
  59. "s",
  60. }),
  61. })
  62. lsp.setup_nvim_cmp({
  63. mapping = cmp_mappings,
  64. window = {
  65. documentation = {
  66. border = { "", "", "", "", "", "", "", "" },
  67. winhighlight = "NormalFloat:NormalFloat,FloatBorder:TelescopeBorder",
  68. },
  69. },
  70. })
  71. lsp.on_attach(function(client, bufnr)
  72. local opts = {buffer = bufnr, remap = false}
  73. -- Key mappings
  74. vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
  75. vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
  76. vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
  77. vim.keymap.set("n", "[e", function () vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR}) end, opts)
  78. vim.keymap.set("n", "]e", function () vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR}) end, opts)
  79. vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts)
  80. vim.keymap.set("n", "<leader>ka", vim.lsp.buf.code_action, opts)
  81. vim.keymap.set("n", "<leader>of", vim.diagnostic.open_float, opts)
  82. vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
  83. vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
  84. -- vim.keymap.set("n", "gr", function() vim.lsp.buf.references({ includeDeclaration = false }) end, opts)
  85. vim.keymap.set("n", "gh", vim.lsp.buf.signature_help, opts)
  86. vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
  87. vim.keymap.set("n", "gT", vim.lsp.buf.type_definition, opts)
  88. end)
  89. lsp.setup()