From d5b94112566eeb381230b306f3c4c01020986edd Mon Sep 17 00:00:00 2001 From: Emmanuel Thompson Date: Thu, 5 Nov 2020 13:59:53 -0500 Subject: [PATCH] Add a GetConfigurations function --- README.md | 10 +++++++ autoload/vimspector.vim | 10 +++++++ python3/vimspector/debug_session.py | 45 ++++++++++++++++------------- tests/get_configurations.test.vim | 21 ++++++++++++++ 4 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 tests/get_configurations.test.vim diff --git a/README.md b/README.md index 38bd320..1e4e491 100644 --- a/README.md +++ b/README.md @@ -671,6 +671,16 @@ Vimspector uses the following logic to choose a configuration to launch: See [the reference guide][vimspector-ref-config-selection] for details. + +### Get configurations + +* Use `vimspector#GetConfigurations()` to get a list of configurations + +For example, to get an array of configurations and fuzzy matching on the result +```viml +:call matchfuzzy(vimspector#GetConfigurations(), "test::case_1") +``` + ## Breakpoints * Use `vimspector#ToggleBreakpoint([ { 'condition': '' } ])` diff --git a/autoload/vimspector.vim b/autoload/vimspector.vim index 410fd2d..6509e14 100644 --- a/autoload/vimspector.vim +++ b/autoload/vimspector.vim @@ -277,6 +277,16 @@ function! vimspector#ListBreakpoints() abort py3 _vimspector_session.ListBreakpoints() endfunction +function! vimspector#GetConfigurations() abort + if !s:Enabled() + return + endif + let configurations = py3eval( + \ 'list( _vimspector_session.GetConfigurations()[ 1 ].keys() )' + \ . ' if _vimspector_session else []' ) + return configurations +endfunction + function! vimspector#CompleteOutput( ArgLead, CmdLine, CursorPos ) abort if !s:Enabled() return diff --git a/python3/vimspector/debug_session.py b/python3/vimspector/debug_session.py index 42b6340..4f1ef0b 100644 --- a/python3/vimspector/debug_session.py +++ b/python3/vimspector/debug_session.py @@ -79,6 +79,24 @@ class DebugSession( object ): self._server_capabilities = {} self.ClearTemporaryBreakpoints() + def GetConfigurations( self ): + current_file = utils.GetBufferFilepath( vim.current.buffer ) + filetypes = utils.GetBufferFiletypes( vim.current.buffer ) + configurations = {} + + for launch_config_file in PathsToAllConfigFiles( VIMSPECTOR_HOME, + current_file, + filetypes ): + self._logger.debug( f'Reading configurations from: {launch_config_file}' ) + if not launch_config_file or not os.path.exists( launch_config_file ): + continue + + with open( launch_config_file, 'r' ) as f: + database = json.loads( minify( f.read() ) ) + configurations.update( database.get( 'configurations' or {} ) ) + + return launch_config_file, configurations + def Start( self, launch_variables = None ): # We mutate launch_variables, so don't mutate the default argument. # https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments @@ -91,10 +109,15 @@ class DebugSession( object ): self._adapter = None current_file = utils.GetBufferFilepath( vim.current.buffer ) - filetypes = utils.GetBufferFiletypes( vim.current.buffer ) - configurations = {} + launch_config_file, configurations = self.GetConfigurations() adapters = {} + if not configurations: + utils.UserMessage( 'Unable to find any debug configurations. ' + 'You need to tell vimspector how to launch your ' + 'application.' ) + return + glob.glob( install.GetGadgetDir( VIMSPECTOR_HOME ) ) for gadget_config_file in PathsToAllGadgetConfigs( VIMSPECTOR_HOME, current_file ): @@ -106,24 +129,6 @@ class DebugSession( object ): a = json.loads( minify( f.read() ) ).get( 'adapters' ) or {} adapters.update( a ) - for launch_config_file in PathsToAllConfigFiles( VIMSPECTOR_HOME, - current_file, - filetypes ): - self._logger.debug( f'Reading configurations from: {launch_config_file}' ) - if not launch_config_file or not os.path.exists( launch_config_file ): - continue - - with open( launch_config_file, 'r' ) as f: - database = json.loads( minify( f.read() ) ) - adapters.update( database.get( 'adapters' ) or {} ) - configurations.update( database.get( 'configurations' or {} ) ) - - if not configurations: - utils.UserMessage( 'Unable to find any debug configurations. ' - 'You need to tell vimspector how to launch your ' - 'application.' ) - return - if 'configuration' in launch_variables: configuration_name = launch_variables.pop( 'configuration' ) elif ( len( configurations ) == 1 and diff --git a/tests/get_configurations.test.vim b/tests/get_configurations.test.vim new file mode 100644 index 0000000..4a37d01 --- /dev/null +++ b/tests/get_configurations.test.vim @@ -0,0 +1,21 @@ +function! SetUp() + call vimspector#test#setup#SetUpWithMappings( v:none ) +endfunction + +function! ClearDown() + call vimspector#test#setup#ClearDown() +endfunction + +function Test_Get_Configurations() + lcd ../support/test/csharp/ + + let configs = vimspector#GetConfigurations() + call assert_equal([ + \ 'launch - netcoredbg', + \ 'launch - mono', + \ ], configs) + + lcd - + %bwipe! +endfunction +