-
-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snippet priority and visibility in nvim-cmp #267
Comments
TY! 😄
Currently we're just returning the first matching snippet (see here), there is no concept of priority (so far). -- load(....)
table.insert(ls.snippets.markdown, s(....))
-- or alternatively:
vim.list_extend(ls.snippets.markdown, {<multiple snippets>}) That will work as long as the snippets are loaded from the same filetype, different filtetypes will be searched sequentially. print(vim.inspect(require("luasnip.util.util").get_snippet_filetypes())) in a given file. ls.filetype_extend("ft1", {"ft2"}) -- leads to {"ft1", "ft2", "all"}
ls.filetype_set("ft1", {"ft2", "ft1"}) -- leads to {"ft2", "ft1", "all"} If that doesn't help, we might have to implement priority properly. |
They should expand so that the trigger is editable, so Apart from that, if You could define a wrapper around local function s_reg(trigger, nodes)
return s({trig = trigger, regTrig = true, hidden = true}, nodes)
end
s_reg("[%d%w]+d", {
t"yey, expanded"
}) |
Thank you for the quick response! local status_ok, vsloader = pcall(require, "luasnip.loaders.from_vscode")
if not status_ok then
return
end
vsloader.load({paths = "~/.config/nvim/snippets/"}) Then define local texsnippets = {
-- Snippets for math text
s(
{ trig = "fancy([a-z])", name = "fancy math text", dscr = "expands 'fancy a' to \\mathcal{A}", regTrig = true, hidden = true },
f(function(_, snip)
return "\\mathcal{ " .. snip.captures[1]:upper() .. "}"
end, {})
),
....
} and then finally use vim.list_extend(ls.snippets.markdown, texsnippets) to append the snippets. The |
Ohhh, I thought you were loading |
Actually im loading them both for markdown and for latex using In my {
"name": "mysnippets",
"engines": {
"vscode": "^1.11.0"
},
"contributes": {
"snippets": [
{
"language": [
"tex"
],
"path": "./tex.json"
}
]
}
} And I changed my luasnip file to local status_ok, vsloader = pcall(require, "luasnip.loaders.from_vscode")
if not status_ok then
return
end
vsloader.load({paths = "~/.config/nvim/snippets/"})
...
local texsnippets = {
-- Snippets for math text
s(
{ trig = "fancy([a-z])", name = "fancy math text", dscr = "expands 'fancy a' to \\mathcal{A}", regTrig = true, hidden = true },
f(function(_, snip)
return "\\mathcal{ " .. snip.captures[1]:upper() .. "}"
end, {})
),
....
}
vim.list_extend(ls.snippets.tex, texsnippets)
ls.filetype_set("markdown", {"tex"}) which still does not work. |
Ohhh, that's because we read the files asynchronally, so the snippets are only added after the list is extended 🤦 We do have an event in place that a loader fires when it adds new snippets, so you could listen to that and add the snippets once the loader has added all -- requires texsnippets to be global.
vim.cmd([[au User LuasnipSnippetsAdded lua if ls.session.latest_load_ft == "tex" do vim.list_extend(ls.snippets.tex, texsnippets) end]]) The above is only possible if there's just one package adding tex-snippets, if multiple packages add tex-snippets, In short, we probably should implement some kind of priority (or make the loading synchronous, that would also make it simpler). |
Okay I think this can now, finally, be done pretty comfortably, check the loader-docs :D |
First of all thanks for the really great plugin!
Im in the middle of switching to neovim and Im loving it because there are so many awesome plugins, including this one.
I would like to know how Luasnip chooses which snippet to expand when there are multiple snippets that could be expanded. In particular I would like to know whether there is a way to prioritize snippets loaded from vscode style json files over regex snippets.
For example I have a couple of snippets for tex and markdown in vscode style like
in -> \in
.I also created some regex snippets and one of them is a choice snippets which expands letters and numbers followed by
d, i, k
orn
, like for example5n
orblak
to5^n
OR5_n
/bla^k
ORbla_k
.This collides with some snippets, one of them being
in -> \in
. Is there a way to fix this? (I tried putting the load from vscode command at the top of my config file and define the snippets at the bottom but that did not change anything)Another thing that I would like to know is whether I can turn off regex snippets to pop up in the nvim-cmp completion menu. When selecting them, they just expand to the trigger which is not desirable in most cases I think.
Thanks in advance, I really appreciate it!
The text was updated successfully, but these errors were encountered: