From e8dd631ee4cbb0a5b68b51106d9cc73a4f3fb81d Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Wed, 22 Feb 2023 14:29:11 +0100 Subject: [PATCH] Support label/value objects in ${input} pickString options Co-authored-by: Giancarlo Misasi --- doc/dap.txt | 7 +++- lua/dap/ext/vscode.lua | 8 +++- tests/ext_vscode_spec.lua | 88 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/doc/dap.txt b/doc/dap.txt index 65d6a0ab..8ecaa4bf 100644 --- a/doc/dap.txt +++ b/doc/dap.txt @@ -343,7 +343,12 @@ They are declared in an "inputs" array and each input must have the following pr - "description": Descriptive text shown to the user - "default": Default value (Defaults to '' if not provided) -`pickString` has an additional "options" property, which is an array of strings. +pickString` has an additional "options" property, which is an array of strings +or an array of option objects with label and value: + +- [ "my value 1", "my value 2", "my value 3" ] +- [ { "label": "my label", "value", "my value"} ] + These are shown to the user as options. Inputs can be referenced with `${input:}` placeholders. diff --git a/lua/dap/ext/vscode.lua b/lua/dap/ext/vscode.lua index 09e86819..a861cf75 100644 --- a/lua/dap/ext/vscode.lua +++ b/lua/dap/ext/vscode.lua @@ -18,12 +18,16 @@ local function create_input(type_, input) return function() local options = assert(input.options, "input of type pickString must have an `options` property") local opts = { - prompt = input.description + prompt = input.description, + format_item = function(x) + return x.label and x.label or x + end, } local co = coroutine.running() vim.ui.select(options, opts, function(option) vim.schedule(function() - coroutine.resume(co, option or input.default or '') + local value = option and option.value or option + coroutine.resume(co, value or (input.default or '')) end) end) return coroutine.yield() diff --git a/tests/ext_vscode_spec.lua b/tests/ext_vscode_spec.lua index e5fa781c..c693d28c 100644 --- a/tests/ext_vscode_spec.lua +++ b/tests/ext_vscode_spec.lua @@ -53,9 +53,11 @@ describe('dap.ext.vscode', function() it('supports pickString input', function() local options local opts + local label vim.ui.select = function(options_, opts_, on_choice) options = options_ opts = opts_ + label = opts_.format_item(options_[1]) on_choice(options_[1]) end local jsonstr = [[ @@ -87,6 +89,7 @@ describe('dap.ext.vscode', function() end)() vim.wait(1000, function() return ok end) assert.are.same(true, ok, "coroutine must finish") + assert.are.same("one", label) assert.are.same("${workspaceFolder}/one", result) assert.are.same("Select input", opts.prompt) assert.are.same({"one", "two", "three"}, options) @@ -229,4 +232,89 @@ describe('dap.ext.vscode', function() assert.are.same("Your input: ", prompt) assert.are.same("", default) end) + + it('supports pickString with options', function() + local opts + local label + vim.ui.select = function(options_, opts_, on_choice) + opts = opts_ + label = opts_.format_item(options_[1]) + on_choice(options_[1]) + end + local jsonstr = [[ + { + "configurations": [ + { + "type": "dummy", + "request": "launch", + "name": "Dummy", + "program": "${workspaceFolder}/${input:my_input}" + } + ], + "inputs": [ + { + "id": "my_input", + "type": "pickString", + "options": [ + { "label": "First value", "value": "one" }, + { "label": "Second value", "value": "two" } + ], + "description": "Select input" + } + ] + } + ]] + local configs = vscode._load_json(jsonstr) + local ok = false + local result + coroutine.wrap(function() + result = configs[1].program() + ok = true + end)() + vim.wait(1000, function() return ok end) + assert.are.same(true, ok, "coroutine must finish") + assert.are.same("${workspaceFolder}/one", result) + assert.are.same("Select input", opts.prompt) + assert.are.same("First value", label) + end) + + it('supports pickString with options, nothing selected', function() + vim.ui.select = function(_, _, on_choice) + on_choice(nil) + end + local jsonstr = [[ + { + "configurations": [ + { + "type": "dummy", + "request": "launch", + "name": "Dummy", + "program": "${workspaceFolder}/${input:my_input}" + } + ], + "inputs": [ + { + "id": "my_input", + "type": "pickString", + "options": [ + { "label": "First value", "value": "one" }, + { "label": "Second value", "value": "two" } + ], + "description": "Select input" + } + ] + } + ]] + local configs = vscode._load_json(jsonstr) + local ok = false + local result + coroutine.wrap(function() + result = configs[1].program() + ok = true + end)() + vim.wait(1000, function() return ok end) + assert.are.same(true, ok, "coroutine must finish") + -- input defaults to '' + assert.are.same("${workspaceFolder}/", result) + end) end)