Skip to content

Commit

Permalink
Add variable to control AutoTrigger (#1551)
Browse files Browse the repository at this point in the history
Now it is possible to disable AutoTrigger via global variable `g:UltiSnipsAutoTrigger`, and to dynamically toggle AutoTrigger on/off with `Ultisnips#ToggleAutoTrigger()`.
  • Loading branch information
jdujava committed Mar 15, 2024
1 parent b393ba6 commit 808114c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
5 changes: 5 additions & 0 deletions autoload/UltiSnips.vim
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ function! UltiSnips#CanJumpBackwards() abort
return can_jump_backwards
endfunction

function! UltiSnips#ToggleAutoTrigger() abort
py3 vim.command("let autotrigger = %d" % UltiSnips_Manager._toggle_autotrigger())
return autotrigger
endfunction

function! UltiSnips#SaveLastVisualSelection() range abort
py3 UltiSnips_Manager._save_last_visual_selection()
return ""
Expand Down
19 changes: 19 additions & 0 deletions doc/UltiSnips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,21 @@ situation. This is useful in conditional mappings.
This function returns 1 if UltiSnips can jump backwards in the current
situation. This is useful in conditional mappings.

3.4.7 UltiSnips#ToggleAutoTrigger *UltiSnips#ToggleAutoTrigger*

This function toggles the `autotrigger` functionality. Its return value
corresponds to the new state of the `autotrigger` (0: disabled, 1: enabled).

For example, the following mapping can be used to toggle the `autotrigger`
in the Insert mode: >
inoremap <silent> <C-t> <CMD>call UltiSnips#ToggleAutoTrigger()<CR>
<
*g:UltiSnipsAutoTrigger*
By default, `autotrigger` is enabled (relevant for snippets with the option 'A').
If you want to disable it by default, add the following line to your vimrc: >
let g:UltiSnipsAutoTrigger = 0
3.5 Warning about missing python support *UltiSnips-python-warning*
----------------------------------------

Expand Down Expand Up @@ -1833,6 +1848,10 @@ will be triggered.
*Warning:* using of this feature might lead to significant vim slowdown. If
you discovered that, please report an issue.

If you want to temporarily disable autotriggering, the `autotrigger` behavior
can be toggled via |UltiSnips#ToggleAutoTrigger|. By default it is enabled,
but it is possible to customize it with |g:UltiSnipsAutoTrigger|.

Consider following useful Go snippets:
------------------- SNIP -------------------
>
Expand Down
11 changes: 10 additions & 1 deletion pythonx/UltiSnips/snippet_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def __init__(self, expand_trigger, forward_trigger, backward_trigger):
if enable_snipmate == "1":
self.register_snippet_source("snipmate_files", SnipMateFileSource())

self._autotrigger = True
if vim_helper.eval("exists('g:UltiSnipsAutoTrigger')") == "1":
self._autotrigger = vim_helper.eval("g:UltiSnipsAutoTrigger") == "1"

self._should_update_textobjects = False
self._should_reset_visual = False

Expand Down Expand Up @@ -852,6 +856,10 @@ def can_jump_forwards(self):
def can_jump_backwards(self):
return self.can_jump(JumpDirection.BACKWARD)

def _toggle_autotrigger(self):
self._autotrigger = not self._autotrigger
return self._autotrigger

@property
def _current_snippet(self):
"""The current snippet or None."""
Expand Down Expand Up @@ -956,7 +964,8 @@ def _track_change(self):
before = vim_helper.buf.line_till_cursor

if (
before
self._autotrigger
and before
and self._last_change[0] != ""
and before[-1] == self._last_change[0]
):
Expand Down
47 changes: 47 additions & 0 deletions test/test_Autotrigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,50 @@ class Autotrigger_CanMatchPreviouslySelectedPlaceholder(_VimTest):
}
keys = "if" + EX + "=" + ESC + "o="
wanted = "if var == nil: pass\n="

class Autotrigger_GlobalDisable(_VimTest):
def _extra_vim_config(self, vim_config):
vim_config.append("let g:UltiSnipsAutoTrigger=0")
files = {
"us/all.snippets": r"""
snippet a "desc" A
autotriggered
endsnippet
"""
}
keys = "a"
wanted = "a"

class Autotrigger_CanToggle(_VimTest):
files = {
"us/all.snippets": r"""
snippet a "desc" A
autotriggered
endsnippet
"""
}
keys = (
"a"
+ ESC + ":call UltiSnips#ToggleAutoTrigger()\n"
+ "o" + "a"
+ ESC + ":call UltiSnips#ToggleAutoTrigger()\n"
+ "o" + "a"
)
wanted = "autotriggered\na\nautotriggered"

class Autotrigger_GlobalDisableThenToggle(_VimTest):
def _extra_vim_config(self, vim_config):
vim_config.append("let g:UltiSnipsAutoTrigger=0")
files = {
"us/all.snippets": r"""
snippet a "desc" A
autotriggered
endsnippet
"""
}
keys = (
"a"
+ ESC + ":call UltiSnips#ToggleAutoTrigger()\n"
+ "o" + "a"
)
wanted = "a\nautotriggered"

0 comments on commit 808114c

Please sign in to comment.