Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register readable prefix for grouped keymap settings #1134

Merged
merged 19 commits into from
Jan 22, 2024
Merged

Register readable prefix for grouped keymap settings #1134

merged 19 commits into from
Jan 22, 2024

Conversation

Cyberczy
Copy link
Contributor

@Cyberczy Cyberczy commented Jan 7, 2024

No description provided.

@ayamir
Copy link
Owner

ayamir commented Jan 8, 2024

IMO it's better to register prefix for which-key when the keymappings are binded, i.e., the time invoking the bind.nvim_load_mapping.

@Cyberczy
Copy link
Contributor Author

Cyberczy commented Jan 8, 2024

It's up to you. I'm just making a suggestion.

@ayamir
Copy link
Owner

ayamir commented Jan 8, 2024

It's up to you. I'm just making a suggestion.

OK. The reason why current approach doesn't work for keymappings binded to buffer may causes from the absense of the buffer info in the section defined in prefix.lua.

@Cheny-chui
Copy link
Contributor

I have made some improvements to this pr based on ayamir's suggestion, can you please give me modification access to this pr? @Cyberczy (I'm not sure if I need your action, but I really can't push to your repository). Or maybe need the maintainer's action @ayamir .

@Cyberczy
Copy link
Contributor Author

我根据ayamir的建议对这个pr做了一些改进,你能给我这个pr的修改权限吗?@Cyberczy(我不确定是否需要您的操作,但我确实无法自动到达您的存储库)。或者可能维护者的操作@ayamir

I don't have the authority, lol

@Cyberczy
Copy link
Contributor Author

Ok, I've just given you modification access. @Cheny-chui

@ayamir
Copy link
Owner

ayamir commented Jan 12, 2024

@CharlesChiuGit Could you please review this PR?

@CharlesChiuGit
Copy link
Collaborator

ok, let me take a look.

Copy link
Collaborator

@CharlesChiuGit CharlesChiuGit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz remove lazy-lock

lazy-lock.json Outdated Show resolved Hide resolved
@ayamir
Copy link
Owner

ayamir commented Jan 12, 2024

Current prefix settings are not enough and needed to be added manually. I'm thinking automatically setting whether is a better idea. Though this requires keymap setup to keep align with it.
Additionally, maybe we can refactor current keymap settings through this PR, such as:
<leader>D -> <leader>hd
<leader><leader>D -> <leader>hD
What do you think? @CharlesChiuGit @Jint-lzxy

@vabatta
Copy link

vabatta commented Jan 14, 2024

What is the general rationale behind the current key mapping?
Is it quick commands, frequent commands or "grouped" commands (like LazyVim which maps into groups like <leader>w for windows, <leader>b for buffers, ...)?

CharlesChiuGit
CharlesChiuGit previously approved these changes Jan 15, 2024
@CharlesChiuGit CharlesChiuGit dismissed their stale review January 15, 2024 03:36

more test still need to be done

@ayamir
Copy link
Owner

ayamir commented Jan 15, 2024

What is the general rationale behind the current key mapping? Is it quick commands, frequent commands or "grouped" commands (like LazyVim which maps into groups like <leader>w for windows, <leader>b for buffers, ...)?

Current keymap settings are mixed but a little confusing due to history reasons.

@ayamir ayamir changed the title Use Nerd Font to replace the +prefix (It's just a demo) Register readable prefix to grouped keymap settings Jan 15, 2024
@ayamir ayamir changed the title Register readable prefix to grouped keymap settings Register readable prefix for grouped keymap settings Jan 15, 2024
@CharlesChiuGit
Copy link
Collaborator

CharlesChiuGit commented Jan 15, 2024

lua\keymap\prefix.lua

--- Need to expand by hand
local prefix_desc = {
	["<leader>b"] = icons.ui_sep.Buffer .. "Buffer",
	["<leader>c"] = icons.ui_sep.Character .. "Character",
	["<leader>d"] = icons.ui_sep.Bug .. "Debug",
	["<leader>f"] = icons.ui_sep.Telescope .. "Fuzzy Find",
	["<leader>h"] = icons.ui_sep.Git .. "Git Hunk",
	["<leader>l"] = icons.ui_sep.Lsp .. "Lsp",
	["<leader>p"] = icons.ui_sep.Package .. "Package",
	["<leader>n"] = icons.ui_sep.FolderOpen .. "Nvim Tree",
	["<leader>s"] = icons.ui_sep.Tmux .. "Session",
	["<leader>t"] = icons.ui_sep.Lsp .. "Lsp",
	["<leader><space>"] = icons.ui_sep.Git .. "Git: Close diff",
}

After pressing leader key one time, some prefix still can be shown, hmmm.
image

@CharlesChiuGit
Copy link
Collaborator

CharlesChiuGit commented Jan 15, 2024

@ayamir @Jint-lzxy
since our keymap group is a bit confusing right now, our which-key prefix nerdfont hit might be a bit misleading too.

I wonder how u guys think about current layout.
image

for example:
image
image

@ayamir
Copy link
Owner

ayamir commented Jan 15, 2024

IMO it's better to refactor it.

@ayamir
Copy link
Owner

ayamir commented Jan 17, 2024

It looks tidy now.
image

@Cyberczy
Copy link
Contributor Author

LGTM!!!

@Cyberczy
Copy link
Contributor Author

The right half of which-key seems a bit empty. Perhaps we can add a bit of padding.

@ayamir
Copy link
Owner

ayamir commented Jan 17, 2024

The right half of which-key seems a bit empty. Perhaps we can add a bit of padding.

IMO it is unnecessary.

@Cyberczy
Copy link
Contributor Author

The right half of which-key seems a bit empty. Perhaps we can add a bit of padding.

IMO it is unnecessary.

Alright, it's just a suggestion

@Jint-lzxy
Copy link
Collaborator

Sorry for the delay! My sched's been a bit tight recently. But I'll probably review this in the next day or two lol

Copy link
Collaborator

@CharlesChiuGit CharlesChiuGit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@Jint-lzxy Jint-lzxy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! This PR is awesome lmao


(idk if this is related) But I'm wondering if the real issue might lie in the implementation of which-key.nvim, as I used a different approach to handle keymap registration, but it seems that (only recently) which-key sometimes displays +prefix even for named groups (like <leader>h, see the screenshot below):
S

Then I did a git bisect, and the issue seems to originate from this commit (strangely tho, this means that which-key thinks some of my keymap groups don't have a valid name, but obviously I did give them names). For some reason, in this call, the (to be processed) group still has a name, but immediately in the next line, that name disappears. Based on this, IMO a simpler solution 'might' be to revert 8503c0 (I can create a fork just like we made a temporary fix for lsp-signature), and then wait for folke's response (I'll open an issue for this); or just merge this PR. What do you guys think @ayamir @CharlesChiuGit?

@ayamir
Copy link
Owner

ayamir commented Jan 20, 2024

Made improvement for startup time.
Before:
image
Now:
image

@ayamir
Copy link
Owner

ayamir commented Jan 20, 2024

(idk if this is related) But I'm wondering if the real issue might lie in the implementation of which-key.nvim, as I used a different approach to handle keymap registration, but it seems that (only recently) which-key sometimes displays +prefix even for named groups (like <leader>h, see the screenshot below): S

I didn't view settings about <leader>h for group name in https://github.com/Jint-lzxy/nvimconfig/blob/f5e4e72cb7ab884c7afef4274c64a991ad850713/lua/modules/configs/tool/which-key.lua#L34-L145.
I tend to just merge it, notify user about the BREAKING CHANGE of keymap and make keybinding-related changes in wiki.

@Jint-lzxy
Copy link
Collaborator

I didn't view settings about <leader>h for group name ...

Ah, I think this probably isn't related. Sorry for the noise here! (I didn't notice that our impl is kinda like an event loop 🙈) Here's a similar issue with more details: folke/which-key.nvim#482.

@ayamir With that being said, I'm still a bit puzzled as to why we need to register these mappings dynamically. I feel like just statically registering them using require("which-key").register({}) would be enough, since which-key should do the grouping work (correct me if I'm misunderstanding something lmao). Registering a callback that gets invoked every 300 milliseconds seems a bit inefficient to me.

@ayamir
Copy link
Owner

ayamir commented Jan 21, 2024

With that being said, I'm still a bit puzzled as to why we need to register these mappings dynamically. I feel like just statically registering them using require("which-key").register({}) would be enough, since which-key should do the grouping work (correct me if I'm misunderstanding something lmao).

It's b/c the statical way only registers the group name once when init and doesn't work for our lsp-related keymaps are binded with buffer after LspAttach event. But after the keymaps were refactored, this problem is addressed now (b/c trouble.nvim-related keymaps were merged into the <leader>l group). Indeed, now we can use the statical way to avoid unnecessary complexity lol.

return function()
	local icons = {
		ui = require("modules.utils.icons").get("ui"),
		misc = require("modules.utils.icons").get("misc"),
		git = require("modules.utils.icons").get("git", true),
		cmp = require("modules.utils.icons").get("cmp", true),
	}

	require("which-key").register({
		["<leader>"] = {
			b = {
				name = icons.ui.Buffer .. " Buffer",
			},
			d = {
				name = icons.ui.Bug .. " Debug",
			},
			f = {
				name = icons.ui.Telescope .. " Fuzzy Find",
			},
			g = {
				name = icons.git.Git .. "Git",
			},
			l = {
				name = icons.misc.LspAvailable .. " Lsp",
			},
			n = {
				name = icons.ui.FolderOpen .. " Nvim Tree",
			},
			p = {
				name = icons.ui.Package .. " Package",
			},
			s = {
				name = icons.cmp.tmux .. "Session",
			},
		},
	})

	require("modules.utils").load_plugin("which-key", {
		plugins = {
			presets = {
				operators = false,
				motions = false,
				text_objects = false,
				windows = false,
				nav = false,
				z = true,
				g = true,
			},
		},

		icons = {
			breadcrumb = icons.ui.Separator,
			separator = icons.misc.Vbar,
			group = "",
		},

		window = {
			border = "none",
			position = "bottom",
			margin = { 1, 0, 1, 0 },
			padding = { 1, 1, 1, 1 },
			winblend = 0,
		},
	})
end

image

@Cyberczy
Copy link
Contributor Author

So, should we stick with statically registering them, or should we maintain the current state by dynamically registering them?

@ayamir
Copy link
Owner

ayamir commented Jan 22, 2024

It seems to me now that the statical way is better.

@ayamir ayamir merged commit de99f14 into ayamir:main Jan 22, 2024
2 checks passed
ttbug pushed a commit to ttbug/nvimconf that referenced this pull request Mar 27, 2024
* Use Nerd Font to replace the +prefix (It's just a demo)

* Move insert registration into bind.

* Fix unexpected shadow.

* format code.

* For tab instead of space.

* chore(ci): fix linting

* fix: works for buffer-related keymap by registering prefix immediately.

Signed-off-by: ayamir <[email protected]>

* chore(which-key): add more nerdfont for which-key prefix

* fix: works for <space> as prefix.

* refactor: update keymaps and defer register operation to shorten startup time.

* clean up.

* fixup: remove redundant code.

* perf: reduce number of register queue insertion.

Signed-off-by: ayamir <[email protected]>

* refactor: use the statical way to set group names.

* clean files.

Signed-off-by: ayamir <[email protected]>

---------

Signed-off-by: ayamir <[email protected]>
Co-authored-by: Chui <[email protected]>
Co-authored-by: CharlesChiuGit <[email protected]>
Co-authored-by: ayamir <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants