Compare commits

...

9 Commits

9 changed files with 217 additions and 131 deletions

View File

@ -115,20 +115,19 @@ map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning" })
-- stylua: ignore start -- stylua: ignore start
-- toggle options -- toggle options
map("n", "<leader>uf", function() LazyVim.format.toggle() end, { desc = "Toggle Auto Format (Global)" }) LazyVim.toggle.map("<leader>uf", LazyVim.toggle.format())
map("n", "<leader>uF", function() LazyVim.format.toggle(true) end, { desc = "Toggle Auto Format (Buffer)" }) LazyVim.toggle.map("<leader>uF", LazyVim.toggle.format(true))
map("n", "<leader>us", function() LazyVim.toggle("spell") end, { desc = "Toggle Spelling" }) LazyVim.toggle.map("<leader>us", LazyVim.toggle("spell", { name = "Spelling" }))
map("n", "<leader>uw", function() LazyVim.toggle("wrap") end, { desc = "Toggle Word Wrap" }) LazyVim.toggle.map("<leader>uw", LazyVim.toggle("wrap", { name = "Wrap" }))
map("n", "<leader>uL", function() LazyVim.toggle("relativenumber") end, { desc = "Toggle Relative Line Numbers" }) LazyVim.toggle.map("<leader>uL", LazyVim.toggle("relativenumber", { name = "Relative Number" }))
map("n", "<leader>ul", function() LazyVim.toggle.number() end, { desc = "Toggle Line Numbers" }) LazyVim.toggle.map("<leader>ud", LazyVim.toggle.diagnostics)
map("n", "<leader>ud", function() LazyVim.toggle.diagnostics() end, { desc = "Toggle Diagnostics" }) LazyVim.toggle.map("<leader>ul", LazyVim.toggle.number)
local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3 LazyVim.toggle.map( "<leader>uc", LazyVim.toggle("conceallevel", { values = { 0, vim.o.conceallevel > 0 and vim.o.conceallevel or 2 } }))
map("n", "<leader>uc", function() LazyVim.toggle("conceallevel", false, {0, conceallevel}) end, { desc = "Toggle Conceal" }) LazyVim.toggle.map("<leader>uT", LazyVim.toggle.treesitter)
if vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint then LazyVim.toggle.map("<leader>ub", LazyVim.toggle("background", { values = { "light", "dark" }, name = "Background" }))
map( "n", "<leader>uh", function() LazyVim.toggle.inlay_hints() end, { desc = "Toggle Inlay Hints" }) if vim.lsp.inlay_hint then
LazyVim.toggle.map("<leader>uh", LazyVim.toggle.inlay_hints)
end end
map("n", "<leader>uT", function() if vim.b.ts_highlight then vim.treesitter.stop() else vim.treesitter.start() end end, { desc = "Toggle Treesitter Highlight" })
map("n", "<leader>ub", function() LazyVim.toggle("background", false, {"light", "dark"}) end, { desc = "Toggle Background" })
-- lazygit -- lazygit
map("n", "<leader>gg", function() LazyVim.lazygit( { cwd = LazyVim.root.git() }) end, { desc = "Lazygit (Root Dir)" }) map("n", "<leader>gg", function() LazyVim.lazygit( { cwd = LazyVim.root.git() }) end, { desc = "Lazygit (Root Dir)" })
@ -181,7 +180,7 @@ map("n", "<leader>w-", "<C-W>s", { desc = "Split Window Below", remap = true })
map("n", "<leader>w|", "<C-W>v", { desc = "Split Window Right", remap = true }) map("n", "<leader>w|", "<C-W>v", { desc = "Split Window Right", remap = true })
map("n", "<leader>-", "<C-W>s", { desc = "Split Window Below", remap = true }) map("n", "<leader>-", "<C-W>s", { desc = "Split Window Below", remap = true })
map("n", "<leader>|", "<C-W>v", { desc = "Split Window Right", remap = true }) map("n", "<leader>|", "<C-W>v", { desc = "Split Window Right", remap = true })
map("n", "<leader>wm", function() LazyVim.toggle.maximize() end, { desc = "Maximize Toggle" }) LazyVim.toggle.map("<leader>wm", LazyVim.toggle.maximize)
-- tabs -- tabs
map("n", "<leader><tab>l", "<cmd>tablast<cr>", { desc = "Last Tab" }) map("n", "<leader><tab>l", "<cmd>tablast<cr>", { desc = "Last Tab" })

View File

@ -140,20 +140,6 @@ return {
-- better deal with markdown code blocks -- better deal with markdown code blocks
markdown = true, markdown = true,
}, },
keys = {
{
"<leader>up",
function()
vim.g.minipairs_disable = not vim.g.minipairs_disable
if vim.g.minipairs_disable then
LazyVim.warn("Disabled auto pairs", { title = "Option" })
else
LazyVim.info("Enabled auto pairs", { title = "Option" })
end
end,
desc = "Toggle Auto Pairs",
},
},
config = function(_, opts) config = function(_, opts)
LazyVim.mini.pairs(opts) LazyVim.mini.pairs(opts)
end, end,

View File

@ -176,9 +176,9 @@ return {
{ "<leader>gh", group = "hunks" }, { "<leader>gh", group = "hunks" },
{ "<leader>q", group = "quit/session" }, { "<leader>q", group = "quit/session" },
{ "<leader>s", group = "search" }, { "<leader>s", group = "search" },
{ "<leader>u", group = "ui" }, { "<leader>u", group = "ui", icon = { icon = "󰙵 ", color = "cyan" } },
{ "<leader>w", group = "windows" }, { "<leader>w", group = "windows" },
{ "<leader>x", group = "diagnostics/quickfix" }, { "<leader>x", group = "diagnostics/quickfix", icon = { icon = "󱖫 ", color = "green" } },
{ "[", group = "prev" }, { "[", group = "prev" },
{ "]", group = "next" }, { "]", group = "next" },
{ "g", group = "goto" }, { "g", group = "goto" },
@ -193,7 +193,7 @@ return {
function() function()
require("which-key").show({ global = false }) require("which-key").show({ global = false })
end, end,
desc = "Buffer Local Keymaps (which-key)", desc = "Buffer Keymaps (which-key)",
}, },
}, },
config = function(_, opts) config = function(_, opts)

View File

@ -14,6 +14,16 @@ return {
end, { expr = true }) end, { expr = true })
end end
LazyVim.toggle.map("<leader>ua", {
name = "Mini Animate",
get = function()
return not vim.g.minianimate_disable
end,
set = function(state)
vim.g.minianimate_disable = not state
end,
})
local animate = require("mini.animate") local animate = require("mini.animate")
return { return {
resize = { resize = {

View File

@ -7,9 +7,9 @@ return {
"mason.nvim", "mason.nvim",
{ "williamboman/mason-lspconfig.nvim", config = function() end }, { "williamboman/mason-lspconfig.nvim", config = function() end },
}, },
---@class PluginLspOpts
opts = function() opts = function()
return { ---@class PluginLspOpts
local ret = {
-- options for vim.diagnostic.config() -- options for vim.diagnostic.config()
---@type vim.diagnostic.Opts ---@type vim.diagnostic.Opts
diagnostics = { diagnostics = {
@ -114,6 +114,7 @@ return {
-- ["*"] = function(server, opts) end, -- ["*"] = function(server, opts) end,
}, },
} }
return ret
end, end,
---@param opts PluginLspOpts ---@param opts PluginLspOpts
config = function(_, opts) config = function(_, opts)
@ -150,7 +151,7 @@ return {
and vim.bo[buffer].buftype == "" and vim.bo[buffer].buftype == ""
and not vim.tbl_contains(opts.inlay_hints.exclude, vim.bo[buffer].filetype) and not vim.tbl_contains(opts.inlay_hints.exclude, vim.bo[buffer].filetype)
then then
LazyVim.toggle.inlay_hints(buffer, true) vim.lsp.inlay_hint.enable(true, { bufnr = buffer })
end end
end) end)
end end

View File

@ -1,4 +1,14 @@
return { return {
{
"folke/which-key.nvim",
opts = {
spec = {
{ "<BS>", desc = "Decrement Selection", mode = "x" },
{ "<c-space>", desc = "Increment Selection", mode = { "x", "n" } },
},
},
},
-- Treesitter is a new parser generator tool that we can -- Treesitter is a new parser generator tool that we can
-- use in Neovim to power faster and more accurate -- use in Neovim to power faster and more accurate
-- syntax highlighting. -- syntax highlighting.

View File

@ -97,10 +97,19 @@ end
---@param buf? boolean ---@param buf? boolean
function M.toggle(buf) function M.toggle(buf)
M.enable(not M.enabled(), buf)
end
---@param enable? boolean
---@param buf? boolean
function M.enable(enable, buf)
if enable == nil then
enable = true
end
if buf then if buf then
vim.b.autoformat = not M.enabled() vim.b.autoformat = enable
else else
vim.g.autoformat = not M.enabled() vim.g.autoformat = enable
vim.b.autoformat = nil vim.b.autoformat = nil
end end
M.info() M.info()

View File

@ -121,6 +121,15 @@ end
---@param opts {skip_next: string, skip_ts: string[], skip_unbalanced: boolean, markdown: boolean} ---@param opts {skip_next: string, skip_ts: string[], skip_unbalanced: boolean, markdown: boolean}
function M.pairs(opts) function M.pairs(opts)
LazyVim.toggle.map("<leader>up", {
name = "Mini Pairs",
get = function()
return not vim.g.minipairs_disable
end,
set = function(state)
vim.g.minipairs_disable = not state
end,
})
local pairs = require("mini.pairs") local pairs = require("mini.pairs")
pairs.setup(opts) pairs.setup(opts)
local open = pairs.open local open = pairs.open

View File

@ -1,114 +1,176 @@
---@class lazyvim.util.toggle ---@class lazyvim.util.toggle
local M = {} local M = {}
---@param silent boolean? ---@class lazyvim.Toggle
---@param values? {[1]:any, [2]:any} ---@field name string
function M.option(option, silent, values) ---@field get fun():boolean
if values then ---@field set fun(state:boolean)
if vim.opt_local[option]:get() == values[1] then ---@overload fun()
---@diagnostic disable-next-line: no-unknown local T = {}
vim.opt_local[option] = values[2] T.__index = T
---@param lhs string
---@param toggle lazyvim.Toggle
function M.map(lhs, toggle)
LazyVim.safe_keymap_set("n", lhs, function()
local state = not toggle.get()
toggle.set(state)
if state then
LazyVim.info("Enabled " .. toggle.name, { title = toggle.name })
else else
---@diagnostic disable-next-line: no-unknown LazyVim.warn("Disabled " .. toggle.name, { title = toggle.name })
vim.opt_local[option] = values[1]
end end
return LazyVim.info("Set " .. option .. " to " .. vim.opt_local[option]:get(), { title = "Option" }) end, { desc = "Toggle" .. toggle.name })
M.wk(lhs, toggle)
end
function M.wk(lhs, toggle)
if not LazyVim.has("which-key.nvim") then
return
end end
---@diagnostic disable-next-line: no-unknown require("which-key").add({
vim.opt_local[option] = not vim.opt_local[option]:get() {
if not silent then lhs,
if vim.opt_local[option]:get() then icon = function()
LazyVim.info("Enabled " .. option, { title = "Option" }) return toggle.get() and { icon = "", color = "green" } or { icon = "", color = "yellow" }
end,
desc = function()
return (toggle.get() and "Disable " or "Enable ") .. toggle.name
end,
},
})
end
---@type lazyvim.Toggle
M.treesitter = {
name = "Treesitter Highlight",
get = function()
return vim.b.ts_highlight
end,
set = function(state)
if state then
vim.treesitter.start()
else else
LazyVim.warn("Disabled " .. option, { title = "Option" }) vim.treesitter.stop()
end end
end end,
}
---@param buf? boolean
function M.format(buf)
---@type lazyvim.Toggle
local ret = {
name = "Auto Format (" .. (buf and "Buffer" or "Global") .. ")",
get = function()
if not buf then
return vim.g.autoformat == nil or vim.g.autoformat
end
return LazyVim.format.enabled()
end,
set = function(state)
LazyVim.format.enable(state, buf)
end,
}
return ret
end
---@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 = name,
get = function()
return vim.opt_local[option]:get() == on
end,
set = function(state)
vim.opt_local[option] = state and on or off
end,
}
return ret
end end
local nu = { number = true, relativenumber = true } local nu = { number = true, relativenumber = true }
function M.number() ---@type lazyvim.Toggle
if vim.opt_local.number:get() or vim.opt_local.relativenumber:get() then M.number = {
nu = { number = vim.opt_local.number:get(), relativenumber = vim.opt_local.relativenumber:get() } name = "Line Numbers",
vim.opt_local.number = false get = function()
vim.opt_local.relativenumber = false return vim.opt_local.number:get() or vim.opt_local.relativenumber:get()
LazyVim.warn("Disabled line numbers", { title = "Option" }) end,
else set = function(state)
vim.opt_local.number = nu.number if state then
vim.opt_local.relativenumber = nu.relativenumber vim.opt_local.number = nu.number
LazyVim.info("Enabled line numbers", { title = "Option" }) vim.opt_local.relativenumber = nu.relativenumber
end else
end nu = { number = vim.opt_local.number:get(), relativenumber = vim.opt_local.relativenumber:get() }
vim.opt_local.number = false
local enabled = true vim.opt_local.relativenumber = false
function M.diagnostics()
-- if this Neovim version supports checking if diagnostics are enabled
-- then use that for the current state
if vim.diagnostic.is_enabled then
enabled = vim.diagnostic.is_enabled()
elseif vim.diagnostic.is_disabled then
enabled = not vim.diagnostic.is_disabled()
end
enabled = not enabled
if enabled then
vim.diagnostic.enable()
LazyVim.info("Enabled diagnostics", { title = "Diagnostics" })
else
vim.diagnostic.disable()
LazyVim.warn("Disabled diagnostics", { title = "Diagnostics" })
end
end
---@param buf? number
---@param value? boolean
function M.inlay_hints(buf, value)
local ih = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint
if type(ih) == "function" then
ih(buf, value)
elseif type(ih) == "table" and ih.enable then
if value == nil then
value = not ih.is_enabled({ bufnr = buf or 0 })
end end
ih.enable(value, { bufnr = buf }) end,
end }
end
---@type lazyvim.Toggle
M.diagnostics = {
name = "Diagnostics",
get = function()
return vim.diagnostic.is_enabled and vim.diagnostic.is_enabled()
end,
set = vim.diagnostic.enable,
}
---@type lazyvim.Toggle
M.inlay_hints = {
name = "Inlay Hints",
get = function()
return vim.lsp.inlay_hint.is_enabled({ bufnr = 0 })
end,
set = function(state)
vim.lsp.inlay_hint.enable(state, { bufnr = 0 })
end,
}
---@type {k:string, v:any}[] ---@type {k:string, v:any}[]
M._maximized = nil M._maximized = nil
---@param state boolean? ---@type lazyvim.Toggle
function M.maximize(state) M.maximize = {
if state == (M._maximized ~= nil) then name = "Maximize",
return get = function()
end return M._maximized ~= nil
if M._maximized then end,
for _, opt in ipairs(M._maximized) do set = function(state)
vim.o[opt.k] = opt.v if state then
M._maximized = {}
local function set(k, v)
table.insert(M._maximized, 1, { k = k, v = vim.o[k] })
vim.o[k] = v
end
set("winwidth", 999)
set("winheight", 999)
set("winminwidth", 10)
set("winminheight", 4)
vim.cmd("wincmd =")
-- `QuitPre` seems to be executed even if we quit a normal window, so we don't want that
-- `VimLeavePre` might be another consideration? Not sure about differences between the 2
vim.api.nvim_create_autocmd("ExitPre", {
once = true,
group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }),
desc = "Restore width/height when close Neovim while maximized",
callback = function()
M.maximize.set(false)
end,
})
else
for _, opt in ipairs(M._maximized) do
vim.o[opt.k] = opt.v
end
M._maximized = nil
vim.cmd("wincmd =")
end end
M._maximized = nil end,
vim.cmd("wincmd =") }
else
M._maximized = {}
local function set(k, v)
table.insert(M._maximized, 1, { k = k, v = vim.o[k] })
vim.o[k] = v
end
set("winwidth", 999)
set("winheight", 999)
set("winminwidth", 10)
set("winminheight", 4)
vim.cmd("wincmd =")
end
-- `QuitPre` seems to be executed even if we quit a normal window, so we don't want that
-- `VimLeavePre` might be another consideration? Not sure about differences between the 2
vim.api.nvim_create_autocmd("ExitPre", {
once = true,
group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }),
desc = "Restore width/height when close Neovim while maximized",
callback = function()
M.maximize(false)
end,
})
end
setmetatable(M, { setmetatable(M, {
__call = function(m, ...) __call = function(m, ...)