Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for command input type in launch.json #1083

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lua/dap/ext/vscode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local M = {}
M.json_decode = vim.json.decode
M.type_to_filetypes = {}

local function create_input(type_, input)
local function create_input(type_, input, command_callbacks)
if type_ == "promptString" then
return function()
local description = input.description or 'Input'
Expand All @@ -28,9 +28,20 @@ local function create_input(type_, input)
return vim.fn.input(description, input.default or '')
end
end
elseif type_ == "pickString" then
elseif type_ == "pickString" or type_ == "command" then
return function()
local options = assert(input.options, "input of type pickString must have an `options` property")
local options = {}
assert(type ~= "pickString" or input.options, "input of type pickString must have an `options` property")
assert(type ~= "command" or input.command, "input of type command must have a `command` property")
if input.options then
options = input.options
elseif input.command then
if command_callbacks[input.command] then
options = command_callbacks[input.command](input.command, input.args)
elseif command_callbacks[1] then
options = command_callbacks[1](input.command, input.args)
end
end
local opts = {
prompt = input.description,
format_item = function(x)
Expand All @@ -53,13 +64,13 @@ local function create_input(type_, input)
end


local function create_inputs(inputs)
local function create_inputs(inputs, command_callbacks)
local result = {}
for _, input in ipairs(inputs) do
local id = assert(input.id, "input must have a `id`")
local key = "${input:" .. id .. "}"
local type_ = assert(input.type, "input must have a `type`")
local fn = create_input(type_, input)
local fn = create_input(type_, input, command_callbacks)
if fn then
result[key] = fn
end
Expand Down Expand Up @@ -132,9 +143,9 @@ local function lift(tbl, key)
end


function M._load_json(jsonstr)
function M._load_json(jsonstr, command_callbacks)
local data = assert(M.json_decode(jsonstr), "launch.json must contain a JSON object")
local inputs = create_inputs(data.inputs or {})
local inputs = create_inputs(data.inputs or {}, command_callbacks)
local has_inputs = next(inputs) ~= nil

local sysname
Expand All @@ -156,8 +167,9 @@ end


--- Extends dap.configurations with entries read from .vscode/launch.json
function M.load_launchjs(path, type_to_filetypes)
function M.load_launchjs(path, type_to_filetypes, command_callbacks)
type_to_filetypes = vim.tbl_extend('keep', type_to_filetypes or {}, M.type_to_filetypes)
command_callbacks = command_callbacks or {}
local resolved_path = path or (vim.fn.getcwd() .. '/.vscode/launch.json')
if not vim.loop.fs_stat(resolved_path) then
return
Expand All @@ -169,7 +181,7 @@ function M.load_launchjs(path, type_to_filetypes)
end
end
local contents = table.concat(lines, '\n')
local configurations = M._load_json(contents)
local configurations = M._load_json(contents, command_callbacks)

assert(configurations, "launch.json must have a 'configurations' key")
for _, config in ipairs(configurations) do
Expand Down