feat: add WK plugin#447
Conversation
I have way too many surrounds to rememeber them, and having hints is a must. This commit adds an option WK plugin that shows surround hints. ADRs: - Used the WK plugin functionality instead of WK expand, because it fits the use-case better: we only have a single-level of hints, we can keep most of the added functionality/complexity in a single module. - Used the hardcoded "⌨S" keymap: WK requires a keymap. "⌨" as the prefix practically guarantees no conflicts. - Requiring providing labels instead of automatically generating them: there are some corner-cases that automation wouldn't be able to solve, e.g., `i`. Better to have one mechanism for labels than have manual labels and automatic labels. Known limitations: - This plugin can not accept arbitrary characters. WK requires registering all possible answers, so I decided against it to not pollute the popup (there’s `i` for arbitrary surrounds). - This plugin shows all possible surrounds regardless of whether a surround is available. It makes the implementation simpler, but could be worked on if desired.
|
Related ticket (#205). |
|
After looking at things a little bit closer, is it possible to factor this code out into a separate plugin? It seems like while small, it still does affect some of the core behavior of the plugin (i.e. by modifying the |
|
Sounds fair that we make it more modular and put it into a separate plugin. There are two issues I’d like to discuss: It would be very beneficial if nvim-surround (NS) had The plugin will have to monkey patch LMK what you think. |
|
Sorry, what is the difference between a label and a description? I feel like having the which-key integration define the labels shouldn't be too much more work, given that the default surrounds haven't really changed in ~4 years, and I don't expect them to change in the forseeable future |
They are two words for the same thing. I’ll use “description” now on. I meant that the current relationship between Which-Key and Neovim keymaps is that Neovim keeps the state of all keymaps together with their descriptions. Which-key doesn’t have a separate table for descriptions. graph LR
subgraph neovim
keymaps[(Keymaps with descriptions)]
end
subgraph which-key
key-tree[(Key tree)]
end
which-key --> keymaps
Note Which-key happens to post-process keymaps into a TRIE-like structure for WK’s key traversal, but that’s irrelevant for our discussion. I believe that a similar approach where Nvim-surround maintains descriptions (i.e., the approach used in this PR) would be best for the WK nvim-surround plugin. What if the WK plugin keeps its own descriptions?
I don’t think so. I think it will be meaningfully more complicated and fragile to keep labels it the WK plugins. Let’s walk through implications of such an architecture. The WK plugin (WKP) will have to have its own tables that map chars to surrounds. Nvim-surround (NS) has a global table and buffer tables so WKP will have to have such tables as well in order to accurately represent descriptions. The big issue with this is that we have a split-brain problem. WKP will have to make sure that its state is consistent with NS. All those global and per-buffer tables across both plugins will have to have the same chars (keys) to provide consistent experience. graph LR
subgraph nvim-surround
ns-surrounds[(char to surrounds)]
end
subgraph wk-plugin
wkp-surrounds[(char to descriptions)]
end
ns-surrounds <-.->|must keep consistent| wkp-surrounds
A globally optimal solution would keep the necessary state entirely within NS. Then there’s an issue of how both tables are filled. Users can provide their own surround to NS. WKP will have to have its own API for providing descriptions, and it would either mean that:
Both options make the plugin more burdensome, because they add additional burdens on the user (additional APIs). LMK if this is still unclear. |
|
I think I am OK with adding a |
Sounds good, I think. I will now fork this PR off into a separate plugin to make sure that those labels are the only needed feature. I will be back with either label-only PR or further comments. |
|
OK, I got the plugin ready at https://github.com/gregorias/nvim-surround-wk and the label PR in #449. I’m closing this PR. |
Add labels to surrounds. This will be useful to plugins that show key hints. For discussion, see kylechui#447.
Add labels to surrounds. This will be useful to plugins that show key hints. For discussion, see #447.
I have way too many surrounds to rememeber them, and having hints is a must. This commit adds an option WK plugin that shows surround hints.
ADRs:
i. Better to have one mechanism for labels than have manual labels and automatic labels.Known limitations:
ifor arbitrary surrounds).