Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telescope sends ModeChanged autocommands with incorrect modes #2636

Open
mawkler opened this issue Aug 5, 2023 · 4 comments
Open

Telescope sends ModeChanged autocommands with incorrect modes #2636

mawkler opened this issue Aug 5, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@mawkler
Copy link

mawkler commented Aug 5, 2023

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
Copy link
Member

fdschmidt93 commented Aug 6, 2023

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
Copy link
Contributor

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
Copy link
Member

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.

local search_cb_jump = function(self, bufnr, query)
if not query then
return
end
vim.api.nvim_buf_call(bufnr, function()
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.winid)
vim.cmd "norm! gg"
vim.fn.search(query, "W")
vim.cmd "norm! zz"
self.state.hl_id = vim.fn.matchadd("TelescopePreviewMatch", query)
end)
end

preview scrolling e.g. also switches modes

vim.api.nvim_win_call(self.state.winid, function()
vim.cmd([[normal! ]] .. count .. input)
end)

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

@mawkler
Copy link
Author

mawkler commented Oct 13, 2023

@Conni2461 This is the reason I opened the issue: mawkler/modicator.nvim#17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants