Skip to content

Commit

Permalink
Support label/value objects in ${input} pickString options
Browse files Browse the repository at this point in the history
Co-authored-by: Giancarlo Misasi <[email protected]>
  • Loading branch information
mfussenegger and giancarlo-misasi committed Feb 22, 2023
1 parent 0199d02 commit e8dd631
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
7 changes: 6 additions & 1 deletion doc/dap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:<id>}` placeholders.
Expand Down
8 changes: 6 additions & 2 deletions lua/dap/ext/vscode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
88 changes: 88 additions & 0 deletions tests/ext_vscode_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [[
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit e8dd631

Please sign in to comment.