]> git.madduck.net Git - etc/vim.git/blob - autoload/asyncomplete/utils/_on_change/timer.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/asyncomplete/' content from commit 016590d2
[etc/vim.git] / autoload / asyncomplete / utils / _on_change / timer.vim
1 let s:callbacks = []
2
3 let s:change_timer = -1
4 let s:last_tick = []
5
6 function! asyncomplete#utils#_on_change#timer#init() abort
7     call s:setup_if_required()
8     return {
9         \ 'name': 'timer',
10         \ 'register': function('s:register'),
11         \ 'unregister': function('s:unregister'),
12     \ }
13 endfunction
14
15 function! s:setup_if_required() abort
16     augroup asyncomplete_utils_on_change_timer
17         autocmd!
18         autocmd InsertEnter * call s:on_insert_enter()
19         autocmd InsertLeave * call s:on_insert_leave()
20         autocmd TextChangedI * call s:on_text_changed_i()
21     augroup END
22 endfunction
23
24 function! s:register(cb) abort
25     call add(s:callbacks , a:cb)
26 endfunction
27
28 function! s:unregister(obj, cb) abort
29     " TODO: remove from s:callbacks
30 endfunction
31
32 function! s:on_insert_enter() abort
33     let s:previous_position = getcurpos()
34     call s:change_tick_start()
35 endfunction
36
37 function! s:on_insert_leave() abort
38     unlet s:previous_position
39     call s:change_tick_stop()
40 endfunction
41
42 function! s:on_text_changed_i() abort
43     call s:check_changes()
44 endfunction
45
46 function! s:change_tick_start() abort
47     if !exists('s:change_timer')
48         let s:last_tick = s:change_tick()
49         " changes every 30ms, which is 0.03s, it should be fast enough
50         let s:change_timer = timer_start(30, function('s:check_changes'), { 'repeat': -1 })
51     endif
52 endfunction
53
54 function! s:change_tick_stop() abort
55     if exists('s:change_timer')
56         call timer_stop(s:change_timer)
57         unlet s:change_timer
58         let s:last_tick = []
59     endif
60 endfunction
61
62 function! s:check_changes(...) abort
63     let l:tick = s:change_tick()
64     if l:tick != s:last_tick
65         let s:last_tick = l:tick
66         call s:maybe_notify_on_change()
67     endif
68 endfunction
69
70 function! s:maybe_notify_on_change() abort
71     " enter to new line or backspace to previous line shouldn't cause change trigger
72     let l:previous_position = s:previous_position
73     let s:previous_position = getcurpos()
74     if l:previous_position[1] ==# getcurpos()[1]
75         for l:Cb in s:callbacks
76             call l:Cb()
77         endfor
78     endif
79 endfunction
80
81 function! s:change_tick() abort
82     return [b:changedtick, getcurpos()]
83 endfunction