nvim-telescope / telescope.nvim

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

Telescope sends ModeChanged autocommands with incorrect modes #2636

Open mawkler opened 1 year ago

mawkler commented 1 year ago

Description

When typing in insert mode in telescope's prompt, Telescope sends various ModeChanged commands. When checking the mode with vim.api.nvim_get_mode().mode it sometimes evaluates to i (insert) and sometimes n (normal), despite always staying in insert mode.

Neovim version

NVIM v0.10.0-dev-735+g8fe9f41f7
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3

Operating system and version

Manjaro Linux x86_64

Telescope version / branch / rev

31b05ad

checkhealth telescope

==============================================================================
telescope: require("telescope.health").check()

Checking for required plugins ~
- OK plenary installed.
- WARNING nvim-treesitter not found. (Required for `:Telescope treesitter`.)

Checking external dependencies ~
- OK rg: found ripgrep 13.0.0
- OK fd: found fd 8.7.0

===== Installed extensions ===== ~

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

Steps to reproduce

  1. nvim -nu init.lua
  2. :Telescope
  3. Start typing the phrase lsp type while looking at the mode that gets printed at the bottom left

Expected behavior

Telescope doesn't send any ModeChanged autocommand while typing in insert mode

Actual behavior

The mode changes between i and n even though we're always in insert mode while typing

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

  vim.o.cmdheight = 1
  vim.o.showmode = false

  vim.api.nvim_create_autocmd('ModeChanged', {
    callback = function() print(vim.api.nvim_get_mode().mode) end,
  })
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()]]
fdschmidt93 commented 1 year ago

When checking the mode with vim.api.nvim_get_mode().mode it sometimes evaluates to i (insert) and sometimes n (normal), despite always staying in insert mode.

I would try to get the mode via vim.fn.mode(). I loosely recall from various uses of vim.api.nvim_get_mode that it can be quite flaky in telescope context (e.g., initial mode setting).

When typing in insert mode in telescope's prompt

What happens when you type in the telescope prompt is that on_lines from nvim_buf_attach callbacks send the content of the prompt to the finder, which would be my initial hunch as to why ModeChanged occurs (couldn't think of anything else). vim.api.nvim_get_mode(), unlike vim.fn.mode() I believe, works in :h api-fast contexts, which is why I suppose you observe changing modes on on_lines callbacks. This is however only my best guess.

No clue if ModeChanged could be avoided, if it stems from what I'm hypothesizing it does.

jamestrew commented 1 year ago

I did try out using vim.fn.mode in the minimal config example but it produces the same result.

But I think your hunch is right that the on_lines, specifically the sending of the on_lines content over a channel or rather some of the async stuff we do on the receiving end is probably the culprit.

Conni2461 commented 11 months ago

This issue is only reproducible with previewer enabled. If you do :Telescope builtin previewer=false

The preview window/buffer sometimes needs to switch modes to do some jumping or similar, this is expected behavior.

https://github.com/nvim-telescope/telescope.nvim/blob/74ce793a60759e3db0d265174f137fb627430355/lua/telescope/previewers/buffer_previewer.lua#L289-L301

preview scrolling e.g. also switches modes https://github.com/nvim-telescope/telescope.nvim/blob/74ce793a60759e3db0d265174f137fb627430355/lua/telescope/previewers/buffer_previewer.lua#L318-L320

Why is this an issue for you? And if its not really an issue for you can this be closed?

mawkler commented 11 months ago

@Conni2461 This is the reason I opened the issue: https://github.com/mawkler/modicator.nvim/issues/17