nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.41k stars 823 forks source link

strange cursor move when telescope closes #2319

Open Martinits opened 1 year ago

Martinits commented 1 year ago

Description

When telescope closes, the cursor will strangely move right by 1 column. That will influence some other plugins like https://github.com/AckslD/nvim-neoclip.lua . Neoclip uses telescope to provide a selection of yank history, and paste the selected item where it launched. This strange move makes neoclip paste to a wrong position.(https://github.com/AckslD/nvim-neoclip.lua/issues/96). Also, if you launch require('telescope.builtin').help_tag, choose one and close the help window, the cursor will move right.

Neovim version

NVIM v0.8.2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Operating system and version

archlinux

Telescope version / branch / rev

master

checkhealth telescope

telescope: require("telescope.health").check()
========================================================================
## Checking for required plugins
  - OK: plenary installed.
  - OK: nvim-treesitter installed.

## Checking external dependencies
  - OK: rg: found ripgrep 13.0.0
  - OK: fd: found fd 8.6.0

## ===== Installed extensions =====

## Telescope Extension: `fzf`
  - OK: lib working as expected
  - OK: file_sorter correctly configured
  - OK: generic_sorter correctly configured

## Telescope Extension: `macroscope`
  - INFO: No healthcheck provided

## Telescope Extension: `neoclip`
  - INFO: No healthcheck provided

Steps to reproduce

Expected behavior

No response

Actual behavior

described above

Minimal config

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvim/site]]
local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'
local function load_plugins()
  require('packer').startup {
    {
      'wbthomason/packer.nvim',
      {
        'nvim-telescope/telescope.nvim',
        requires = {
          'nvim-lua/plenary.nvim',
          { 'nvim-telescope/telescope-fzf-native.nvim', run = 'make' },
        },
      },
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. '/plugin/packer_compiled.lua',
      display = { non_interactive = true },
    },
  }
end
_G.load_config = function()
  require('telescope').setup()
  require('telescope').load_extension('fzf')
  -- ADD INIT.LUA SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
end
if vim.fn.isdirectory(install_path) == 0 then
  print("Installing Telescope and dependencies.")
  vim.fn.system { 'git', 'clone', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path }
end
load_plugins()
require('packer').sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]]
cristiansofronie commented 1 year ago

This is weird. The problem seems to be when calling actions.close. The function contains the following line which sets the cursor:

pcall(a.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] + 1 })

If I would wrap it in defer_fn the cursor would be shifted all the times, that is also when hitting or . If I wrap the vim.cmd calls from internal.man_pages and internal.help_tags in defer_fn the problem would go away.

The following line in actions.close gets the cursor position where to move:

local cursor_valid, original_cursor = pcall(a.nvim_win_get_cursor, original_win_id)

I printed the position to the screen and it seems to be correct in both cases. I used :lua print(vim.inspect(vim.api.nvim_win_get_cursor(0))) before calling the picker and checked the value with the one shown by the print(vim.inspect(original_cursor)) I inserted after the call to nvim_win_get_cursor.

The problem as far as I can tell seems to be with nvim_win_set_cursor having some weird race conditions.

cristiansofronie commented 1 year ago

If in actions.close I replace:

pcall(a.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] + 1})

with

vim.defer_fn(function()
  pcall(a.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] })
end, 0)

The + 1 was added in commit b4c45e8c610c8dc60483b49936dee8a4d0b9c532. But if we defer the call to nvim_win_set_cursor it doesn't seem to be required any more.

Martinits commented 1 year ago

I can confirm that will fix this issue.

Conni2461 commented 1 year ago

needs to be reverted in https://github.com/nvim-telescope/telescope.nvim/pull/2538 because it broke custom actions that rely that the picker is actually closed with actions.close

danielo515 commented 1 year ago

@Conni2461 is there a plan to re-fix this issue?

jamestrew commented 10 months ago

This column off by one error also happens when opening files in normal mode.