From 07d13e67ae978e9f583981c82ca99e9bdd5863eb Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Wed, 20 Mar 2024 01:12:42 +0100 Subject: [PATCH] use luasnip in cmp and telescope --- lua/plugins/cmp.lua | 20 ++++++++------------ lua/plugins/init.lua | 12 +++++++++--- lua/plugins/luasnip.lua | 5 +++++ lua/plugins/telescope.lua | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 lua/plugins/luasnip.lua diff --git a/lua/plugins/cmp.lua b/lua/plugins/cmp.lua index 05ab1a0..d5829df 100644 --- a/lua/plugins/cmp.lua +++ b/lua/plugins/cmp.lua @@ -2,7 +2,6 @@ local utils = require('utils') -vim.cmd [[set shortmess+=c]] utils.opt('o', 'completeopt', 'menuone,noselect') local cmp = require('cmp') @@ -50,13 +49,13 @@ cmp.setup({ end }, snippet = { - -- REQUIRED - you must specify a snippet engine - expand = function(args) - vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. - -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. - -- require('snippy').expand_snippet(args.body) -- For `snippy` users. - -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. - end, + expand = function(args) + local luasnip = require("luasnip") + if not luasnip then + return + end + luasnip.lsp_expand(args.body) + end, }, window = { completion = cmp.config.window.bordered(), @@ -74,10 +73,7 @@ cmp.setup({ }), sources = cmp.config.sources({ { name = 'nvim_lsp' }, - { name = 'vsnip' }, -- For vsnip users. - -- { name = 'luasnip' }, -- For luasnip users. - -- { name = 'ultisnips' }, -- For ultisnips users. - -- { name = 'snippy' }, -- For snippy users. + { name = 'luasnip' }, -- For luasnip users. }, { { name = 'buffer' }, }) diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 7d2215c..ffe2b25 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -32,10 +32,15 @@ require('packer').startup(function() { 'hrsh7th/cmp-buffer' }, { 'hrsh7th/cmp-path' }, { 'hrsh7th/cmp-cmdline' }, - { 'hrsh7th/vim-vsnip' }, - { 'hrsh7th/vim-vsnip-integ' }, } } + use { 'L3MON4D3/LuaSnip' } + use { "rafamadriz/friendly-snippets" } + use { 'saadparwaiz1/cmp_luasnip' } + use { + "benfowler/telescope-luasnip.nvim", + module = "telescope._extensions.luasnip", -- if you wish to lazy-load + } -- Statusbar, colors and syntax use { 'vim-airline/vim-airline' } @@ -46,7 +51,7 @@ require('packer').startup(function() use { 'weilbith/nvim-code-action-menu', cmd = 'CodeActionMenu' } use { 'folke/trouble.nvim' } -- lsp diagnostics - -- Languages + -- Languages that need a plugin to extend default lsp use { 'cespare/vim-toml' } use { 'gleam-lang/gleam.vim' } use { 'rust-lang/rust.vim' } @@ -71,6 +76,7 @@ require('packer').startup(function() end) +require('plugins.luasnip') require('plugins.cmp') require('plugins.gitsigns') require('plugins.neogit') diff --git a/lua/plugins/luasnip.lua b/lua/plugins/luasnip.lua new file mode 100644 index 0000000..02dc094 --- /dev/null +++ b/lua/plugins/luasnip.lua @@ -0,0 +1,5 @@ +local languages = { + 'python', + 'rust', +} +require('luasnip.loaders.from_vscode').load({ include = languages }) -- Load only python snippets diff --git a/lua/plugins/telescope.lua b/lua/plugins/telescope.lua index 77a45dd..4fc5d0d 100644 --- a/lua/plugins/telescope.lua +++ b/lua/plugins/telescope.lua @@ -2,6 +2,9 @@ local telescope = require('telescope') local actions = require('telescope.actions') local builtin = require('telescope.builtin') +local luasnip = require('luasnip') +local lst = telescope.extensions.luasnip + telescope.setup { defaults = { layout_strategy = "vertical", @@ -31,10 +34,22 @@ telescope.setup { previewer = false, }, }, + extensions = { + luasnip = { + search = function(entry) + return lst.filter_null(entry.context.trigger) .. " " .. + lst.filter_null(entry.context.name) .. " " .. + entry.ft .. " " .. + lst.filter_description(entry.context.name, entry.context.description) .. + lst.get_docstring(luasnip, entry.ft, entry.context)[1] + end + }, + } } vim.keymap.set('n', 'ff', builtin.find_files, {}) vim.keymap.set('n', 'fg', builtin.git_files, {}) +vim.keymap.set('n', 'fs', telescope.extensions.luasnip.luasnip, {}) vim.keymap.set('n', 'gg', builtin.live_grep, {}) vim.keymap.set('n', 'bb', builtin.buffers, {}) vim.keymap.set('n', 'hh', builtin.help_tags, {})