DEV Community

Discussion on: I just published my first Vim plugin, import-js-from-history

Collapse
 
idanarye profile image
Idan Arye

When using inputlist, the first item should be a prompt (like "Choose import statement to add" or something). Otherwise hitting <Esc>, or hitting <Cr> without entering a number, will make inputlist return 0 - which will insert the first import from the list, even though the user probably thought it will cancel the action (the builtin prompt even says explicitly "(empty cancels)"). If the first line is a prompt, not picking is the same as picking the prompt - which will do nothing.

Another advantage is that you can start the list from 1. Arrays start from 0, but as UI lists that start from 0 look weird...

While having the first item be the prompt is clearly the intention (it's even written in :help inputlist), but it looks like this function was meant to be used with an hard-coded list and the API is not comfortable when you want a generated one. In my plugins I usually write a wrapper function which also does pagination if the list is too long. You can steal it if you want.

Collapse
 
acro5piano profile image
Kay Gosho

Thank you very much! I wanted to find the correct solution for user cancellation.
I will put 0 as cancel for the prompt.