From aca0f21a3d81a2195b1fe22715f202c39844baca Mon Sep 17 00:00:00 2001 From: micbou Date: Sun, 21 Aug 2016 17:12:04 +0200 Subject: [PATCH] Trigger BufferVisit event only when buffer has changed When selecting candidates during completion, Vim jumps to the preview window (if enabled) and jumps back to the actual buffer, triggering twice the BufEnter autocommand event. This results in YCM sending two BufferVisit and one FileReadyToParse event notifications to ycmd, which is completely unnecessary since the current buffer did not change. We improve this by only sending these events when the entered buffer allowed to be completed has changed. --- autoload/youcompleteme.vim | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 37ed5724..9acff345 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -28,6 +28,7 @@ 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 = strlen( getline('.') ) +let s:previous_allowed_buffer_number = 0 function! s:UsingPython2() @@ -90,7 +91,8 @@ function! youcompleteme#Enable() " the user does :enew and then :set ft=something, we need to run buf init " code again. autocmd BufReadPre * call s:OnBufferReadPre( expand( ':p' ) ) - autocmd BufRead,BufEnter,FileType * call s:OnBufferVisit() + autocmd BufRead,FileType * call s:OnBufferRead() + autocmd BufEnter * call s:OnBufferEnter() autocmd BufUnload * call s:OnBufferUnload( expand( ':p' ) ) autocmd CursorHold,CursorHoldI * call s:OnCursorHold() autocmd InsertLeave * call s:OnInsertLeave() @@ -118,7 +120,7 @@ function! youcompleteme#Enable() " triggering for the first loaded file. This should be the last commands " executed in this function! call s:OnBufferReadPre( expand( ':p' ) ) - call s:OnBufferVisit() + call s:OnBufferRead() endfunction @@ -324,7 +326,7 @@ function! s:TurnOffSyntasticForCFamily() endfunction -function! s:AllowedToCompleteInCurrentFile() +function! s:AllowedToCompleteInCurrentBuffer() if empty( &filetype ) || \ getbufvar( winbufnr( winnr() ), "&buftype" ) ==# 'nofile' || \ &filetype ==# 'qf' @@ -343,6 +345,19 @@ function! s:AllowedToCompleteInCurrentFile() endfunction +function! s:VisitedBufferRequiresReparse() + if !s:AllowedToCompleteInCurrentBuffer() + return 0 + endif + + if bufnr( '' ) ==# s:previous_allowed_buffer_number + return 0 + endif + let s:previous_allowed_buffer_number = bufnr( '' ) + return 1 +endfunction + + function! s:SetUpCommands() command! YcmRestartServer call s:RestartServer() command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic() @@ -427,13 +442,13 @@ function! s:OnBufferReadPre(filename) endif endfunction -function! s:OnBufferVisit() +function! s:OnBufferRead() " We need to do this even when we are not allowed to complete in the current - " file because we might be allowed to complete in the future! The canonical + " buffer because we might be allowed to complete in the future! The canonical " example is creating a new buffer with :enew and then setting a filetype. call s:SetUpYcmChangedTick() - if !s:AllowedToCompleteInCurrentFile() + if !s:AllowedToCompleteInCurrentBuffer() return endif @@ -449,8 +464,18 @@ function! s:OnBufferVisit() endfunction +function! s:OnBufferEnter() + if !s:VisitedBufferRequiresReparse() + return + endif + + exec s:python_command "ycm_state.OnBufferVisit()" + call s:OnFileReadyToParse() +endfunction + + function! s:OnBufferUnload( deleted_buffer_file ) - if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file ) + if !s:AllowedToCompleteInCurrentBuffer() || empty( a:deleted_buffer_file ) return endif @@ -460,7 +485,7 @@ endfunction function! s:OnCursorHold() - if !s:AllowedToCompleteInCurrentFile() + if !s:AllowedToCompleteInCurrentBuffer() return endif @@ -509,7 +534,7 @@ function! s:SetOmnicompleteFunc() endfunction function! s:OnCursorMovedInsertMode() - if !s:AllowedToCompleteInCurrentFile() + if !s:AllowedToCompleteInCurrentBuffer() return endif @@ -546,7 +571,7 @@ endfunction function! s:OnCursorMovedNormalMode() - if !s:AllowedToCompleteInCurrentFile() + if !s:AllowedToCompleteInCurrentBuffer() return endif @@ -556,7 +581,7 @@ endfunction function! s:OnInsertLeave() - if !s:AllowedToCompleteInCurrentFile() + if !s:AllowedToCompleteInCurrentBuffer() return endif @@ -573,7 +598,7 @@ endfunction function! s:OnInsertEnter() let s:previous_num_chars_on_current_line = strlen( getline('.') ) - if !s:AllowedToCompleteInCurrentFile() + if !s:AllowedToCompleteInCurrentBuffer() return endif