From 9d2e828b2130cb7851590e122aa11980078d3aa3 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Wed, 28 Dec 2022 18:53:00 +1030 Subject: [PATCH] Wed 28 Dec 2022 18:53:00 ACDT --- .config/nvim/lua/config/lsp.lua | 101 ++++++++++++++++++++++++++++++++ update.sh | 5 +- 2 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 .config/nvim/lua/config/lsp.lua diff --git a/.config/nvim/lua/config/lsp.lua b/.config/nvim/lua/config/lsp.lua new file mode 100644 index 0000000..3c34a91 --- /dev/null +++ b/.config/nvim/lua/config/lsp.lua @@ -0,0 +1,101 @@ +local lsp = require('lsp-zero') +lsp.preset('recommended') +local luasnip = require("luasnip") + +lsp.ensure_installed({ + 'tsserver', + 'eslint', + 'sumneko_lua', + '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", + "c", + }), + [""] = 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", + "c", + }), +}) + +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() diff --git a/update.sh b/update.sh index 26e811c..d9300b3 100755 --- a/update.sh +++ b/update.sh @@ -12,7 +12,6 @@ declare -a FILES=( .config/nvim/lua/packer-plugins.lua .config/nvim/lua/helper/toggle-tab-width.lua .config/nvim/lua/config/tmux.lua - .config/nvim/lua/config/barbar.lua .config/nvim/lua/config/treesitter.lua .config/nvim/lua/config/php-doc.lua .config/nvim/lua/config/go.lua @@ -24,9 +23,7 @@ declare -a FILES=( .config/nvim/lua/config/general/init.lua .config/nvim/lua/config/gitsigns.lua .config/nvim/lua/config/luasnip.lua - .config/nvim/lua/config/lsp/init.lua - .config/nvim/lua/config/lsp/keymaps.lua - .config/nvim/lua/config/lsp/installer.lua + .config/nvim/lua/config/lsp.lua .config/nvim/lua/config/nvim-neo-tree.lua .config/nvim/lua/config/lualine.lua .config/nvim/lua/config/telescope.lua