123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- let s:save_cpo = &cpo
- set cpo&vim
- let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
- let s:searched_and_results_found = 0
- let s:omnifunc_mode = 0
- let s:old_cursor_position = []
- let s:cursor_moved = 0
- let s:moved_vertically_in_insert_mode = 0
- let s:previous_num_chars_on_current_line = -1
- function! youcompleteme#Enable()
-
- if &diff
- return
- endif
- call s:SetUpBackwardsCompatibility()
- py import sys
- py import vim
- exe 'python sys.path.insert( 0, "' . s:script_folder_path . '/../python" )'
- py from ycm import base
- py from ycm import vimsupport
- py from ycm import user_options_store
- py user_options_store.SetAll( base.BuildServerConf() )
- if !pyeval( 'base.CompatibleWithYcmCore()')
- echohl WarningMsg |
- \ echomsg "YouCompleteMe unavailable: ycm_core too old, PLEASE RECOMPILE ycm_core" |
- \ echohl None
- return
- endif
- py from ycm.youcompleteme import YouCompleteMe
- py ycm_state = YouCompleteMe( user_options_store.GetAll() )
- call s:SetUpCpoptions()
- call s:SetUpCompleteopt()
- call s:SetUpKeyMappings()
- if g:ycm_register_as_syntastic_checker
- call s:ForceSyntasticCFamilyChecker()
- endif
- if g:ycm_allow_changing_updatetime
- set ut=2000
- endif
- augroup youcompleteme
- autocmd!
- autocmd CursorMovedI * call s:OnCursorMovedInsertMode()
- autocmd CursorMoved * call s:OnCursorMovedNormalMode()
-
- " so if you do "vim foo.cc
-
- " that happens *after" BufRead/BufEnter has already triggered for the
-
- autocmd BufRead,BufEnter * call s:OnBufferVisit()
- autocmd BufUnload * call s:OnBufferUnload( expand( '<afile>:p' ) )
- autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
- autocmd InsertLeave * call s:OnInsertLeave()
- autocmd InsertEnter * call s:OnInsertEnter()
- autocmd VimLeave * call s:OnVimLeave()
- augroup END
-
-
-
- call s:OnBufferVisit()
- endfunction
- function! s:SetUpKeyMappings()
-
-
-
- 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
- for key in g:ycm_key_list_select_completion
-
-
-
- exe 'inoremap <expr>' . key .
- \ ' pumvisible() ? "\<C-n>" : "\' . key .'"'
- endfor
- for key in g:ycm_key_list_previous_completion
-
- exe 'inoremap <expr>' . key .
- \ ' pumvisible() ? "\<C-p>" : "\' . key .'"'
- endfor
- if !empty( g:ycm_key_invoke_completion )
- let invoke_key = g:ycm_key_invoke_completion
-
- if invoke_key ==# '<C-Space>' && !has('gui_running')
- let invoke_key = '<Nul>'
- endif
-
-
- silent! exe 'inoremap <unique> ' . invoke_key . ' <C-X><C-O><C-P>'
- endif
- if !empty( g:ycm_key_detailed_diagnostics )
- silent! exe 'nnoremap <unique> ' . g:ycm_key_detailed_diagnostics .
- \ ' :YcmShowDetailedDiagnostic<cr>'
- endif
- endfunction
- function! s:SetUpBackwardsCompatibility()
- let complete_in_comments_and_strings =
- \ get( g:, 'ycm_complete_in_comments_and_strings', 0 )
- if complete_in_comments_and_strings
- let g:ycm_complete_in_strings = 1
- let g:ycm_complete_in_comments = 1
- endif
- endfunction
- function! s:ForceSyntasticCFamilyChecker()
-
- let g:syntastic_cpp_checkers = ['ycm']
- let g:syntastic_c_checkers = ['ycm']
- let g:syntastic_objc_checkers = ['ycm']
- let g:syntastic_objcpp_checkers = ['ycm']
- endfunction
- function! s:AllowedToCompleteInCurrentFile()
- if empty( &filetype ) || getbufvar(winbufnr(winnr()), "&buftype") ==# 'nofile'
- 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
- endfunction
- function! s:SetUpCpoptions()
-
-
- set cpoptions+=B
- endfunction
- function! s:SetUpCompleteopt()
-
-
-
-
-
-
- set completeopt-=menu
- set completeopt+=menuone
-
-
-
-
-
- set completeopt-=longest
- if g:ycm_add_preview_to_completeopt
- set completeopt+=preview
- endif
- endfunction
- function! s:SetUpYcmChangedTick()
- let b:ycm_changedtick =
- \ get( b:, 'ycm_changedtick', {
- \ 'file_ready_to_parse' : -1,
- \ } )
- endfunction
- function! s:OnVimLeave()
- py ycm_state.OnVimLeave()
- endfunction
- function! s:OnBufferVisit()
-
-
-
- call s:SetUpYcmChangedTick()
- if !s:AllowedToCompleteInCurrentFile()
- return
- endif
- call s:SetUpCompleteopt()
- call s:SetCompleteFunc()
- py ycm_state.OnBufferVisit()
- call s:OnFileReadyToParse()
- endfunction
- function! s:OnBufferUnload( deleted_buffer_file )
- if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file )
- return
- endif
- py ycm_state.OnBufferUnload( vim.eval( 'a:deleted_buffer_file' ) )
- endfunction
- function! s:OnCursorHold()
- if !s:AllowedToCompleteInCurrentFile()
- return
- endif
- call s:SetUpCompleteopt()
-
-
-
- call s:OnFileReadyToParse()
- endfunction
- function! s:OnFileReadyToParse()
-
-
- call s:SetUpYcmChangedTick()
- let buffer_changed = b:changedtick != b:ycm_changedtick.file_ready_to_parse
- if buffer_changed
- py ycm_state.OnFileReadyToParse()
- endif
- let b:ycm_changedtick.file_ready_to_parse = b:changedtick
- endfunction
- function! s:SetCompleteFunc()
- let &completefunc = 'youcompleteme#Complete'
- let &l:completefunc = 'youcompleteme#Complete'
- if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
- let &omnifunc = 'youcompleteme#OmniComplete'
- let &l:omnifunc = 'youcompleteme#OmniComplete'
-
-
-
- elseif &omnifunc == 'youcompleteme#OmniComplete'
- let &omnifunc = ''
- let &l:omnifunc = ''
- endif
- endfunction
- function! s:OnCursorMovedInsertMode()
- if !s:AllowedToCompleteInCurrentFile()
- return
- endif
- call s:UpdateCursorMoved()
-
-
-
-
-
-
-
- if !s:BufferTextChangedSinceLastMoveInInsertMode()
- return
- endif
- call s:IdentifierFinishedOperations()
- if g:ycm_autoclose_preview_window_after_completion
- call s:ClosePreviewWindowIfNeeded()
- endif
- call s:InvokeCompletion()
- endfunction
- function! s:OnCursorMovedNormalMode()
- if !s:AllowedToCompleteInCurrentFile()
- return
- endif
-
- call s:OnFileReadyToParse()
- endfunction
- function! s:OnInsertLeave()
- if !s:AllowedToCompleteInCurrentFile()
- return
- endif
- let s:omnifunc_mode = 0
-
- call s:OnFileReadyToParse()
- py ycm_state.OnInsertLeave()
- if g:ycm_autoclose_preview_window_after_completion ||
- \ g:ycm_autoclose_preview_window_after_insertion
- call s:ClosePreviewWindowIfNeeded()
- endif
- endfunction
- function! s:OnInsertEnter()
- if !s:AllowedToCompleteInCurrentFile()
- return
- endif
- let s:old_cursor_position = []
- endfunction
- function! s:UpdateCursorMoved()
- let current_position = getpos('.')
- let s:cursor_moved = current_position != s:old_cursor_position
- let s:moved_vertically_in_insert_mode = s:old_cursor_position != [] &&
- \ current_position[ 1 ] != s:old_cursor_position[ 1 ]
- let s:old_cursor_position = current_position
- endfunction
- 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
- function! s:ClosePreviewWindowIfNeeded()
- let current_buffer_name = bufname('')
-
- " "[Command Line]
-
- if current_buffer_name[ 0 ] == '['
- return
- endif
- if s:searched_and_results_found
-
-
- pclose
- endif
- endfunction
- function! s:UpdateDiagnosticNotifications()
- if get( g:, 'loaded_syntastic_plugin', 0 ) &&
- \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) &&
- \ pyeval( 'ycm_state.DiagnosticsForCurrentFileReady()' ) &&
- \ g:ycm_register_as_syntastic_checker
- SyntasticCheck
- endif
- endfunction
- function! s:IdentifierFinishedOperations()
- if !pyeval( 'base.CurrentIdentifierFinished()' )
- return
- endif
- py ycm_state.OnCurrentIdentifierFinished()
- let s:omnifunc_mode = 0
- endfunction
- function! s:InsideCommentOrString()
-
-
- let syntax_group = synIDattr( synIDtrans( synID( line( '.' ), col( '.' ) - 1, 1 ) ), 'name')
- if stridx(syntax_group, 'Comment') > -1
- return 1
- endif
- if stridx(syntax_group, 'String') > -1
- return 2
- endif
- return 0
- endfunction
- function! s:InsideCommentOrStringAndShouldStop()
- let retval = s:InsideCommentOrString()
- let inside_comment = retval == 1
- let inside_string = retval == 2
- if inside_comment && g:ycm_complete_in_comments ||
- \ inside_string && g:ycm_complete_in_strings
- return 0
- endif
- return retval
- endfunction
- function! s:OnBlankLine()
- return pyeval( 'not vim.current.line or vim.current.line.isspace()' )
- endfunction
- function! s:InvokeCompletion()
- if &completefunc != "youcompleteme#Complete"
- return
- endif
- if s:InsideCommentOrStringAndShouldStop() || s:OnBlankLine()
- return
- endif
-
-
-
-
-
-
-
-
- if !s:cursor_moved
- return
- endif
-
-
-
-
-
-
-
- call feedkeys( "\<C-X>\<C-U>\<C-P>", 'n' )
- endfunction
- python << EOF
- def GetCompletions( query ):
- request = ycm_state.GetCurrentCompletionRequest()
- request.Start( query )
- results_ready = False
- while not results_ready:
- results_ready = request.Done()
- if bool( int( vim.eval( 'complete_check()' ) ) ):
- return { 'words' : [], 'refresh' : 'always'}
- results = base.AdjustCandidateInsertionText( request.Results() )
- return { 'words' : results, 'refresh' : 'always' }
- EOF
- function! s:CompletionsForQuery( query )
- py results = GetCompletions( vim.eval( 'a:query' ) )
- let results = pyeval( 'results' )
- let s:searched_and_results_found = len( results.words ) != 0
- return results
- endfunction
- function! youcompleteme#Complete( findstart, base )
-
-
-
-
- if s:omnifunc_mode
- return youcompleteme#OmniComplete( a:findstart, a:base )
- endif
- if a:findstart
-
-
-
- if !s:cursor_moved
-
-
- return -2
- endif
- py request = ycm_state.CreateCompletionRequest()
- return pyeval( 'request.CompletionStartColumn()' )
- else
- return s:CompletionsForQuery( a:base )
- endif
- endfunction
- function! youcompleteme#OmniComplete( findstart, base )
- if a:findstart
- let s:omnifunc_mode = 1
-
- return pyeval( 'ycm_state.CreateCompletionRequest().CompletionStartColumn()' )
- else
- return s:CompletionsForQuery( a:base )
- endif
- endfunction
- function! s:ShowDetailedDiagnostic()
- py ycm_state.ShowDetailedDiagnostic()
- endfunction
- command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic()
- function! youcompleteme#CurrentFileDiagnostics()
-
-
- return []
- endfunction
- 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()
- function! s:CompleterCommand(...)
-
- " If the first arguments is of the form "ft=...
- " completer to use (for example "ft=cpp
-
-
- " "ft=ycm:ident
-
- let arguments = copy(a:000)
- let completer = ''
- if a:0 > 0 && strpart(a:1, 0, 3) == 'ft='
- if a:1 == 'ft=ycm:ident'
- let completer = 'identifier'
- endif
- let arguments = arguments[1:]
- endif
- py ycm_state.SendCommandRequest( vim.eval( 'l:arguments' ),
- \ vim.eval( 'l:completer' ) )
- endfunction
- function! youcompleteme#OpenGoToList()
- set lazyredraw
- cclose
- execute 'belowright copen 3'
- set nolazyredraw
- au WinLeave <buffer> q
- redraw!
- endfunction
- command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete
- \ YcmCompleter call s:CompleterCommand(<f-args>)
- function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos )
- return join( pyeval( 'ycm_state.GetDefinedSubcommands()' ),
- \ "\n")
- endfunction
- function! s:ForceCompile()
- if !pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
- echom "Native filetype completion not supported for current file, "
- \ . "cannot force recompilation."
- return 0
- 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
- let getting_completions = pyeval(
- \ 'ycm_state.GettingCompletions()' )
- if !getting_completions
- echom "Unable to retrieve diagnostics, see output of `:mes` for possible details."
- return 0
- endif
- sleep 100m
- endwhile
- return 1
- endfunction
- function! s:ForceCompileAndDiagnostics()
- let compilation_succeeded = s:ForceCompile()
- if !compilation_succeeded
- return
- endif
- call s:UpdateDiagnosticNotifications()
- echom "Diagnostics refreshed."
- endfunction
- command! YcmForceCompileAndDiagnostics call s:ForceCompileAndDiagnostics()
- function! s:ShowDiagnostics()
- let compilation_succeeded = s:ForceCompile()
- if !compilation_succeeded
- return
- endif
- 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()
- let &cpo = s:save_cpo
- unlet s:save_cpo
|