rcarriga / nvim-dap-ui

A UI for nvim-dap
MIT License
2.41k stars 88 forks source link

get_current_expr incorrectly includes an extra symbol when eval with no parameters is called on selection in visual mode #365

Open shmerl opened 1 month ago

shmerl commented 1 month ago

I have such configuration for my selection:

set selection=exclusive

So when I select some text for example ab from abc in the visual mode

abc

nvim dap ui when calling eval with <M-k> (mapping enabled with vnoremap <M-k> <Cmd>lua require("dapui").eval()<CR>), picks one extra symbol, i.e. abc, instead of ab. I think the issue is with get_current_expr.

shmerl commented 1 month ago

I think it's due to this: https://github.com/rcarriga/nvim-dap-ui/blob/b7267003ba4dd860350be86f75b9d9ea287cedca/lua/dapui/util.lua#L41

    local start = nio.fn.getpos("v")
    local finish = nio.fn.getpos(".")
    local lines = M.get_selection(start, finish)

i.e. finish is the cursor position, but with set selection=exclusive, cursor is not included in the selection, so that creates a mismatch.

'selection' 'sel'       string  (default "inclusive")
                        global
        This option defines the behavior of the selection.  It is only used
        in Visual and Select mode.
        Possible values:
           value        past line     inclusive 
           old             no           yes
           inclusive       yes          yes
           exclusive       yes          no
...
    "inclusive" means that the last character of the selection is included
    in an operation.  For example, when "x" is used to delete the
    selection.
shmerl commented 1 month ago

Hmm. looks like there is a builtin for that now, that handles exclusive / inclusive logic automatically:

:help getregion

shmerl commented 1 month ago

OK, we are getting somewhere:

vim.fn.getregion(vim.fn.getpos('v'), vim.fn.getpos('.'))[1]

That returns the first string in the list (it breaks up selection by lines I think?)

UPDATE:

This combines the result into a single string separated by endlines:

table.concat(vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos(".")), "\n")
shmerl commented 1 month ago

Final result for lua configuration that works around the bug:

vim.keymap.set('v', '<M-k>', function() require("dapui").eval(table.concat(vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos(".")), "\n")) end)