liuchengxu / vim-which-key

:tulip: Vim plugin that shows keybindings in popup
https://liuchengxu.github.io/vim-which-key/
MIT License
1.93k stars 65 forks source link

Buffer local mappings are cached even with g:which_key_run_map_on_popup set to 1 #242

Closed mmrwoods closed 1 year ago

mmrwoods commented 1 year ago

After updating vim-which-key recently I've noticed that buffer local mappings seem to be cached even with let g:which_key_run_map_on_popup = 1 in my vimrc.

Environment (please complete the following information):

Describe the bug With g:which_key_run_map_on_popup set to 1, if I create a buffer local mapping and load the which key menu, e.g. :WhichKey ',', my buffer local mapping is shown as expected. If I then open another buffer which does not have this mapping and again load the which key menu the buffer local mapping I added for the previous buffer is still shown.

To Reproduce

  1. Create the minimal vimrc min.vim:
set nocompatible
set runtimepath^=/Users/mwoods/dotfiles/vim/pack/bundle/start/which-key
set packpath=$VIMRUNTIME
syntax on
filetype plugin indent on

set timeoutlen=500

let g:which_key_run_map_on_popup = 1

let mapleader = ","
nnoremap <silent> , :WhichKey ','<cr>

nnoremap <leader>f :echo 'foo'<cr>

autocmd FileType markdown
  \ nnoremap <buffer> <leader>b :echo 'bar'<cr>
  1. Create foo.md and foo.vim files, e.g. echo foo > foo.md, echo foo > foo.vim
  2. Start vim with command: vim -u min.vim
  3. Type , and you will see the \f mapping in the menu.
  4. Type :edit foo.md<cr> followed by , and you will see both the \f and \b mappings in the menu.
  5. Type :verbose nmap<cr> and note that you can see the \b mapping in the list.
  6. Type :edit foo.vim<cr> followed by , and you will still see both the \f and \b mappings in the menu.
  7. Type :verbose map<cr> and note there is no \b mapping, yet it has appeared in the which key menu.

Expected behavior When g:which_key_run_map_on_popup is set to 1, the which key menu items should not be cached and buffer local mappings should only appear in the menu when editing the relevant buffer.

Screenshots Screenshot 2023-08-27 at 09 11 22 Which key menu after opening vim without editing a file.

Screenshot 2023-08-27 at 09 11 41 Which key menu after opening a markdown file (buffer local mapping)

Screenshot 2023-08-27 at 09 12 05 Which key menu after opening a non-markdown file (no buffer local mapping)

Additional context git bisect tells me this bug was introduced in commit 654dfc15, but I have not investigated further (if I find some time I'll try to track down the root cause and fix it in a PR).

mmrwoods commented 1 year ago

cc @rene-descartes2021, it looks like this bug was introduced in one of your commits. Restoring some deleted conditional code fixes this bug, but I don't know if it will break something else. Maybe you can confirm this fix?

Applying the following patch fixes the issue with cached buffer local mappings, but I don't know if it will break something else...

index bc96b91..a5c0c97 100644
--- a/autoload/which_key.vim
+++ b/autoload/which_key.vim
@@ -141,6 +141,9 @@ function! s:cache_key(mode, key)
   let mode = a:mode
   let key = a:key
   if !has_key(s:cache[mode], key) || g:which_key_run_map_on_popup
+    if key !=? '<C-I>'
+      let s:cache[mode][key] = {}
+    endif
     call which_key#mappings#parse(key, s:cache[mode], mode)
   endif
 endfunction