From b17d513beee21b113cb99c501b8e8325f5dccb61 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Thu, 9 Dec 2021 20:40:34 +0100 Subject: [PATCH] Expand variables in keys of nested tables in configurations Closes https://github.com/mfussenegger/nvim-dap/issues/374 --- lua/dap.lua | 6 +++++- tests/integration_spec.lua | 16 +++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lua/dap.lua b/lua/dap.lua index da62c524..b5542680 100644 --- a/lua/dap.lua +++ b/lua/dap.lua @@ -138,7 +138,11 @@ local function expand_config_variables(option) option = option() end if type(option) == "table" then - return vim.tbl_map(expand_config_variables, option) + local result = {} + for k, v in pairs(option) do + result[expand_config_variables(k)] = expand_config_variables(v) + end + return result end if type(option) ~= "string" then return option diff --git a/tests/integration_spec.lua b/tests/integration_spec.lua index 5df8e398..9e2f03c5 100644 --- a/tests/integration_spec.lua +++ b/tests/integration_spec.lua @@ -28,17 +28,20 @@ describe('dap', function() name = 'Launch file', program = program, dummy_payload = { - cwd = '${workspaceFolder}' + cwd = '${workspaceFolder}', + ['key_with_${workspaceFolder}'] = 'value', + numbers = {1, 2, 3, 4}, + strings = {'a', 'b', 'c'}, } } local bp_lnum = 8 local bufnr = vim.fn.bufadd(program) breakpoints.set({}, bufnr, bp_lnum) local events = {} - local dummy_value = nil + local dummy_payload = nil dap.listeners.after.event_initialized['dap.tests'] = function(session) events.initialized = true - dummy_value = session.config.dummy_payload.cwd + dummy_payload = session.config.dummy_payload end dap.listeners.after.setBreakpoints['dap.tests'] = function(_, _, resp) events.setBreakpoints = resp @@ -70,10 +73,13 @@ describe('dap', function() }, events) -- variable must expand to concrete value - assert.are_not.equals(dummy_value, '${workspaceFolder}') + assert.are_not.equals(dummy_payload.cwd, '${workspaceFolder}') + assert.are.same(dummy_payload.numbers, {1, 2, 3, 4}) + assert.are.same(dummy_payload.strings, {'a', 'b', 'c'}) + assert.are.equals(dummy_payload['key_with_' .. vim.fn.getcwd()], 'value') -- ensure `called_with` below passes - config.dummy_payload.cwd = dummy_value + config.dummy_payload = dummy_payload it('passed cwd to adapter process', function() luassert.spy(launch).was.called_with(dap.adapters.python, config, { cwd = venv_dir })