2023-10-10 19:29:24 +02:00
|
|
|
---@class lazyvim.util.ui
|
2023-10-03 14:56:09 +02:00
|
|
|
local M = {}
|
|
|
|
|
2024-11-13 21:11:32 +01:00
|
|
|
-- foldtext for Neovim < 0.10.0
|
2023-10-03 14:56:28 +02:00
|
|
|
function M.foldtext()
|
2024-11-13 21:11:32 +01:00
|
|
|
return vim.api.nvim_buf_get_lines(0, vim.v.lnum - 1, vim.v.lnum, false)[1]
|
|
|
|
end
|
2023-10-03 17:35:14 +02:00
|
|
|
|
2024-11-13 21:11:32 +01:00
|
|
|
-- optimized treesitter foldexpr for Neovim >= 0.10.0
|
|
|
|
function M.foldexpr()
|
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
|
|
|
if vim.b[buf].ts_folds == nil then
|
|
|
|
-- as long as we don't have a filetype, don't bother
|
|
|
|
-- checking if treesitter is available (it won't)
|
|
|
|
if vim.bo[buf].filetype == "" then
|
|
|
|
return "0"
|
|
|
|
end
|
2024-11-18 21:27:34 +01:00
|
|
|
if vim.bo[buf].filetype:find("dashboard") then
|
|
|
|
vim.b[buf].ts_folds = false
|
|
|
|
else
|
|
|
|
vim.b[buf].ts_folds = pcall(vim.treesitter.get_parser, buf)
|
|
|
|
end
|
2023-10-03 17:35:14 +02:00
|
|
|
end
|
2024-11-13 21:11:32 +01:00
|
|
|
return vim.b[buf].ts_folds and vim.treesitter.foldexpr() or "0"
|
2023-10-03 14:56:28 +02:00
|
|
|
end
|
|
|
|
|
2024-03-26 12:41:57 +01:00
|
|
|
---@return {fg?:string}?
|
2023-10-10 19:29:24 +02:00
|
|
|
function M.fg(name)
|
2024-11-13 21:14:29 +01:00
|
|
|
local hl = vim.api.nvim_get_hl(0, { name = name, link = false })
|
|
|
|
local fg = hl and hl.fg or hl.foreground
|
|
|
|
return fg and { fg = string.format("#%06x", fg) } or nil
|
2023-10-10 19:29:24 +02:00
|
|
|
end
|
|
|
|
|
2024-11-07 15:54:47 +01:00
|
|
|
function M.maximize()
|
|
|
|
---@type {k:string, v:any}[]?
|
|
|
|
local maximized = nil
|
2024-12-02 22:34:37 +01:00
|
|
|
local ret
|
|
|
|
ret = Snacks.toggle({
|
2024-11-07 15:54:47 +01:00
|
|
|
name = "Maximize",
|
|
|
|
get = function()
|
|
|
|
return maximized ~= nil
|
|
|
|
end,
|
|
|
|
set = function(state)
|
|
|
|
if state then
|
|
|
|
maximized = {}
|
|
|
|
local function set(k, v)
|
|
|
|
table.insert(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", {
|
|
|
|
group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }),
|
|
|
|
desc = "Restore width/height when close Neovim while maximized",
|
|
|
|
callback = function()
|
2024-12-02 22:34:37 +01:00
|
|
|
ret:set(false)
|
2024-11-07 15:54:47 +01:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
for _, opt in ipairs(maximized) do
|
|
|
|
vim.o[opt.k] = opt.v
|
|
|
|
end
|
|
|
|
maximized = nil
|
|
|
|
vim.cmd("wincmd =")
|
2024-05-17 10:10:28 +02:00
|
|
|
end
|
2024-11-07 15:54:47 +01:00
|
|
|
end,
|
|
|
|
})
|
2024-12-02 22:34:37 +01:00
|
|
|
return ret
|
2024-05-17 10:10:28 +02:00
|
|
|
end
|
|
|
|
|
2023-10-03 14:56:09 +02:00
|
|
|
return M
|