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

fix: restore cursor to correct position on picker `close`

Open jamestrew opened this issue 1 year ago • 5 comments

I think we have several factors contributing to flaky (off by one error) behaviors in the cursor position on picker close.

Mainly, I think we should be scheduling nvim_win_set_cursor. Without doing so, we've had to "arbitrarily" +1 on the columns to compensate: https://github.com/nvim-telescope/telescope.nvim/blob/87e92ea31b2b61d45ad044cf7b2d9b66dad2a618/lua/telescope/actions/set.lua#L214-L216

This is made worse by a couple of other factors

  1. ripgrep gives columns in 1-index whereas nvim_win_set_cursor expects 0-index. This wasn't previously an issue probably due to not scheduling nvim_win_set_cursor but I think we were just depending on UB.
  2. ~not accounting for original mode on select_default. like this~ I think this is no longer necessary and was used to compensate for the lack of scheduling set cursor. https://github.com/nvim-telescope/telescope.nvim/blob/87e92ea31b2b61d45ad044cf7b2d9b66dad2a618/lua/telescope/actions/init.lua#L388-L390

I recognize this is a very sensitive part of our code that we've had repeat issues with so I still need to do some testing and verify this closes a few issues without any obvious or previously experienced regressions

closes #2319 and semi-related https://github.com/nvim-telescope/telescope.nvim/issues/2319#issuecomment-1806854277 closes https://github.com/nvim-telescope/telescope.nvim/issues/2849 closes https://github.com/nvim-telescope/telescope.nvim/issues/1366

TODO (all with both norma/insert mode as original mode):

  • [x] check 0/1 index of columns in lsp/treesitter/etc pickers
    • all except treesitter picker was using 1-index row/col (treesitter cursor position was actually broken) so taking 1-index as the true column index
  • [x] closing pickers without selecting anything (no change)
  • [x] select_default with no row/col info (find_files)
  • [x] select_default with row/col/both info (live_grep with/without --invert-match for no col info)
  • [x] actions.close() calls (like in #2849)
  • [x] #2319
  • [ ] anything else?

jamestrew avatar Jan 06 '24 18:01 jamestrew

let me run this for a couple of days before merging :D but thanks for addressing this issue :)

Conni2461 avatar Jan 07 '24 21:01 Conni2461

i rebased the branch and continue to run it till friday, if there arent any issue till then, i think this is good to go thanks :)

Conni2461 avatar Jan 10 '24 22:01 Conni2461

I found an regression with :Telescope help_tags related to opening an existing buffer at a different location.

  1. :Telescope help_tags -> search for bufhidden -> this opens the options.txt doc buffer
  2. :Telescope help_tags -> search for hidden -> same options.txt but doesn't move, still on bufhidden

jamestrew avatar Jan 13 '24 17:01 jamestrew

I found an regression with :Telescope help_tags related to opening an existing buffer at a different location.

1. `:Telescope help_tags` -> search for `bufhidden` -> this opens the `options.txt` doc buffer

2. `:Telescope help_tags` -> search for `hidden` -> same `options.txt` but doesn't move, still on `bufhidden`

The problem is that we're scheduling cursor position inside actions.close() so when a picker calls actions.close() then does something that moves the cursor under the current window, the previously scheduled cursor position moves the cursor again.

That why I had to schedule this so it happens after the cursor position is reset https://github.com/nvim-telescope/telescope.nvim/pull/2850/files#diff-f192865bab9995c5a69e48b85b5d4534094c885a1556b10bb1e9407b0e0570d0R387-R390

For this help_tags issue, I can fix it by doing the same by scheduling this https://github.com/nvim-telescope/telescope.nvim/blob/da8b3d485975a8727bea127518b65c980521ae22/lua/telescope/builtin/__internal.lua#L778-L784

Ideally, we just defer/schedule everything after actions.close() to happen after cursor position is set. I gotta think about how to do this without significant refactors... I don't like the idea of throwing vim.schedule case by case on things after actions.close(). This really puts the responsibility on each picker actions way too much.

jamestrew avatar Jan 13 '24 19:01 jamestrew

Yeah idno how to cleanly fix this without API changes and big refactors.

jamestrew avatar Jan 14 '24 03:01 jamestrew