which-key.nvim icon indicating copy to clipboard operation
which-key.nvim copied to clipboard

WhichKey closes Telescope window

Open danielnieto opened this issue 2 years ago • 5 comments

I have both Telescope and WhichKey installed, today I was trying to paste some text (from a register) into Telescope, but as soon as I hit " in normal mode, or Ctrl+R in insert mode, the telescope window is closed, and I can't even see the whichkey window. I assume that it's because WhichKey is trying to show me the registers and it somehow collides with the telescope window? Here's a video of it:

https://user-images.githubusercontent.com/2120107/165871564-b89e1440-4307-4196-8baa-45082010626d.mp4

danielnieto avatar Apr 29 '22 01:04 danielnieto

the same, but I get an error:

E5108: Error executing lua ...te/pack/packer/opt/which-key.nvim/lua/which-key/view.lua:52: Window was closed immediately
stack traceback:
        [C]: in function 'nvim_open_win'
        ...te/pack/packer/opt/which-key.nvim/lua/which-key/view.lua:52: in function 'show'
        ...te/pack/packer/opt/which-key.nvim/lua/which-key/view.lua:262: in function 'on_keys'
        ...te/pack/packer/opt/which-key.nvim/lua/which-key/view.lua:226: in function 'open'
        ...te/pack/packer/opt/which-key.nvim/lua/which-key/init.lua:48: in function 'show'
        [string ":lua"]:1: in main chunk

askfiy avatar May 06 '22 05:05 askfiy

An ugly workaround that disables the which-key popup if the Telescope is opened

Put this before setup()

local wk = require("which-key")
local show = wk.show
wk.show = function(keys, opts)
	if vim.bo.filetype == "TelescopePrompt" then
		return
	end
	show(keys, opts)
end

unavaliabl3 avatar May 07 '22 16:05 unavaliabl3

@rekaerst

This method may be better, it at least guarantees that <c-r> is executed correctly:

    local wk = require("which-key")
    local show = wk.show
    wk.show = function(keys, opts)
        if vim.bo.filetype == "TelescopePrompt" then
            local map = "<c-r>"
            local key = vim.api.nvim_replace_termcodes(map, true, false, true)
            vim.api.nvim_feedkeys(key, "i", true)
        end
        show(keys, opts)
    end

askfiy avatar May 24 '22 07:05 askfiy

I've inspired form source code. Just skip show new float window, this will keep current float window without blocking keys' executing. But there are some problems seem caused by timeoutlen.

local which_key = require 'which-key'
local which_key_view = require 'which-key.view'
local ignore_filetype = { 'TelescopePrompt' }
local show = which_key.show
which_key.show = function(keys, opts)
  if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then
    which_key_view.keys = keys
    which_key_view.mode = opts.mode
    which_key_view.count = vim.api.nvim_get_vvar 'count'
    which_key_view.reg = vim.api.nvim_get_vvar 'register'
    which_key_view.execute(keys, opts.mode, vim.api.nvim_get_current_buf())
    return
  else
    show(keys, opts)
  end
end

theoolee avatar May 26 '22 15:05 theoolee

Finally I made a hack to the source code, which just comment out lines around M.show(). Maybe we can make a pr about 'hide on filetypes'.

local M = require 'which-key.view'
local Keys = require 'which-key.keys'
local Layout = require 'which-key.layout'
local Util = require 'which-key.util'
local config = require 'which-key.config'


local function on_keys(opts)
  local buf = vim.api.nvim_get_current_buf()

  while true do
    -- loop
    M.read_pending()

    local results = Keys.get_mappings(M.mode, M.keys, buf)

    --- Check for an exact match. Feedkeys with remap
    if results.mapping and not results.mapping.group and #results.mappings == 0 then
      M.hide()
      if results.mapping.fn then
        results.mapping.fn()
      else
        M.execute(M.keys, M.mode, buf)
      end
      return
    end

    -- Check for no mappings found. Feedkeys without remap
    if #results.mappings == 0 then
      M.hide()
      -- only execute if an actual key was typed while WK was open
      if opts.auto then
        M.execute(M.keys, M.mode, buf)
      end
      return
    end

    local layout = Layout:new(results)

    -- if not M.is_valid() then
    --   M.show()
    -- end

    M.render(layout:layout(M.win))

    vim.cmd([[redraw]])

    local c = M.getchar()

    if c == Util.t("<esc>") then
      M.on_close()
      break
    elseif c == Util.t(config.options.popup_mappings.scroll_down) then
      M.scroll(false)
    elseif c == Util.t(config.options.popup_mappings.scroll_up) then
      M.scroll(true)
    elseif c == Util.t("<bs>") then
      M.back()
    else
      M.keys = M.keys .. c
    end
  end
end

local function open(keys, opts)
  opts = opts or {}
  M.keys = keys or ''
  M.mode = opts.mode or Util.get_mode()
  M.count = vim.api.nvim_get_vvar 'count'
  M.reg = vim.api.nvim_get_vvar 'register'

  on_keys(opts)
end

local ignore_filetype = { 'TelescopePrompt' }
local show = which_key.show
which_key.show = function(keys, opts)
  if vim.tbl_contains(ignore_filetype, vim.bo.filetype) then
    open(keys, opts)
    return
  else
    show(keys, opts)
  end
end

theoolee avatar May 26 '22 15:05 theoolee

I just pushed a new update where you can define buftypes and filetypes for which the WhichKey popup will be disabled. TelescopePrompt has been added as a default, so this problem no longer occurs.

folke avatar Sep 06 '22 18:09 folke