Quellcode durchsuchen

Auto merge of #3229 - micbou:keep-previous-window, r=puremourning

[READY] Preserve previous window when updating matches

Ensure `<c-w>p` still go to the previous window after updating matches for the current buffer.

Fixes #3228.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/valloric/youcompleteme/3229)
<!-- Reviewable:end -->
zzbot vor 6 Jahren
Ursprung
Commit
f2d999bbb1
2 geänderte Dateien mit 24 neuen und 5 gelöschten Zeilen
  1. 17 5
      python/ycm/tests/test_utils.py
  2. 7 0
      python/ycm/vimsupport.py

+ 17 - 5
python/ycm/tests/test_utils.py

@@ -178,6 +178,14 @@ def _MockVimBufferEval( value ):
   return None
   return None
 
 
 
 
+def _MockVimWindowEval( value ):
+  if value == 'winnr("#")':
+    # For simplicity, we always assume there is no previous window.
+    return 0
+
+  return None
+
+
 def _MockVimOptionsEval( value ):
 def _MockVimOptionsEval( value ):
   result = VIM_OPTIONS.get( value )
   result = VIM_OPTIONS.get( value )
   if result is not None:
   if result is not None:
@@ -266,6 +274,10 @@ def _MockVimEval( value ):
   if result is not None:
   if result is not None:
     return result
     return result
 
 
+  result = _MockVimWindowEval( value )
+  if result is not None:
+    return result
+
   result = _MockVimMatchEval( value )
   result = _MockVimMatchEval( value )
   if result is not None:
   if result is not None:
     return result
     return result
@@ -485,10 +497,10 @@ class VimWindows( object ):
 
 
   def __getitem__( self, number ):
   def __getitem__( self, number ):
     """Emulates vim.windows[ number ]"""
     """Emulates vim.windows[ number ]"""
-    for window in self._windows:
-      if number == window.number:
-        return window
-    raise KeyError( number )
+    try:
+      return self._windows[ number ]
+    except IndexError:
+      raise IndexError( 'no such window' )
 
 
 
 
   def __iter__( self ):
   def __iter__( self ):
@@ -581,7 +593,7 @@ def MockVimBuffers( buffers, window_buffers, cursor_position = ( 1, 1 ) ):
   with patch( 'vim.buffers', VimBuffers( buffers ) ):
   with patch( 'vim.buffers', VimBuffers( buffers ) ):
     with patch( 'vim.windows', VimWindows( window_buffers,
     with patch( 'vim.windows', VimWindows( window_buffers,
                                            cursor_position ) ) as windows:
                                            cursor_position ) ) as windows:
-      with patch( 'vim.current', VimCurrent( windows[ 1 ] ) ):
+      with patch( 'vim.current', VimCurrent( windows[ 0 ] ) ):
         yield VIM_MOCK
         yield VIM_MOCK
 
 
 
 

+ 7 - 0
python/ycm/vimsupport.py

@@ -1237,16 +1237,23 @@ def AutocommandEventsIgnored( events = [ 'all' ] ):
     vim.options[ 'eventignore' ] = old_eventignore
     vim.options[ 'eventignore' ] = old_eventignore
 
 
 
 
+def GetPreviousWindowNumber():
+  return GetIntValue( 'winnr("#")' ) - 1
+
+
 @contextlib.contextmanager
 @contextlib.contextmanager
 def CurrentWindow():
 def CurrentWindow():
   """Context manager to perform operations on other windows than the current one
   """Context manager to perform operations on other windows than the current one
   without triggering autocommands related to window movement. Use the
   without triggering autocommands related to window movement. Use the
   SwitchWindow function to move to other windows while under the context."""
   SwitchWindow function to move to other windows while under the context."""
+  previous_window = vim.windows[ GetPreviousWindowNumber() ]
   current_window = vim.current.window
   current_window = vim.current.window
   with AutocommandEventsIgnored( [ 'WinEnter', 'Winleave' ] ):
   with AutocommandEventsIgnored( [ 'WinEnter', 'Winleave' ] ):
     try:
     try:
       yield
       yield
     finally:
     finally:
+      # Ensure <c-w>p still go to the previous window.
+      vim.current.window = previous_window
       vim.current.window = current_window
       vim.current.window = current_window