From e28c709b997f0d15fd0b0f8af9cd84c609d1d9db Mon Sep 17 00:00:00 2001 From: Andrea Cedraro Date: Wed, 3 Dec 2014 10:38:02 +0100 Subject: [PATCH] Disable on large file closes #1238 --- README.md | 9 +++++++++ autoload/youcompleteme.vim | 24 +++++++++++++++++++++--- plugin/youcompleteme.vim | 3 +++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ec781461..1af3d76c 100644 --- a/README.md +++ b/README.md @@ -1468,6 +1468,15 @@ Default: `'same-buffer'` let g:ycm_goto_buffer_command = 'same-buffer' +### The `g:ycm_disable_for_files_larger_than_kb` option + +Defines the max size (in Kb) for a file to be considered for completion. If this +option is set to 0 then no check is made on the size of the file you're opening + +Default: 1000 + + let g:ycm_disable_for_files_larger_than_kb = 1000 + FAQ --- diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index e3652ef6..3c34661a 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -77,6 +77,7 @@ function! youcompleteme#Enable() " 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 " code again. + autocmd BufReadPre * call s:OnBufferReadPre( expand( ':p' ) ) autocmd BufRead,BufEnter,FileType * call s:OnBufferVisit() autocmd BufUnload * call s:OnBufferUnload( expand( ':p' ) ) autocmd CursorHold,CursorHoldI * call s:OnCursorHold() @@ -85,9 +86,10 @@ function! youcompleteme#Enable() autocmd VimLeave * call s:OnVimLeave() augroup END - " Calling this once solves the problem of BufRead/BufEnter not triggering for - " the first loaded file. This should be the last command executed in this - " function! + " Calling these once solves the problem of BufReadPre/BufRead/BufEnter not + " triggering for the first loaded file. This should be the last commands + " executed in this function! + call s:OnBufferReadPre( expand( ':p' ) ) call s:OnBufferVisit() endfunction @@ -285,6 +287,10 @@ function! s:AllowedToCompleteInCurrentFile() return 0 endif + if exists( 'b:ycm_largefile' ) + 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 ) @@ -347,6 +353,18 @@ function! s:OnVimLeave() endfunction +function! s:OnBufferReadPre(filename) + let threshold = g:ycm_disable_for_files_larger_than_kb * 1024 + + if threshold > 0 && getfsize( a:filename ) > threshold + echohl WarningMsg | + \ echomsg "YouCompleteMe is disabled in this buffer; " . + \ "the file exceeded the max size (see YCM options)." | + \ echohl None + let b:ycm_largefile = 1 + endif +endfunction + function! s:OnBufferVisit() " 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 diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 8ab7c657..f1624cf5 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -167,6 +167,9 @@ let g:ycm_warning_symbol = let g:ycm_goto_buffer_command = \ get( g:, 'ycm_goto_buffer_command', 'same-buffer' ) +let g:ycm_disable_for_files_larger_than_kb = + \ get( g:, 'ycm_disable_for_files_larger_than_kb', 1000 ) + " On-demand loading. Let's use the autoload folder and not slow down vim's " startup procedure. augroup youcompletemeStart