liuchengxu/vim-which-key

Comments at the end of map commands destroy the result

arlt opened this issue · 1 comments

arlt commented

Environment:

  • OS: CentOS 7.8.2003

  • (Neo)Vim version: v0.3.0

  • vim-which-key version: Master downloaded 2020-06-19 - sorry provided package in our company has no git information

  • Have you reproduced with a minimal vimrc: yes

Describe the bug
Popup shows mapping plus comment (if not explicitely set via g:which_key_map) like :nmap does. This is ok.
The problem: If you press the key, the comment isn't used as comment but as real text.

To Reproduce

  1. Create the minimal vimrc min.vim:
set nocompatible
set runtimepath^=/path/to/vim-which-key
syntax on
filetype plugin indent on
let mapleader = '\<Space>'
let maplocalleader = ','
noremap <silent> <leader>      :<c-u>WhichKey '<Space>'<CR>
nnoremap <silent> <localleader> :<c-u>WhichKey  ','<CR>
call which_key#register(',', "g:which_key_map")
let g:which_key_map = {}
let g:which_key_map.j = 'join while/for/if with do'
nnoremap <localleader>j mi$Ji;<Esc>`i" join while/for/if with do
  1. Start neovim with command: nvim -u min.vim

  2. Create the following content and position the cursor at the first line

for in in 1 2 3
do
:
done

If use press ,j you get the result:

for in in 1 2 3; do
:
done

Undo the joining of the lines.

  1. See error if using the same conversion using vim-what-key:
  • Press ,
  • Wait 1 second
  • See j → join while/for/if with do displayed from vim-what-key
  • Press j

Result:

for in in 1 2 3; do
:
in while/for/if with do
done

Expected behavior
The same result as without vim-what-key:

for in in 1 2 3; do
:
done

Additional context
It would be a great feature if the comments from the mappings would automaticall be used as description because it is much easier to maintain.

When you invoke the mapping via vim-which-key, it's actually the rhs from map entry that gets executed using feedkeys().

let Cmd = s:join('call', 'feedkeys("'.Cmd.'")')
endif
execute Cmd

In your case, the entry in map:

n  ,j          * mi$Ji;<Esc>`i" join while/for/if with do

will be actually executed as:

call feedkeys("mi$Ji;\<Esc>`i\" join while/for/if with do", "nt")

vim-which-key won't parse the rhs content, so the solution is not to put the comment at the end. If you have better solution, PR welcome.