├── README.md ├── after └── plugin │ └── cmp_luasnip_choice.lua └── lua └── cmp_luasnip_choice └── init.lua /README.md: -------------------------------------------------------------------------------- 1 | # cmp-luasnip-choice 2 | 3 | [luasnip](https://github.com/L3MON4D3/LuaSnip) choice node completion source for [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) 4 | 5 | 6 | https://user-images.githubusercontent.com/25270060/202857316-f6d13c97-7deb-4bfb-bb17-4937755b40cf.mp4 7 | 8 | 9 | ## Installation via [Packer](https://github.com/wbthomason/packer.nvim) 10 | 11 | ```lua 12 | use { 'L3MON4D3/LuaSnip' } 13 | use { 14 | 'hrsh7th/nvim-cmp', 15 | config = function () 16 | require'cmp'.setup { 17 | snippet = { 18 | expand = function(args) 19 | require'luasnip'.lsp_expand(args.body) 20 | end 21 | }, 22 | 23 | sources = { 24 | { name = 'luasnip_choice' }, 25 | -- more sources 26 | }, 27 | } 28 | end 29 | } 30 | use { 31 | 'doxnit/cmp-luasnip-choice', 32 | config = function() 33 | require('cmp_luasnip_choice').setup({ 34 | auto_open = true, -- Automatically open nvim-cmp on choice node (default: true) 35 | }); 36 | end, 37 | } 38 | ``` 39 | -------------------------------------------------------------------------------- /after/plugin/cmp_luasnip_choice.lua: -------------------------------------------------------------------------------- 1 | require('cmp').register_source('luasnip_choice', require('cmp_luasnip_choice').source.new()) 2 | -------------------------------------------------------------------------------- /lua/cmp_luasnip_choice/init.lua: -------------------------------------------------------------------------------- 1 | local cmp = require('cmp') 2 | 3 | local default_config = { 4 | auto_open = true, 5 | } 6 | 7 | local M = { 8 | source = {} 9 | } 10 | 11 | function M.setup(config) 12 | if config == nil then 13 | config = default_config 14 | else 15 | config = vim.tbl_deep_extend('keep', config, default_config) 16 | end 17 | 18 | if config.auto_open then 19 | vim.api.nvim_create_autocmd('User', { 20 | pattern = 'LuasnipChoiceNodeEnter', 21 | callback = function() 22 | vim.schedule(function() 23 | cmp.complete({ 24 | config = { 25 | sources = { 26 | {name = 'luasnip_choice'} 27 | }, 28 | }, 29 | }) 30 | end) 31 | end, 32 | }) 33 | end 34 | end 35 | 36 | function M.source.new() 37 | return setmetatable({}, {__index = M.source}) 38 | end 39 | 40 | function M.source:is_available() 41 | return require('luasnip.session').active_choice_node 42 | end 43 | 44 | function M.source:execute(completion_item, callback) 45 | require('luasnip').set_choice(completion_item.index) 46 | callback(completion_item) 47 | end 48 | 49 | function M.source:get_keyword_pattern() 50 | return '' 51 | end 52 | 53 | function M.source:complete(params, callback) 54 | local items = {} 55 | local choices = require('luasnip.session').active_choice_node.choices 56 | for _, choice in ipairs(choices) do 57 | local copy = choice:copy() 58 | copy:static_init() 59 | 60 | local text = '' 61 | for _, str in ipairs(copy:get_static_text()) do 62 | text = text .. str .. '\n' 63 | end 64 | 65 | table.insert(items, { 66 | label = copy:get_static_text()[1], 67 | word = copy:get_static_text()[1], 68 | index = _, 69 | documentation = text, 70 | kind = cmp.lsp.CompletionItemKind.Snippet, 71 | }) 72 | end 73 | callback(items) 74 | end 75 | 76 | function M.source:get_debug_name() 77 | return 'luasnip_choice' 78 | end 79 | 80 | return M 81 | --------------------------------------------------------------------------------