summaryrefslogtreecommitdiff
path: root/autoload/deoplete/handler.vim
blob: 737c9291ce770e13d1172f763ab9b761330c7171 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"=============================================================================
" 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:on_complete_done()
  augroup END

  for event in [
        \ 'InsertEnter', 'BufReadPost', 'BufWritePost', 'VimLeavePre',
        \ ]
    call s:define_on_event(event)
  endfor

  if deoplete#custom#_get_option('on_text_changed_i')
    call s:define_completion_via_timer('TextChangedI')
  endif
  if deoplete#custom#_get_option('on_insert_enter')
    call s:define_completion_via_timer('InsertEnter')
  endif
  if deoplete#custom#_get_option('refresh_always')
    if exists('##TextChangedP')
      call s:define_completion_via_timer('TextChangedP')
    else
      call s:define_completion_via_timer('InsertCharPre')
    endif
  endif

  " Note: Vim 8 GUI(MacVim and Win32) is broken
  " dummy timer call is needed before complete()
  if !has('nvim') && has('gui_running')
        \ && (has('gui_macvim') || has('win32'))
    let s:dummy_timer = timer_start(200, {timer -> 0}, {'repeat': -1})
  endif

  if deoplete#util#has_yarp()
    " To fix "RuntimeError: Event loop is closed" issue
    " Note: Workaround
    autocmd deoplete VimLeavePre * call s:kill_yarp()
  endif
endfunction

function! deoplete#handler#_do_complete() abort
  let context = g:deoplete#_context
  let event = get(context, 'event', '')
  let modes = (event ==# 'InsertEnter') ? ['n', 'i'] : ['i']
  if s:is_exiting() || index(modes, mode()) < 0 || s:check_input_method()
    return
  endif

  if !has_key(context, 'candidates')
        \ || deoplete#util#get_input(context.event) !=# context.input
    return
  endif

  let prev = g:deoplete#_prev_completion
  let prev.event = context.event
  let prev.input = context.input
  let prev.candidates = context.candidates
  let prev.complete_position = context.complete_position
  let prev.linenr = line('.')

  let auto_popup = deoplete#custom#_get_option(
        \ 'auto_complete_popup') !=# 'manual'

  " Enable auto refresh when popup is displayed
  if deoplete#util#check_popup()
    let auto_popup = v:true
  endif

  if context.event ==# 'Manual'
    let context.event = ''
  elseif !exists('g:deoplete#_saved_completeopt') && auto_popup
    call deoplete#mapping#_set_completeopt()
  endif

  if auto_popup
    call feedkeys("\<Plug>_", 'i')
  endif
endfunction

function! deoplete#handler#_check_omnifunc(context) abort
  let prev = g:deoplete#_prev_completion
  let blacklist = ['LanguageClient#complete']
  if a:context.event ==# 'Manual'
        \ || &l:omnifunc ==# ''
        \ || index(blacklist, &l:omnifunc) >= 0
        \ || prev.input ==# a:context.input
        \ || s:check_input_method()
        \ || deoplete#custom#_get_option('auto_complete_popup') ==# 'manual'
    return
  endif

  for filetype in a:context.filetypes
    for pattern in deoplete#util#convert2list(
          \ deoplete#custom#_get_filetype_option(
          \   'omni_patterns', filetype, ''))
      if pattern !=# '' && a:context.input =~# '\%('.pattern.'\)$'
        let g:deoplete#_context.candidates = []

        let prev.event = a:context.event
        let prev.input = a:context.input
        let prev.candidates = []

        if &completeopt =~# 'noselect'
          call deoplete#mapping#_set_completeopt()
          call feedkeys("\<C-x>\<C-o>", 'in')
        else
          call deoplete#util#print_error(
                \ 'omni_patterns feature is disabled.')
          call deoplete#util#print_error(
                \ 'You need to set "noselect" in completeopt option.')
        endif
        return 1
      endif
    endfor
  endfor
endfunction

function! s:completion_timer_start(event) abort
  if exists('s:completion_timer')
    call s:completion_timer_stop()
  endif

  let delay = deoplete#custom#_get_option('auto_complete_delay')
  if delay > 0
    let s:completion_timer = timer_start(
          \ delay, {-> deoplete#handler#_completion_begin(a:event)})
  else
    call deoplete#handler#_completion_begin(a:event)
  endif
endfunction
function! s:completion_timer_stop() abort
  if !exists('s:completion_timer')
    return
  endif

  call timer_stop(s:completion_timer)
  unlet s:completion_timer
endfunction

function! s:check_prev_completion(event) abort
  let prev = g:deoplete#_prev_completion
  if a:event ==# 'Async' || a:event ==# 'Update' || mode() !=# 'i'
        \ || empty(get(prev, 'candidates', []))
        \ || s:check_input_method()
    return
  endif

  let input = deoplete#util#get_input(a:event)
  let complete_str = matchstr(input, '\w\+$')
  let min_pattern_length = deoplete#custom#_get_option('min_pattern_length')
  if prev.linenr != line('.') || len(complete_str) < min_pattern_length
    return
  endif

  call deoplete#mapping#_set_completeopt()

  let mode = deoplete#custom#_get_option('prev_completion_mode')
  let candidates = copy(prev.candidates)

  if mode ==# 'filter' || mode ==# 'length'
    let input = input[prev.complete_position :]
    let escaped_input = escape(input, '~\.^$[]*')
    let pattern = substitute(escaped_input, '\w', '\\w*\0', 'g')
    call filter(candidates, 'v:val.word =~? pattern')
    if mode ==# 'length'
      call filter(candidates, 'len(v:val.word) > len(input)')
    endif
  elseif mode ==# 'mirror'
    " pass
  else
    return
  endif

  let g:deoplete#_filtered_prev = {
        \ 'complete_position': prev.complete_position,
        \ 'candidates': candidates,
        \ }
  call feedkeys("\<Plug>+", 'i')
endfunction

function! deoplete#handler#_async_timer_start() abort
  let delay = deoplete#custom#_get_option('auto_refresh_delay')
  if delay <= 0
    return
  endif

  call timer_start(max([20, delay]), {-> deoplete#auto_complete()})
endfunction

function! deoplete#handler#_completion_begin(event) abort
  call deoplete#custom#_update_cache()

  if s:is_skip(a:event)
    let g:deoplete#_context.candidates = []

    " Close the popup
    if deoplete#util#check_popup()
      call feedkeys("\<Plug>_", 'i')
    endif
    return
  endif

  call s:check_prev_completion(a:event)

  if a:event !=# 'Update' && a:event !=# 'Async'
    call deoplete#init#_prev_completion()
  endif

  call deoplete#util#rpcnotify(
        \ 'deoplete_auto_completion_begin', {'event': a:event})
endfunction
function! s:is_skip(event) abort
  if a:event ==# 'TextChangedP' && !empty(v:completed_item)
    return 1
  endif

  if s:is_skip_text(a:event)
    return 1
  endif

  let auto_complete = deoplete#custom#_get_option('auto_complete')

  if &paste
        \ || (a:event !=# 'Manual' && a:event !=# 'Update' && !auto_complete)
        \ || (&l:completefunc !=# '' && &l:buftype =~# 'nofile')
        \ || (a:event !=# 'InsertEnter' && mode() !=# 'i')
    return 1
  endif

  return 0
endfunction
function! s:is_skip_text(event) abort
  let input = deoplete#util#get_input(a:event)

  let lastchar = matchstr(input, '.$')
  let skip_multibyte = deoplete#custom#_get_option('skip_multibyte')
  if skip_multibyte && len(lastchar) != strwidth(lastchar)
        \ && empty(get(b:, 'eskk', []))
    return 1
  endif

  " Note: Use g:deoplete#_context is needed instead of
  " g:deoplete#_prev_completion
  let prev_input = get(g:deoplete#_context, 'input', '')
  if input ==# prev_input
        \ && input !=# ''
        \ && a:event !=# 'Manual'
        \ && a:event !=# 'Async'
        \ && a:event !=# 'Update'
        \ && a:event !=# 'TextChangedP'
    return 1
  endif
  if a:event ==# 'Update' && prev_input !=# '' && input !=# prev_input
    return 1
  endif

  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#custom#_get_option('skip_chars')

  return (a:event !=# 'Manual' && input !=# ''
        \     && index(skip_chars, input[-1:]) >= 0)
endfunction
function! s:check_input_method() abort
  return exists('*getimstatus') && getimstatus()
endfunction

function! s:define_on_event(event) abort
  if !exists('##' . a:event)
    return
  endif

  execute 'autocmd deoplete' a:event
        \ '* if !&l:previewwindow | call deoplete#send_event('
        \ .string(a:event).') | endif'
endfunction
function! s:define_completion_via_timer(event) abort
  if !exists('##' . a:event)
    return
  endif

  execute 'autocmd deoplete' a:event
        \ '* call s:completion_timer_start('.string(a:event).')'
endfunction

function! s:on_insert_leave() abort
  call deoplete#mapping#_restore_completeopt()
  let g:deoplete#_context = {}
  call deoplete#init#_prev_completion()
endfunction

function! s:on_complete_done() abort
  if get(v:completed_item, 'word', '') ==# ''
    return
  endif
  call deoplete#handler#_skip_next_completion()

  if get(v:completed_item, 'user_data', '') !=# ''
    try
      if type(v:completed_item.user_data) == type('')
        call s:substitute_suffix(json_decode(v:completed_item.user_data))
      endif
    catch /.*/
    endtry
  endif
endfunction
function! s:substitute_suffix(user_data) abort
  if !has_key(a:user_data, 'old_suffix')
        \ || !has_key(a:user_data, 'new_suffix')
    return
  endif
  let old_suffix = a:user_data.old_suffix
  let new_suffix = a:user_data.new_suffix

  let next_text = deoplete#util#get_next_input('CompleteDone')
  if stridx(next_text, old_suffix) != 0
    return
  endif

  let next_text = new_suffix . next_text[len(old_suffix):]
  call setline('.', deoplete#util#get_input('CompleteDone') . next_text)
endfunction

function! deoplete#handler#_skip_next_completion() abort
  if !exists('g:deoplete#_context')
    return
  endif

  let input = deoplete#util#get_input('CompleteDone')
  if input !~# '[/.]$'
    let g:deoplete#_context.input = input
  endif
  call deoplete#mapping#_restore_completeopt()
  call deoplete#init#_prev_completion()
endfunction

function! s:is_exiting() abort
  return exists('v:exiting') && v:exiting != v:null
endfunction

function! s:kill_yarp() abort
  if !exists('g:deoplete#_yarp')
    return
  endif

  if g:deoplete#_yarp.job_is_dead
    return
  endif

  let job = g:deoplete#_yarp.job
  if !has('nvim') && !exists('g:yarp_jobstart')
    " Get job object from vim-hug-neovim-rpc
    let job = g:_neovim_rpc_jobs[job].job
  endif

  if has('nvim')
    call jobstop(job)
  else
    call job_stop(job, 'kill')
  endif

  let g:deoplete#_yarp.job_is_dead = 1
endfunction