local lsp = require('lsp-zero') lsp.preset('recommended') local luasnip = require("luasnip") lsp.ensure_installed({ 'tsserver', 'eslint', 'gopls' }) local has_words_before = function() local line, col = unpack(vim.api.nvim_win_get_cursor(0)) return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil end local cmp = require('cmp') -- local cmp_select = {behavior = cmp.SelectBehavior.Select} local cmp_mappings = lsp.defaults.cmp_mappings({ [""] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), [""] = cmp.mapping { i = cmp.mapping.close(), c = cmp.mapping.close() }, [""] = cmp.mapping { i = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }, c = function(fallback) if cmp.visible() then cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false } else fallback() end end, }, [""] = cmp.mapping( function(fallback) if cmp.visible() then cmp.select_next_item() elseif luasnip.expand_or_jumpable() then luasnip.expand_or_jump() elseif has_words_before() then cmp.complete() else fallback() end end, { "i", "s", }), [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() elseif luasnip.jumpable(-1) then luasnip.jump(-1) else fallback() end end, { "i", "s", }), }) lsp.setup_nvim_cmp({ mapping = cmp_mappings, window = { documentation = { border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, winhighlight = "NormalFloat:NormalFloat,FloatBorder:TelescopeBorder", }, }, }) lsp.on_attach(function(client, bufnr) local opts = {buffer = bufnr, remap = false} -- Key mappings vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts) vim.keymap.set("n", "[e", function () vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR}) end, opts) vim.keymap.set("n", "]e", function () vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR}) end, opts) vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) vim.keymap.set("n", "ka", vim.lsp.buf.code_action, opts) vim.keymap.set("n", "of", vim.diagnostic.open_float, opts) vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) -- vim.keymap.set("n", "gr", function() vim.lsp.buf.references({ includeDeclaration = false }) end, opts) vim.keymap.set("n", "gh", vim.lsp.buf.signature_help, opts) vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) vim.keymap.set("n", "gT", vim.lsp.buf.type_definition, opts) end) lsp.setup()