Skip to content

Commit e8dd631

Browse files
Support label/value objects in ${input} pickString options
Co-authored-by: Giancarlo Misasi <[email protected]>
1 parent 0199d02 commit e8dd631

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

doc/dap.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,12 @@ They are declared in an "inputs" array and each input must have the following pr
343343
- "description": Descriptive text shown to the user
344344
- "default": Default value (Defaults to '' if not provided)
345345

346-
`pickString` has an additional "options" property, which is an array of strings.
346+
pickString` has an additional "options" property, which is an array of strings
347+
or an array of option objects with label and value:
348+
349+
- [ "my value 1", "my value 2", "my value 3" ]
350+
- [ { "label": "my label", "value", "my value"} ]
351+
347352
These are shown to the user as options.
348353

349354
Inputs can be referenced with `${input:<id>}` placeholders.

lua/dap/ext/vscode.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ local function create_input(type_, input)
1818
return function()
1919
local options = assert(input.options, "input of type pickString must have an `options` property")
2020
local opts = {
21-
prompt = input.description
21+
prompt = input.description,
22+
format_item = function(x)
23+
return x.label and x.label or x
24+
end,
2225
}
2326
local co = coroutine.running()
2427
vim.ui.select(options, opts, function(option)
2528
vim.schedule(function()
26-
coroutine.resume(co, option or input.default or '')
29+
local value = option and option.value or option
30+
coroutine.resume(co, value or (input.default or ''))
2731
end)
2832
end)
2933
return coroutine.yield()

tests/ext_vscode_spec.lua

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ describe('dap.ext.vscode', function()
5353
it('supports pickString input', function()
5454
local options
5555
local opts
56+
local label
5657
vim.ui.select = function(options_, opts_, on_choice)
5758
options = options_
5859
opts = opts_
60+
label = opts_.format_item(options_[1])
5961
on_choice(options_[1])
6062
end
6163
local jsonstr = [[
@@ -87,6 +89,7 @@ describe('dap.ext.vscode', function()
8789
end)()
8890
vim.wait(1000, function() return ok end)
8991
assert.are.same(true, ok, "coroutine must finish")
92+
assert.are.same("one", label)
9093
assert.are.same("${workspaceFolder}/one", result)
9194
assert.are.same("Select input", opts.prompt)
9295
assert.are.same({"one", "two", "three"}, options)
@@ -229,4 +232,89 @@ describe('dap.ext.vscode', function()
229232
assert.are.same("Your input: ", prompt)
230233
assert.are.same("", default)
231234
end)
235+
236+
it('supports pickString with options', function()
237+
local opts
238+
local label
239+
vim.ui.select = function(options_, opts_, on_choice)
240+
opts = opts_
241+
label = opts_.format_item(options_[1])
242+
on_choice(options_[1])
243+
end
244+
local jsonstr = [[
245+
{
246+
"configurations": [
247+
{
248+
"type": "dummy",
249+
"request": "launch",
250+
"name": "Dummy",
251+
"program": "${workspaceFolder}/${input:my_input}"
252+
}
253+
],
254+
"inputs": [
255+
{
256+
"id": "my_input",
257+
"type": "pickString",
258+
"options": [
259+
{ "label": "First value", "value": "one" },
260+
{ "label": "Second value", "value": "two" }
261+
],
262+
"description": "Select input"
263+
}
264+
]
265+
}
266+
]]
267+
local configs = vscode._load_json(jsonstr)
268+
local ok = false
269+
local result
270+
coroutine.wrap(function()
271+
result = configs[1].program()
272+
ok = true
273+
end)()
274+
vim.wait(1000, function() return ok end)
275+
assert.are.same(true, ok, "coroutine must finish")
276+
assert.are.same("${workspaceFolder}/one", result)
277+
assert.are.same("Select input", opts.prompt)
278+
assert.are.same("First value", label)
279+
end)
280+
281+
it('supports pickString with options, nothing selected', function()
282+
vim.ui.select = function(_, _, on_choice)
283+
on_choice(nil)
284+
end
285+
local jsonstr = [[
286+
{
287+
"configurations": [
288+
{
289+
"type": "dummy",
290+
"request": "launch",
291+
"name": "Dummy",
292+
"program": "${workspaceFolder}/${input:my_input}"
293+
}
294+
],
295+
"inputs": [
296+
{
297+
"id": "my_input",
298+
"type": "pickString",
299+
"options": [
300+
{ "label": "First value", "value": "one" },
301+
{ "label": "Second value", "value": "two" }
302+
],
303+
"description": "Select input"
304+
}
305+
]
306+
}
307+
]]
308+
local configs = vscode._load_json(jsonstr)
309+
local ok = false
310+
local result
311+
coroutine.wrap(function()
312+
result = configs[1].program()
313+
ok = true
314+
end)()
315+
vim.wait(1000, function() return ok end)
316+
assert.are.same(true, ok, "coroutine must finish")
317+
-- input defaults to ''
318+
assert.are.same("${workspaceFolder}/", result)
319+
end)
232320
end)

0 commit comments

Comments
 (0)