Move the vimscript into vimscript; it's so much easier. Support multiple commands and jobs

This commit is contained in:
Ben Jackson 2019-02-21 16:12:58 +00:00
commit 75851e0652
4 changed files with 128 additions and 55 deletions

View file

@ -19,6 +19,7 @@ import json
import os
import functools
import subprocess
import shlex
from collections import defaultdict
@ -430,6 +431,10 @@ class DebugSession( object ):
assert not self._run_on_server_exit
self._run_on_server_exit = callback
arguments = {}
if self._server_capabilities.get( 'supportTerminateDebuggee' ):
arguments[ 'terminateDebugee' ] = True
self._connection.DoRequest( handler, {
'command': 'disconnect',
'arguments': {
@ -439,9 +444,15 @@ class DebugSession( object ):
def _PrepareAttach( self, adapter_config, launch_config ):
atttach_config = adapter_config[ 'attach' ]
atttach_config = adapter_config.get( 'attach' )
if not atttach_config:
return
if 'remote' in atttach_config:
# FIXME: We almost want this to feed-back variables to be expanded later,
# e.g. expand variables when we use them, not all at once. This would
# remove the whole %PID% hack.
remote = atttach_config[ 'remote' ]
ssh = [ 'ssh' ]
@ -462,13 +473,16 @@ class DebugSession( object ):
utils.UserMessage( 'Unable to get PID', persist = True )
return
cmd = ssh + remote[ 'attachCommand' ][:]
commands = self._GetCommands( remote, 'attach' )
for index, item in enumerate( cmd ):
cmd[ index ] = item.replace( '%PID%', pid )
for command in commands:
cmd = ssh + command[:]
self._logger.debug( 'Running remote app: %s', cmd )
self._outputView.RunJobWithOutput( 'Remote', cmd )
for index, item in enumerate( cmd ):
cmd[ index ] = item.replace( '%PID%', pid )
self._logger.debug( 'Running remote app: %s', cmd )
self._outputView.RunJobWithOutput( 'Remote', cmd )
else:
if atttach_config[ 'pidSelect' ] == 'ask':
pid = utils.AskForInput( 'Enter PID to attach to: ' )
@ -481,6 +495,7 @@ class DebugSession( object ):
atttach_config[ 'pidSelect' ] ) )
def _PrepareLaunch( self, command_line, adapter_config, launch_config ):
run_config = adapter_config.get( 'launch', {} )
@ -492,21 +507,46 @@ class DebugSession( object ):
else:
ssh.append( remote[ 'host' ] )
cmd = ssh + remote[ 'runCommand' ][:]
full_cmd = []
for item in cmd:
if isinstance( command_line, list ):
if item == '%CMD%':
full_cmd.extend( command_line )
commands = self._GetCommands( remote, 'run' )
for index, command in enumerate( commands ):
cmd = ssh + command[:]
full_cmd = []
for item in cmd:
if isinstance( command_line, list ):
if item == '%CMD%':
full_cmd.extend( command_line )
else:
full_cmd.append( item )
else:
full_cmd.append( item )
else:
full_cmd.append( item.replace( '%CMD%', command_line ) )
full_cmd.append( item.replace( '%CMD%', command_line ) )
self._logger.debug( 'Running remote app: %s', full_cmd )
self._outputView.RunJobWithOutput( 'Remote', full_cmd )
self._logger.debug( 'Running remote app: %s', full_cmd )
self._outputView.RunJobWithOutput( 'Remote{}'.format( index ),
full_cmd )
def _GetCommands( self, remote, pfx ):
commands = remote.get( pfx + 'Commands', None )
if isinstance( commands, list ):
return commands
elif commands is not None:
raise ValueError( "Invalid commands; must be list" )
commands = remote[ pfx + 'Command' ]
if isinstance( commands, str ):
commands = shlex.split( commands )
if not isinstance( commands, list ):
raise ValueError( "Invalid command; must be list/string" )
if not commands:
raise ValueError( 'Could not determine commands for ' + pfx )
return commands
def _Initialise( self ):
adapter_config = self._adapter
self._connection.DoRequest( lambda msg: self._Launch(), {

View file

@ -96,7 +96,12 @@ class OutputView( object ):
for category, tab_buffer in self._buffers.items():
if tab_buffer.is_job:
utils.CleanUpCommand( category )
vim.command( 'bdelete! {0}'.format( tab_buffer.buf.number ) )
try:
vim.command( 'bdelete! {0}'.format( tab_buffer.buf.number ) )
except vim.error as e:
# FIXME: For now just ignore the "no buffers were deleted" error
if 'E516' not in e:
raise
self._buffers.clear()
@ -159,10 +164,15 @@ class OutputView( object ):
cmd = [ 'tail', '-F', '-n', '+1', '--', file_name ]
if cmd is not None:
buf = utils.SetUpCommandBuffer( cmd, category )
self._buffers[ category ] = TabBuffer( buf, len( self._buffers ) )
self._buffers[ category ].is_job = True
self._RenderWinBar( category )
out, err = utils.SetUpCommandBuffer( cmd, category )
self._buffers[ category + '-out' ] = TabBuffer( out,
len( self._buffers ) )
self._buffers[ category + '-out' ].is_job = True
self._buffers[ category + '-err' ] = TabBuffer( err,
len( self._buffers ) )
self._buffers[ category + '-err' ].is_job = False
self._RenderWinBar( category + '-out' )
self._RenderWinBar( category + '-err' )
else:
vim.command( 'enew' )
tab_buffer = TabBuffer( vim.current.buffer, len( self._buffers ) )

View file

@ -53,43 +53,17 @@ def OpenFileInCurrentWindow( file_name ):
def SetUpCommandBuffer( cmd, name ):
vim.command(
'let g:vimspector_command_job_{name} = job_start('
' {cmd},'
' {{'
' "out_io": "buffer",'
' "in_io": "null",'
' "err_io": "buffer",'
' "out_name": "_vimspector_log_{name}",'
' "err_name": "_vimspector_log_{name}",'
' "out_modifiable": 0,'
' "err_modifiable": 0,'
' "stoponexit": "term",'
' }} )'.format( name = name,
cmd = json.dumps( cmd ) ) )
bufs = vim.bindeval(
'vimspector#internal#job#StartCommandWithLog( {}, "{}" )'.format(
json.dumps( cmd ),
name ) )
stdout = vim.eval( 'ch_getbufnr( '
' job_getchannel( g:vimspector_command_job_{name} ), '
' "out"'
')'.format( name = name ) )
stderr = vim.eval( 'ch_getbufnr( '
' job_getchannel( g:vimspector_command_job_{name} ), '
' "err"'
')'.format( name = name ) )
assert stdout == stderr
return vim.buffers[ int( stdout ) ]
return ( vim.buffers[ bufnr ] for bufnr in bufs )
def CleanUpCommand( name ):
cmd = 'job_stop( g:vimspector_command_job_{name}, "kill" )'.format(
name = name )
vim.eval( cmd )
def TerminateJob( job ):
if vim.eval( 'job_status( {} )'.format( job ) ) == 'run':
vim.eval( 'job_stop( {} )'.format( job ) )
return vim.eval( 'vimspector#internal#job#CleanUpCommand( "{}" )'.format(
name ) )
def SetUpScratchBuffer( buf, name ):