diff --git a/README.md b/README.md index 60b6ce4d..924dd91e 100644 --- a/README.md +++ b/README.md @@ -1579,7 +1579,8 @@ Default: `1` Defines where `GoTo*` commands result should be opened. Can take one of the following values: -`[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab' ]` +`[ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab', + 'new-or-existing-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. diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index fff54f4f..d6b5d91d 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -301,6 +301,25 @@ def EscapedFilepath( filepath ): return filepath.replace( ' ' , r'\ ' ) +# Both |line| and |column| need to be 1-based +def TryJumpLocationInOpenedTab( filename, line, column ): + filepath = os.path.realpath( filename ) + + for tab in vim.tabpages: + for win in tab.windows: + if win.buffer.name == filepath: + vim.current.tabpage = tab + vim.current.window = win + vim.current.window.cursor = ( line, column - 1 ) + + # Center the screen on the jumped-to location + vim.command( 'normal! zz' ) + return + else: + # 'filename' is not opened in any tab pages + raise ValueError + + # Both |line| and |column| need to be 1-based def JumpToLocation( filename, line, column ): # Add an entry to the jumplist @@ -314,6 +333,14 @@ def JumpToLocation( filename, line, column ): # Sadly this fails on random occasions and the undesired jump remains in the # jumplist. user_command = user_options_store.Value( 'goto_buffer_command' ) + + if user_command == 'new-or-existing-tab': + try: + TryJumpLocationInOpenedTab( filename, line, column ) + return + except ValueError: + user_command = 'new-tab' + command = BUFFER_COMMAND_MAP.get( user_command, 'edit' ) if command == 'edit' and not BufferIsUsable( vim.current.buffer ): command = 'split'