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

Provide a function to get arguements for the program as user input #629

Closed
ppebb opened this issue Jul 29, 2022 · 6 comments · Fixed by #1276
Closed

Provide a function to get arguements for the program as user input #629

ppebb opened this issue Jul 29, 2022 · 6 comments · Fixed by #1276

Comments

@ppebb
Copy link

ppebb commented Jul 29, 2022

Problem Statement

The majority of debuggers require args for the program being debugged to be passed in as an array. To handle things like escape characters with \ or not splitting things between quotes as separate items in the array is a pain, so I think providing this functionality in nvim-dap itself would be useful. Obviously I could just use vim.fn.input and split it by spaces into an array but that's not optimal.

Ideas or possible solutions

Just some function that takes a string of args as input and returns them split into an array while handling \ and quotes.

@wookayin
Copy link
Contributor

wookayin commented Oct 31, 2022

Workaround: Currently the DAP configuration can take a function to resolve the value dynamically (but in a blocking way):

Example code
dap.configurations.cpp = {
  ...
  {
    name = "Launch this file with arguments",
    type = "cppdbg",
    request = "launch",
    program = function()
      return "${fileDirname}/${fileBasenameNoExtension}"
    end,
    args = function()
      local x = vim.fn.input('Args :');
      if x and x ~= "" then return vim.split(s, ' ') end  -- Note: does not deal with quotes and spaces correctly
      return nil
    end,
    cwd = '${workspaceFolder}',
  },
  ...
}

To my knowledge there is no way to resolve the configuration this asynchronously, e.g., via vim.ui.input() until the nvim-dap client supports a way. Or you can first invoke some UI component to store the user input into some variable, and then launch dap during the async callback function.

@mfussenegger
Copy link
Owner

To my knowledge there is no way to resolve the configuration this asynchronously, e.g., via vim.ui.input() until the nvim-dap client supports a way.

asynchronous operations are supported. See :help dap-configuration, it includes an example for using vim.ui.select.

@wookayin
Copy link
Contributor

wookayin commented Nov 1, 2022

I see, the idea is to use a coroutine. It's very smart. So users can use vim.ui.{input,select} to enter the argument to run with.

An example code
...
      args = function()
        return coroutine.create(function(dap_run_co)
          vim.ui.input({ prompt = 'Args >'}, function(choice)
            choice = choice or ''
            local arg = vim.split(choice, ' ')  -- TODO: use a function that handles quotes and spaces
            coroutine.resume(dap_run_co, arg)
          end)
        end)
      end,
...

@Fabian-programmer
Copy link

I'm using lua code to solve it, without a coroutine, maybe it helps you.
When I start a session. args ->input is getting called before program -> input. Is it the same for you? That is quite counter-intuitive

...
args = function()
        local args = {}
        local args_string = vim.fn.input('Args: ')
        for word in args_string:gmatch("%S+") do
          table.insert(args, word)
        end
        return args
end,
...

@dasupradyumna
Copy link

dasupradyumna commented Jan 5, 2023

Hey Mathias!
I have been using the coroutine method for dynamically selecting from a list of options during debug session launch. I noticed one thing though ; when I do not choose / enter any value during this step, the debugger is not properly launched but ends up in a limbo state like this -

issue_nvimdap_vim_ui_select
I have to manually terminate this by trying to run a new session, and selecting "Terminate".

issue_nvimdap_vim_ui_select2

How can I better handle a vim.ui.select cancel? For instance, if I do not select an option, then the debugger should not try to launch a session with a nil value, and stop the debug session launch process.

Cheers for the awesome plugin ~

@mfussenegger
Copy link
Owner

I'm not sure how to best deal with that as nil could be an intentional result as well to remove the property.
You could try to raise an error instead of nil. That would probably result in an error message but should abort the session-launch routine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants