Allow for variable expansion in configuration

We expand environment variables, ~ and workspaceRoot. The latter is to
match vscode and typical launch configs. As we don't know what the
workspeace root might be, we use the location of the .vimspector.json.
This commit is contained in:
Ben Jackson 2018-06-10 23:31:20 +01:00
commit 79b25c0860
2 changed files with 32 additions and 0 deletions

View file

@ -133,9 +133,17 @@ class DebugSession( object ):
if not configuration: if not configuration:
return return
utils.ExpandReferencesInDict( launch_config[ configuration ], {
'workspaceRoot': os.path.dirname( launch_config_file )
} )
adapter = launch_config[ configuration ].get( 'adapter' ) adapter = launch_config[ configuration ].get( 'adapter' )
if isinstance( adapter, str ): if isinstance( adapter, str ):
adapter = adapters.get( adapter ) adapter = adapters.get( adapter )
utils.ExpandReferencesInDict( adapter, {
'workspaceRoot': os.path.dirname( launch_config_file )
} )
self._StartWithConfiguration( launch_config[ configuration ], self._StartWithConfiguration( launch_config[ configuration ],
adapter ) adapter )
@ -144,6 +152,11 @@ class DebugSession( object ):
self._configuration = configuration self._configuration = configuration
self._adapter = adapter self._adapter = adapter
self._logger.info( 'Configuration: {0}'.format( json.dumps(
self._configuration ) ) )
self._logger.info( 'Adapter: {0}'.format( json.dumps(
self._adapter ) ) )
def start(): def start():
self._StartDebugAdapter() self._StartDebugAdapter()
self._Initialise() self._Initialise()

View file

@ -19,6 +19,7 @@ import os
import contextlib import contextlib
import vim import vim
import json import json
import string
_log_handler = logging.FileHandler( os.path.expanduser( '~/.vimspector.log' ) ) _log_handler = logging.FileHandler( os.path.expanduser( '~/.vimspector.log' ) )
_log_handler.setFormatter( _log_handler.setFormatter(
@ -222,3 +223,21 @@ def ClearBuffer( buf ):
def IsCurrent( window, buf ): def IsCurrent( window, buf ):
return vim.current.window == window and vim.current.window.buffer == buf return vim.current.window == window and vim.current.window.buffer == buf
def ExpandReferencesInDict( obj, mapping, **kwargs ):
def expand_refs( s ):
UserMessage( type( s ), persist=True )
s = string.Template( s ).safe_substitute( mapping, **kwargs )
s = os.path.expanduser( s )
s = os.path.expandvars( s )
return s
for k in obj.keys():
if isinstance( obj[ k ], dict ):
ExpandReferencesInDict( obj[ k ], mapping, **kwargs )
elif isinstance( obj[ k ], list ):
for i, _ in enumerate( obj[ k ] ):
obj[ k ][ i ] = expand_refs( obj[ k ][ i ] )
elif isinstance( obj[ k ], str ):
obj[ k ] = expand_refs( obj[ k ] )