From 79b25c0860c085f3725fa78bac8a018d2fb03c00 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Sun, 10 Jun 2018 23:31:20 +0100 Subject: [PATCH] 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. --- python3/vimspector/debug_session.py | 13 +++++++++++++ python3/vimspector/utils.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/python3/vimspector/debug_session.py b/python3/vimspector/debug_session.py index 863febe..6d5551e 100644 --- a/python3/vimspector/debug_session.py +++ b/python3/vimspector/debug_session.py @@ -133,9 +133,17 @@ class DebugSession( object ): if not configuration: return + utils.ExpandReferencesInDict( launch_config[ configuration ], { + 'workspaceRoot': os.path.dirname( launch_config_file ) + } ) + adapter = launch_config[ configuration ].get( 'adapter' ) if isinstance( adapter, str ): adapter = adapters.get( adapter ) + utils.ExpandReferencesInDict( adapter, { + 'workspaceRoot': os.path.dirname( launch_config_file ) + } ) + self._StartWithConfiguration( launch_config[ configuration ], adapter ) @@ -144,6 +152,11 @@ class DebugSession( object ): self._configuration = configuration 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(): self._StartDebugAdapter() self._Initialise() diff --git a/python3/vimspector/utils.py b/python3/vimspector/utils.py index 5800b8b..0553d8e 100644 --- a/python3/vimspector/utils.py +++ b/python3/vimspector/utils.py @@ -19,6 +19,7 @@ import os import contextlib import vim import json +import string _log_handler = logging.FileHandler( os.path.expanduser( '~/.vimspector.log' ) ) _log_handler.setFormatter( @@ -222,3 +223,21 @@ def ClearBuffer( buf ): def IsCurrent( window, 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 ] )