Skip to content

Commit

Permalink
refactor: extract Python code to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
smjonas committed May 26, 2024
1 parent 24bca5c commit b8719dc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
70 changes: 0 additions & 70 deletions autoload/cmp_nvim_ultisnips.vim
Original file line number Diff line number Diff line change
@@ -1,73 +1,3 @@
" TODO: move python code into separate files

" Retrieves additional snippet information that is not directly accessible
" using the UltiSnips API functions. Returns a list of tables (one table
" per snippet) with the keys "trigger", "description", "options" and "value".
"
" If 'expandable_only' is "True", only expandable snippets are returned, otherwise all
" 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
import vim
from UltiSnips import UltiSnips_Manager, vim_helper

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
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, {"
"'trigger': py3eval('snippet._trigger'),"
"'description': py3eval('snippet._description'),"
"'options': py3eval('snippet._opts'),"
"'value': py3eval('snippet._value'),"
"'matched': py3eval('snippet._matched'),"
"})"
)
EOF
return g:_cmpu_current_snippets
endfunction

function cmp_nvim_ultisnips#set_filetype(filetype)
python3 << EOF
import vim
from UltiSnips import vim_helper

filetype = vim.eval("a:filetype")
class CustomVimBuffer(vim_helper.VimBuffer):
@property
def filetypes(self):
return [filetype]

vim_helper._orig_buf = vim_helper.buf
vim_helper.buf = CustomVimBuffer() # TODO: avoid creating a new class instance every time
EOF
endfunction

function! cmp_nvim_ultisnips#reset_filetype()
python3 << EOF
from UltiSnips import vim_helper

# Restore to original VimBuffer instance
vim_helper.buf = vim_helper._orig_buf
EOF
endfunction

function! cmp_nvim_ultisnips#setup_treesitter_autocmds()
augroup cmp_nvim_ultisnips
autocmd!
Expand Down
1 change: 1 addition & 0 deletions lua/cmp_nvim_ultisnips/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local source
function M.create_source()
-- Source UltiSnips file in case it is not loaded yet (GH issue #49)
vim.cmd("runtime! plugin/UltiSnips.vim")
vim.cmd("py3 import ultisnips_utils")
-- This is necessary because we want to be able to define our own
-- select mode mappings used to jump between snippet tabstops (GH issue #5)
vim.g.UltiSnipsRemoveSelectModeMappings = 0
Expand Down
7 changes: 4 additions & 3 deletions lua/cmp_nvim_ultisnips/snippets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ function M.load_snippets(expandable_only)
if expandable_only then
-- Do not cache snippets since the set of expandable
-- snippets can change on every keystroke.
return vim.fn["cmp_nvim_ultisnips#get_current_snippets"](true)
vim.cmd("py3 ultisnips_utils.fetch_current_snippets(True)")
return vim.g["cmpu_current_snippets"]
end
local ft = vim.bo.filetype
local snippets = snippets_for_ft[ft]
if not snippets then
snippets = vim.fn["cmp_nvim_ultisnips#get_current_snippets"](false)
snippets_for_ft[ft] = snippets
vim.cmd("py3 ultisnips_utils.fetch_current_snippets(False)")
snippets_for_ft[ft] = vim.g["_cmpu_current_snippets"]
end
return snippets
end
Expand Down
4 changes: 2 additions & 2 deletions lua/cmp_nvim_ultisnips/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ local cur_ft_at_cursor
function M.set_filetype()
local new_ft = get_ft_at_cursor()
if new_ft ~= nil and new_ft ~= cur_ft_at_cursor and new_ft ~= vim.bo.filetype then
vim.fn["cmp_nvim_ultisnips#set_filetype"](new_ft)
vim.cmd("py3 ultisnips_utils.set_filetype(new_ft)")
cur_ft_at_cursor = new_ft
end
end

function M.reset_filetype()
if cur_ft_at_cursor ~= nil then
vim.fn["cmp_nvim_ultisnips#reset_filetype"]()
vim.cmd("py3 ultisnips_utils.reset_filetype()")
cur_ft_at_cursor = nil
end
end
Expand Down
48 changes: 48 additions & 0 deletions python3/ultisnips_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import vim
from UltiSnips import UltiSnips_Manager, vim_helper

# Retrieves additional snippet information that is not directly accessible
# using the UltiSnips API functions. Stores a list of dictionaries (one per
# snippet) with the keys "trigger", "description", "options" and "value"
# in the vim variable g:_cmpu_current_snippets.
#
# If 'expandable_only' is True, only expandable snippets are stored, otherwise
# all snippets for the current filetype are added.


cmpu_cur_snippet = None


def fetch_current_snippets(expandable_only):
if expandable_only:
before = vim_helper.buf.line_till_cursor
snippets = UltiSnips_Manager._snips(before, True)
else:
snippets = UltiSnips_Manager._snips("", True)

vim.command("let g:_cmpu_current_snippets = []")
global cmpu_cur_snippet
for snippet in snippets:
cmpu_cur_snippet = snippet
vim.command(
"call add(g:_cmpu_current_snippets, {"
"'trigger': pyxeval('str(cmpu_cur_snippet._trigger)'),"
"'description': pyxeval('str(cmpu_cur_snippet._description)'),"
"'options': pyxeval('str(cmpu_cur_snippet._opts)'),"
"'value': pyxeval('str(cmpu_cur_snippet._value)'),"
"})"
)


def set_filetype(ft):
class CustomVimBuffer(vim_helper.VimBuffer):
@property
def filetypes(self):
return [ft]

vim_helper._orig_buf = vim_helper.buf
vim_helper.buf = CustomVimBuffer()


def reset_filetype():
vim_helper.buf = vim_helper._orig_buf

0 comments on commit b8719dc

Please sign in to comment.