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

feat(action): Allow passing a range to filter the undo items by lines (#57)

Open Ajaymamtora opened this issue 1 year ago • 3 comments

Ajaymamtora avatar Aug 13 '24 20:08 Ajaymamtora

Hey no worries @debugloop , yep I'll do that. For some reason I cant find a clean way to do a keymap that reliably passes in the boundaries of the visual selection into telescope, this works but seems overkill:

    vim.api.nvim_create_user_command("TelescopeUndoVisual", function()
      -- Save the current view
      local view = vim.fn.winsaveview()

      -- Save the current mode
      local mode = vim.fn.mode()

      -- Temporarily switch to normal mode to ensure correct range capture
      vim.cmd("normal! \27")

      -- Get the first and last line numbers of the visual selection
      local start_line = vim.fn.line("'<")
      local end_line = vim.fn.line("'>")

      -- Call the function with the line range
      require("telescope").extensions.undo.undo({
        get_previewer = get_previewer,
        line_range = { start_line, end_line },
      })

      -- Restore the original view
      vim.fn.winrestview(view)

      -- If we were in visual mode, reselect the previous selection
      if mode == "v" or mode == "V" or mode == "\22" then
        vim.cmd("normal! gv")
      end
    end, { range = true })

Also nothing crazy just checking line numbers like I mentioned in the issue I raised haha

It should just be as simple as

      local start_line = vim.fn.line("'<")
      local end_line = vim.fn.line("'>")

then pass these in but for some reason they return 0 when used in a keymap? it works perfectly fine if I manually enter it while in a visual selection. Have you got any ideas?

Ajaymamtora avatar Sep 07 '24 17:09 Ajaymamtora

Sorry, been on vacation and whatnot :) It's probably because this is referring to the telescope buffer then. I've got no idea for a fix though 🤔

Let's maybe leave it at those README additions, someone will figure it out in time and hopefully contribute :)

debugloop avatar Oct 20 '24 08:10 debugloop

Sorry, been on vacation and whatnot :) It's probably because this is referring to the telescope buffer then. I've got no idea for a fix though 🤔

Let's maybe leave it at those README additions, someone will figure it out in time and hopefully contribute :)

No worries, Ive been using this and it has worked fine:

    local function open_undo_ts(args)
      require("telescope").extensions.undo.undo({
        get_previewer = get_previewer,
        line_range = args.line_range,
      })
    end

    vim.api.nvim_create_user_command("TelescopeUndo", function()
      open_undo_ts({})
    end, {})

    vim.api.nvim_create_user_command("TelescopeUndoVisual", function()
      -- Save the current view
      local view = vim.fn.winsaveview()

      -- Save the current mode
      local mode = vim.fn.mode()

      -- Temporarily switch to normal mode to ensure correct range capture
      vim.cmd("normal! \27")

      -- Get the first and last line numbers of the visual selection
      local start_line = vim.fn.line("'<")
      local end_line = vim.fn.line("'>")

      -- Call the function with the line range
      require("telescope").extensions.undo.undo({
        get_previewer = get_previewer,
        line_range = { start_line, end_line },
      })

      -- Restore the original view
      vim.fn.winrestview(view)

      -- If we were in visual mode, reselect the previous selection
      if mode == "v" or mode == "V" or mode == "\22" then
        vim.cmd("normal! gv")
      end
    end, { range = true })

But like you said lets just leave something in the readme :)

Ajaymamtora avatar Oct 20 '24 13:10 Ajaymamtora