Skip to content

Commit

Permalink
Simplify expansion condition and handle show_snippets = "all" correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
smjonas committed Apr 12, 2022
1 parent eea8c37 commit c4e5f45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
11 changes: 7 additions & 4 deletions autoload/cmp_nvim_ultisnips.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
" per snippet) with the keys "trigger", "description", "options" and "value".
"
" If 'expandable_only' is "True", only expandable snippets are returned, otherwise all
" snippets for the current filetype are returned.
" snippets except regex and custom context snippets for the current filetype are returned.
function! cmp_nvim_ultisnips#get_current_snippets(expandable_only)
let g:_cmpu_current_snippets = []
python3 << EOF
Expand All @@ -22,10 +22,13 @@ else:

for snippet in snippets:
is_context_snippet = snippet._context_code != None
# For custom context snippets, we need to check if the snippet is in the right context to be expanded
is_expandable = not expandable_only or not is_context_snippet or snippet._context_match(visual_content, before)
if not is_expandable:
is_regex_snippet = "r" in snippet._opts
# If show_snippets == "all", the snippets are cached so ignore "dynamic" snippets.
if not expandable_only and (is_context_snippet or is_regex_snippet):
continue
# For custom context snippets, always check if the context matches.
if is_context_snippet and not snippet._context_match(visual_content, before):
continue

vim.command(
"call add(g:_cmpu_current_snippets, {"
Expand Down
21 changes: 9 additions & 12 deletions lua/cmp_nvim_ultisnips/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,18 @@ function source.complete(self, _, callback)

for _, snippet in pairs(snippets) do
local is_regex_snippet = snippet.options:match("r")
-- Avoid expanding a regex snippet with an invalid insertText when self.expandable_only == false
-- (_cmpu_line_till_cursor is only set when self.expandable_only == true)
if not is_regex_snippet or is_regex_snippet and self.expandable_only then
local item = {
insertText = (is_regex_snippet and snippet.matched) or snippet.trigger,
label = snippet.trigger,
kind = cmp.lsp.CompletionItemKind.Snippet,
snippet = snippet,
}
table.insert(items, item)
end
local item = {
insertText = (is_regex_snippet and snippet.matched) or snippet.trigger,
label = snippet.trigger,
kind = cmp.lsp.CompletionItemKind.Snippet,
snippet = snippet,
}
table.insert(items, item)
end
callback {
items = items,
-- Cmp will update the items on every keystroke
-- If true, cmp will update the items on every keystroke.
-- When self.expandable_only == false, the snippets are cached so no need to update.
isIncomplete = self.expandable_only,
}
end
Expand Down

0 comments on commit c4e5f45

Please sign in to comment.