diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 52d9f5af..5a69051d 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -91,7 +91,7 @@ function! youcompleteme#Enable() autocmd BufReadPre * call s:OnBufferReadPre( expand( ':p' ) ) autocmd BufRead,FileType * call s:OnBufferRead() autocmd BufEnter * call s:OnBufferEnter() - autocmd BufUnload * call s:OnBufferUnload( expand( ':p' ) ) + autocmd BufUnload * call s:OnBufferUnload() autocmd CursorHold,CursorHoldI * call s:OnCursorHold() autocmd InsertLeave * call s:OnInsertLeave() autocmd InsertEnter * call s:OnInsertEnter() @@ -323,10 +323,12 @@ function! s:TurnOffSyntasticForCFamily() endfunction -function! s:AllowedToCompleteInCurrentBuffer() - if empty( &filetype ) || - \ getbufvar( winbufnr( winnr() ), "&buftype" ) ==# 'nofile' || - \ &filetype ==# 'qf' +function! s:AllowedToCompleteInBuffer( buffer ) + let buffer_filetype = getbufvar( a:buffer, '&filetype' ) + + if empty( buffer_filetype ) || + \ getbufvar( a:buffer, '&buftype' ) ==# 'nofile' || + \ buffer_filetype ==# 'qf' return 0 endif @@ -335,13 +337,18 @@ function! s:AllowedToCompleteInCurrentBuffer() 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 ) + \ has_key( g:ycm_filetype_whitelist, buffer_filetype ) + let blacklist_allows = !has_key( g:ycm_filetype_blacklist, buffer_filetype ) return whitelist_allows && blacklist_allows endfunction +function! s:AllowedToCompleteInCurrentBuffer() + return s:AllowedToCompleteInBuffer( '%' ) +endfunction + + function! s:VisitedBufferRequiresReparse() if !s:AllowedToCompleteInCurrentBuffer() return 0 @@ -471,13 +478,16 @@ function! s:OnBufferEnter() endfunction -function! s:OnBufferUnload( deleted_buffer_file ) - if !s:AllowedToCompleteInCurrentBuffer() || empty( a:deleted_buffer_file ) +function! s:OnBufferUnload() + " Expanding returns the unloaded buffer number as a string but we want + " it as a true number for the getbufvar function. + if !s:AllowedToCompleteInBuffer( str2nr( expand( '' ) ) ) return endif + let deleted_buffer_file = expand( ':p' ) exec s:python_command "ycm_state.OnBufferUnload(" - \ "vim.eval( 'a:deleted_buffer_file' ) )" + \ "vim.eval( 'deleted_buffer_file' ) )" endfunction diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 97f4bf31..1fae1ea8 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -154,20 +154,29 @@ class BaseRequest( object ): hmac_secret = '' -def BuildRequestData( include_buffer_data = True ): +def BuildRequestData( filepath = None ): + """Build request for the current buffer or the buffer corresponding to + |filepath| if specified.""" + current_filepath = vimsupport.GetCurrentBufferFilepath() + + if filepath and current_filepath != filepath: + # Cursor position is irrelevant when filepath is not the current buffer. + return { + 'filepath': filepath, + 'line_num': 1, + 'column_num': 1, + 'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData( filepath ) + } + line, column = vimsupport.CurrentLineAndColumn() - filepath = vimsupport.GetCurrentBufferFilepath() - request_data = { + + return { + 'filepath': current_filepath, 'line_num': line + 1, 'column_num': column + 1, - 'filepath': filepath + 'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData( current_filepath ) } - if include_buffer_data: - request_data[ 'file_data' ] = vimsupport.GetUnsavedAndCurrentBufferData() - - return request_data - def JsonFromFuture( future ): response = future.result() diff --git a/python/ycm/client/event_notification.py b/python/ycm/client/event_notification.py index b9a60d18..bd73f31f 100644 --- a/python/ycm/client/event_notification.py +++ b/python/ycm/client/event_notification.py @@ -32,15 +32,16 @@ from ycm.client.base_request import ( BaseRequest, BuildRequestData, class EventNotification( BaseRequest ): - def __init__( self, event_name, extra_data = None ): + def __init__( self, event_name, filepath = None, extra_data = None ): super( EventNotification, self ).__init__() self._event_name = event_name + self._filepath = filepath self._extra_data = extra_data self._cached_response = None def Start( self ): - request_data = BuildRequestData() + request_data = BuildRequestData( self._filepath ) if self._extra_data: request_data.update( self._extra_data ) request_data[ 'event_name' ] = self._event_name @@ -74,8 +75,10 @@ class EventNotification( BaseRequest ): return self._cached_response if self._cached_response else [] -def SendEventNotificationAsync( event_name, extra_data = None ): - event = EventNotification( event_name, extra_data ) +def SendEventNotificationAsync( event_name, + filepath = None, + extra_data = None ): + event = EventNotification( event_name, filepath, extra_data ) event.Start() diff --git a/python/ycm/test_utils.py b/python/ycm/test_utils.py index ea341684..4cee2fe4 100644 --- a/python/ycm/test_utils.py +++ b/python/ycm/test_utils.py @@ -33,9 +33,12 @@ import functools from ycmd.utils import ToUnicode -BUFNR_REGEX = re.compile( r"^bufnr\('(.+)', ([0-9]+)\)$" ) -BUFWINNR_REGEX = re.compile( r"^bufwinnr\(([0-9]+)\)$" ) -BWIPEOUT_REGEX = re.compile( r"^(?:silent! )bwipeout!? ([0-9]+)$" ) +BUFNR_REGEX = re.compile( '^bufnr\(\'(?P.+)\', ([01])\)$' ) +BUFWINNR_REGEX = re.compile( '^bufwinnr\((?P[0-9]+)\)$' ) +BWIPEOUT_REGEX = re.compile( + '^(?:silent! )bwipeout!? (?P[0-9]+)$' ) +GETBUFVAR_REGEX = re.compile( + '^getbufvar\((?P[0-9]+), "&(?P