From edae6fd0a28cbff0cb995e0d4c262b9fb74de1c5 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 3 Aug 2013 15:05:25 -0700 Subject: [PATCH 1/5] Using b:changedtick for change detection For now, doing it just for detecting whether a change was made on move in insert mode. Using b:changedtick instead of our homebrew way of detecting the changed should be both faster and more robust. --- autoload/youcompleteme.vim | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 46cc12a2..3d72853d 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -28,8 +28,6 @@ let s:omnifunc_mode = 0 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 = -1 function! youcompleteme#Enable() " When vim is in diff mode, don't run @@ -206,6 +204,20 @@ function! s:SetUpCompleteopt() endif endfunction + +" For various functions/use-cases, we want to keep track of whether the buffer +" has changed since the last time they were invoked. We keep the state of +" b:changedtick of the last time the specific function was called in +" b:ycm_changedtick. +function! s:SetUpYcmChangedTick() + let b:ycm_changedtick = + \ get( b:, 'ycm_changedtick', { + \ 'insert_mode_move' : -1, + \ 'file_ready_to_parse' : -1, + \ } ) +endfunction + + function! s:OnVimLeave() py ycm_state.OnVimLeave() py extra_conf_store.CallExtraConfVimCloseIfExists() @@ -217,6 +229,7 @@ function! s:OnBufferVisit() return endif + call s:SetUpYcmChangedTick() call s:SetUpCompleteopt() call s:SetCompleteFunc() py ycm_state.OnBufferVisit() @@ -340,23 +353,9 @@ endfunction function! s:BufferTextChangedSinceLastMoveInInsertMode() - if s:moved_vertically_in_insert_mode - let s:previous_num_chars_on_current_line = -1 - return 0 - endif - - let num_chars_in_current_cursor_line = strlen( getline('.') ) - - if s:previous_num_chars_on_current_line == -1 - let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line - return 0 - endif - - let changed_text_on_current_line = num_chars_in_current_cursor_line != - \ s:previous_num_chars_on_current_line - let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line - - return changed_text_on_current_line + let buffer_changed = b:changedtick != b:ycm_changedtick.insert_mode_move + let b:ycm_changedtick.insert_mode_move = b:changedtick + return buffer_changed endfunction From fc3a303ace9ab2764e9c54224511f37586d7b9fa Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 3 Aug 2013 15:47:11 -0700 Subject: [PATCH 2/5] Only parsing file if changes detected Previously we would do it on every CursorHold, buffer changed or not. Fixes #485. --- autoload/youcompleteme.vim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 3d72853d..096c6be9 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -260,7 +260,11 @@ endfunction function! s:OnFileReadyToParse() - py ycm_state.OnFileReadyToParse() + let buffer_changed = b:changedtick != b:ycm_changedtick.file_ready_to_parse + if buffer_changed + py ycm_state.OnFileReadyToParse() + endif + let b:ycm_changedtick.file_ready_to_parse = b:changedtick endfunction From 4db6fb8d7f335219b2347be6c4d2a39966f4080d Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 3 Aug 2013 18:42:16 -0700 Subject: [PATCH 3/5] Revert to old way of tracking insert mode change b:changedtick is incremented by the feedkeys() call which screws us up and there doesn't seem to be a way around it that's worth the hassle. --- autoload/youcompleteme.vim | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 096c6be9..cdf3c0ae 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -28,6 +28,8 @@ let s:omnifunc_mode = 0 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 = -1 function! youcompleteme#Enable() " When vim is in diff mode, don't run @@ -357,9 +359,23 @@ endfunction function! s:BufferTextChangedSinceLastMoveInInsertMode() - let buffer_changed = b:changedtick != b:ycm_changedtick.insert_mode_move - let b:ycm_changedtick.insert_mode_move = b:changedtick - return buffer_changed + if s:moved_vertically_in_insert_mode + let s:previous_num_chars_on_current_line = -1 + return 0 + endif + + let num_chars_in_current_cursor_line = strlen( getline('.') ) + + if s:previous_num_chars_on_current_line == -1 + let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line + return 0 + endif + + let changed_text_on_current_line = num_chars_in_current_cursor_line != + \ s:previous_num_chars_on_current_line + let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line + + return changed_text_on_current_line endfunction From 360a70b1b3081c137d329fc4c231acefb7f4b331 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Mon, 5 Aug 2013 13:36:11 -0700 Subject: [PATCH 4/5] Deleting some dead code --- autoload/youcompleteme.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index cdf3c0ae..67c912b6 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -214,7 +214,6 @@ endfunction function! s:SetUpYcmChangedTick() let b:ycm_changedtick = \ get( b:, 'ycm_changedtick', { - \ 'insert_mode_move' : -1, \ 'file_ready_to_parse' : -1, \ } ) endfunction From 60c9908d25a157be684aa920c95b388cbd6e2e00 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 7 Aug 2013 16:54:14 -0700 Subject: [PATCH 5/5] Ensure g:ycm_changedtick is always set. Fixes #493. --- autoload/youcompleteme.vim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 67c912b6..ae75ee26 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -226,11 +226,15 @@ 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 + " example is creating a new buffer with :enew and then setting a filetype. + call s:SetUpYcmChangedTick() + if !s:AllowedToCompleteInCurrentFile() return endif - call s:SetUpYcmChangedTick() call s:SetUpCompleteopt() call s:SetCompleteFunc() py ycm_state.OnBufferVisit()