From 0938d72a8c3097d2b83b8e7e5f7d9cd35c87edcd Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Sat, 11 Jul 2020 22:00:36 +0100 Subject: [PATCH] Re-use a window if we can, as it's more efficient; don't wipe out the generated code buffers in case the code window gets used for temporary buffer switch --- python3/vimspector/stack_trace.py | 9 ++++++++- python3/vimspector/utils.py | 28 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/python3/vimspector/stack_trace.py b/python3/vimspector/stack_trace.py index 4504d10..0afeb81 100644 --- a/python3/vimspector/stack_trace.py +++ b/python3/vimspector/stack_trace.py @@ -35,6 +35,7 @@ class StackTraceView( object ): self._threads = [] self._sources = {} + self._scratch_buffers = [] utils.SetUpHiddenBuffer( self._buf, 'vimspector.StackTrace' ) @@ -82,6 +83,11 @@ class StackTraceView( object ): def Reset( self ): self.Clear() utils.CleanUpHiddenBuffer( self._buf ) + for b in self._scratch_buffers: + utils.CleanUpHiddenBuffer( b ) + + self._scratch_buffers = [] + self._buf = None def LoadThreads( self, infer_current_frame ): pending_request = False @@ -290,7 +296,8 @@ class StackTraceView( object ): self._logger.debug( "Received source %s: %s", buf_name, msg ) buf = utils.BufferForFile( buf_name ) - utils.SetUpScratchBuffer( buf, buf_name ) + self._scratch_buffers.append( buf ) + utils.SetUpHiddenBuffer( buf, buf_name ) source[ 'path' ] = buf_name with utils.ModifiableScratchBuffer( buf ): utils.SetBufferContents( buf, msg[ 'body' ][ 'content' ] ) diff --git a/python3/vimspector/utils.py b/python3/vimspector/utils.py index 03dbb4d..9fc1206 100644 --- a/python3/vimspector/utils.py +++ b/python3/vimspector/utils.py @@ -52,6 +52,14 @@ def BufferForFile( file_name ): return vim.buffers[ BufferNumberForFile( file_name ) ] +def WindowForBuffer( buf ): + for w in vim.current.tabpage.windows: + if w.buffer == buf: + return w + + return None + + def OpenFileInCurrentWindow( file_name ): buffer_number = BufferNumberForFile( file_name ) try: @@ -190,6 +198,18 @@ def RestoreCurrentBuffer( window ): vim.current.buffer = old_buffer +@contextlib.contextmanager +def AnyWindowForBuffer( buf ): + # Only checks the current tab page, which is what we want + current_win = WindowForBuffer( buf ) + if current_win is not None: + with LetCurrentWindow( current_win ): + yield + else: + with LetCurrentBuffer( buf ): + yield + + @contextlib.contextmanager def LetCurrentWindow( window ): with RestoreCurrentWindow(): @@ -566,11 +586,11 @@ def SetSyntax( current_syntax, syntax, *args ): if current_syntax == syntax: return + # We use set syn= because just setting vim.Buffer.options[ 'syntax' ] + # doesn't actually trigger the Syntax autocommand, and i'm not sure that + # 'doautocmd Syntax' is the right solution or not for buf in args: - with LetCurrentBuffer( buf ): - # We use set syn= because just setting vim.Buffer.options[ 'syntax' ] - # doesn't actually trigger the Syntax autocommand, and i'm not sure that - # 'doautocmd Syntax' is the right solution or not + with AnyWindowForBuffer( buf ): vim.command( 'set syntax={}'.format( Escape( syntax ) ) ) return syntax