summaryrefslogtreecommitdiff
path: root/autoload/deoplete/init.vim
blob: d859e55293ec5e6bad42044375e691caba3d6947 (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
"=============================================================================
" FILE: init.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================

if !exists('s:is_handler_enabled')
  let s:is_handler_enabled = 0
endif

let s:is_windows = ((has('win32') || has('win64')) ? v:true : v:false)

function! deoplete#init#_is_handler_enabled() abort
  return s:is_handler_enabled
endfunction

function! deoplete#init#_initialize() abort
  if exists('g:deoplete#_initialized')
    return 1
  endif

  let g:deoplete#_initialized = v:false

  call s:init_internal_variables()
  call deoplete#init#_custom_variables()

  if deoplete#init#_channel()
    return 1
  endif

  call deoplete#mapping#_init()
endfunction
function! deoplete#init#_channel() abort
  if !exists('g:deoplete#_serveraddr')
    return 1
  endif

  let python3 = get(g:, 'python3_host_prog', 'python3')
  if !executable(python3)
    call deoplete#util#print_error(
          \ string(python3) . ' is not executable.')
    call deoplete#util#print_error(
          \ 'You need to set g:python3_host_prog.')
  endif

  try
    if deoplete#util#has_yarp()
      let g:deoplete#_yarp = yarp#py3('deoplete')
      call g:deoplete#_yarp.notify('deoplete_init')
    else
      " rplugin.vim may not be loaded on VimEnter
      if !exists('g:loaded_remote_plugins')
        runtime! plugin/rplugin.vim
      endif

      call _deoplete_init()
    endif
  catch
    call deoplete#util#print_error(v:exception)
    call deoplete#util#print_error(v:throwpoint)

    if !has('python3')
      call deoplete#util#print_error(
            \ 'deoplete requires Python3 support("+python3").')
    endif

    if deoplete#util#has_yarp()
      echomsg string(expand('<sfile>'))
      if !exists('*yarp#py3')
        call deoplete#util#print_error(
              \ 'deoplete requires nvim-yarp plugin.')
      endif
    else
      call deoplete#util#print_error(
          \ 'deoplete failed to load. '
          \ .'Try the :UpdateRemotePlugins command and restart Neovim. '
          \ .'See also :checkhealth.')
    endif

    return 1
  endtry
endfunction
function! deoplete#init#_channel_initialized() abort
  return get(g:, 'deoplete#_initialized', v:false)
endfunction
function! deoplete#init#_enable_handler() abort
  call deoplete#handler#_init()
  let s:is_handler_enabled = 1
endfunction
function! deoplete#init#_disable_handler() abort
  augroup deoplete
    autocmd!
  augroup END
  let s:is_handler_enabled = 0
endfunction

function! s:init_internal_variables() abort
  call deoplete#init#_prev_completion()
  let g:deoplete#_cached_context = deoplete#init#_cached_context()
  let g:deoplete#_context = {}
  let g:deoplete#_rank = {}
  if !exists('g:deoplete#_logging')
    let g:deoplete#_logging = {}
  endif
  unlet! g:deoplete#_initialized
  try
    let g:deoplete#_serveraddr =
          \ deoplete#util#has_yarp() ?
          \ neovim_rpc#serveraddr() : v:servername
    if g:deoplete#_serveraddr ==# ''
      " Use NVIM_LISTEN_ADDRESS
      let g:deoplete#_serveraddr = $NVIM_LISTEN_ADDRESS
    endif
  catch
    if deoplete#util#has_yarp() && !exists('*neovim_rpc#serveraddr')
      call deoplete#util#print_error(
            \ 'deoplete requires vim-hug-neovim-rpc plugin in Vim.')
    endif
  endtry
endfunction
function! deoplete#init#_custom_variables() abort
  if get(g:, 'deoplete#disable_auto_complete', v:false)
    call deoplete#custom#option('auto_complete', v:false)
  endif
  call s:check_custom_option(
        \ 'g:deoplete#auto_complete_delay',
        \ 'auto_complete_delay')
  call s:check_custom_option(
        \ 'g:deoplete#auto_refresh_delay',
        \ 'auto_refresh_delay')
  call s:check_custom_option(
        \ 'g:deoplete#camel_case',
        \ 'camel_case')
  call s:check_custom_option(
        \ 'g:deoplete#delimiters',
        \ 'delimiters')
  call s:check_custom_option(
        \ 'g:deoplete#ignore_case',
        \ 'ignore_case')
  call s:check_custom_option(
        \ 'g:deoplete#ignore_sources',
        \ 'ignore_sources')
  call s:check_custom_option(
        \ 'g:deoplete#keyword_patterns',
        \ 'keyword_patterns')
  call s:check_custom_option(
        \ 'g:deoplete#max_list',
        \ 'max_list')
  call s:check_custom_option(
        \ 'g:deoplete#num_processes',
        \ 'num_processes')
  call s:check_custom_option(
        \ 'g:deoplete#auto_complete_start_length',
        \ 'min_pattern_length')
  call s:check_custom_option(
        \ 'g:deoplete#enable_on_insert_enter',
        \ 'on_insert_enter')
  call s:check_custom_option(
        \ 'g:deoplete#enable_profile',
        \ 'profile')
  call s:check_custom_option(
        \ 'g:deoplete#enable_refresh_always',
        \ 'refresh_always')
  call s:check_custom_option(
        \ 'g:deoplete#skip_chars',
        \ 'skip_chars')
  call s:check_custom_option(
        \ 'g:deoplete#sources',
        \ 'sources')
  call s:check_custom_option(
        \ 'g:deoplete#enable_smart_case',
        \ 'smart_case')
  call s:check_custom_option(
        \ 'g:deoplete#enable_yarp',
        \ 'yarp')

  " Source variables
  call s:check_custom_var('file',
        \ 'g:deoplete#file#enable_buffer_path',
        \ 'enable_buffer_path')
  call s:check_custom_var('omni',
        \ 'g:deoplete#omni#input_patterns',
        \ 'input_patterns')
  call s:check_custom_var('omni',
        \ 'g:deoplete#omni#functions',
        \ 'functions')
endfunction

function! deoplete#init#_cached_context() abort
  let bufnr = expand('<abuf>') !=# '' ? expand('<abuf>') : bufnr('%')
  let bufname = bufname(bufnr)
  let bufpath = fnamemodify(bufname, ':p')
  if !filereadable(bufpath) || getbufvar(bufnr, '&buftype') =~# 'nofile'
    let bufpath = ''
  endif

  return {
        \ 'is_windows': s:is_windows,
        \ 'encoding': &encoding,
        \ 'ignorecase': deoplete#custom#_get_option('ignore_case'),
        \ 'smartcase': deoplete#custom#_get_option('smart_case'),
        \ 'camelcase': deoplete#custom#_get_option('camel_case'),
        \ 'bufnr': bufnr,
        \ 'bufname': bufname,
        \ 'bufpath': bufpath,
        \ 'complete_str': '',
        \ 'cwd': getcwd(),
        \ 'vars': filter(copy(g:),
        \       "stridx(v:key, 'deoplete#') == 0
        \        && v:key !=# 'deoplete#_yarp'"),
        \ 'custom': deoplete#custom#_get(),
        \ 'omni__omnifunc': &l:omnifunc,
        \ }
endfunction
function! deoplete#init#_context(event, sources) abort
  let input = deoplete#util#get_input(a:event)

  let [filetype, filetypes, same_filetypes] =
        \ deoplete#util#get_context_filetype(input, a:event)

  let event = (deoplete#util#get_prev_event() ==# 'Refresh') ?
        \ 'Manual' : a:event

  let width = winwidth(0) - col('.') + len(matchstr(input, '\w*$'))
  let max_width = (width * 2 / 3)

  return extend({
        \ 'changedtick': b:changedtick,
        \ 'event': event,
        \ 'input': input,
        \ 'next_input': deoplete#util#get_next_input(a:event),
        \ 'position': getpos('.'),
        \ 'filetype': filetype,
        \ 'filetypes': filetypes,
        \ 'same_filetypes': same_filetypes,
        \ 'sources': deoplete#custom#_get_filetype_option(
        \            'sources', filetype, []),
        \ 'max_abbr_width': max_width,
        \ 'max_kind_width': max_width,
        \ 'max_menu_width': max_width,
        \ }, g:deoplete#_cached_context)
endfunction

function! s:check_custom_var(source_name, old_var, new_var) abort
  if exists(a:old_var)
    call deoplete#custom#var(a:source_name, a:new_var, eval(a:old_var))
  endif
endfunction
function! s:check_custom_option(old_var, new_var) abort
  if exists(a:old_var)
    call deoplete#custom#option(a:new_var, eval(a:old_var))
  endif
endfunction

function! deoplete#init#_option() abort
  " Note: HTML omni func use search().
  return {
        \ 'async_timeout': 150,
        \ 'auto_complete': v:true,
        \ 'auto_complete_delay': 20,
        \ 'auto_refresh_delay': 50,
        \ 'camel_case': v:false,
        \ 'complete_method': 'complete',
        \ 'delimiters': ['/'],
        \ 'ignore_case': &ignorecase,
        \ 'ignore_sources': {},
        \ 'max_list': 500,
        \ 'num_processes': s:is_windows ? 1 : 4,
        \ 'keyword_patterns': {'_': '[a-zA-Z_]\k*'},
        \ 'omni_patterns': {
        \  'html': ['<', '</', '<[^>]*\s[[:alnum:]-]*'],
        \  'xhtml': ['<', '</', '<[^>]*\s[[:alnum:]-]*'],
        \  'xml': ['<', '</', '<[^>]*\s[[:alnum:]-]*'],
        \ },
        \ 'on_insert_enter': v:true,
        \ 'on_text_changed_i': v:true,
        \ 'profile': v:false,
        \ 'min_pattern_length': 2,
        \ 'refresh_always': v:true,
        \ 'skip_chars': ['(', ')'],
        \ 'smart_case': &smartcase,
        \ 'sources': {},
        \ 'trigger_key': v:char,
        \ 'yarp': v:false,
        \ }
endfunction
function! deoplete#init#_prev_completion() abort
  let g:deoplete#_prev_completion = {
        \ 'event': '',
        \ 'input': '',
        \ 'candidates': [],
        \ }
endfunction

function! deoplete#init#_python_version_check() abort
  python3 << EOF
import vim
import sys
vim.vars['deoplete#_python_version_check'] = (
    sys.version_info.major,
    sys.version_info.minor,
    sys.version_info.micro) < (3, 5, 0)
EOF
  return g:deoplete#_python_version_check
endfunction