From 24918c25650f8d68d7fa3c68236dfc6fdb9c62e6 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 15 Jul 2024 15:13:07 +0200 Subject: [PATCH] refactor: option names --- lua/lazyvim/config/keymaps.lua | 10 +++++----- lua/lazyvim/util/toggle.lua | 26 +++++++++----------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index 7bfa5dfc..f568bdac 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -117,14 +117,14 @@ map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning" }) -- toggle options LazyVim.toggle.map("uf", LazyVim.toggle.format()) LazyVim.toggle.map("uF", LazyVim.toggle.format(true)) -LazyVim.toggle.map("us", LazyVim.toggle("spell")) -LazyVim.toggle.map("uw", LazyVim.toggle("wrap")) -LazyVim.toggle.map("uL", LazyVim.toggle("relativenumber")) +LazyVim.toggle.map("us", LazyVim.toggle("spell", { name = "Spelling" })) +LazyVim.toggle.map("uw", LazyVim.toggle("wrap", { name = "Wrap" })) +LazyVim.toggle.map("uL", LazyVim.toggle("relativenumber", { name = "Relative Number" })) LazyVim.toggle.map("ud", LazyVim.toggle.diagnostics) LazyVim.toggle.map("ul", LazyVim.toggle.number) -LazyVim.toggle.map("uc", LazyVim.toggle("conceallevel", { 0, vim.o.conceallevel > 0 and vim.o.conceallevel or 2 })) +LazyVim.toggle.map( "uc", LazyVim.toggle("conceallevel", { values = { 0, vim.o.conceallevel > 0 and vim.o.conceallevel or 2 } })) LazyVim.toggle.map("uT", LazyVim.toggle.treesitter) -LazyVim.toggle.map("ub", LazyVim.toggle("background", { "light", "dark" })) +LazyVim.toggle.map("ub", LazyVim.toggle("background", { values = { "light", "dark" }, name = "Background" })) if vim.lsp.inlay_hint then LazyVim.toggle.map("uh", LazyVim.toggle.inlay_hints) end diff --git a/lua/lazyvim/util/toggle.lua b/lua/lazyvim/util/toggle.lua index 6d78e9e4..a81fa4d4 100644 --- a/lua/lazyvim/util/toggle.lua +++ b/lua/lazyvim/util/toggle.lua @@ -74,28 +74,20 @@ function M.format(buf) return ret end ----@param values? {[1]:any, [2]:any} -function M.option(option, values) +---@param opts? {values?: {[1]:any, [2]:any}, name?: string} +function M.option(option, opts) + opts = opts or {} + local name = opts.name or option + local on = opts.values and opts.values[2] or true + local off = opts.values and opts.values[1] or false ---@type lazyvim.Toggle local ret = { - name = option, + name = name, get = function() - if values then - return vim.opt_local[option]:get() == values[2] - end - return vim.opt_local[option]:get() + return vim.opt_local[option]:get() == on end, set = function(state) - if values then - if state then - vim.opt_local[option] = values[2] - else - vim.opt_local[option] = values[1] - end - else - ---@diagnostic disable-next-line: no-unknown - vim.opt_local[option] = state - end + vim.opt_local[option] = state and on or off end, } return ret