]> git.madduck.net Git - etc/vim.git/blob - autoload/asyncomplete/utils/_on_change/textchangedp.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 / textchangedp.vim
1 let s:callbacks = []
2
3 function! asyncomplete#utils#_on_change#textchangedp#init() abort
4     if exists('##TextChangedP')
5         call s:setup_if_required()
6         return {
7             \ 'name': 'TextChangedP',
8             \ 'register': function('s:register'),
9             \ 'unregister': function('s:unregister'),
10         \ }
11     else
12         return { 'name': 'TextChangedP', 'error': 'Requires vim with TextChangedP support' }
13     endif
14 endfunction
15
16 function! s:setup_if_required() abort
17     augroup asyncomplete_utils_on_change_text_changed_p
18         autocmd!
19         autocmd InsertEnter * call s:on_insert_enter()
20         autocmd InsertLeave * call s:on_insert_leave()
21         autocmd TextChangedI * call s:on_text_changed_i()
22         autocmd TextChangedP * call s:on_text_changed_p()
23     augroup END
24 endfunction
25
26 function! s:register(cb) abort
27     call add(s:callbacks , a:cb)
28 endfunction
29
30 function! s:unregister(obj, cb) abort
31     " TODO: remove from s:callbacks
32 endfunction
33
34 function! s:on_insert_enter() abort
35     let l:context = asyncomplete#context()
36     let s:previous_context = {
37         \ 'lnum': l:context['lnum'],
38         \ 'col': l:context['col'],
39         \ 'typed': l:context['typed'],
40         \ }
41 endfunction
42
43 function! s:on_insert_leave() abort
44     unlet! s:previous_context
45 endfunction
46
47 function! s:on_text_changed_i() abort
48     call s:maybe_notify_on_change()
49 endfunction
50
51 function! s:on_text_changed_p() abort
52     call s:maybe_notify_on_change()
53 endfunction
54
55 function! s:maybe_notify_on_change() abort
56     if !exists('s:previous_context')
57         return
58     endif
59     " We notify on_change callbacks only when the cursor position
60     " has changed.
61     " Unfortunatelly we need this check because in insert mode it
62     " is possible to have TextChangedI triggered when the completion
63     " context is not changed at all: When we close the completion
64     " popup menu via <C-e> or <C-y>. If we still let on_change
65     " do the completion in this case we never close the menu.
66     " Vim doesn't allow programmatically changing buffer content
67     " in insert mode, so by comparing the cursor's position and the
68     " completion base we know whether the context has changed.
69     let l:context = asyncomplete#context()
70     let l:previous_context = s:previous_context
71     let s:previous_context = {
72         \ 'lnum': l:context['lnum'],
73         \ 'col': l:context['col'],
74         \ 'typed': l:context['typed'],
75         \ }
76     if l:previous_context !=# s:previous_context
77         for l:Cb in s:callbacks
78             call l:Cb()
79         endfor
80     endif
81 endfunction