2012-04-15 23:28:46 -04:00
|
|
|
" Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
2012-04-15 19:57:10 -04:00
|
|
|
"
|
|
|
|
" This file is part of YouCompleteMe.
|
|
|
|
"
|
|
|
|
" YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
" it under the terms of the GNU General Public License as published by
|
|
|
|
" the Free Software Foundation, either version 3 of the License, or
|
|
|
|
" (at your option) any later version.
|
|
|
|
"
|
|
|
|
" YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
" but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
" GNU General Public License for more details.
|
|
|
|
"
|
|
|
|
" You should have received a copy of the GNU General Public License
|
|
|
|
" along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
" This is basic vim plugin boilerplate
|
|
|
|
let s:save_cpo = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
|
|
|
" This needs to be called outside of a function
|
|
|
|
let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
|
2012-08-12 17:07:38 -04:00
|
|
|
let s:searched_and_results_found = 0
|
2012-08-04 20:46:54 -04:00
|
|
|
let s:should_use_filetype_completion = 0
|
2012-07-10 18:39:59 -04:00
|
|
|
let s:completion_start_column = 0
|
2012-07-28 01:16:23 -04:00
|
|
|
let s:omnifunc_mode = 0
|
2012-08-20 23:55:25 -04:00
|
|
|
|
2012-08-12 00:20:17 -04:00
|
|
|
let s:old_cursor_position = []
|
|
|
|
let s:cursor_moved = 0
|
2012-08-20 23:55:25 -04:00
|
|
|
let s:moved_vertically_in_insert_mode = 0
|
|
|
|
let s:previous_num_chars_on_current_line = -1
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
function! youcompleteme#Enable()
|
2012-07-30 01:09:06 -04:00
|
|
|
" When vim is in diff mode, don't run
|
|
|
|
if &diff
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2013-02-12 23:54:27 -05:00
|
|
|
py import sys
|
|
|
|
py import vim
|
|
|
|
exe 'python sys.path.insert( 0, "' . s:script_folder_path . '/../python" )'
|
|
|
|
py import ycm
|
|
|
|
|
|
|
|
if !pyeval( 'ycm.CompatibleWithYcmCore()')
|
|
|
|
echohl WarningMsg |
|
2013-02-20 13:14:29 -05:00
|
|
|
\ echomsg "YouCompleteMe unavailable: ycm_core too old, PLEASE RECOMPILE ycm_core" |
|
2013-02-12 23:54:27 -05:00
|
|
|
\ echohl None
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
py ycm_state = ycm.YouCompleteMe()
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
augroup youcompleteme
|
|
|
|
autocmd!
|
2012-07-30 01:08:02 -04:00
|
|
|
autocmd CursorMovedI * call s:OnCursorMovedInsertMode()
|
2012-07-30 22:42:41 -04:00
|
|
|
autocmd CursorMoved * call s:OnCursorMovedNormalMode()
|
2012-07-21 18:33:59 -04:00
|
|
|
" Note that these events will NOT trigger for the file vim is started with;
|
|
|
|
" so if you do "vim foo.cc", these events will not trigger when that buffer
|
|
|
|
" is read. This is because youcompleteme#Enable() is called on VimEnter and
|
|
|
|
" that happens *after" BufRead/BufEnter has already triggered for the
|
|
|
|
" initial file.
|
2012-05-12 18:23:45 -04:00
|
|
|
autocmd BufRead,BufEnter * call s:OnBufferVisit()
|
2013-03-14 23:39:44 -04:00
|
|
|
autocmd BufDelete * call s:OnBufferDelete( expand( '<afile>:p' ) )
|
2012-05-12 18:26:04 -04:00
|
|
|
autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
|
2012-07-28 14:42:43 -04:00
|
|
|
autocmd InsertLeave * call s:OnInsertLeave()
|
2012-08-12 00:20:17 -04:00
|
|
|
autocmd InsertEnter * call s:OnInsertEnter()
|
2012-04-15 19:57:10 -04:00
|
|
|
augroup END
|
|
|
|
|
2012-08-13 23:47:45 -04:00
|
|
|
call s:SetUpCompleteopt()
|
2013-02-08 21:29:36 -05:00
|
|
|
call s:SetUpKeyMappings()
|
|
|
|
call s:ForceSyntasticCFamilyChecker()
|
2012-08-05 22:55:06 -04:00
|
|
|
|
|
|
|
if g:ycm_allow_changing_updatetime
|
|
|
|
set ut=2000
|
|
|
|
endif
|
|
|
|
|
2013-02-08 21:29:36 -05:00
|
|
|
" Calling this once solves the problem of BufRead/BufEnter not triggering for
|
|
|
|
" the first loaded file. This should be the last command executed in this
|
|
|
|
" function!
|
|
|
|
call s:OnBufferVisit()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:SetUpKeyMappings()
|
|
|
|
" The g:ycm_key_select_completion and g:ycm_key_previous_completion used to
|
|
|
|
" exist and are now here purely for the sake of backwards compatibility; we
|
|
|
|
" don't want to break users if we can avoid it.
|
|
|
|
|
|
|
|
if exists('g:ycm_key_select_completion') &&
|
|
|
|
\ index(g:ycm_key_list_select_completion,
|
|
|
|
\ g:ycm_key_select_completion) == -1
|
|
|
|
call add(g:ycm_key_list_select_completion, g:ycm_key_select_completion)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if exists('g:ycm_key_previous_completion') &&
|
|
|
|
\ index(g:ycm_key_list_previous_completion,
|
|
|
|
\ g:ycm_key_previous_completion) == -1
|
|
|
|
call add(g:ycm_key_list_previous_completion, g:ycm_key_previous_completion)
|
|
|
|
endif
|
2013-01-31 21:42:12 -05:00
|
|
|
|
2013-02-08 21:29:36 -05:00
|
|
|
for key in g:ycm_key_list_select_completion
|
|
|
|
" With this command, when the completion window is visible, the tab key
|
|
|
|
" (default) will select the next candidate in the window. In vim, this also
|
|
|
|
" changes the typed-in text to that of the candidate completion.
|
|
|
|
exe 'inoremap <expr>' . key .
|
|
|
|
\ ' pumvisible() ? "\<C-n>" : "\' . key .'"'
|
|
|
|
endfor
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2013-02-08 21:29:36 -05:00
|
|
|
|
|
|
|
for key in g:ycm_key_list_previous_completion
|
|
|
|
" This selects the previous candidate for shift-tab (default)
|
|
|
|
exe 'inoremap <expr>' . key .
|
|
|
|
\ ' pumvisible() ? "\<C-p>" : "\' . key .'"'
|
|
|
|
endfor
|
2012-07-23 14:15:25 -04:00
|
|
|
|
2013-03-09 22:31:00 -05:00
|
|
|
if !empty( g:ycm_key_invoke_completion )
|
2013-02-06 21:32:47 -05:00
|
|
|
" <c-x><c-o> trigger omni completion, <c-p> deselects the first completion
|
|
|
|
" candidate that vim selects by default
|
2013-03-17 16:09:55 -04:00
|
|
|
silent! exe 'inoremap <unique> ' . g:ycm_key_invoke_completion .
|
|
|
|
\ ' <C-X><C-O><C-P>'
|
2013-02-06 21:32:47 -05:00
|
|
|
endif
|
2012-07-21 18:33:59 -04:00
|
|
|
|
2013-03-09 22:31:00 -05:00
|
|
|
if !empty( g:ycm_key_detailed_diagnostics )
|
2013-03-17 16:09:55 -04:00
|
|
|
silent! exe 'nnoremap <unique> ' . g:ycm_key_detailed_diagnostics .
|
2013-02-06 21:32:47 -05:00
|
|
|
\ ' :YcmShowDetailedDiagnostic<cr>'
|
|
|
|
endif
|
2013-02-08 21:29:36 -05:00
|
|
|
endfunction
|
2012-08-15 22:39:03 -04:00
|
|
|
|
2013-01-26 21:45:27 -05:00
|
|
|
|
2013-02-08 21:29:36 -05:00
|
|
|
function! s:ForceSyntasticCFamilyChecker()
|
|
|
|
" Needed so that YCM is used as the syntastic checker
|
|
|
|
let g:syntastic_cpp_checkers = ['ycm']
|
|
|
|
let g:syntastic_c_checkers = ['ycm']
|
|
|
|
let g:syntastic_objc_checkers = ['ycm']
|
|
|
|
let g:syntastic_objcpp_checkers = ['ycm']
|
2012-04-15 19:57:10 -04:00
|
|
|
endfunction
|
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-13 22:01:55 -04:00
|
|
|
function! s:AllowedToCompleteInCurrentFile()
|
2013-03-09 23:30:10 -05:00
|
|
|
if empty( &filetype )
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
let whitelist_allows = has_key( g:ycm_filetype_whitelist, '*' ) ||
|
|
|
|
\ has_key( g:ycm_filetype_whitelist, &filetype )
|
|
|
|
let blacklist_allows = !has_key( g:ycm_filetype_blacklist, &filetype )
|
|
|
|
|
|
|
|
return whitelist_allows && blacklist_allows
|
2012-08-13 22:01:55 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-08-13 23:47:45 -04:00
|
|
|
function! s:SetUpCompleteopt()
|
|
|
|
" Some plugins (I'm looking at you, vim-notes) change completeopt by for
|
|
|
|
" instance adding 'longest'. This breaks YCM. So we force our settings.
|
|
|
|
" There's no two ways about this: if you want to use YCM then you have to
|
|
|
|
" have these completeopt settings, otherwise YCM won't work at all.
|
|
|
|
|
|
|
|
" We need menuone in completeopt, otherwise when there's only one candidate
|
|
|
|
" for completion, the menu doesn't show up.
|
|
|
|
set completeopt-=menu
|
|
|
|
set completeopt+=menuone
|
|
|
|
|
|
|
|
" This is unnecessary with our features. People use this option to insert
|
|
|
|
" the common prefix of all the matches and then add more differentiating chars
|
|
|
|
" so that they can select a more specific match. With our features, they
|
|
|
|
" don't need to insert the prefix; they just type the differentiating chars.
|
|
|
|
" Also, having this option set breaks the plugin.
|
|
|
|
set completeopt-=longest
|
|
|
|
|
|
|
|
if g:ycm_add_preview_to_completeopt
|
|
|
|
set completeopt+=preview
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-05-12 18:23:45 -04:00
|
|
|
function! s:OnBufferVisit()
|
2012-08-13 22:01:55 -04:00
|
|
|
if !s:AllowedToCompleteInCurrentFile()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-08-13 23:47:45 -04:00
|
|
|
call s:SetUpCompleteopt()
|
2012-05-12 18:23:45 -04:00
|
|
|
call s:SetCompleteFunc()
|
2012-08-04 20:46:54 -04:00
|
|
|
call s:OnFileReadyToParse()
|
2012-05-12 18:23:45 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2013-03-14 23:39:44 -04:00
|
|
|
function! s:OnBufferDelete( deleted_buffer_file )
|
|
|
|
if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file )
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
py ycm_state.OnBufferDelete( vim.eval( 'a:deleted_buffer_file' ) )
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-05-12 18:26:04 -04:00
|
|
|
function! s:OnCursorHold()
|
2012-08-13 22:01:55 -04:00
|
|
|
if !s:AllowedToCompleteInCurrentFile()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-08-13 23:47:45 -04:00
|
|
|
call s:SetUpCompleteopt()
|
2012-08-12 17:36:36 -04:00
|
|
|
" Order is important here; we need to extract any done diagnostics before
|
|
|
|
" reparsing the file again
|
2012-07-30 01:08:02 -04:00
|
|
|
call s:UpdateDiagnosticNotifications()
|
2012-08-12 17:36:36 -04:00
|
|
|
call s:OnFileReadyToParse()
|
2012-05-12 18:26:04 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
function! s:OnFileReadyToParse()
|
|
|
|
py ycm_state.OnFileReadyToParse()
|
2012-07-26 23:50:56 -04:00
|
|
|
endfunction
|
|
|
|
|
2012-07-28 01:16:23 -04:00
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
function! s:SetCompleteFunc()
|
|
|
|
let &completefunc = 'youcompleteme#Complete'
|
|
|
|
let &l:completefunc = 'youcompleteme#Complete'
|
2012-07-28 01:16:23 -04:00
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
|
2012-08-04 20:46:54 -04:00
|
|
|
let &omnifunc = 'youcompleteme#OmniComplete'
|
|
|
|
let &l:omnifunc = 'youcompleteme#OmniComplete'
|
2013-02-12 00:45:42 -05:00
|
|
|
|
|
|
|
" If we don't have native filetype support but the omnifunc is set to YCM's
|
|
|
|
" omnifunc because the previous file the user was editing DID have native
|
|
|
|
" support, we remove our omnifunc.
|
|
|
|
elseif &omnifunc == 'youcompleteme#OmniComplete'
|
|
|
|
let &omnifunc = ''
|
|
|
|
let &l:omnifunc = ''
|
2012-07-28 01:16:23 -04:00
|
|
|
endif
|
2012-04-15 19:57:10 -04:00
|
|
|
endfunction
|
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-07-30 01:08:02 -04:00
|
|
|
function! s:OnCursorMovedInsertMode()
|
2012-08-13 22:01:55 -04:00
|
|
|
if !s:AllowedToCompleteInCurrentFile()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-08-12 00:20:17 -04:00
|
|
|
call s:UpdateCursorMoved()
|
2012-08-20 23:55:25 -04:00
|
|
|
|
|
|
|
" Basically, we need to only trigger the completion menu when the user has
|
|
|
|
" inserted or deleted a character, NOT just when the user moves in insert mode
|
|
|
|
" (with, say, the arrow keys). If we trigger the menu even on pure moves, then
|
|
|
|
" it's impossible to move in insert mode since the up/down arrows start moving
|
|
|
|
" the selected completion in the completion menu. Yeah, people shouldn't be
|
|
|
|
" moving in insert mode at all (that's what normal mode is for) but explain
|
|
|
|
" that to the users who complain...
|
|
|
|
if !s:BufferTextChangedSinceLastMoveInInsertMode()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-07-28 01:16:23 -04:00
|
|
|
call s:IdentifierFinishedOperations()
|
2013-03-15 14:43:09 -04:00
|
|
|
if g:ycm_autoclose_preview_window_after_completion
|
|
|
|
call s:ClosePreviewWindowIfNeeded()
|
|
|
|
endif
|
2012-04-15 19:57:10 -04:00
|
|
|
call s:InvokeCompletion()
|
|
|
|
endfunction
|
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-07-30 22:42:41 -04:00
|
|
|
function! s:OnCursorMovedNormalMode()
|
2012-08-13 22:01:55 -04:00
|
|
|
if !s:AllowedToCompleteInCurrentFile()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-07-30 22:42:41 -04:00
|
|
|
call s:UpdateDiagnosticNotifications()
|
|
|
|
endfunction
|
2012-07-30 01:08:02 -04:00
|
|
|
|
|
|
|
|
2012-07-28 14:42:43 -04:00
|
|
|
function! s:OnInsertLeave()
|
2012-08-13 22:01:55 -04:00
|
|
|
if !s:AllowedToCompleteInCurrentFile()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-07-28 14:42:43 -04:00
|
|
|
let s:omnifunc_mode = 0
|
2012-07-30 01:08:02 -04:00
|
|
|
call s:UpdateDiagnosticNotifications()
|
2012-08-04 20:46:54 -04:00
|
|
|
py ycm_state.OnInsertLeave()
|
2013-03-15 14:43:09 -04:00
|
|
|
if g:ycm_autoclose_preview_window_after_completion ||
|
|
|
|
\ g:ycm_autoclose_preview_window_after_insertion
|
|
|
|
call s:ClosePreviewWindowIfNeeded()
|
|
|
|
endif
|
2012-08-07 00:29:56 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-08-12 00:20:17 -04:00
|
|
|
function! s:OnInsertEnter()
|
2012-08-13 22:01:55 -04:00
|
|
|
if !s:AllowedToCompleteInCurrentFile()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-08-12 00:20:17 -04:00
|
|
|
let s:old_cursor_position = []
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:UpdateCursorMoved()
|
|
|
|
let current_position = getpos('.')
|
|
|
|
let s:cursor_moved = current_position != s:old_cursor_position
|
2012-08-20 23:55:25 -04:00
|
|
|
|
|
|
|
let s:moved_vertically_in_insert_mode = s:old_cursor_position != [] &&
|
|
|
|
\ current_position[ 1 ] != s:old_cursor_position[ 1 ]
|
|
|
|
|
2012-08-12 00:20:17 -04:00
|
|
|
let s:old_cursor_position = current_position
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-08-20 23:55:25 -04:00
|
|
|
function! s:BufferTextChangedSinceLastMoveInInsertMode()
|
|
|
|
if s:moved_vertically_in_insert_mode
|
|
|
|
let s:previous_num_chars_on_current_line = -1
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
let num_chars_in_current_cursor_line = strlen( getline('.') )
|
|
|
|
|
|
|
|
if s:previous_num_chars_on_current_line == -1
|
|
|
|
let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
let changed_text_on_current_line = num_chars_in_current_cursor_line !=
|
|
|
|
\ s:previous_num_chars_on_current_line
|
|
|
|
let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
|
|
|
|
|
|
|
|
return changed_text_on_current_line
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-08-07 00:29:56 -04:00
|
|
|
function! s:ClosePreviewWindowIfNeeded()
|
2012-08-12 17:07:38 -04:00
|
|
|
if s:searched_and_results_found
|
2012-08-07 00:29:56 -04:00
|
|
|
" This command does the actual closing of the preview window. If no preview
|
|
|
|
" window is shown, nothing happens.
|
|
|
|
pclose
|
|
|
|
endif
|
2012-07-30 01:08:02 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:UpdateDiagnosticNotifications()
|
2012-08-04 20:46:54 -04:00
|
|
|
if get( g:, 'loaded_syntastic_plugin', 0 ) &&
|
2013-02-10 22:55:05 -05:00
|
|
|
\ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) &&
|
2012-08-04 20:46:54 -04:00
|
|
|
\ pyeval( 'ycm_state.DiagnosticsForCurrentFileReady()' )
|
2012-07-30 01:08:02 -04:00
|
|
|
SyntasticCheck
|
|
|
|
endif
|
2012-07-28 14:42:43 -04:00
|
|
|
endfunction
|
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
|
2012-07-28 01:16:23 -04:00
|
|
|
function! s:IdentifierFinishedOperations()
|
|
|
|
if !pyeval( 'ycm.CurrentIdentifierFinished()' )
|
|
|
|
return
|
2012-05-12 18:20:03 -04:00
|
|
|
endif
|
2012-08-04 20:46:54 -04:00
|
|
|
py ycm_state.OnCurrentIdentifierFinished()
|
2012-07-28 01:16:23 -04:00
|
|
|
let s:omnifunc_mode = 0
|
2012-05-12 18:20:03 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-06-24 15:04:45 -04:00
|
|
|
function! s:InsideCommentOrString()
|
2013-02-16 15:56:21 -05:00
|
|
|
if g:ycm_complete_in_comments_and_strings
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2012-06-24 15:04:45 -04:00
|
|
|
" Has to be col('.') -1 because col('.') doesn't exist at this point. We are
|
|
|
|
" in insert mode when this func is called.
|
2013-02-15 05:18:55 -05:00
|
|
|
let syntax_group = synIDattr( synIDtrans( synID( line( '.' ), col( '.' ) - 1, 1 ) ), 'name')
|
2012-06-24 15:04:45 -04:00
|
|
|
if stridx(syntax_group, 'Comment') > -1 || stridx(syntax_group, 'String') > -1
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2013-02-06 22:01:12 -05:00
|
|
|
function! s:OnBlankLine()
|
2013-02-06 22:16:33 -05:00
|
|
|
return pyeval( 'not vim.current.line or vim.current.line.isspace()' )
|
2013-02-06 22:01:12 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
function! s:InvokeCompletion()
|
|
|
|
if &completefunc != "youcompleteme#Complete"
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2013-02-06 22:01:12 -05:00
|
|
|
if s:InsideCommentOrString() || s:OnBlankLine()
|
2012-06-24 15:04:45 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2012-07-10 18:26:07 -04:00
|
|
|
" This is tricky. First, having 'refresh' set to 'always' in the dictionary
|
|
|
|
" that our completion function returns makes sure that our completion function
|
2013-01-13 23:56:10 -05:00
|
|
|
" is called on every keystroke. Second, when the sequence of characters the
|
2012-08-13 22:01:55 -04:00
|
|
|
" user typed produces no results in our search an infinite loop can occur. The
|
|
|
|
" problem is that our feedkeys call triggers the OnCursorMovedI event which we
|
|
|
|
" are tied to. We prevent this infinite loop from starting by making sure that
|
|
|
|
" the user has moved the cursor since the last time we provided completion
|
|
|
|
" results.
|
2012-08-12 00:20:17 -04:00
|
|
|
if !s:cursor_moved
|
2012-04-15 19:57:10 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" <c-x><c-u> invokes the user's completion function (which we have set to
|
2012-07-10 18:26:07 -04:00
|
|
|
" youcompleteme#Complete), and <c-p> tells Vim to select the previous
|
|
|
|
" completion candidate. This is necessary because by default, Vim selects the
|
2012-04-15 19:57:10 -04:00
|
|
|
" first candidate when completion is invoked, and selecting a candidate
|
2012-07-10 18:26:07 -04:00
|
|
|
" automatically replaces the current text with it. Calling <c-p> forces Vim to
|
2012-04-15 19:57:10 -04:00
|
|
|
" deselect the first candidate and in turn preserve the user's current text
|
|
|
|
" until he explicitly chooses to replace it with a completion.
|
|
|
|
call feedkeys( "\<C-X>\<C-U>\<C-P>", 'n' )
|
|
|
|
endfunction
|
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
function! s:CompletionsForQuery( query, use_filetype_completer )
|
|
|
|
if a:use_filetype_completer
|
2013-02-09 19:22:23 -05:00
|
|
|
py completer = ycm_state.GetFiletypeCompleter()
|
2012-08-04 20:46:54 -04:00
|
|
|
else
|
|
|
|
py completer = ycm_state.GetIdentifierCompleter()
|
|
|
|
endif
|
2012-07-10 18:26:07 -04:00
|
|
|
|
2012-07-12 01:41:32 -04:00
|
|
|
" TODO: don't trigger on a dot inside a string constant
|
2012-08-04 20:46:54 -04:00
|
|
|
py completer.CandidatesForQueryAsync( vim.eval( 'a:query' ) )
|
2012-07-15 23:49:56 -04:00
|
|
|
|
|
|
|
let l:results_ready = 0
|
|
|
|
while !l:results_ready
|
2012-08-04 20:46:54 -04:00
|
|
|
let l:results_ready = pyeval( 'completer.AsyncCandidateRequestReady()' )
|
2012-07-15 23:49:56 -04:00
|
|
|
if complete_check()
|
2012-08-12 17:07:38 -04:00
|
|
|
let s:searched_and_results_found = 0
|
2012-07-15 23:49:56 -04:00
|
|
|
return { 'words' : [], 'refresh' : 'always'}
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
let l:results = pyeval( 'completer.CandidatesFromStoredRequest()' )
|
2012-08-12 17:07:38 -04:00
|
|
|
let s:searched_and_results_found = len( l:results ) != 0
|
2012-07-10 18:26:07 -04:00
|
|
|
return { 'words' : l:results, 'refresh' : 'always' }
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" This is our main entry point. This is what vim calls to get completions.
|
|
|
|
function! youcompleteme#Complete( findstart, base )
|
2012-08-04 20:46:54 -04:00
|
|
|
" After the user types one character after the call to the omnifunc, the
|
2012-07-28 01:16:23 -04:00
|
|
|
" completefunc will be called because of our mapping that calls the
|
|
|
|
" completefunc on every keystroke. Therefore we need to delegate the call we
|
|
|
|
" 'stole' back to the omnifunc
|
|
|
|
if s:omnifunc_mode
|
2012-08-04 20:46:54 -04:00
|
|
|
return youcompleteme#OmniComplete( a:findstart, a:base )
|
2012-07-28 01:16:23 -04:00
|
|
|
endif
|
|
|
|
|
2012-07-10 18:26:07 -04:00
|
|
|
if a:findstart
|
2012-08-12 00:20:17 -04:00
|
|
|
" InvokeCompletion has this check but we also need it here because of random
|
|
|
|
" Vim bugs and unfortunate interactions with the autocommands of other
|
|
|
|
" plugins
|
|
|
|
if !s:cursor_moved
|
|
|
|
" for vim, -2 means not found but don't trigger an error message
|
|
|
|
" see :h complete-functions
|
|
|
|
return -2
|
|
|
|
endif
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
|
|
|
|
" TODO: make this a function-local variable instead of a script-local one
|
2012-07-21 13:10:19 -04:00
|
|
|
let s:completion_start_column = pyeval( 'ycm.CompletionStartColumn()' )
|
2012-08-04 20:46:54 -04:00
|
|
|
let s:should_use_filetype_completion =
|
|
|
|
\ pyeval( 'ycm_state.ShouldUseFiletypeCompleter(' .
|
|
|
|
\ s:completion_start_column . ')' )
|
2012-07-10 18:26:07 -04:00
|
|
|
|
2012-08-05 16:12:10 -04:00
|
|
|
if !s:should_use_filetype_completion &&
|
|
|
|
\ !pyeval( 'ycm_state.ShouldUseIdentifierCompleter(' .
|
|
|
|
\ s:completion_start_column . ')' )
|
|
|
|
" for vim, -2 means not found but don't trigger an error message
|
|
|
|
" see :h complete-functions
|
|
|
|
return -2
|
2012-07-10 18:26:07 -04:00
|
|
|
endif
|
2012-07-28 00:41:13 -04:00
|
|
|
return s:completion_start_column
|
2012-07-10 18:26:07 -04:00
|
|
|
else
|
2012-08-04 20:46:54 -04:00
|
|
|
return s:CompletionsForQuery( a:base, s:should_use_filetype_completion )
|
2012-04-15 19:57:10 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2012-07-28 01:16:23 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
function! youcompleteme#OmniComplete( findstart, base )
|
2012-07-28 01:16:23 -04:00
|
|
|
if a:findstart
|
|
|
|
let s:omnifunc_mode = 1
|
|
|
|
let s:completion_start_column = pyeval( 'ycm.CompletionStartColumn()' )
|
|
|
|
return s:completion_start_column
|
|
|
|
else
|
2012-08-04 20:46:54 -04:00
|
|
|
return s:CompletionsForQuery( a:base, 1 )
|
2012-07-28 01:16:23 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2012-07-28 18:27:30 -04:00
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
function! s:ShowDetailedDiagnostic()
|
|
|
|
py ycm_state.ShowDetailedDiagnostic()
|
|
|
|
endfunction
|
|
|
|
|
2013-02-06 21:32:47 -05:00
|
|
|
command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic()
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
|
2012-07-30 01:08:02 -04:00
|
|
|
" This is what Syntastic calls indirectly when it decides an auto-check is
|
|
|
|
" required (currently that's on buffer save) OR when the SyntasticCheck command
|
|
|
|
" is invoked
|
2012-07-28 18:27:30 -04:00
|
|
|
function! youcompleteme#CurrentFileDiagnostics()
|
2012-08-04 20:46:54 -04:00
|
|
|
return pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' )
|
2012-07-28 18:27:30 -04:00
|
|
|
endfunction
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
|
2013-01-26 14:44:42 -05:00
|
|
|
function! s:DebugInfo()
|
|
|
|
echom "Printing YouCompleteMe debug information..."
|
|
|
|
let debug_info = pyeval( 'ycm_state.DebugInfo()' )
|
|
|
|
for line in split( debug_info, "\n" )
|
|
|
|
echom '-- ' . line
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
command! YcmDebugInfo call s:DebugInfo()
|
|
|
|
|
2013-02-25 09:14:01 -05:00
|
|
|
function! s:CompleterCommand(...)
|
|
|
|
" CompleterCommand will call the OnUserCommand function of a completer.
|
|
|
|
" If the first arguments is of the form "ft=..." it can be used to specify the
|
|
|
|
" completer to use (for example "ft=cpp"). Else the native filetype completer
|
|
|
|
" of the current buffer is used. If no native filetype completer is found and
|
|
|
|
" no completer was specified this throws an error. You can use "ft=ycm:omni"
|
|
|
|
" to select the omni completer or "ft=ycm:ident" to select the identifier
|
|
|
|
" completer. The remaining arguments will passed to the completer.
|
|
|
|
let arguments = copy(a:000)
|
|
|
|
|
|
|
|
if a:0 > 0 && strpart(a:1, 0, 3) == 'ft='
|
|
|
|
if a:1 == 'ft=ycm:omni'
|
|
|
|
py completer = ycm_state.GetOmniCompleter()
|
|
|
|
elseif a:1 == 'ft=ycm:ident'
|
|
|
|
py completer = ycm_state.GetIdentifierCompleter()
|
|
|
|
else
|
|
|
|
py completer = ycm_state.GetFiletypeCompleterForFiletype(
|
|
|
|
\ vim.eval('a:1').lstrip('ft=') )
|
|
|
|
endif
|
|
|
|
let arguments = arguments[1:]
|
|
|
|
elseif pyeval( 'ycm_state.NativeFiletypeCompletionAvailable()' )
|
|
|
|
py completer = ycm_state.GetFiletypeCompleter()
|
|
|
|
else
|
|
|
|
echohl WarningMsg |
|
|
|
|
\ echomsg "No native completer found for current buffer." |
|
|
|
|
\ echomsg "Use ft=... as the first argument to specify a completer." |
|
|
|
|
\ echohl None
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
py completer.OnUserCommand( vim.eval( 'l:arguments' ) )
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
command! -nargs=* YcmCompleter call s:CompleterCommand(<f-args>)
|
2013-01-26 14:44:42 -05:00
|
|
|
|
2013-01-31 22:21:11 -05:00
|
|
|
function! s:ForceCompile()
|
2013-02-10 22:55:05 -05:00
|
|
|
if !pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
|
2013-02-12 23:30:14 -05:00
|
|
|
echom "Native filetype completion not supported for current file, "
|
2013-01-30 17:46:58 -05:00
|
|
|
\ . "cannot force recompilation."
|
2013-02-12 23:54:27 -05:00
|
|
|
return 0
|
2013-01-30 17:46:58 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
echom "Forcing compilation, this will block Vim until done."
|
|
|
|
py ycm_state.OnFileReadyToParse()
|
|
|
|
while 1
|
|
|
|
let diagnostics_ready = pyeval(
|
|
|
|
\ 'ycm_state.DiagnosticsForCurrentFileReady()' )
|
|
|
|
if diagnostics_ready
|
|
|
|
break
|
|
|
|
endif
|
2013-02-06 00:22:50 -05:00
|
|
|
|
|
|
|
let getting_completions = pyeval(
|
|
|
|
\ 'ycm_state.GettingCompletions()' )
|
|
|
|
|
|
|
|
if !getting_completions
|
2013-03-17 21:54:57 -04:00
|
|
|
echom "Unable to retrieve diagnostics, see output of `:mes` for possible details."
|
2013-02-06 00:22:50 -05:00
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2013-01-30 17:46:58 -05:00
|
|
|
sleep 100m
|
|
|
|
endwhile
|
2013-02-06 00:22:50 -05:00
|
|
|
return 1
|
2013-01-31 22:21:11 -05:00
|
|
|
endfunction
|
|
|
|
|
2013-01-30 17:46:58 -05:00
|
|
|
|
2013-01-31 22:21:11 -05:00
|
|
|
function! s:ForceCompileAndDiagnostics()
|
2013-02-06 00:22:50 -05:00
|
|
|
let compilation_succeeded = s:ForceCompile()
|
|
|
|
if !compilation_succeeded
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2013-01-30 17:46:58 -05:00
|
|
|
call s:UpdateDiagnosticNotifications()
|
|
|
|
echom "Diagnostics refreshed."
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
command! YcmForceCompileAndDiagnostics call s:ForceCompileAndDiagnostics()
|
|
|
|
|
|
|
|
|
2013-01-31 22:21:11 -05:00
|
|
|
function! s:ShowDiagnostics()
|
2013-03-17 21:52:42 -04:00
|
|
|
let compilation_succeeded = s:ForceCompile()
|
|
|
|
if !compilation_succeeded
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2013-01-31 22:21:11 -05:00
|
|
|
let diags = pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' )
|
|
|
|
if !empty( diags )
|
|
|
|
call setloclist( 0, diags )
|
|
|
|
lopen
|
|
|
|
else
|
|
|
|
echom "No warnings or errors detected"
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
command! YcmDiags call s:ShowDiagnostics()
|
|
|
|
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
" This is basic vim plugin boilerplate
|
|
|
|
let &cpo = s:save_cpo
|
|
|
|
unlet s:save_cpo
|