Tidy up the buffer append logic

Vim seems to insist that a buffer has at least one line. This means that
the first line set in the buffer object has to be a special case. Sigh.
Create a method to wrap that up.
This commit is contained in:
Ben Jackson 2018-06-03 17:16:20 +01:00
commit df10cd84cf
4 changed files with 46 additions and 19 deletions

View file

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

View file

@ -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': '<unknown>' }
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

View file

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

View file

@ -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', '<unknown type>' ),
value = variable.get( 'value', '<unknown 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