diff --git a/python3/vimspector/code.py b/python3/vimspector/code.py index d8f1eac..a69cfcf 100644 --- a/python3/vimspector/code.py +++ b/python3/vimspector/code.py @@ -35,18 +35,17 @@ class CodeView( object ): 'breakpoints': [] } - vim.current.window = self._window + with utils.LetCurrentWindow( self._window ): + vim.command( 'nnoremenu WinBar.Continue :call vimspector#Continue()' ) + vim.command( 'nnoremenu WinBar.Next :call vimspector#StepOver()' ) + vim.command( 'nnoremenu WinBar.Step :call vimspector#StepInto()' ) + vim.command( 'nnoremenu WinBar.Finish :call vimspector#StepOut()' ) + vim.command( 'nnoremenu WinBar.Pause :call vimspector#Pause()' ) + vim.command( 'nnoremenu WinBar.Stop :call vimspector#Stop()' ) + vim.command( 'nnoremenu WinBar.Restart :call vimspector#Restart()' ) + vim.command( 'nnoremenu WinBar.Reset :call vimspector#Reset()' ) - vim.command( 'nnoremenu WinBar.Continue :call vimspector#Continue()' ) - vim.command( 'nnoremenu WinBar.Next :call vimspector#StepOver()' ) - vim.command( 'nnoremenu WinBar.Step :call vimspector#StepInto()' ) - vim.command( 'nnoremenu WinBar.Finish :call vimspector#StepOut()' ) - vim.command( 'nnoremenu WinBar.Pause :call vimspector#Pause()' ) - vim.command( 'nnoremenu WinBar.Stop :call vimspector#Stop()' ) - vim.command( 'nnoremenu WinBar.Restart :call vimspector#Restart()' ) - vim.command( 'nnoremenu WinBar.Reset :call vimspector#Reset()' ) - - vim.command( 'sign define vimspectorPC text=-> texthl=Search' ) + vim.command( 'sign define vimspectorPC text=-> texthl=Search' ) def SetCurrentFrame( self, frame ): @@ -61,7 +60,7 @@ class CodeView( object ): if 'path' not in frame[ 'source' ]: return False - vim.current.window = self._window + utils.JumpToWindow( self._window ) try: utils.OpenFileInCurrentWindow( frame[ 'source' ][ 'path' ] ) @@ -210,8 +209,7 @@ class CodeView( object ): buffer_number = None with utils.TemporaryVimOptions( { 'splitright': True, 'equalalways': False } ): - with utils.RestoreCurrentWindow(): - vim.current.window = self._window + with utils.LetCurrentWindow( self._window ): # TODO/FIXME: Do something about closing this when we reset ? vim_cmd = 'term_start( {}, {} )'.format( json.dumps( args ), json.dumps( options ) ) diff --git a/python3/vimspector/output.py b/python3/vimspector/output.py index 6ca0971..00faec2 100644 --- a/python3/vimspector/output.py +++ b/python3/vimspector/output.py @@ -106,7 +106,7 @@ class OutputView( object ): self._buffers.clear() def _ShowOutput( self, category ): - vim.current.window = self._window + utils.JumpToWindow( self._window ) vim.command( 'bu {0}'.format( self._buffers[ category ].buf.name ) ) vim.command( 'normal G' ) @@ -144,8 +144,7 @@ class OutputView( object ): def _ToggleFlag( self, category, flag ): if self._buffers[ category ].flag != flag: self._buffers[ category ].flag = flag - with utils.RestoreCurrentWindow(): - vim.current.window = self._window + with utils.LetCurrentWindow( self._window ): self._RenderWinBar( category ) @@ -154,9 +153,7 @@ class OutputView( object ): def _CreateBuffer( self, category, file_name = None, cmd = None ): - with utils.RestoreCurrentWindow(): - vim.current.window = self._window - + with utils.LetCurrentWindow( self._window ): with utils.RestoreCurrentBuffer( self._window ): if file_name is not None: diff --git a/python3/vimspector/utils.py b/python3/vimspector/utils.py index 6ef51e1..06f7dbe 100644 --- a/python3/vimspector/utils.py +++ b/python3/vimspector/utils.py @@ -147,11 +147,13 @@ def RestoreCursorPosition(): @contextlib.contextmanager def RestoreCurrentWindow(): - # TODO: Don't trigger autoccommands when shifting windows + # TODO: Don't trigger autocommands when shifting windows + old_tabpage = vim.current.tabpage old_window = vim.current.window try: yield finally: + vim.current.tabpage = old_tabpage vim.current.window = old_window @@ -167,6 +169,18 @@ def RestoreCurrentBuffer( window ): vim.current.buffer = old_buffer +@contextlib.contextmanager +def LetCurrentWindow( window ): + with RestoreCurrentWindow(): + JumpToWindow( window ) + yield + + +def JumpToWindow( window ): + vim.current.tabpage = window.tabpage + vim.current.window = window + + @contextlib.contextmanager def TemporaryVimOptions( opts ): old_value = {} diff --git a/python3/vimspector/variables.py b/python3/vimspector/variables.py index ac19e6b..73ceb1c 100644 --- a/python3/vimspector/variables.py +++ b/python3/vimspector/variables.py @@ -33,9 +33,9 @@ class VariablesView( object ): self._connection = connection # Allows us to hit to expand/collapse variables - vim.current.window = self._vars.win - vim.command( - 'nnoremap :call vimspector#ExpandVariable()' ) + with utils.LetCurrentWindow( self._vars.win ): + vim.command( + 'nnoremap :call vimspector#ExpandVariable()' ) # This is actually the tree (scopes are alwyas the root) # it's just a list of DAP scope dicts, with one magic key (_variables) @@ -52,11 +52,11 @@ class VariablesView( object ): self._watches = [] # Allows us to hit to expand/collapse variables - vim.current.window = self._watch.win - vim.command( - 'nnoremap :call vimspector#ExpandVariable()' ) - vim.command( - 'nnoremap :call vimspector#DeleteWatch()' ) + with utils.LetCurrentWindow( self._watch.win ): + vim.command( + 'nnoremap :call vimspector#ExpandVariable()' ) + vim.command( + 'nnoremap :call vimspector#DeleteWatch()' ) utils.SetUpScratchBuffer( self._vars.win.buffer, 'vimspector.Variables' ) utils.SetUpPromptBuffer( self._watch.win.buffer, diff --git a/tests/tabpage.test.vim b/tests/tabpage.test.vim new file mode 100644 index 0000000..f31d62c --- /dev/null +++ b/tests/tabpage.test.vim @@ -0,0 +1,59 @@ +source lib/shared.vim + +function! SetUp() + if exists ( 'g:loaded_vimpector' ) + unlet g:loaded_vimpector + endif + + let g:vimspector_enable_mappings = 'HUMAN' + source vimrc + + " This is a bit of a hack + runtime! plugin/**/*.vim +endfunction + +function! Test_Step_With_Different_Tabpage() + lcd testdata/cpp/simple + edit simple.cpp + + " Add the breakpoing + " TODO refactor FeedKeys + 15 + call assert_equal( 15, line( '.' ) ) + call feedkeys( "\", 'xt' ) + + " Here we go. Start Debugging + call feedkeys( "\", 'xt' ) + + call assert_equal( 2, len( gettabinfo() ) ) + let vimspector_tabnr = tabpagenr() + call WaitForAssert( {-> + \ assert_equal( 'simple.cpp', bufname( '%' ), 'Current buffer' ) + \ }, 10000 ) + call assert_equal( 15, line( '.' ), 'Current line' ) + call assert_equal( 1, col( '.' ), 'Current column' ) + + " Switch to the other tab + normal gt + + call assert_notequal( vimspector_tabnr, tabpagenr() ) + + " trigger some output by hacking into the vimspector python + call py3eval( '_vimspector_session._outputView.Print( "server",' + \ . ' "This is a test" )' ) + + " Step - jumps back to our vimspector tab + call feedkeys( "\", 'xt' ) + + call WaitForAssert( {-> assert_equal( vimspector_tabnr, tabpagenr() ) } ) + call WaitForAssert( {-> assert_equal( 16, line( '.' ), 'Current line' ) } ) + call assert_equal( 'simple.cpp', bufname( '%' ), 'Current buffer' ) + call assert_equal( 1, col( '.' ), 'Current column' ) + + call vimspector#Reset() + call vimspector#ClearBreakpoints() + + lcd - + %bwipeout! +endfunction +