summaryrefslogtreecommitdiff
path: root/autoload/deoplete/handler.vim
blob: a73ef87455c03bbe1f66208f5f149bd05d65d7ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"=============================================================================
" FILE: handler.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================

function! deoplete#handler#_init() abort "{{{
  augroup deoplete
    autocmd!
    autocmd InsertLeave * call s:on_insert_leave()
    autocmd CompleteDone * call s:complete_done()
    autocmd InsertCharPre * call s:on_insert_char_pre()

    autocmd TextChangedI * call s:completion_check('TextChangedI')
    autocmd InsertEnter * call s:completion_check('InsertEnter')
  augroup END

  for event in ['BufNewFile', 'BufRead', 'BufWritePost']
    execute 'autocmd deoplete' event '* call s:on_event('.string(event).')'
  endfor

  call s:on_event('Init')
endfunction"}}}

function! s:completion_check(event) abort "{{{
  let delay = get(g:deoplete#_context, 'refresh', 0) ?
        \ g:deoplete#auto_refresh_delay : g:deoplete#auto_complete_delay
  if has('timers') && delay > 0
    if exists('s:timer')
      call timer_stop(s:timer.id)
    endif

    if a:event !=# 'Manual'
      let s:timer = { 'event': a:event, 'changedtick': b:changedtick }
      let s:timer.id = timer_start(delay, 's:completion_delayed')
      return
    endif
  endif

  return s:completion_begin(a:event)
endfunction"}}}

function! s:completion_delayed(timer) abort "{{{
  let timer = s:timer
  unlet! s:timer
  if b:changedtick == timer.changedtick
    call s:completion_begin(timer.event)
  endif
endfunction"}}}

function! s:completion_begin(event) abort "{{{
  let context = deoplete#init#_context(a:event, [])
  if s:is_skip(a:event, context)
    return
  endif

  " Save the previous position
  let g:deoplete#_context.position = context.position

  let g:deoplete#_context.refresh = 0

  " Call omni completion
  for filetype in context.filetypes
    for pattern in deoplete#util#convert2list(
          \ deoplete#util#get_buffer_config(filetype,
          \ 'b:deoplete_omni_patterns',
          \ 'g:deoplete#omni_patterns',
          \ 'g:deoplete#_omni_patterns'))
      if pattern != '' && &l:omnifunc != ''
            \ && context.input =~# '\%('.pattern.'\)$'
        call deoplete#mapping#_set_completeopt()
        call feedkeys("\<C-x>\<C-o>", 'n')
        return
      endif
    endfor
  endfor

  call deoplete#mapping#_set_completeopt()
  call rpcnotify(g:deoplete#_channel_id,
        \ 'deoplete_auto_completion_begin', context)
endfunction"}}}
function! s:is_skip(event, context) abort "{{{
  if s:is_skip_text(a:event)
    return 1
  endif

  let disable_auto_complete =
        \ deoplete#util#get_simple_buffer_config(
        \   'b:deoplete_disable_auto_complete',
        \   'g:deoplete#disable_auto_complete')

  if &paste
        \ || mode() !=# 'i'
        \ || (a:event !=# 'Manual' && disable_auto_complete)
        \ || (&l:completefunc != '' && &l:buftype =~# 'nofile')
        \ || (a:event ==# 'InsertEnter'
        \     && has_key(g:deoplete#_context, 'position'))
    return 1
  endif

  if !get(g:deoplete#_context, 'refresh', 0)
        \ && a:context.position ==# get(g:deoplete#_context, 'position', [])
    let word = get(v:completed_item, 'word', '')
    let delimiters = filter(copy(g:deoplete#delimiters),
        \         'strridx(word, v:val) == (len(word) - len(v:val))')
    if word == '' || empty(delimiters)
      return 1
    endif
  endif

  return 0
endfunction"}}}
function! s:is_skip_text(event) abort "{{{
  let input = deoplete#util#get_input(a:event)
  let displaywidth = strdisplaywidth(input) + 1

  if &l:formatoptions =~# '[tca]' && &l:textwidth > 0
        \     && displaywidth >= &l:textwidth
    if &l:formatoptions =~# '[ta]'
          \ || !empty(filter(deoplete#util#get_syn_names(),
          \                  "v:val ==# 'Comment'"))
      return 1
    endif
  endif

  let skip_chars = deoplete#util#get_simple_buffer_config(
        \   'b:deoplete_skip_chars', 'g:deoplete#skip_chars')

  return (!pumvisible() && virtcol('.') != displaywidth)
        \ || (a:event !=# 'Manual' && input != ''
        \     && index(skip_chars, input[-1:]) >= 0)
endfunction"}}}

function! s:on_event(event) abort "{{{
  let context = deoplete#init#_context(a:event, [])
  call rpcnotify(g:deoplete#_channel_id, 'deoplete_on_event', context)
endfunction"}}}

function! s:on_insert_leave() abort "{{{
  if exists('g:deoplete#_saved_completeopt')
    let &completeopt = g:deoplete#_saved_completeopt
    unlet g:deoplete#_saved_completeopt
  endif
  let g:deoplete#_context = {}
endfunction"}}}

function! s:complete_done() abort "{{{
  if get(v:completed_item, 'word', '') != ''
    let word = v:completed_item.word
    if !has_key(g:deoplete#_rank, word)
      let g:deoplete#_rank[word] = 1
    else
      let g:deoplete#_rank[word] += 1
    endif
  endif

  let g:deoplete#_context.position = getpos('.')
endfunction"}}}

function! s:on_insert_char_pre() abort "{{{
  if !pumvisible()
        \ || !g:deoplete#enable_refresh_always
        \ || s:is_skip_text('InsertCharPre')
    return 1
  endif

  " Auto refresh
  call feedkeys("\<Plug>(deoplete_auto_refresh)")
endfunction"}}}

" vim: foldmethod=marker