From 69a152ca94032fc1a0d0903948f7e84d416d7e82 Mon Sep 17 00:00:00 2001 From: winston Date: Thu, 22 Jun 2023 23:16:55 +0200 Subject: [PATCH] feat(nvim): switch to luasnip --- home/apps/neovim/lazy-lock.json | 4 +-- home/apps/neovim/lua/lsp/init.lua | 41 ++++++++++++---------------- home/apps/neovim/lua/plugins/lsp.lua | 4 +-- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/home/apps/neovim/lazy-lock.json b/home/apps/neovim/lazy-lock.json index 5481d59..cd3b1fa 100644 --- a/home/apps/neovim/lazy-lock.json +++ b/home/apps/neovim/lazy-lock.json @@ -1,4 +1,5 @@ { + "LuaSnip": { "branch": "master", "commit": "3d2ad0c0fa25e4e272ade48a62a185ebd0fe26c1" }, "alpha-nvim": { "branch": "main", "commit": "9e33db324b8bb7a147bce9ea5496686ee859461d" }, "asyncrun.vim": { "branch": "master", "commit": "7191d0c30dd105e5d7f897b9a6ee19cabe734466" }, "asynctasks.vim": { "branch": "master", "commit": "784a4b1f75a913cc6c43dd6c4dbe69d648dbbfa5" }, @@ -10,7 +11,7 @@ "cmp-git": { "branch": "main", "commit": "f900a4cf117300fdc3ba31d26f8b6223ccd9c574" }, "cmp-nvim-lsp": { "branch": "main", "commit": "0e6b2ed705ddcff9738ec4ea838141654f12eeef" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, - "cmp-vsnip": { "branch": "main", "commit": "989a8a73c44e926199bfd05fa7a516d51f2d2752" }, + "cmp_luasnip": { "branch": "master", "commit": "18095520391186d634a0045dacaa346291096566" }, "color-picker.nvim": { "branch": "master", "commit": "06cb5f853535dea529a523e9a0e8884cdf9eba4d" }, "comment.nvim": { "branch": "master", "commit": "176e85eeb63f1a5970d6b88f1725039d85ca0055" }, "copilot.lua": { "branch": "master", "commit": "9cb5396205faf609bc9df0e841e133ccb1b70540" }, @@ -80,6 +81,5 @@ "vim-helm": { "branch": "master", "commit": "c2e7b85711d410e1d73e64eb5df7b70b1c4c10eb" }, "vim-just": { "branch": "main", "commit": "9fc9a1afaa9e3567b25f4141a01f6172a1992a0b" }, "vim-table-mode": { "branch": "master", "commit": "9555a3e6e5bcf285ec181b7fc983eea90500feb4" }, - "vim-vsnip": { "branch": "master", "commit": "7753ba9c10429c29d25abfd11b4c60b76718c438" }, "which-key.nvim": { "branch": "main", "commit": "d871f2b664afd5aed3dc1d1573bef2fb24ce0484" } } \ No newline at end of file diff --git a/home/apps/neovim/lua/lsp/init.lua b/home/apps/neovim/lua/lsp/init.lua index 966f648..bc57fc1 100644 --- a/home/apps/neovim/lua/lsp/init.lua +++ b/home/apps/neovim/lua/lsp/init.lua @@ -1,13 +1,17 @@ local lsp_present, lspconfig = pcall(require, "lspconfig") local cmp_present, cmp = pcall(require, "cmp") local navic_present, navic = pcall(require, "nvim-navic") +local luasnip_present, luasnip = pcall(require, "luasnip") -if not (cmp_present and lsp_present) then +if not (cmp_present and lsp_present and luasnip_present) then return end vim.opt.completeopt = "menu,menuone,noselect" -vim.g.vsnip_snippet_dir = vim.fn.stdpath("config") .. "/snippets" +require("luasnip.loaders.from_vscode").lazy_load() +require("luasnip.loaders.from_vscode").load({ + path = { vim.fn.stdpath("config") .. "/snippets" }, +}) vim.lsp.set_log_level("error") -- border style @@ -29,29 +33,17 @@ local cmp_borders = { winhighlight = "Normal:CmpPmenu,FloatBorder:CmpBorder,CursorLine:PmenuSel,Search:None", } +-- stylua: ignore local has_words_before = function() ---@diagnostic disable-next-line: deprecated 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 feedkey = function(key, mode) - vim.api.nvim_feedkeys( - vim.api.nvim_replace_termcodes(key, true, true, true), - mode, - true - ) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil end cmp.setup({ snippet = { expand = function(args) - vim.fn["vsnip#anonymous"](args.body) + luasnip.lsp_expand(args.body) end, }, window = { @@ -64,11 +56,12 @@ cmp.setup({ [""] = cmp.mapping.complete(), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() - elseif vim.fn["vsnip#available"](1) == 1 then - feedkey("(vsnip-expand-or-jump)", "") + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() elseif has_words_before() then cmp.complete() else @@ -76,17 +69,19 @@ cmp.setup({ end end, { "i", "s" }), - [""] = cmp.mapping(function() + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() - elseif vim.fn["vsnip#jumpable"](-1) == 1 then - feedkey("(vsnip-jump-prev)", "") + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() end end, { "i", "s" }), }), sources = cmp.config.sources({ { name = "nvim_lsp" }, - { name = "vsnip" }, + { name = "luasnip" }, { name = "vim-dadbod-completion" }, }, { { name = "buffer" }, diff --git a/home/apps/neovim/lua/plugins/lsp.lua b/home/apps/neovim/lua/plugins/lsp.lua index 9069987..8087539 100644 --- a/home/apps/neovim/lua/plugins/lsp.lua +++ b/home/apps/neovim/lua/plugins/lsp.lua @@ -9,8 +9,8 @@ return { "hrsh7th/cmp-cmdline", "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-path", - "hrsh7th/cmp-vsnip", - "hrsh7th/vim-vsnip", + "L3MON4D3/LuaSnip", + "saadparwaiz1/cmp_luasnip", "petertriho/cmp-git", "onsails/lspkind.nvim", "rafamadriz/friendly-snippets",