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.
"Choose import statement to add"
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.
Thank you very much! I wanted to find the correct solution for user cancellation.
I will put 0 as cancel for the prompt.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.