Skip to content

Commit 8ea96c1

Browse files
Jint-lzxyayamir
andauthored
fix(keymap.helpers): improve _toggle* helper functions (#1397)
* fix(keymap.helpers): improve `_toggle*` helper functions This commit refined the two `_toggle*` functions to cut down redundancy, reworded msgs, and made sure 0/1 are not used in `_toggle_diagnostic` bc it's bug-prone. Signed-off-by: Jint-lzxy <[email protected]> * refactor: change keymaps. --------- Signed-off-by: Jint-lzxy <[email protected]> Co-authored-by: ayamir <[email protected]>
1 parent 31005cc commit 8ea96c1

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

lua/keymap/completion.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ function M.lsp(buf)
6767
:with_silent()
6868
:with_buffer(buf)
6969
:with_desc("lsp: Show outgoing calls"),
70+
["n|<leader>lv"] = map_callback(function()
71+
_toggle_diagnostic()
72+
end)
73+
:with_noremap()
74+
:with_silent()
75+
:with_desc("lsp: Toggle virtual text display of current buffer"),
76+
["n|<leader>lh"] = map_callback(function()
77+
_toggle_inlayhint()
78+
end)
79+
:with_noremap()
80+
:with_silent()
81+
:with_desc("lsp: Toggle inlay hints dispaly of current buffer"),
7082
}
7183
bind.nvim_load_mapping(map)
7284

lua/keymap/editor.lua

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,6 @@ local mappings = {
5050
:with_silent()
5151
:with_desc("edit: Clear search highlight"),
5252
["n|<leader>o"] = map_cr("setlocal spell! spelllang=en_us"):with_desc("edit: Toggle spell check"),
53-
54-
-- Builtins: Lsp
55-
["n|<leader>td"] = map_callback(function()
56-
_toggle_diagnostic()
57-
end)
58-
:with_noremap()
59-
:with_silent()
60-
:with_desc("edit: Toggle global display of virtual text"),
61-
["n|<leader>th"] = map_callback(function()
62-
_toggle_inlayhint()
63-
end)
64-
:with_noremap()
65-
:with_silent()
66-
:with_desc("edit: Toggle global display of inlay hints"),
6753
},
6854
plugins = {
6955
-- Plugin: persisted.nvim

lua/keymap/helpers.lua

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ _G._telescope_collections = function(picker_type)
3838
:find()
3939
end
4040

41+
_G._toggle_inlayhint = function()
42+
local is_enabled = vim.lsp.inlay_hint.is_enabled()
43+
44+
vim.lsp.inlay_hint.enable(not is_enabled)
45+
vim.notify(
46+
(is_enabled and "Inlay hint disabled successfully" or "Inlay hint enabled successfully"),
47+
vim.log.levels.INFO,
48+
{ title = "LSP Inlay Hint" }
49+
)
50+
end
51+
52+
local _vt_enabled = require("core.settings").diagnostics_virtual_text
53+
_G._toggle_diagnostic = function()
54+
if vim.diagnostic.is_enabled() then
55+
_vt_enabled = not _vt_enabled
56+
vim.diagnostic[_vt_enabled and "show" or "hide"]()
57+
vim.notify(
58+
(_vt_enabled and "Virtual text is now displayed" or "Virtual text is now hidden"),
59+
vim.log.levels.INFO,
60+
{ title = "LSP Diagnostic" }
61+
)
62+
end
63+
end
64+
4165
_G._flash_esc_or_noh = function()
4266
local flash_active, state = pcall(function()
4367
return require("flash.plugins.char").state
@@ -65,32 +89,6 @@ _G._toggle_lazygit = function()
6589
vim.notify("Command [lazygit] not found!", vim.log.levels.ERROR, { title = "toggleterm.nvim" })
6690
end
6791
end
68-
69-
_G._toggle_inlayhint = function()
70-
if vim.lsp.inlay_hint.is_enabled() then
71-
vim.lsp.inlay_hint.enable(false)
72-
vim.notify("Disable inlay hint successfully!", vim.log.levels.INFO, { title = "LSP Inlay Hint" })
73-
else
74-
vim.lsp.inlay_hint.enable(true)
75-
vim.notify("Enable inlay hint successfully!", vim.log.levels.INFO, { title = "LSP Inlay Hint" })
76-
end
77-
end
78-
79-
local _diagnostic = 1
80-
_G._toggle_diagnostic = function()
81-
if vim.diagnostic.is_enabled() then
82-
if _diagnostic == 1 then
83-
_diagnostic = 0
84-
vim.diagnostic.hide()
85-
vim.notify("Hide virtual text successfully!", vim.log.levels.INFO, { title = "LSP Diagnostic" })
86-
else
87-
_diagnostic = 1
88-
vim.diagnostic.show()
89-
vim.notify("Show virtual text successfully!", vim.log.levels.INFO, { title = "LSP Diagnostic" })
90-
end
91-
end
92-
end
93-
9492
_G._async_compile_and_debug = function()
9593
local file_ext = vim.fn.expand("%:e")
9694
local file_path = vim.fn.expand("%:p")

0 commit comments

Comments
 (0)