Coder Social home page Coder Social logo

cmp_luasnip's Introduction

cmp_luasnip

luasnip completion source for nvim-cmp

-- Installation
use { 'L3MON4D3/LuaSnip' }
use {
  'hrsh7th/nvim-cmp',
  config = function ()
    require'cmp'.setup {
    snippet = {
      expand = function(args)
        require'luasnip'.lsp_expand(args.body)
      end
    },

    sources = {
      { name = 'luasnip' },
      -- more sources
    },
  }
  end
}
use { 'saadparwaiz1/cmp_luasnip' }

To disable filtering completion candidates by snippet's show_condition use the following options in sources:

sources = {
  { name = 'luasnip', option = { use_show_condition = false } },
  -- more sources
},

This can also be configured on per-buffer basis as described in cmp's README here and here.

The same way you can de-/activate whether autosnippets should be included in the completion list or not (including them can be a bit problematic since when you select the entry, the text gets inserted and the snippet automatically triggers). This option defaults to false to be backwards compatible. Example:

sources = {
  { name = 'luasnip', option = { show_autosnippets = true } },
  -- more sources
},

Hint: If you want to just hide some autosnippets consider the hidden option of luaSnip

cmp_luasnip's People

Contributors

abzcoding avatar atticus-sullivan avatar jedrzejboczar avatar l3mon4d3 avatar n-p-e avatar saadparwaiz1 avatar uga-rosa avatar woodgear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmp_luasnip's Issues

No longer directly access `ls.snippets`

Hi!

Luasnip is removing direct access to ls.snippets (and changing the internal snippet-storage), which means that we'll have to adapt cmp_luasnip to no longer store an index into the ft-tables.

It will still be possible to get a list of snippets by filetype, and indices into these would work, but storing a direct reference, in some way, to the snippet would be preferable (imo).
I opened an issue in cmp about just that, not sure if it'll lead to changes in cmp, if it doesn't, we'll implement some snippet->key/key->snippet-conversion in luasnip.

I'm not sure if it'll be easy to adapt the cache, haven't really thought about it yet, this is more of a FYI :P

Error detected while processing TextChangedI Autocommands for "*":

Error detected while processing TextChangedI Autocommands for "*":
E5108: Error executing lua ...user/.vim/plugged/cmp_luasnip/lua/cmp_luasnip/init.lua:47: attempt to index a boolean value
Press ENTER or type command to continue

Any idea what's causing this? I am trying to make cmp work with luasnip. I have no config for luasnip nor for cmp_luasnip. My config for cmp is the following:

local has_words_before = function()
  local cursor = vim.api.nvim_win_get_cursor(0)
  return not vim.api.nvim_get_current_line():sub(1, cursor[2]):match('^%s$')
end

local luasnip = require('luasnip')
local cmp = require('cmp')
cmp.setup {

    snippet = {
        expand = function(args)
            require("luasnip").lsp_expand(args.body)
        end,
    },

  mapping = {

    ['<Tab>'] = cmp.mapping(function(fallback)
      if vim.fn.pumvisible() == 1 then
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-n>', true, true, true), 'n', true)
      elseif has_words_before() and luasnip.expand_or_jumpable() then
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Plug>luasnip-expand-or-jump', true, true, true), '')
      else
        fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
      end
    end, { 'i', 's' }),

    ['<S-Tab>'] = cmp.mapping(function()
      if vim.fn.pumvisible() == 1 then
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-p>', true, true, true), 'n', true)
      elseif luasnip.jumpable(-1) then
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Plug>luasnip-jump-prev', true, true, true), '', true)
      end
    end, { 'i', 's' }),

  },

  sources = {
    { name = "luasnip" },
    { name = "nvim_lsp" },
    { name = "buffer" },
    { name = "path" },
  },

}

Respect `trigEngine`.

Hi!
There were some additions to luasnip, some time ago actually, where lua-pattern-triggers may now be defined via trigEngine. I think it would make sense to handle snippets where it is set to "pattern" like those where regTrig is set, the other predefined ones could (with lots of effort) be handled similarly, but I don't think that's really worth it.
For this to work, luasnip needs some so cmp_luasnip can tell whether a snippet has a lua-pattern as its trigger (because trigEngine is just some function, and I don't think it should be exposed)
I can implement this, but pretty busy right now, so will take some time.

Neovim freezing when pressing Tab

Problem

Neovim freezes when pressing tab or strg-I

Conditions

Pressing tab or strg-I in normal mode without there being a double-dot ':' put in.

Info

I tracked the problem down to this function.

function source:resolve(completion_item, callback)

It somehow gets called when pressing tab, but i unfortunately i don't know were it is mapped from, but it should not be a local mapping by me.

Keep getting sporadic errors (may be related to packer.nvim?)

Every now and then I get the error below. I've tried to find a way to reproduce it but it's very difficult. I think it only occurs after I run source <afile> | PackerCompile after saving my init.lua, and it appears to trigger randomly every now and then when I type in insert mode and a snippet appears as the hovered selection in the popup menu.

Without a way to reproduce it's not much help but I still wanted to post this issue to hear if there's an easy fix or if anyone else is having the same issue.

Error executing vim.schedule lua callback: ...e/pack/packer/start/cmp_luasnip/lua/cmp_luasnip/init.lua:106: attempt to index a nil value
stack traceback:
	...e/pack/packer/start/cmp_luasnip/lua/cmp_luasnip/init.lua:106: in function 'resolve'
	.../nvim/site/pack/packer/start/nvim-cmp/lua/cmp/source.lua:348: in function 'resolve'
	...e/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/entry.lua:454: in function 'resolve'
	...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/view.lua:225: in function 'fn'
	.../site/pack/packer/start/nvim-cmp/lua/cmp/utils/async.lua:41: in function <.../site/pack/packer/start/nvim-cmp/lua/cmp/utils/async.lua:39>

attempt to index local 'snip' (a nil value)

E5108: Error executing lua ...ite/pack/packer/opt/cmp_luasnip/lua/cmp_luasnip/init.lua:132: attempt to index local 'sn
ip' (a nil value)                                                                                                     
stack traceback:                                                                                                      
        ...ite/pack/packer/opt/cmp_luasnip/lua/cmp_luasnip/init.lua:132: in function 'execute'                        
        ...re/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/source.lua:360: in function 'execute'                        
        ...are/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/entry.lua:439: in function 'execute'                        
        ...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/core.lua:468: in function <43/168e(25%)/[Loadingkworkspace]
t/nvim-cmp/lua/cmp/core.lua:467>                                                      42/275 (14%) [Loading workspace]
        ...site/pack/packer/opt/nvim-cmp/lua/cmp/utils/feedkeys.lua:51: in function <...site/pack/packer/β”Œpsumneko_lua
lua/cmp/utils/feedkeys.lua:49>

Snippets triggers no longer work? Is this due to new changes to luasnip ?

Would you like a min config for this ?

Sources appear in `LuaSnipListAvailable` but not in cmp suggestions list

Hey I've got a bunch of snips in LuaSnipListAvailable like this one:

{
      description = { "Alert - Info background" },
      name = "rh-color-blue-50",
      regTrig = false,
      trigger = "--rh-color-blue-50",
      wordTrig = true
}

loaded via

require 'luasnip.loaders.from_vscode'.lazy_load { paths = {
  '~/Developer/redhat-ux/red-hat-design-tokens/editor/vscode'
} }

sources config is

  sources = cmp.config.sources({
    { name = 'nvim_lsp' },
    { name = 'nvim_lsp_signature_help' },
  }, {
    { name = 'luasnip', option = { use_show_condition = false } },
  }, {
    { name = 'treesitter' },
    { name = 'buffer', keyword_length = 3 },
  }, {
    { name = 'plugins' },
    { name = 'nvim_lua' },
    { name = 'npm', keyword_length = 4 },
    { name = 'fish' },
    { name = 'calc' },
    { name = 'emoji' },
  }),

but none of them are showing up when i type --rh-color. Y?

config: https://github.com/bennypowers/dotfiles/blob/master/.config/nvim/lua/config/cmp.lua
vscode extension bundle: https://github.com/RedHat-UX/red-hat-design-tokens/releases/tag/v1.0.0-beta.9

Create tag when version requirement changes

Recently a breaking change was introduced (use 0.7 API) will be great to create a tag when the minimum nvim version changes, so we do not have a broken config while the package system catch up.

Thanks!

Snippets are not reloaded after adding a new snippet

I noticed this autocommand in plugin/cmp_luasnip.lua:

autocmd User LuasnipSnippetsAdded lua require'cmp_luasnip'.refresh()

So I assumed that this causes snippets to be automatically reloaded once I add a new snippet. However, that doesn't seem to be the case.

The help docs for luasnip show this:

After snippets were lazy-loaded, the User LuasnipSnippetsAdded-event will be triggered.

There is no mention of this event being called when the snippet definitions change.

So I don't think this autocommand works as intended.

I am not even sure how to achieve this since Lua caches all modules so maybe luasnip would need to be reloaded somehow.

Snippet-Docstring

Just FYI, Luasnip now supports docstrings (textual representations) of snippets via snip:get_docstring(), could be interesting to display in the documentation, if you'd like to support that :)

Neovim freezing

(Disclaimer) I already adressed this problem in #53 issue and i'm sry for the duplicate.

Problem

Neovim is freezing when pressing tab in normal mode. To be exact this issue seems to be only with neovim-qt, but i don't know why.
I originally thought the problem came from the resolve method in the init file, but i actually found out it is caused by multiple functions like this:

if params.option.use_show_condition then

and
local doc_itm = doc_cache[completion_item.data.filetype] or {}

they seem to have some kind of problem with the qt version of neovim.
Due to why this is i unfortunatly can't tell, maybe someone should look into this.

Probably README should be updated.

[nvim-cmp] sources[number].opts is deprecated.
[nvim-cmp] Please use sources[number].option instead.

to:

sources = {
  { name = 'luasnip', options = { use_show_condition = false } },
  -- more sources
},

How to trigger expand on <C-n>?

I like this plugin, but all other cmp result items automatically load when cycling through the list with <C-n>.

How can I make luasnip snippets do that as well so i dont have ot actually fully confirm them

Help wanted: Treesitter highlight lost after enabling cmp_luasnip

I met a confused problem after enabling this plugin, the treesitter highlight would be ineffective in the first buffer. But highlight will be restored in next buffers, whatever methods to open them.

Environment:

  • nvim 0.6.1/ 0.7.0 nightly
  • CentOS 7.9
NVIM v0.7.0-dev+1233-gabbc9148d
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by p8zhang@l1sw-env

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/host/workdir/local/share/nvim"

Run :checkhealth for more info

Steps to Reproduce:

  1. download the file as miniconf.lua,
    miniconf.txt

  2. install the packer.nvim, ignore this step if it has been installed,

  3. run nvim -u miniconf.lua +PackerSync

  4. wait for the completion of process of packer.nvim, then open a lua code, python code or c/cpp code file use :e <somefile>,

  5. you will found no highlight enabled but highlight group created by treesitter could be found in output of command :hi,

  6. open another buffer, you could found the highlight enabled.

A potential fix for the regex triggers

I was thinking that a potential fix for the snippets with regexes as trigger (that most of the time misbehave) and I came up with an idea that can potentially fix it.
If the regex is capable of matching the current input, then the current input should be the item in the list.
If it does not then the input in the list could be a string derived from the pattern (I mean derive in the academic sense).

For instance:
A snippet with the regex
lorem(%d*)

For a text lorem should return lorem as item for the list (because the pattern matches the text).
For a text lor should return lorem0 as item for the list (because lorem0 is a minimal string derived from the pattern).

The derivations from regexes to strings can be hard to implement, but fortunately in this case lua patterns are very simple so they should be easily derivable.

module 'cmp' not found

I am getting this error when I try to install cmp_luasnip

Error

Using packer:

use({ "L3MON4D3/LuaSnip" })

-- auto-completion engine
use({
    "hrsh7th/nvim-cmp",
    config = [[require('config.nvim-cmp')]],
    after = "lspkind-nvim",
    requires = {
        "saadparwaiz1/cmp_luasnip",
        "hrsh7th/cmp-nvim-lua",
        "hrsh7th/cmp-nvim-lsp",
        "hrsh7th/cmp-path",
        "hrsh7th/cmp-buffer",
        "hrsh7th/cmp-omni",
    },
})

use({ "saadparwaiz1/cmp_luasnip" })

use({
    "hrsh7th/cmp-nvim-lua",
    after = "cmp_luasnip",
})

-- nvim-cmp completion sources
use({ "hrsh7th/cmp-nvim-lsp", after = "nvim-cmp" })
use({ "hrsh7th/cmp-path", after = "nvim-cmp" })
use({ "hrsh7th/cmp-buffer", after = "nvim-cmp" })
use({ "hrsh7th/cmp-omni", after = "nvim-cmp" })

Here is my nvim-config

Prevent putting the same information twice in documentation

Extracted from L3MON4D3/LuaSnip#1006

This comes from a slight annoyance of duplicated information for no reason.

With a simple snippet without explicit description, the doc looks like:
Screenshot_20230917_181700

You can see we have modeline repeated twice in the doc popup
(and even 4 times if we count the completion popup and what I wrote / the inserted completion)

I would like to at least remove the docstring when it is the same as the trigger/name of the snippet.

Here is a simulation of the result I'd like to have:
Screenshot_20230917_181532

πŸ‘‰ I'd even argue that when the snippet name is the same as the trigger (which is the default when the name isn't specified when defining a snippet), we could remove the name, or replace it with something else πŸ‘€

@L3MON4D3 mentioned we could just compare the name/trigger with the docstring and not display it if it's the same

Plugin license

Okay, so it's gonna look really bad on my profile that all I've been doing on github is complain to people about licenses (I use sr.ht instead of github for code hosting, so I don't have any projects here) but... could you please put a license on this plugin? Thanks!

Snippet Completion Options disappear

Consider the following LuaSnip config:

local ls = require 'luasnip'

ls.config.set_config {
    history = true,
    updateevents = 'TextChanged,TextChangedI',
}

local extras = require 'luasnip.extras'
local fmt = require('luasnip.extras.fmt').fmt
local i = ls.insert_node
local rep = extras.rep
local s = ls.s

ls.snippets = {
    lua = {
        s('lr', fmt("local {} = require '{}'", { i(1), i(2) })),
        s('<', fmt('<{}>{}</{}>', { i(1), i(2), rep(1) })),
        s('</', fmt('<{}/>', { i(1, '') })),
    },
}

Replicate:

  1. Typing "lr" in a lua file with nvim-cmp on correctly previews lr~ Snippet in the completion dropdown.
  2. Typing "<" correctly previews both <~ Snippet and </~ Snippet
  3. However, typing "</" previews a whole load of completion options (snippets for function, do, if, etc.), as if completion was opened on an empty new line, when I believe it should preview the </~ Snippet option alone.

Can you replicate this config, and if so, is this the desirable outcome? It seems like it is dismissing the </ options, perhaps because of the forward slash?

JSON snippets with autotrigger not in cmp menu

Luasnip's commit @737a2e7 introduced the possibility to specify a "luasnip" field in a snippet JSON file so the snippets can autotrigger.

If that field is set and autotrigger is true, the snippet will not be listed in the nvim-cmp menu. I believe it should show up in the cmp menu, no matter the autotrigger behavior.

Example snippet config

  "uuid": {
    "prefix": "UUID",
    "body": [
      "${CURRENT_YEAR}${CURRENT_MONTH}${CURRENT_DATE}${CURRENT_HOUR}${CURRENT_MINUTE}"
    ],
    "description": "UUID",
    "luasnip": {
      "autotrigger": true
    }
  },

Filter snippets by condition

Hi! If I have the following snippets

local ls = require('luasnip')
local i = ls.insert_node
local fmta = require('luasnip.extras.fmt').fmta
local line_begin = require('luasnip.extras.expand_conditions').line_begin

return {
    -- Lua
    s(
        { trig = 'rq', dscr = 'Require' },
        fmta(
            [[
                require('<>')
            ]],
            {
                i(1, 'package'),
            }
        ),
        { condition = line_begin }
    ),
}

then cmp shows the rq completion even when I'm not at the beginning of line. Is it possible to filter such snippet from the completion menu?

image

Choice Node Integration

I would like to revive this issue and ask if you could add native choice node integration into this cmp source. While it certainly possible to create an alternative popup menu, having choice nodes integrated will streamline workflows (unified settings and styles). Thanks for considering.

Can't install plugin

Hey I'm using Packer and I can't seem to install this plguin I get this error:

 βœ— Failed to install saadparwaizl/cmp_luasnip
  Errors:
    Cloning into '/home/dan/.local/share/nvim/site/pack/packer/start/cmp_luasnip'...
    fatal: could not read Username for 'https://github.com': terminal prompts disabled

Not really sure whats going on here I have my github ssh key set up correctly as about 20 other plugins have no problem installing.

I have been following this tutorial https://www.youtube.com/watch?v=ub0REXjhpmk but fail in installing this plugin.

for me it doesnt work. maybe i have something in wrong order

my lua folder:

ο„• .
β”œβ”€β”€  abbreviations.lua
β”œβ”€β”€  gitsigns-settings.lua
β”œβ”€β”€  keybindings.lua
β”œβ”€β”€  lualine-settings.lua
β”œβ”€β”€  nvim-autopairs-settings.lua
β”œβ”€β”€  nvim-cmp-settings.lua
β”œβ”€β”€  nvim-comment-settings.lua
β”œβ”€β”€  nvim-lspinstall-settings.lua
β”œβ”€β”€  nvim-sets.lua
β”œβ”€β”€  nvim-tree-settings.lua
β”œβ”€β”€  plugins.lua
β”œβ”€β”€  solarized-theme-settings.lua

init.lua:

-- packer and plugins
require('plugins')


-- built-in settings
require('keybindings')
require('nvim-sets')
require("abbreviations")


-- plugins settings
-- cmp needs to be first
require("nvim-cmp-settings")

require('lualine-settings')
require('solarized-theme-settings')
require("nvim-tree-settings")
require("nvim-lspinstall-settings")
require("gitsigns-settings")
require("nvim-comment-settings")

require("nvim-autopairs-settings")

plugins.lua (where packer is ):

local fn = vim.fn
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
	fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
	vim.cmd 'packadd packer.nvim'
end



return require('packer').startup(function()
	-- Packer can manage itself
	-- package manager made in lua
	use 'wbthomason/packer.nvim'

	use {
		'hrsh7th/nvim-cmp'
	}

       -- many other plugins


	-- a collection of very much quantity of snippets for many langs
	use {
		'rafamadriz/friendly-snippets'
	}

	-- snippets engine
	use {
		'L3MON4D3/LuaSnip'
	}

	-- have that snippets inside cmp
	use {
		'saadparwaiz1/cmp_luasnip'
	}

end)

and nvim-cmp settings:


-- all nvim-cmp sub plugins configured in one setup
-- https://github.com/hrsh7th/cmp-nvim-lua
-- https://github.com/hrsh7th/cmp-buffer
-- https://github.com/saadparwaiz1/cmp_luasnip
require'cmp'.setup {
	snippet = {
		expand = function(args)
			require"luasnip".lsp_expand(args.body)
		end
	},
	sources = {
		{ name = 'nvim_lua' },
		{ name = 'path' },
		{ name = 'buffer' },
		{ name = 'luasnip' }
	}
}

i tried to use your config from readme, but as you can see i cant config nvim-cmp inside packer because i have its configuration in nvim-cmp-settings.lua, but i tried to respect that order.

when i enter python file, for example
image

i dont get any snippets for the for keyword.

any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.