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

This commit is contained in:
Ben Jackson 2020-07-11 22:00:36 +01:00
commit 0938d72a8c
2 changed files with 32 additions and 5 deletions

View file

@ -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