Skip to content
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

Feat: add support for custom context snippets #72

Merged
merged 4 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
</p>

## Features
- **Composable Mappings**: get rid of boilerplate code in your config
- **Treesitter Integration**: show snippets based on the filetype at your cursor position
- **Regular Expression Snippets**: snippets with the `r` option are supported
- **Composable mappings**: get rid of boilerplate code in your config
- **Treesitter integration**: show snippets based on the filetype at your cursor position
- **Regular expression snippets**: snippets with the `r` option are supported
- **Custom context snippets**: snippets with custom context code are handled correctly
- **Customization**: change which and how snippets are displayed by cmp

## Dependencies
Expand Down Expand Up @@ -134,7 +135,8 @@ are handled entirely by UltiSnips.
If set to `"expandable"`, only those snippets currently expandable by UltiSnips will be
shown. The snippets will always be in sync with the currently available UltiSnips snippets.

`"all"` will show all snippets for the current filetype. If using this option, be aware
`"all"` will show all snippets for the current filetype (including custom context snippets
that are not currently expandable). If using this option, be aware
that all snippets for the current buffer will be cached (even if the snippet definitions
changed). You can then manually reload the snippets with the command `:CmpUltisnipsReloadSnippets`
or by using an autocommand:
Expand All @@ -143,8 +145,6 @@ or by using an autocommand:
autocmd BufWritePost *.snippets :CmpUltisnipsReloadSnippets
```

Custom context snippets (option `e`) are currently not available.

**Default:** `"expandable"`

---
Expand Down
22 changes: 15 additions & 7 deletions autoload/cmp_nvim_ultisnips.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@ python3 << EOF
import vim
from UltiSnips import UltiSnips_Manager, vim_helper

if vim.eval("a:expandable_only") == "True":
before = vim_helper.buf.line_till_cursor
before = vim_helper.buf.line_till_cursor
visual_content = UltiSnips_Manager._visual_content
expandable_only = vim.eval("a:expandable_only") == "True"
if expandable_only:
snippets = UltiSnips_Manager._snips(before, True)
else:
snippets = UltiSnips_Manager._snips("", True)

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:
continue

vim.command(
"call add(g:_cmpu_current_snippets, {"
"'trigger': py3eval('str(snippet._trigger)'),"
"'description': py3eval('str(snippet._description)'),"
"'options': py3eval('str(snippet._opts)'),"
"'value': py3eval('str(snippet._value)'),"
"'matched': py3eval('str(snippet._matched)'),"
"'trigger': py3eval('snippet._trigger'),"
"'description': py3eval('snippet._description'),"
"'options': py3eval('snippet._opts'),"
"'value': py3eval('snippet._value'),"
"'matched': py3eval('snippet._matched'),"
"})"
)
EOF
Expand Down
25 changes: 11 additions & 14 deletions lua/cmp_nvim_ultisnips/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ function source.complete(self, _, callback)
local snippets = cmpu_snippets.load_snippets(self.expandable_only)

for _, snippet in pairs(snippets) do
-- Skip expression snippets for now
if not snippet.options:match("e") then
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 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
end
callback {
Expand Down