Merge s:OnBufferRead and s:OnBufferEnter

Fix issue where completeopt, completefunc, and omnifunc are not set for buffers
opened before the plugin is loaded.
This commit is contained in:
micbou 2017-03-27 01:34:07 +02:00
parent 01570aac03
commit 2542105e2f
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05

View File

@ -90,8 +90,7 @@ function! youcompleteme#Enable()
" We also need to trigger buf init code on the FileType event because when " We also need to trigger buf init code on the FileType event because when
" 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 BufRead,FileType * call s:OnBufferRead() autocmd BufRead,BufEnter,FileType * call s:OnBufferVisit()
autocmd BufEnter * call s:OnBufferEnter()
autocmd BufUnload * call s:OnBufferUnload() autocmd BufUnload * call s:OnBufferUnload()
autocmd CursorHold,CursorHoldI * call s:OnCursorHold() autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
autocmd InsertLeave * call s:OnInsertLeave() autocmd InsertLeave * call s:OnInsertLeave()
@ -101,11 +100,9 @@ function! youcompleteme#Enable()
augroup END augroup END
" BufRead/FileType events are not triggered for the first loaded file. " BufRead/FileType events are not triggered for the first loaded file.
" However, we don't directly call the s:OnBufferRead function because it would " However, we don't directly call the s:OnBufferVisit function because it
" send requests that can't succeed as the server is not ready yet and would " would send requests that can't succeed as the server is not ready yet and
" slow down startup. " would slow down startup.
call s:DisableOnLargeFile( expand( '%' ) )
if s:AllowedToCompleteInCurrentBuffer() if s:AllowedToCompleteInCurrentBuffer()
call s:SetCompleteFunc() call s:SetCompleteFunc()
endif endif
@ -313,6 +310,23 @@ function! s:TurnOffSyntasticForCFamily()
endfunction endfunction
function! s:DisableOnLargeFile( buffer )
if exists( 'b:ycm_largefile' )
return b:ycm_largefile
endif
let threshold = g:ycm_disable_for_files_larger_than_kb * 1024
let b:ycm_largefile =
\ threshold > 0 && getfsize( expand( a:buffer ) ) > threshold
if b:ycm_largefile
exec s:python_command "vimsupport.PostVimMessage(" .
\ "'YouCompleteMe is disabled in this buffer; " .
\ "the file exceeded the max size (see YCM options).' )"
endif
return b:ycm_largefile
endfunction
function! s:AllowedToCompleteInBuffer( buffer ) function! s:AllowedToCompleteInBuffer( buffer )
let buffer_filetype = getbufvar( a:buffer, '&filetype' ) let buffer_filetype = getbufvar( a:buffer, '&filetype' )
@ -322,7 +336,7 @@ function! s:AllowedToCompleteInBuffer( buffer )
return 0 return 0
endif endif
if exists( 'b:ycm_largefile' ) if s:DisableOnLargeFile( a:buffer )
return 0 return 0
endif endif
@ -401,22 +415,6 @@ function! s:SetUpYcmChangedTick()
endfunction endfunction
function! s:DisableOnLargeFile( filename )
if exists( 'b:ycm_largefile' )
return
endif
let threshold = g:ycm_disable_for_files_larger_than_kb * 1024
if threshold > 0 && getfsize( a:filename ) > threshold
exec s:python_command "vimsupport.PostVimMessage(" .
\ "'YouCompleteMe is disabled in this buffer; " .
\ "the file exceeded the max size (see YCM options).' )"
let b:ycm_largefile = 1
endif
endfunction
function! s:OnVimLeave() function! s:OnVimLeave()
exec s:python_command "ycm_state.OnVimLeave()" exec s:python_command "ycm_state.OnVimLeave()"
endfunction endfunction
@ -427,15 +425,13 @@ function! s:OnCompleteDone()
endfunction endfunction
function! s:OnBufferRead() function! s:OnBufferVisit()
" 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
" buffer 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()
call s:DisableOnLargeFile( expand( '<afile>:p' ) ) if !s:VisitedBufferRequiresReparse()
if !s:AllowedToCompleteInCurrentBuffer()
return return
endif endif
@ -448,16 +444,6 @@ function! s:OnBufferRead()
endfunction endfunction
function! s:OnBufferEnter()
if !s:VisitedBufferRequiresReparse()
return
endif
exec s:python_command "ycm_state.OnBufferVisit()"
call s:OnFileReadyToParse()
endfunction
function! s:OnBufferUnload() function! s:OnBufferUnload()
" Expanding <abuf> returns the unloaded buffer number as a string but we want " Expanding <abuf> returns the unloaded buffer number as a string but we want
" it as a true number for the getbufvar function. " it as a true number for the getbufvar function.