From 8f5b928e4b8dfd150f153ada38d11cad7a8cd794 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Thu, 23 Jul 2020 16:25:04 +0100 Subject: [PATCH] Allow a statically configured list of gadgets Useful for storing config in source control --- README.md | 13 +++++++++++++ autoload/vimspector.vim | 3 --- python3/vimspector/installer.py | 20 ++++++++++++++------ python3/vimspector/settings.py | 9 ++++++++- python3/vimspector/utils.py | 30 ++++++++++++++++++++++-------- 5 files changed, 57 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3c2e2a6..db6e025 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,19 @@ If the installation is successful, the output window is closed (and the output lost forever). Use a `!` to keep it open (e.g. `:VimspectorInstall! --verbose --all` or `:VimspectorUpdate!` (etc.). +If you know in advance which gadgets you want to install, for example so that +you can reproduce your config from source control, you can set +`g:vimspector_install_gadgets` to a list of gadgets. This will be used when: + +* Running `:VimspectorInstall` with no arguments, or +* Running `:VimspectorUpdate` + +For example: + +```viml +let g:vimspector_install_gadgets = [ 'debugpy', 'vscode-cpptools', 'CodeLLDB' ] +``` + ### install\_gadget.py By default `install_gadget.py` will overwrite your `.gadgets.json` with the set diff --git a/autoload/vimspector.vim b/autoload/vimspector.vim index afbd52c..3b5dd75 100644 --- a/autoload/vimspector.vim +++ b/autoload/vimspector.vim @@ -236,9 +236,6 @@ function! vimspector#Install( bang, ... ) abort if !s:enabled return endif - if a:0 < 1 - return - endif let prefix = vimspector#internal#state#GetAPIPrefix() py3 __import__( 'vimspector', \ fromlist = [ 'installer' ] ).installer.RunInstaller( diff --git a/python3/vimspector/installer.py b/python3/vimspector/installer.py index 52497b5..3e4440d 100644 --- a/python3/vimspector/installer.py +++ b/python3/vimspector/installer.py @@ -99,9 +99,15 @@ def RunInstaller( api_prefix, leave_open, *args, **kwargs ): from vimspector import utils, output, settings import vim + if not args: + args = settings.List( 'install_gadgets' ) + + if not args: + return + args = GadgetListToInstallerArgs( *args ) - vimspector_home = utils.GetVimString( vim.vars, 'vimspector_home' ) + vimspector_home = utils.GetVimValue( vim.vars, 'vimspector_home' ) vimspector_base_dir = utils.GetVimspectorBase() global OUTPUT_VIEW @@ -145,16 +151,18 @@ def RunInstaller( api_prefix, leave_open, *args, **kwargs ): def RunUpdate( api_prefix, leave_open, *args ): - from vimspector import utils + from vimspector import utils, settings Configure( vimspector_base = utils.GetVimspectorBase() ) - args = list( args ) + insatller_args = list( args ) + insatller_args.extend( settings.List( 'install_gadgets' ) ) + current_adapters = ReadAdapters( read_existing = True ) for adapter_name in current_adapters.keys(): - args.extend( FindGadgetForAdapter( adapter_name ) ) + insatller_args.extend( FindGadgetForAdapter( adapter_name ) ) - if args: - RunInstaller( api_prefix, leave_open, *args ) + if insatller_args: + RunInstaller( api_prefix, leave_open, *insatller_args ) def _ResetInstaller(): diff --git a/python3/vimspector/settings.py b/python3/vimspector/settings.py index c365499..08f3066 100644 --- a/python3/vimspector/settings.py +++ b/python3/vimspector/settings.py @@ -16,11 +16,18 @@ import vim import builtins +from vimspector import utils def Get( option: str, default=None, cls=str ): - return cls( vim.vars.get( f'vimspector_{ option }', default ) ) + return cls( utils.GetVimValue( vim.vars, + f'vimspector_{ option }', + default ) ) def Int( option: str, default=0 ): return Get( option, default=default, cls=builtins.int ) + + +def List( option: str, default=[] ): + return utils.GetVimList( vim.vars, f'vimspector_{ option }', default ) diff --git a/python3/vimspector/utils.py b/python3/vimspector/utils.py index ad5bc9a..0aea216 100644 --- a/python3/vimspector/utils.py +++ b/python3/vimspector/utils.py @@ -23,7 +23,7 @@ import string import functools import subprocess import shlex - +import collections LOG_FILE = os.path.expanduser( os.path.join( '~', '.vimspector.log' ) ) @@ -646,7 +646,7 @@ def HideSplash( api_prefix, splash ): return None -def GetVimString( vim_dict, name, default=None ): +def GetVimValue( vim_dict, name, default=None ): # FIXME: use 'encoding' ? try: @@ -659,13 +659,27 @@ def GetVimString( vim_dict, name, default=None ): return value +def GetVimList( vim_dict, name, default=None ): + try: + value = vim_dict[ name ] + except KeyError: + return default + + if not isinstance( value, collections.abc.Iterable ): + raise ValueError( f"Expected a list for { name }, but found " + f"{ type( value ) }" ) + + return [ i.decode( 'utf-8' ) if isinstance( i, bytes ) else i for i in value ] + + + def GetVimspectorBase(): - return GetVimString( vim.vars, - 'vimspector_base_dir', - os.path.abspath( - os.path.join( os.path.dirname( __file__ ), - '..', - '..' ) ) ) + return GetVimValue( vim.vars, + 'vimspector_base_dir', + os.path.abspath( + os.path.join( os.path.dirname( __file__ ), + '..', + '..' ) ) ) def GetUnusedLocalPort():