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

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

function! deoplete#init#_is_enabled() abort "{{{
  return s:is_enabled
endfunction"}}}
function! s:is_initialized() abort "{{{
  return exists('g:deoplete#_channel_id')
endfunction"}}}

function! deoplete#init#_initialize() abort "{{{
  if s:is_initialized()
    return
  endif

  augroup deoplete
    autocmd!
  augroup END

  if !has('nvim') || !has('python3')
    call deoplete#util#print_error(
          \ 'deoplete.nvim does not work with this version.')
    call deoplete#util#print_error(
          \ 'It requires Neovim with Python3 support("+python3").')
    return 1
  endif

  if &completeopt !~# 'noinsert\|noselect'
    let save_completeopt = &completeopt
    try
      set completeopt+=noselect
    catch
      call deoplete#util#print_error(
            \ 'deoplete.nvim does not work with this version.')
      call deoplete#util#print_error(
            \ 'Please update neovim to latest version.')
      return 1
    finally
      let &completeopt = save_completeopt
    endtry
  endif

  try
    if !exists('g:loaded_remote_plugins')
      runtime! plugin/rplugin.vim
    endif
    call _deoplete()
  catch
    call deoplete#util#print_error(
          \ 'deoplete.nvim is not registered as Neovim remote plugins.')
    call deoplete#util#print_error(
          \ 'Please execute :UpdateRemotePlugins command and restart Neovim.')
    return 1
  endtry

  call deoplete#mappings#_init()
  call deoplete#init#_variables()

  let s:is_enabled = g:deoplete#enable_at_startup
  if s:is_enabled
    call deoplete#init#_enable()
  else
    call deoplete#init#_disable()
  endif
endfunction"}}}
function! deoplete#init#_enable() abort "{{{
  call deoplete#handlers#_init()
  let s:is_enabled = 1
endfunction"}}}
function! deoplete#init#_disable() abort "{{{
  augroup deoplete
    autocmd!
  augroup END
  let s:is_enabled = 0
endfunction"}}}

function! deoplete#init#_variables() abort "{{{
  let g:deoplete#_context = {}
  let g:deoplete#_rank = {}

  " User vairables
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_at_startup', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#auto_complete_start_length', 2)
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_ignore_case', &ignorecase)
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_smart_case', &smartcase)
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_camel_case', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_refresh_always', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#disable_auto_complete', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#delimiters', ['/', '.', '::', ':', '#'])
  call deoplete#util#set_default(
        \ 'g:deoplete#max_list', 100)
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_debug', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#enable_profile', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#auto_complete_delay', 0)
  call deoplete#util#set_default(
        \ 'g:deoplete#max_abbr_width', 80)
  call deoplete#util#set_default(
        \ 'g:deoplete#max_menu_width', 40)

  call deoplete#util#set_default(
        \ 'g:deoplete#keyword_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#_keyword_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#omni_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#_omni_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#sources', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#ignore_sources', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#_ignore_sources', {})

  " Source variables
  call deoplete#util#set_default(
        \ 'g:deoplete#omni#input_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#omni#_input_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#omni#functions', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#omni#_functions', { '_': '' })
  call deoplete#util#set_default(
        \ 'g:deoplete#member#prefix_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#member#_prefix_patterns', {})
  call deoplete#util#set_default(
        \ 'g:deoplete#tag#cache_limit_size', 500000)

  " Initialize default keyword pattern. "{{{
  call deoplete#util#set_pattern(
        \ g:deoplete#_keyword_patterns,
        \ '_',
        \ '[a-zA-Z_]\k*')
  "}}}

  " Initialize omni completion pattern. "{{{
  " Note: HTML omni func use search().
  call deoplete#util#set_pattern(
        \ g:deoplete#_omni_patterns,
        \ 'html,xhtml,xml,markdown,mkd', ['<', '<[^>]*\s[[:alnum:]-]*'])

  call deoplete#util#set_pattern(
        \ g:deoplete#omni#_input_patterns,
        \ 'css,less,scss,sass', ['\w+', '\w+[):;]?\s+\w*', '[@!]'])
  call deoplete#util#set_pattern(
        \ g:deoplete#omni#_input_patterns,
        \ 'ruby', ['[^. \t0-9]\.\w*', '[a-zA-Z_]\w*::\w*'])
  call deoplete#util#set_pattern(
        \ g:deoplete#omni#_input_patterns,
        \ 'lua', ['\w+[.:]', 'require\s*\(?["'']\w*'])
  "}}}

  " Initialize member prefix pattern. "{{{
  call deoplete#util#set_pattern(
        \ g:deoplete#member#_prefix_patterns,
        \ '_', '\.')
  call deoplete#util#set_pattern(
        \ g:deoplete#member#_prefix_patterns,
        \ 'c,objc', ['\.', '->'])
  call deoplete#util#set_pattern(
        \ g:deoplete#member#_prefix_patterns,
        \ 'cpp,objcpp', ['\.', '->', '::'])
  call deoplete#util#set_pattern(
        \ g:deoplete#member#_prefix_patterns,
        \ 'perl,php', ['->'])
  call deoplete#util#set_pattern(
        \ g:deoplete#member#_prefix_patterns,
        \ 'ruby', ['\.', '::'])
  call deoplete#util#set_pattern(
        \ g:deoplete#member#_prefix_patterns,
        \ 'lua', ['\.', ':'])
  "}}}
endfunction"}}}

function! deoplete#init#_context(event, sources) abort "{{{
  let filetype = (exists('*context_filetype#get_filetype') ?
        \   context_filetype#get_filetype() :
        \   (&filetype == '' ? 'nothing' : &filetype))
  let filetypes = exists('*context_filetype#get_filetypes') ?
        \   context_filetype#get_filetypes() :
        \   &filetype == '' ? ['nothing'] :
        \                     deoplete#util#uniq([&filetype]
        \                          + split(&filetype, '\.'))

  let sources = deoplete#util#convert2list(a:sources)
  if a:event !=# 'Manual' && empty(sources)
    " Use default sources
    let sources = deoplete#util#get_buffer_config(
          \ filetype,
          \ 'b:deoplete_sources',
          \ 'g:deoplete#sources',
          \ '{}', [])
  endif

  let keyword_patterns = join(deoplete#util#convert2list(
        \   deoplete#util#get_buffer_config(
        \   filetype, 'b:deoplete_keyword_patterns',
        \   'g:deoplete#keyword_patterns',
        \   'g:deoplete#_keyword_patterns')), '|')

  " Convert keyword pattern.
  let pattern = deoplete#util#vimoption2python(
        \ &l:iskeyword . (&l:lisp ? ',-' : ''))
  let keyword_patterns = substitute(keyword_patterns,
        \ '\\k', '\=pattern', 'g')

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

  let input = deoplete#util#get_input(a:event)

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

  return {
        \ 'changedtick': b:changedtick,
        \ 'event': event,
        \ 'input': input,
        \ 'next_input': deoplete#util#get_next_input(a:event),
        \ 'complete_str': '',
        \ 'position': getpos('.'),
        \ 'filetype': filetype,
        \ 'filetypes': filetypes,
        \ 'ignorecase': g:deoplete#enable_ignore_case,
        \ 'smartcase': g:deoplete#enable_smart_case,
        \ 'camelcase': g:deoplete#enable_camel_case,
        \ 'delay': g:deoplete#auto_complete_delay,
        \ 'sources': sources,
        \ 'keyword_patterns': keyword_patterns,
        \ 'max_abbr_width': (width * 2 / 3),
        \ 'max_menu_width': (width * 2 / 3),
        \ 'runtimepath': &runtimepath,
        \ 'bufnr': bufnr('%'),
        \ 'bufname': bufname('%'),
        \ 'cwd': getcwd(),
        \ 'start_complete': "\<Plug>(deoplete_start_complete)",
        \ 'vars': filter(copy(g:), "stridx(v:key, 'deoplete#') == 0"),
        \ 'bufvars': filter(copy(b:), "stridx(v:key, 'deoplete#') == 0"),
        \ 'custom': deoplete#custom#get(),
        \ 'omni__omnifunc': &l:omnifunc,
        \ 'dict__dictionary': &l:dictionary,
        \ }
endfunction"}}}

" vim: foldmethod=marker