telescope-undo.nvim icon indicating copy to clipboard operation
telescope-undo.nvim copied to clipboard

[Request] Filter entries by given range?

Open Ajaymamtora opened this issue 1 year ago • 3 comments

Could we add an option to filter the picker entries by a given range?

For example we could bind in visual selection to take the start and end lines, and pass that into telescope-undo, then filter entries if they don't have a diff between the given lines? I used this a lot in IntelliJ and it would be useful to have here. It probably doesnt have to be smart and try and resolve where code has moved about between diffs, could probably just check the diff lies between the given line numbers

Maybe something like this before passing it into telescope:

local function undo_history_for_range(startline, endline)
    local undotree = vim.fn.undotree()
    local changes = {}

    for _, entry in ipairs(undotree.entries) do
        for _, change in ipairs(entry.changes or {}) do
            if change.new_start >= startline and change.old_end <= endline then
                table.insert(changes, change)
            end
        end
    end

    return changes
end

-- Example usage: Get undo history for lines 10 to 20
local changes = undo_history_for_range(10, 20)
print(vim.inspect(changes))

https://www.jetbrains.com/help/idea/local-history.html

Ajaymamtora avatar Aug 11 '24 12:08 Ajaymamtora

Hi, that's an interesting one. In principle this plugin just uses the builtin undo states which does not support any kind of querying based on content. However, we precalculate all diffs while visiting the undo states and building the tree, so we could tell which lines are affected with a minimum of fancy code necessary 🤔 What do you think about the following:

  1. If you restore a state shown by this _for_range function, you will still get any intermediate changes not shown. Yank maps will work nicely though.
  2. The tree view will be broken and not really fixable without a lot of effort.

Otherwise, this might be very doable and useful, yeah 👍🏻

debugloop avatar Aug 13 '24 17:08 debugloop

Ah okay, I personally wouldnt mind it because like you said theres always the option to yank the changes 👍🏻. Also maybe any change can create a new undo state on-top of the tree? Not sure if that's possible.

Ajaymamtora avatar Aug 13 '24 20:08 Ajaymamtora

I had a play and this seems to work in my very brief testing but thought you might want to use it as a base?

https://github.com/debugloop/telescope-undo.nvim/pull/58

Ajaymamtora avatar Aug 13 '24 20:08 Ajaymamtora