From 0102d23bfefe4307607be3b4cf0d237995552c68 Mon Sep 17 00:00:00 2001 From: davits Date: Sun, 23 Feb 2014 17:50:51 +0400 Subject: [PATCH 1/5] Added options to choose whether GoTo commands result should be opened in the new buffer and in which one. --- README.md | 17 +++++++++++++++++ plugin/youcompleteme.vim | 10 ++++++++++ python/ycm/vimsupport.py | 14 ++++++++++---- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27c2fabd..7a4c3c01 100644 --- a/README.md +++ b/README.md @@ -1336,6 +1336,23 @@ Default: `1` let g:ycm_use_ultisnips_completer = 1 +### The `g:ycm_goto_same_buffer` option + +Indicates whether GoTo command result should be opened in the current buffer. +However if current buffer has unsaved modifications then this option will be ignored and result will be opened in the new buffer created with `g:ycm_goto_buffer_command` command. + +Default: `1` + + let g:ycm_goto_same_buffer = 1 + +### The `g:ycm_goto_buffer_command` option + +Defines command for the new buffer creation where GoTo command result will be opened. +Value should be one of the following vim commands `[ 'sp[lit]', 'vs[plit]', 'tabe[dit]' ]` + +Default: `split` + let g:ycm_goto_buffer_command = 'split' + FAQ --- diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index bfdad8db..8118a2fa 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -144,6 +144,16 @@ let g:ycm_warning_symbol = \ get( g:, 'ycm_warning_symbol', \ get( g:, 'syntastic_warning_symbol', '>>' ) ) +let g:ycm_goto_same_buffer = + \ get( g:, 'ycm_goto_same_buffer', 1 ) + +let g:ycm_goto_buffer_command = + \ get( g:, 'ycm_goto_buffer_command', 'split' ) + +if index( [ 'sp', 'split', 'vs', 'vsplit', 'tabe', 'tabedit' ], g:ycm_goto_buffer_command ) < 0 + let g:ycm_goto_buffer_command = 'split' +endif + " On-demand loading. Let's use the autoload folder and not slow down vim's " startup procedure. augroup youcompletemeStart diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 508fb49e..9c86b3c9 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -21,6 +21,7 @@ import vim import os import json from ycm.utils import ToUtf8IfNeeded +from ycm import user_options_store def CurrentLineAndColumn(): """Returns the 0-based current line and 0-based current column.""" @@ -65,10 +66,10 @@ def GetBufferOption( buffer_object, option ): return GetVariableValue( to_eval ) -def GetUnsavedAndCurrentBufferData(): - def BufferModified( buffer_object ): - return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) +def BufferModified( buffer_object ): + return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) +def GetUnsavedAndCurrentBufferData(): buffers_data = {} for buffer_object in vim.buffers: if not ( BufferModified( buffer_object ) or @@ -244,7 +245,12 @@ def JumpToLocation( filename, line, column ): # location, not to the start of the newly opened file. # Sadly this fails on random occasions and the undesired jump remains in the # jumplist. - vim.command( 'keepjumps edit {0}'.format( filename ) ) + if ( user_options_store.Value( 'goto_same_buffer' ) and + not BufferModified( vim.current.buffer ) ): + vim.command( 'keepjumps edit {0}'.format( filename ) ) + else: + vim.command( 'keepjumps {0} {1}'.format( user_options_store.Value( 'goto_buffer_command'), + filename ) ) vim.current.window.cursor = ( line, column - 1 ) # Center the screen on the jumped-to location From 8c6857bf913e6289cda3c76310858d7db82afe96 Mon Sep 17 00:00:00 2001 From: davits Date: Mon, 3 Mar 2014 10:26:06 +0400 Subject: [PATCH 2/5] missing space --- python/ycm/vimsupport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 9c86b3c9..a91422f6 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -249,7 +249,7 @@ def JumpToLocation( filename, line, column ): not BufferModified( vim.current.buffer ) ): vim.command( 'keepjumps edit {0}'.format( filename ) ) else: - vim.command( 'keepjumps {0} {1}'.format( user_options_store.Value( 'goto_buffer_command'), + vim.command( 'keepjumps {0} {1}'.format( user_options_store.Value( 'goto_buffer_command' ), filename ) ) vim.current.window.cursor = ( line, column - 1 ) From 40dc235136b8a043fbd84aed0a133d8d84643cf1 Mon Sep 17 00:00:00 2001 From: davits Date: Tue, 4 Mar 2014 14:47:43 +0400 Subject: [PATCH 3/5] Combined same_buffer and buffer_command options. --- README.md | 18 +++++------------- plugin/youcompleteme.vim | 9 +-------- python/ycm/vimsupport.py | 16 ++++++++++------ 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 7a4c3c01..eb77991f 100644 --- a/README.md +++ b/README.md @@ -1336,22 +1336,14 @@ Default: `1` let g:ycm_use_ultisnips_completer = 1 -### The `g:ycm_goto_same_buffer` option - -Indicates whether GoTo command result should be opened in the current buffer. -However if current buffer has unsaved modifications then this option will be ignored and result will be opened in the new buffer created with `g:ycm_goto_buffer_command` command. - -Default: `1` - - let g:ycm_goto_same_buffer = 1 - ### The `g:ycm_goto_buffer_command` option -Defines command for the new buffer creation where GoTo command result will be opened. -Value should be one of the following vim commands `[ 'sp[lit]', 'vs[plit]', 'tabe[dit]' ]` +Defines where `GoTo*` commands result should be opened. +Can take one of the following values: `[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]` +If this option is set to `'same-buffer'` but current buffer is modified then result will be opened in horizontal split. -Default: `split` - let g:ycm_goto_buffer_command = 'split' +Default: `'same-buffer'` + let g:ycm_goto_buffer_command = 'same-buffer' FAQ --- diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 8118a2fa..fd88aa29 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -144,15 +144,8 @@ let g:ycm_warning_symbol = \ get( g:, 'ycm_warning_symbol', \ get( g:, 'syntastic_warning_symbol', '>>' ) ) -let g:ycm_goto_same_buffer = - \ get( g:, 'ycm_goto_same_buffer', 1 ) - let g:ycm_goto_buffer_command = - \ get( g:, 'ycm_goto_buffer_command', 'split' ) - -if index( [ 'sp', 'split', 'vs', 'vsplit', 'tabe', 'tabedit' ], g:ycm_goto_buffer_command ) < 0 - let g:ycm_goto_buffer_command = 'split' -endif + \ get( g:, 'ycm_goto_buffer_command', 'same-buffer' ) " On-demand loading. Let's use the autoload folder and not slow down vim's " startup procedure. diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index a91422f6..9bf5454a 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -23,6 +23,11 @@ import json from ycm.utils import ToUtf8IfNeeded from ycm import user_options_store +BUFFER_COMMAND_MAP = { 'same-buffer' : 'edit', + 'horizontal-split' : 'split', + 'vertical-split' : 'vsplit', + 'new-tab' : 'tabedit' } + def CurrentLineAndColumn(): """Returns the 0-based current line and 0-based current column.""" # See the comment in CurrentColumn about the calculation for the line and @@ -245,12 +250,11 @@ def JumpToLocation( filename, line, column ): # location, not to the start of the newly opened file. # Sadly this fails on random occasions and the undesired jump remains in the # jumplist. - if ( user_options_store.Value( 'goto_same_buffer' ) and - not BufferModified( vim.current.buffer ) ): - vim.command( 'keepjumps edit {0}'.format( filename ) ) - else: - vim.command( 'keepjumps {0} {1}'.format( user_options_store.Value( 'goto_buffer_command' ), - filename ) ) + user_command = user_options_store.Value( 'goto_buffer_command' ) + command = BUFFER_COMMAND_MAP.get( user_command, 'edit' ) + if command == 'edit' and BufferModified( vim.current.buffer ): + command = 'split' + vim.command( 'keepjumps {0} {1}'.format( command, filename ) ) vim.current.window.cursor = ( line, column - 1 ) # Center the screen on the jumped-to location From b9109af709a0fa005ee8099c4add015dbfbc06c4 Mon Sep 17 00:00:00 2001 From: Davit Samvelyan Date: Sat, 22 Mar 2014 14:24:16 +0400 Subject: [PATCH 4/5] Added check for Vim 'hidden' option when trying to open result in the same buffer --- README.md | 7 +++++-- python/ycm/vimsupport.py | 13 +++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eb77991f..702328a7 100644 --- a/README.md +++ b/README.md @@ -1339,8 +1339,11 @@ Default: `1` ### The `g:ycm_goto_buffer_command` option Defines where `GoTo*` commands result should be opened. -Can take one of the following values: `[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]` -If this option is set to `'same-buffer'` but current buffer is modified then result will be opened in horizontal split. +Can take one of the following values: +`[ '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'` let g:ycm_goto_buffer_command = 'same-buffer' diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 9bf5454a..65fc5618 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -72,7 +72,8 @@ def GetBufferOption( buffer_object, option ): def BufferModified( buffer_object ): - return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) + return buffer_object.options[ 'mod' ] + def GetUnsavedAndCurrentBufferData(): buffers_data = {} @@ -238,6 +239,14 @@ def VimExpressionToPythonType( vim_expression ): 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 def JumpToLocation( filename, line, column ): # Add an entry to the jumplist @@ -252,7 +261,7 @@ def JumpToLocation( filename, line, column ): # jumplist. user_command = user_options_store.Value( 'goto_buffer_command' ) 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' vim.command( 'keepjumps {0} {1}'.format( command, filename ) ) vim.current.window.cursor = ( line, column - 1 ) From b4b94429ddfe445cd75c0740c9216bfaa425f399 Mon Sep 17 00:00:00 2001 From: Davit Samvelyan Date: Sun, 23 Mar 2014 12:33:27 +0400 Subject: [PATCH 5/5] reverted back to the GetBufferOption --- python/ycm/vimsupport.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 65fc5618..6782c8f6 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -72,7 +72,7 @@ def GetBufferOption( buffer_object, option ): def BufferModified( buffer_object ): - return buffer_object.options[ 'mod' ] + return bool( int( GetBufferOption( buffer_object, 'mod' ) ) ) def GetUnsavedAndCurrentBufferData(): @@ -240,7 +240,7 @@ def VimExpressionToPythonType( vim_expression ): def HiddenEnabled( buffer_object ): - return vim.options[ 'hid' ] + return bool( int( GetBufferOption( buffer_object, 'hid' ) ) ) def BufferIsUsable( buffer_object ):