nvim-telescope / telescope.nvim

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

Opening file with telescope messes with last position jump on `BufEnter` #1366

Open jesseleite opened 2 years ago

jesseleite commented 2 years ago

Description

For those of us using Neovim's recommended autocmd for last position jump on BufEnter (see :help last-position-jump), Neovim will restore last cursor position when re-opening a file.

This works perfectly when opening a file using :edit somefile. However, if I open somefile using a Telescope fuzzy finder, it will open the file and move the cursor one character to the left.

I never noticed this before, but it's messing with a custom autocmd I'm trying to write where I automatically highlight some text on BufEnter.

Neovim version

NVIM v0.5.1 Build type: Release LuaJIT 2.1.0-beta3 Compilation: clang -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -O2 -DNDEBUG -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/tmp/neovim-20210927-1441-demfsz/neovim-0.5.1/build/config -I/tmp/neovim-20210927-1441-demfsz/neovim-0.5.1/src -I/usr/local/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/usr/include -I/usr/local/opt/gettext/include -I/tmp/neovim-20210927-1441-demfsz/neovim-0.5.1/build/src/nvim/auto -I/tmp/neovim-20210927-1441-demfsz/neovim-0.5.1/build/include Compiled by brew@BigSur

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

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

Run :checkhealth for more info

Operating system and version

macOS 11.2.1

checkhealth telescope

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

## Checking external dependencies
  - OK: rg: found ripgrep 13.0.0
  - WARNING: fd: not found. Install [sharkdp/fd](https://github.com/sharkdp/fd) for extended capabilities

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

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

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

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

Steps to reproduce

  1. Add the autocmd suggested by :help last-position-jump.
  2. Open a file using :edit somefile.
    • You should notice it will restore your exact cursor position.
  3. Close that file, and re-open it using a finder like :Telescope history.
    • You should notice it will restore your last cursor position, one character to the left from where you last were.

Expected behavior

I would expect this last cursor position autocmd to behave exactly the same, whether you open a file using telescope, or by other means.

Actual behavior

Telescope finders are not restoring last cursor position correctly.

Minimal config

" Remember last cursor position, as suggested by `:help last-position-jump`
augroup neovim_last_position
  autocmd!
  autocmd BufReadPost *
    \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
    \ |   exe "normal! g`\""
    \ | endif
augroup END
l-kershaw commented 2 years ago

This seems similar to #1185, but can't tell if it is exactly the same issue.

jesseleite commented 2 years ago

@l-kershaw Indeed looks that way, but I'm at a loss to what might be causing it.

smhc commented 2 years ago

I believe this may be related to the fact Telescope is causing the re-firing of the BufReadPost event when loaded. This is particularly evident when using lazy loading of telescope (e.g with packer) and trying to use the 'grep_files' Telescope command. The cursor position will always jump (due to this autocommand) and you grep the wrong word.

Running the command the second time works ok.

Not sure where in Telescope would be causing this event to fire.

It should be noted that the latest version of this 'jump last position' autocmd does not suffer from this problem

    autocmd BufRead * autocmd FileType <buffer> ++once
      \ if &ft !~# 'commit\|rebase' && line("'\"") > 1 && line("'\"") <= line("$") | exe 'normal! g`"' | endif

But there does still seem to be an issue with telescope.