Auto merge of #2312 - micbou:on-buffer-visit-changed, r=puremourning

[READY] Do not send BufferVisit and FileReadyToParse notification events when current allowed buffer has not changed

See discussion in PR #2265 and the commit message.

Since only the `BufEnter` autocommand event is concerned, we are splitting `s:OnBufferVisit` in two functions:
 - `s:OnBufferRead`, identical to current `s:OnBufferVisit`, for the `BufRead` and `FileType` autocommand events;
 - `s:OnBufferEnter` for the `BufEnter` event.

We only send the `BufferVisit` and `FileReadyToParse` event notifications to `ycmd` in `s:OnBufferEnter` when the entered buffer, in which we are allowed to complete, is different from the last one.

Closes #2265.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/valloric/youcompleteme/2312)
<!-- Reviewable:end -->
This commit is contained in:
Homu 2016-09-10 22:37:45 +09:00
commit e03b49e3be

View File

@ -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( '<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 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( '<afile>: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