Skip to content

Commit

Permalink
fix(keymap.helpers): improve _toggle* helper functions (#1397)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Jint-lzxy and ayamir authored Jan 30, 2025
1 parent 31005cc commit 8ea96c1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
12 changes: 12 additions & 0 deletions lua/keymap/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ function M.lsp(buf)
:with_silent()
:with_buffer(buf)
:with_desc("lsp: Show outgoing calls"),
["n|<leader>lv"] = map_callback(function()
_toggle_diagnostic()
end)
:with_noremap()
:with_silent()
:with_desc("lsp: Toggle virtual text display of current buffer"),
["n|<leader>lh"] = map_callback(function()
_toggle_inlayhint()
end)
:with_noremap()
:with_silent()
:with_desc("lsp: Toggle inlay hints dispaly of current buffer"),
}
bind.nvim_load_mapping(map)

Expand Down
14 changes: 0 additions & 14 deletions lua/keymap/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ local mappings = {
:with_silent()
:with_desc("edit: Clear search highlight"),
["n|<leader>o"] = map_cr("setlocal spell! spelllang=en_us"):with_desc("edit: Toggle spell check"),

-- Builtins: Lsp
["n|<leader>td"] = map_callback(function()
_toggle_diagnostic()
end)
:with_noremap()
:with_silent()
:with_desc("edit: Toggle global display of virtual text"),
["n|<leader>th"] = map_callback(function()
_toggle_inlayhint()
end)
:with_noremap()
:with_silent()
:with_desc("edit: Toggle global display of inlay hints"),
},
plugins = {
-- Plugin: persisted.nvim
Expand Down
50 changes: 24 additions & 26 deletions lua/keymap/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ _G._telescope_collections = function(picker_type)
:find()
end

_G._toggle_inlayhint = function()
local is_enabled = vim.lsp.inlay_hint.is_enabled()

vim.lsp.inlay_hint.enable(not is_enabled)
vim.notify(
(is_enabled and "Inlay hint disabled successfully" or "Inlay hint enabled successfully"),
vim.log.levels.INFO,
{ title = "LSP Inlay Hint" }
)
end

local _vt_enabled = require("core.settings").diagnostics_virtual_text
_G._toggle_diagnostic = function()
if vim.diagnostic.is_enabled() then
_vt_enabled = not _vt_enabled
vim.diagnostic[_vt_enabled and "show" or "hide"]()
vim.notify(
(_vt_enabled and "Virtual text is now displayed" or "Virtual text is now hidden"),
vim.log.levels.INFO,
{ title = "LSP Diagnostic" }
)
end
end

_G._flash_esc_or_noh = function()
local flash_active, state = pcall(function()
return require("flash.plugins.char").state
Expand Down Expand Up @@ -65,32 +89,6 @@ _G._toggle_lazygit = function()
vim.notify("Command [lazygit] not found!", vim.log.levels.ERROR, { title = "toggleterm.nvim" })
end
end

_G._toggle_inlayhint = function()
if vim.lsp.inlay_hint.is_enabled() then
vim.lsp.inlay_hint.enable(false)
vim.notify("Disable inlay hint successfully!", vim.log.levels.INFO, { title = "LSP Inlay Hint" })
else
vim.lsp.inlay_hint.enable(true)
vim.notify("Enable inlay hint successfully!", vim.log.levels.INFO, { title = "LSP Inlay Hint" })
end
end

local _diagnostic = 1
_G._toggle_diagnostic = function()
if vim.diagnostic.is_enabled() then
if _diagnostic == 1 then
_diagnostic = 0
vim.diagnostic.hide()
vim.notify("Hide virtual text successfully!", vim.log.levels.INFO, { title = "LSP Diagnostic" })
else
_diagnostic = 1
vim.diagnostic.show()
vim.notify("Show virtual text successfully!", vim.log.levels.INFO, { title = "LSP Diagnostic" })
end
end
end

_G._async_compile_and_debug = function()
local file_ext = vim.fn.expand("%:e")
local file_path = vim.fn.expand("%:p")
Expand Down

0 comments on commit 8ea96c1

Please sign in to comment.