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.
This commit is contained in:
micbou 2016-08-21 17:12:04 +02:00
parent e332cdb2a0
commit aca0f21a3d
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05

View File

@ -28,6 +28,7 @@ let s:old_cursor_position = []
let s:cursor_moved = 0 let s:cursor_moved = 0
let s:moved_vertically_in_insert_mode = 0 let s:moved_vertically_in_insert_mode = 0
let s:previous_num_chars_on_current_line = strlen( getline('.') ) let s:previous_num_chars_on_current_line = strlen( getline('.') )
let s:previous_allowed_buffer_number = 0
function! s:UsingPython2() 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 " the user does :enew and then :set ft=something, we need to run buf init
" code again. " code again.
autocmd BufReadPre * call s:OnBufferReadPre( expand( '<afile>:p' ) ) autocmd BufReadPre * call s:OnBufferReadPre( expand( '<afile>: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( '<afile>:p' ) ) autocmd BufUnload * call s:OnBufferUnload( expand( '<afile>:p' ) )
autocmd CursorHold,CursorHoldI * call s:OnCursorHold() autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
autocmd InsertLeave * call s:OnInsertLeave() autocmd InsertLeave * call s:OnInsertLeave()
@ -118,7 +120,7 @@ function! youcompleteme#Enable()
" triggering for the first loaded file. This should be the last commands " triggering for the first loaded file. This should be the last commands
" executed in this function! " executed in this function!
call s:OnBufferReadPre( expand( '<afile>:p' ) ) call s:OnBufferReadPre( expand( '<afile>:p' ) )
call s:OnBufferVisit() call s:OnBufferRead()
endfunction endfunction
@ -324,7 +326,7 @@ function! s:TurnOffSyntasticForCFamily()
endfunction endfunction
function! s:AllowedToCompleteInCurrentFile() function! s:AllowedToCompleteInCurrentBuffer()
if empty( &filetype ) || if empty( &filetype ) ||
\ getbufvar( winbufnr( winnr() ), "&buftype" ) ==# 'nofile' || \ getbufvar( winbufnr( winnr() ), "&buftype" ) ==# 'nofile' ||
\ &filetype ==# 'qf' \ &filetype ==# 'qf'
@ -343,6 +345,19 @@ function! s:AllowedToCompleteInCurrentFile()
endfunction 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() function! s:SetUpCommands()
command! YcmRestartServer call s:RestartServer() command! YcmRestartServer call s:RestartServer()
command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic() command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic()
@ -427,13 +442,13 @@ function! s:OnBufferReadPre(filename)
endif endif
endfunction endfunction
function! s:OnBufferVisit() function! s:OnBufferRead()
" We need to do this even when we are not allowed to complete in the current " 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. " example is creating a new buffer with :enew and then setting a filetype.
call s:SetUpYcmChangedTick() call s:SetUpYcmChangedTick()
if !s:AllowedToCompleteInCurrentFile() if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif
@ -449,8 +464,18 @@ function! s:OnBufferVisit()
endfunction 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 ) function! s:OnBufferUnload( deleted_buffer_file )
if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file ) if !s:AllowedToCompleteInCurrentBuffer() || empty( a:deleted_buffer_file )
return return
endif endif
@ -460,7 +485,7 @@ endfunction
function! s:OnCursorHold() function! s:OnCursorHold()
if !s:AllowedToCompleteInCurrentFile() if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif
@ -509,7 +534,7 @@ function! s:SetOmnicompleteFunc()
endfunction endfunction
function! s:OnCursorMovedInsertMode() function! s:OnCursorMovedInsertMode()
if !s:AllowedToCompleteInCurrentFile() if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif
@ -546,7 +571,7 @@ endfunction
function! s:OnCursorMovedNormalMode() function! s:OnCursorMovedNormalMode()
if !s:AllowedToCompleteInCurrentFile() if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif
@ -556,7 +581,7 @@ endfunction
function! s:OnInsertLeave() function! s:OnInsertLeave()
if !s:AllowedToCompleteInCurrentFile() if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif
@ -573,7 +598,7 @@ endfunction
function! s:OnInsertEnter() function! s:OnInsertEnter()
let s:previous_num_chars_on_current_line = strlen( getline('.') ) let s:previous_num_chars_on_current_line = strlen( getline('.') )
if !s:AllowedToCompleteInCurrentFile() if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif