SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.55k stars 691 forks source link

Add variable to control AutoTrigger #1551

Closed jdujava closed 8 months ago

jdujava commented 10 months ago

~Now it is possible to disable AutoTrigger in the current buffer by setting the buffer variable b:UltiSnipsAutoTrigger = 0.~

Now it is possible to disable AutoTrigger via global variable g:UltiSnipsAutoTrigger, and to dynamically toggle AutoTrigger on/off with Ultisnips#ToggleAutoTrigger().

This is just a first crude shot at implementing this "toggling feature" (documentation and so on TODO). What do you think, is this alright, or is there perhaps some better way in your opinion?

SirVer commented 9 months ago

I worry a bit about performance. Calling out to vim.eval is slow and this code will run on every cursor movement. This should probably be checked in vimscript before calling into python. Overall, I am fine with the feature.

ces42 commented 9 months ago

I worry a bit about performance. Calling out to vim.eval is slow and this code will run on every cursor movement. This should probably be checked in vimscript before calling into python. Overall, I am fine with the feature.

Yes this can be done much better by removing/re-creating the UltiSnips_AutoTrigger autogroup from plugin/UltiSnips.vim using vimscript. There's no need to write python code for this.

jdujava commented 9 months ago

To not complicate things, I added only a global toggling mechanism.

@ces42 To be honest, I wasn't sure wheter UltiSnips_Manager._track_change() was needed also for other purposes than AutoTrigger, so I tried it this way. But I am certainly open to other (possibly better) solutions!

ces42 commented 9 months ago

Adding this to autoload/UltiSnips.vim provides control over autotrigger:

function! UltiSnips#EnableAutotrigger() abort
    augroup UltiSnips_AutoTrigger
        au!
        au InsertCharPre * call UltiSnips#TrackChange()
        au TextChangedI * call UltiSnips#TrackChange()
        if exists('##TextChangedP')
            au TextChangedP * call UltiSnips#TrackChange()
        endif
    augroup END
endfunction

function! UltiSnips#DisableAutotrigger() abort
    augroup UltiSnips_AutoTrigger
        au!
    augroup END
endfunction

function! UltiSnips#ToggleAutotrigger() abort
    " if exists('#UltiSnips_AutoTrigger')
    if exists('#UltiSnips_AutoTrigger#TextChangedI')
        call UltiSnips#DisableAutotrigger()
    else
        call UltiSnips#EnableAutotrigger()
    endif
endfunction

EDIT: apparently the augroup doesn't get deleted when you do au!, only it's autocommands. So you need to replace exists('#UltiSnips_AutoTrigger') with exists('#UltiSnips_AutoTrigger#TextChangedI').

SirVer commented 9 months ago

Great, but a test is still missing. Will you work on that @jdujava ?

jdujava commented 9 months ago

@SirVer Added tests and documentation, does it look OK?

jdujava commented 8 months ago

@SirVer is it good to merge, or is there perhaps anything else to do?

SirVer commented 8 months ago

@jdujava Yes, it looks great, an exemplary contribution to an open source project. Thank you very much! Sorry that I took so long to get around to it.