diff --git a/python3/vimspector/output.py b/python3/vimspector/output.py index c74fe3e..cb91e51 100644 --- a/python3/vimspector/output.py +++ b/python3/vimspector/output.py @@ -32,7 +32,8 @@ class OutputView( object ): self._CreateBuffer( category ) with utils.ModifiableScratchBuffer( self._buffers[ category ] ): - self._buffers[ category ].append( event[ 'output' ].splitlines() ) + utils.AppendToBuffer( self._buffers[ category ], + event[ 'output' ].splitlines() ) def Reset( self ): self.Clear() @@ -54,7 +55,8 @@ class OutputView( object ): with utils.RestoreCurrentBuffer( self._window ): vim.command( 'enew' ) self._buffers[ category ] = vim.current.buffer - self._buffers[ category ].append( category + '-----' ) + utils.AppendToBuffer( self._buffers[ category ], + category + '-----' ) utils.SetUpHiddenBuffer( self._buffers[ category ], 'vimspector.Output:{0}'.format( category ) ) diff --git a/python3/vimspector/stack_trace.py b/python3/vimspector/stack_trace.py index ae65db4..a71cef9 100644 --- a/python3/vimspector/stack_trace.py +++ b/python3/vimspector/stack_trace.py @@ -93,8 +93,11 @@ class StackTraceView( object ): for thread in self._threads: icon = '+' if '_frames' not in thread else '-' - self._buf.append( '{0} Thread: {1}'.format( icon, thread[ 'name' ] ) ) - self._line_to_thread[ len( self._buf ) ] = thread + line = utils.AppendToBuffer( + self._buf, + '{0} Thread: {1}'.format( icon, thread[ 'name' ] ) ) + + self._line_to_thread[ line ] = thread self._DrawStackTrace( thread ) @@ -193,9 +196,10 @@ class StackTraceView( object ): else: source = { 'name': '' } - self._buf.append( + line = utils.AppendToBuffer( + self._buf, ' {0}: {1}@{2}:{3}'.format( frame[ 'id' ], frame[ 'name' ], source[ 'name' ], frame[ 'line' ] ) ) - self._line_to_frame[ len( self._buf ) ] = frame + self._line_to_frame[ line ] = frame diff --git a/python3/vimspector/utils.py b/python3/vimspector/utils.py index 93f2a94..e102063 100644 --- a/python3/vimspector/utils.py +++ b/python3/vimspector/utils.py @@ -24,6 +24,7 @@ _log_handler = logging.FileHandler( os.path.expanduser( '~/.vimspector.log' ) ) _log_handler.setFormatter( logging.Formatter( '%(asctime)s - %(levelname)s - %(message)s' ) ) + def SetUpLogging( logger ): logger.setLevel( logging.DEBUG ) if _log_handler not in logger.handlers: @@ -171,3 +172,20 @@ def SelectFromList( prompt, options ): def AskForInput( prompt ): with InputSave(): return vim.eval( "input( '{0}' )".format( Escape( prompt ) ) ) + + +def AppendToBuffer( buf, line_or_lines ): + # After clearing the buffer (using buf[:] = None) there is always a single + # empty line in the buffer object and no "is empty" method. + if len( buf ) > 1 or buf[ 0 ]: + line = len( buf ) + 1 + buf.append( line_or_lines ) + elif isinstance( line_or_lines, str ): + line = 1 + buf[-1] = line_or_lines + else: + line = 1 + buf[:] = line_or_lines + + # Return the first Vim line number (1-based) that we just set. + return line diff --git a/python3/vimspector/variables.py b/python3/vimspector/variables.py index bc88cf8..2ed284d 100644 --- a/python3/vimspector/variables.py +++ b/python3/vimspector/variables.py @@ -21,6 +21,7 @@ from vimspector import utils View = namedtuple( 'View', [ 'win', 'lines', 'draw' ] ) + class VariablesView( object ): def __init__( self, connection, variables_win, watches_win ): self._vars = View( variables_win, {}, self._DrawScopes ) @@ -190,8 +191,8 @@ class VariablesView( object ): def _DrawVariables( self, view, variables, indent ): for variable in variables: - view.lines[ len( view.win.buffer ) + 1 ] = variable - view.win.buffer.append( + line = utils.AppendToBuffer( + view.win.buffer, '{indent}{icon} {name} ({type_}): {value}'.format( indent = ' ' * indent, icon = '+' if ( variable[ 'variablesReference' ] > 0 and @@ -199,6 +200,7 @@ class VariablesView( object ): name = variable[ 'name' ], type_ = variable.get( 'type', '' ), value = variable.get( 'value', '' ) ).split( '\n' ) ) + view.lines[ line ] = variable if '_variables' in variable: self._DrawVariables( view, variable[ '_variables' ], indent + 2 ) @@ -216,22 +218,22 @@ class VariablesView( object ): with utils.RestoreCursorPosition(): with utils.ModifiableScratchBuffer( self._watch.win.buffer ): self._watch.win.buffer[:] = None - self._watch.win.buffer.append( 'Watches: ----' ) + utils.AppendToBuffer( self._watch.win.buffer, 'Watches: ----' ) for watch in self._watches: - self._watch.win.buffer.append( - 'Expression: ' + watch[ 'expression' ] ) - watch[ '_line' ] = len( self._watch.win.buffer ) + line = utils.AppendToBuffer( self._watch.win.buffer, + 'Expression: ' + watch[ 'expression' ] ) + watch[ '_line' ] = line self._DrawWatchResult( 2, watch ) def _DrawScope( self, indent, scope ): icon = '+' if ( scope[ 'variablesReference' ] > 0 and '_variables' not in scope ) else '-' - self._vars.lines[ len( self._vars.win.buffer ) + 1 ] = scope - self._vars.win.buffer.append( '{0}{1} Scope: {2}'.format( - ' ' * indent, - icon, - scope[ 'name' ] ) ) + line = utils.AppendToBuffer( self._vars.win.buffer, + '{0}{1} Scope: {2}'.format( ' ' * indent, + icon, + scope[ 'name' ] ) ) + self._vars.lines[ line ] = scope if '_variables' in scope: indent += 2 @@ -242,7 +244,6 @@ class VariablesView( object ): return result = watch[ '_result' ] - self._watch.lines[ len( self._watch.win.buffer ) + 1 ] = result icon = '+' if ( result[ 'variablesReference' ] > 0 and '_variables' not in result ) else '-' @@ -250,7 +251,9 @@ class VariablesView( object ): line = '{0}{1} Result: {2} '.format( ' ' * indent, icon, result[ 'result' ] ) - self._watch.win.buffer.append( line.split( '\n' ) ) + line = utils.AppendToBuffer( self._watch.win.buffer, + line.split( '\n' ) ) + self._watch.lines[ line ] = result if '_variables' in result: indent = 4