diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index a9e76d4e..9efa2e92 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -87,7 +87,10 @@ function! youcompleteme#Enable() " is read. This is because youcompleteme#Enable() is called on VimEnter and " that happens *after" BufRead/BufEnter has already triggered for the " initial file. - autocmd BufRead,BufEnter * call s:OnBufferVisit() + " 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 BufRead,BufEnter,FileType * call s:OnBufferVisit() autocmd BufUnload * call s:OnBufferUnload( expand( ':p' ) ) autocmd CursorHold,CursorHoldI * call s:OnCursorHold() autocmd InsertLeave * call s:OnInsertLeave() diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index d67836a5..9f59f287 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -85,13 +85,14 @@ class BaseRequest( object ): def BuildRequestData( start_column = None, query = None ): line, column = vimsupport.CurrentLineAndColumn() + filepath = vimsupport.GetCurrentBufferFilepath() request_data = { 'filetypes': vimsupport.CurrentFiletypes(), 'line_num': line, 'column_num': column, 'start_column': start_column, 'line_value': vim.current.line, - 'filepath': vim.current.buffer.name, + 'filepath': filepath, 'file_data': vimsupport.GetUnsavedAndCurrentBufferData() } diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index c4663c09..21d1daf2 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -69,7 +69,7 @@ def GetUnsavedAndCurrentBufferData(): buffer_object == vim.current.buffer ): continue - buffers_data[ buffer_object.name ] = { + buffers_data[ GetBufferFilepath( buffer_object ) ] = { 'contents': '\n'.join( buffer_object ), 'filetypes': FiletypesForBuffer( buffer_object ) } @@ -83,6 +83,18 @@ def GetBufferNumberForFilename( filename, open_file_if_needed = True ): int( open_file_if_needed ) ) ) ) +def GetCurrentBufferFilepath(): + return GetBufferFilepath( vim.current.buffer ) + + +def GetBufferFilepath( buffer_object ): + if buffer_object.name: + return buffer_object.name + # Buffers that have just been created by a command like :enew don't have any + # buffer name so we use the buffer number for that. + return os.path.join( os.getcwd(), str( buffer_object.number ) ) + + # Given a dict like {'a': 1}, loads it into Vim as if you ran 'let g:a = 1' # When |overwrite| is True, overwrites the existing value in Vim. def LoadDictIntoVimGlobals( new_globals, overwrite = True ): @@ -111,7 +123,7 @@ def JumpToLocation( filename, line, column ): # Add an entry to the jumplist vim.command( "normal! m'" ) - if filename != vim.current.buffer.name: + if filename != GetCurrentBufferFilepath(): # We prefix the command with 'keepjumps' so that opening the file is not # recorded in the jumplist. So when we open the file and move the cursor to # a location in it, the user can use CTRL-O to jump back to the original diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index f63c4872..2fd1adf5 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -116,7 +116,8 @@ class YouCompleteMe( object ): def NativeFiletypeCompletionAvailable( self ): try: - return _NativeFiletypeCompletionAvailableForFile( vim.current.buffer.name ) + return _NativeFiletypeCompletionAvailableForFile( + vimsupport.GetCurrentBufferFilepath() ) except: return False