Added check for Vim 'hidden' option when trying to open result in the same buffer

This commit is contained in:
Davit Samvelyan 2014-03-22 14:24:16 +04:00
parent 40dc235136
commit b9109af709
2 changed files with 16 additions and 4 deletions

View File

@ -1339,8 +1339,11 @@ Default: `1`
### The `g:ycm_goto_buffer_command` option ### The `g:ycm_goto_buffer_command` option
Defines where `GoTo*` commands result should be opened. Defines where `GoTo*` commands result should be opened.
Can take one of the following values: `[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]` Can take one of the following values:
If this option is set to `'same-buffer'` but current buffer is modified then result will be opened in horizontal split. `[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]`
If this option is set to the `'same-buffer'` but current buffer can not
be switched (when buffer is modified and `nohidden` option is set),
then result will be opened in horizontal split.
Default: `'same-buffer'` Default: `'same-buffer'`
let g:ycm_goto_buffer_command = 'same-buffer' let g:ycm_goto_buffer_command = 'same-buffer'

View File

@ -72,7 +72,8 @@ def GetBufferOption( buffer_object, option ):
def BufferModified( buffer_object ): def BufferModified( buffer_object ):
return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) return buffer_object.options[ 'mod' ]
def GetUnsavedAndCurrentBufferData(): def GetUnsavedAndCurrentBufferData():
buffers_data = {} buffers_data = {}
@ -238,6 +239,14 @@ def VimExpressionToPythonType( vim_expression ):
return result return result
def HiddenEnabled( buffer_object ):
return vim.options[ 'hid' ]
def BufferIsUsable( buffer_object ):
return not BufferModified( buffer_object ) or HiddenEnabled( buffer_object )
# Both |line| and |column| need to be 1-based # Both |line| and |column| need to be 1-based
def JumpToLocation( filename, line, column ): def JumpToLocation( filename, line, column ):
# Add an entry to the jumplist # Add an entry to the jumplist
@ -252,7 +261,7 @@ def JumpToLocation( filename, line, column ):
# jumplist. # jumplist.
user_command = user_options_store.Value( 'goto_buffer_command' ) user_command = user_options_store.Value( 'goto_buffer_command' )
command = BUFFER_COMMAND_MAP.get( user_command, 'edit' ) command = BUFFER_COMMAND_MAP.get( user_command, 'edit' )
if command == 'edit' and BufferModified( vim.current.buffer ): if command == 'edit' and not BufferIsUsable( vim.current.buffer ):
command = 'split' command = 'split'
vim.command( 'keepjumps {0} {1}'.format( command, filename ) ) vim.command( 'keepjumps {0} {1}'.format( command, filename ) )
vim.current.window.cursor = ( line, column - 1 ) vim.current.window.cursor = ( line, column - 1 )