2022-12-24 18:26:09 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2023-06-12 14:28:10 +08:00
|
|
|
local Util = require("lazy.util")
|
2023-10-09 17:25:42 +08:00
|
|
|
local ViewConfig = require("lazy.view.config")
|
2022-12-24 18:26:09 +08:00
|
|
|
|
2023-01-05 23:52:02 +08:00
|
|
|
---@class LazyFloatOptions
|
2022-12-24 18:26:09 +08:00
|
|
|
---@field buf? number
|
|
|
|
---@field file? string
|
|
|
|
---@field margin? {top?:number, right?:number, bottom?:number, left?:number}
|
2022-12-30 17:23:12 +08:00
|
|
|
---@field size? {width:number, height:number}
|
2023-01-05 23:52:02 +08:00
|
|
|
---@field zindex? number
|
|
|
|
---@field style? "" | "minimal"
|
|
|
|
---@field border? "none" | "single" | "double" | "rounded" | "solid" | "shadow"
|
2023-05-27 20:28:09 +08:00
|
|
|
---@field title? string
|
|
|
|
---@field title_pos? "center" | "left" | "right"
|
2023-06-03 16:45:53 +08:00
|
|
|
---@field persistent? boolean
|
|
|
|
---@field ft? string
|
|
|
|
---@field noautocmd? boolean
|
2024-03-27 02:52:16 +08:00
|
|
|
---@field backdrop? float
|
2022-12-24 18:26:09 +08:00
|
|
|
|
|
|
|
---@class LazyFloat
|
|
|
|
---@field buf number
|
|
|
|
---@field win number
|
2023-01-05 23:52:02 +08:00
|
|
|
---@field opts LazyFloatOptions
|
|
|
|
---@field win_opts LazyWinOpts
|
2024-03-27 02:52:16 +08:00
|
|
|
---@field backdrop_buf number
|
|
|
|
---@field backdrop_win number
|
2024-03-27 15:48:55 +08:00
|
|
|
---@field id number
|
2023-01-05 23:52:02 +08:00
|
|
|
---@overload fun(opts?:LazyFloatOptions):LazyFloat
|
2022-12-24 18:26:09 +08:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
setmetatable(M, {
|
|
|
|
__call = function(_, ...)
|
|
|
|
return M.new(...)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2024-03-27 15:48:55 +08:00
|
|
|
local _id = 0
|
|
|
|
local function next_id()
|
|
|
|
_id = _id + 1
|
|
|
|
return _id
|
|
|
|
end
|
|
|
|
|
2023-01-05 23:52:02 +08:00
|
|
|
---@param opts? LazyFloatOptions
|
2022-12-24 18:26:09 +08:00
|
|
|
function M.new(opts)
|
|
|
|
local self = setmetatable({}, { __index = M })
|
|
|
|
return self:init(opts)
|
|
|
|
end
|
|
|
|
|
2023-01-05 23:52:02 +08:00
|
|
|
---@param opts? LazyFloatOptions
|
2022-12-24 18:26:09 +08:00
|
|
|
function M:init(opts)
|
2023-06-03 20:40:14 +08:00
|
|
|
require("lazy.view.colors").setup()
|
2024-03-27 15:48:55 +08:00
|
|
|
self.id = next_id()
|
2023-01-05 23:52:02 +08:00
|
|
|
self.opts = vim.tbl_deep_extend("force", {
|
|
|
|
size = Config.options.ui.size,
|
|
|
|
style = "minimal",
|
2023-05-22 02:51:41 +08:00
|
|
|
border = Config.options.ui.border or "none",
|
2024-03-27 02:52:16 +08:00
|
|
|
backdrop = Config.options.ui.backdrop or 60,
|
2023-01-05 23:52:02 +08:00
|
|
|
zindex = 50,
|
|
|
|
}, opts or {})
|
|
|
|
|
|
|
|
---@class LazyWinOpts
|
|
|
|
---@field width number
|
|
|
|
---@field height number
|
|
|
|
---@field row number
|
|
|
|
---@field col number
|
|
|
|
self.win_opts = {
|
|
|
|
relative = "editor",
|
|
|
|
style = self.opts.style ~= "" and self.opts.style or nil,
|
|
|
|
border = self.opts.border,
|
|
|
|
zindex = self.opts.zindex,
|
2023-06-03 16:45:53 +08:00
|
|
|
noautocmd = self.opts.noautocmd,
|
2023-05-27 20:28:09 +08:00
|
|
|
title = self.opts.title,
|
|
|
|
title_pos = self.opts.title and self.opts.title_pos or nil,
|
2023-01-05 23:52:02 +08:00
|
|
|
}
|
2022-12-24 18:26:09 +08:00
|
|
|
self:mount()
|
2024-03-27 15:48:55 +08:00
|
|
|
self:on("VimEnter", function()
|
|
|
|
vim.schedule(function()
|
|
|
|
if not self:win_valid() then
|
|
|
|
self:close()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end, { buffer = false })
|
2022-12-24 18:26:09 +08:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:layout()
|
|
|
|
local function size(max, value)
|
|
|
|
return value > 1 and math.min(value, max) or math.floor(max * value)
|
|
|
|
end
|
2023-01-05 23:52:02 +08:00
|
|
|
self.win_opts.width = size(vim.o.columns, self.opts.size.width)
|
|
|
|
self.win_opts.height = size(vim.o.lines, self.opts.size.height)
|
|
|
|
self.win_opts.row = math.floor((vim.o.lines - self.win_opts.height) / 2)
|
|
|
|
self.win_opts.col = math.floor((vim.o.columns - self.win_opts.width) / 2)
|
2022-12-24 18:26:09 +08:00
|
|
|
|
2023-05-22 02:51:41 +08:00
|
|
|
if self.opts.border ~= "none" then
|
|
|
|
self.win_opts.row = self.win_opts.row - 1
|
|
|
|
self.win_opts.col = self.win_opts.col - 1
|
|
|
|
end
|
|
|
|
|
2022-12-24 18:26:09 +08:00
|
|
|
if self.opts.margin then
|
|
|
|
if self.opts.margin.top then
|
2023-01-05 23:52:02 +08:00
|
|
|
self.win_opts.height = self.win_opts.height - self.opts.margin.top
|
|
|
|
self.win_opts.row = self.win_opts.row + self.opts.margin.top
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
|
|
|
if self.opts.margin.right then
|
2023-01-05 23:52:02 +08:00
|
|
|
self.win_opts.width = self.win_opts.width - self.opts.margin.right
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
|
|
|
if self.opts.margin.bottom then
|
2023-01-05 23:52:02 +08:00
|
|
|
self.win_opts.height = self.win_opts.height - self.opts.margin.bottom
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
|
|
|
if self.opts.margin.left then
|
2023-01-05 23:52:02 +08:00
|
|
|
self.win_opts.width = self.win_opts.width - self.opts.margin.left
|
|
|
|
self.win_opts.col = self.win_opts.col + self.opts.margin.left
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:mount()
|
2023-06-03 16:45:53 +08:00
|
|
|
if self:buf_valid() then
|
|
|
|
-- keep existing buffer
|
|
|
|
self.buf = self.buf
|
|
|
|
elseif self.opts.file then
|
2022-12-24 18:26:09 +08:00
|
|
|
self.buf = vim.fn.bufadd(self.opts.file)
|
2023-10-13 17:37:38 +08:00
|
|
|
vim.bo[self.buf].readonly = true
|
|
|
|
vim.bo[self.buf].swapfile = false
|
2022-12-24 18:26:09 +08:00
|
|
|
vim.fn.bufload(self.buf)
|
|
|
|
vim.bo[self.buf].modifiable = false
|
|
|
|
elseif self.opts.buf then
|
|
|
|
self.buf = self.opts.buf
|
|
|
|
else
|
2023-06-03 16:45:53 +08:00
|
|
|
self.buf = vim.api.nvim_create_buf(false, true)
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
|
|
|
|
2024-05-04 16:01:20 +08:00
|
|
|
local normal, has_bg
|
|
|
|
if vim.fn.has("nvim-0.9.0") == 0 then
|
|
|
|
normal = vim.api.nvim_get_hl_by_name("Normal", true)
|
|
|
|
has_bg = normal and normal.background ~= nil
|
|
|
|
else
|
|
|
|
normal = vim.api.nvim_get_hl(0, { name = "Normal" })
|
|
|
|
has_bg = normal and normal.bg ~= nil
|
|
|
|
end
|
2024-03-28 21:52:05 +08:00
|
|
|
|
|
|
|
if has_bg and self.opts.backdrop and self.opts.backdrop < 100 and vim.o.termguicolors then
|
2024-03-27 02:52:16 +08:00
|
|
|
self.backdrop_buf = vim.api.nvim_create_buf(false, true)
|
|
|
|
self.backdrop_win = vim.api.nvim_open_win(self.backdrop_buf, false, {
|
|
|
|
relative = "editor",
|
|
|
|
width = vim.o.columns,
|
|
|
|
height = vim.o.lines,
|
|
|
|
row = 0,
|
|
|
|
col = 0,
|
|
|
|
style = "minimal",
|
|
|
|
focusable = false,
|
|
|
|
zindex = self.opts.zindex - 1,
|
|
|
|
})
|
|
|
|
vim.api.nvim_set_hl(0, "LazyBackdrop", { bg = "#000000", default = true })
|
|
|
|
Util.wo(self.backdrop_win, "winhighlight", "Normal:LazyBackdrop")
|
|
|
|
Util.wo(self.backdrop_win, "winblend", self.opts.backdrop)
|
|
|
|
vim.bo[self.backdrop_buf].buftype = "nofile"
|
2024-03-30 03:28:45 +08:00
|
|
|
vim.bo[self.backdrop_buf].filetype = "lazy_backdrop"
|
2024-03-27 02:52:16 +08:00
|
|
|
end
|
|
|
|
|
2022-12-24 18:26:09 +08:00
|
|
|
self:layout()
|
2023-01-05 23:52:02 +08:00
|
|
|
self.win = vim.api.nvim_open_win(self.buf, true, self.win_opts)
|
2024-03-27 15:48:55 +08:00
|
|
|
self:on("WinClosed", function()
|
|
|
|
self:close()
|
|
|
|
self:augroup(true)
|
|
|
|
end, { win = true })
|
2022-12-24 18:26:09 +08:00
|
|
|
self:focus()
|
2024-03-27 15:48:55 +08:00
|
|
|
self:on_key(ViewConfig.keys.close, self.close)
|
|
|
|
self:on({ "BufDelete", "BufHidden" }, self.close)
|
2022-12-24 18:26:09 +08:00
|
|
|
|
2023-06-03 16:45:53 +08:00
|
|
|
if vim.bo[self.buf].buftype == "" then
|
|
|
|
vim.bo[self.buf].buftype = "nofile"
|
|
|
|
end
|
2022-12-24 18:26:09 +08:00
|
|
|
if vim.bo[self.buf].filetype == "" then
|
2023-06-03 16:45:53 +08:00
|
|
|
vim.bo[self.buf].filetype = self.opts.ft or "lazy"
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
2023-01-11 20:43:17 +08:00
|
|
|
|
|
|
|
local function opts()
|
2023-06-03 16:45:53 +08:00
|
|
|
vim.bo[self.buf].bufhidden = self.opts.persistent and "hide" or "wipe"
|
2023-06-12 14:28:10 +08:00
|
|
|
Util.wo(self.win, "conceallevel", 3)
|
|
|
|
Util.wo(self.win, "foldenable", false)
|
|
|
|
Util.wo(self.win, "spell", false)
|
|
|
|
Util.wo(self.win, "wrap", true)
|
|
|
|
Util.wo(self.win, "winhighlight", "Normal:LazyNormal")
|
|
|
|
Util.wo(self.win, "colorcolumn", "")
|
2023-01-11 20:43:17 +08:00
|
|
|
end
|
|
|
|
opts()
|
2022-12-24 18:26:09 +08:00
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd("VimResized", {
|
|
|
|
callback = function()
|
2023-01-12 00:13:03 +08:00
|
|
|
if not (self.win and vim.api.nvim_win_is_valid(self.win)) then
|
2022-12-24 18:26:09 +08:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
self:layout()
|
|
|
|
local config = {}
|
|
|
|
for _, key in ipairs({ "relative", "width", "height", "col", "row" }) do
|
2022-12-26 17:24:47 +08:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2023-01-05 23:52:02 +08:00
|
|
|
config[key] = self.win_opts[key]
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
2023-01-11 20:43:17 +08:00
|
|
|
config.style = self.opts.style ~= "" and self.opts.style or nil
|
2022-12-24 18:26:09 +08:00
|
|
|
vim.api.nvim_win_set_config(self.win, config)
|
2024-03-27 02:52:16 +08:00
|
|
|
|
|
|
|
if self.backdrop_win and vim.api.nvim_win_is_valid(self.backdrop_win) then
|
|
|
|
vim.api.nvim_win_set_config(self.backdrop_win, {
|
|
|
|
width = vim.o.columns,
|
|
|
|
height = vim.o.lines,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-01-11 20:43:17 +08:00
|
|
|
opts()
|
2023-01-13 16:00:15 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyFloatResized", modeline = false })
|
2022-12-24 18:26:09 +08:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-03-27 15:48:55 +08:00
|
|
|
---@param clear? boolean
|
|
|
|
function M:augroup(clear)
|
|
|
|
return vim.api.nvim_create_augroup("trouble.window." .. self.id, { clear = clear == true })
|
|
|
|
end
|
|
|
|
|
2022-12-24 18:26:09 +08:00
|
|
|
---@param events string|string[]
|
2024-03-27 15:48:55 +08:00
|
|
|
---@param fn fun(self:LazyFloat, event:{buf:number}):boolean?
|
|
|
|
---@param opts? vim.api.keyset.create_autocmd | {buffer: false, win?:boolean}
|
2022-12-24 18:26:09 +08:00
|
|
|
function M:on(events, fn, opts)
|
2024-03-27 15:48:55 +08:00
|
|
|
opts = opts or {}
|
|
|
|
if opts.win then
|
|
|
|
opts.pattern = self.win .. ""
|
|
|
|
opts.win = nil
|
|
|
|
elseif opts.buffer == nil then
|
|
|
|
opts.buffer = self.buf
|
|
|
|
elseif opts.buffer == false then
|
|
|
|
opts.buffer = nil
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
2024-03-27 15:48:55 +08:00
|
|
|
if opts.pattern then
|
|
|
|
opts.buffer = nil
|
|
|
|
end
|
|
|
|
local _self = Util.weak(self)
|
|
|
|
opts.callback = function(e)
|
|
|
|
local this = _self()
|
|
|
|
if not this then
|
|
|
|
-- delete the autocmd
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return fn(this, e)
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
2024-03-27 15:48:55 +08:00
|
|
|
opts.group = self:augroup()
|
|
|
|
vim.api.nvim_create_autocmd(events, opts)
|
2022-12-24 18:26:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param key string
|
|
|
|
---@param fn fun(self?)
|
|
|
|
---@param desc? string
|
2024-05-26 16:40:08 +08:00
|
|
|
---@param mode? string[]
|
2024-06-30 19:35:11 +08:00
|
|
|
function M:on_key(key, fn, desc, mode)
|
2024-05-26 16:40:08 +08:00
|
|
|
vim.keymap.set(mode or "n", key, function()
|
2022-12-24 18:26:09 +08:00
|
|
|
fn(self)
|
|
|
|
end, {
|
|
|
|
nowait = true,
|
|
|
|
buffer = self.buf,
|
|
|
|
desc = desc,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-06-03 16:45:53 +08:00
|
|
|
---@param opts? {wipe:boolean}
|
|
|
|
function M:close(opts)
|
2024-03-27 15:48:55 +08:00
|
|
|
self:augroup(true)
|
2022-12-24 18:26:09 +08:00
|
|
|
local buf = self.buf
|
|
|
|
local win = self.win
|
2023-06-03 16:45:53 +08:00
|
|
|
local wipe = opts and opts.wipe
|
|
|
|
if wipe == nil then
|
|
|
|
wipe = not self.opts.persistent
|
|
|
|
end
|
|
|
|
|
2022-12-24 18:26:09 +08:00
|
|
|
self.win = nil
|
2023-06-03 16:45:53 +08:00
|
|
|
if wipe then
|
|
|
|
self.buf = nil
|
|
|
|
end
|
2024-03-27 02:52:16 +08:00
|
|
|
local backdrop_buf = self.backdrop_buf
|
|
|
|
local backdrop_win = self.backdrop_win
|
|
|
|
self.backdrop_buf = nil
|
|
|
|
self.backdrop_win = nil
|
|
|
|
|
2022-12-24 18:26:09 +08:00
|
|
|
vim.schedule(function()
|
2024-03-27 02:52:16 +08:00
|
|
|
if backdrop_win and vim.api.nvim_win_is_valid(backdrop_win) then
|
|
|
|
vim.api.nvim_win_close(backdrop_win, true)
|
|
|
|
end
|
|
|
|
if backdrop_buf and vim.api.nvim_buf_is_valid(backdrop_buf) then
|
|
|
|
vim.api.nvim_buf_delete(backdrop_buf, { force = true })
|
|
|
|
end
|
2022-12-24 18:26:09 +08:00
|
|
|
if win and vim.api.nvim_win_is_valid(win) then
|
|
|
|
vim.api.nvim_win_close(win, true)
|
|
|
|
end
|
2023-06-03 16:45:53 +08:00
|
|
|
if wipe and buf and vim.api.nvim_buf_is_valid(buf) then
|
2023-01-13 16:00:38 +08:00
|
|
|
vim.diagnostic.reset(Config.ns, buf)
|
2022-12-24 18:26:09 +08:00
|
|
|
vim.api.nvim_buf_delete(buf, { force = true })
|
|
|
|
end
|
2024-06-30 19:35:11 +08:00
|
|
|
vim.cmd.redraw()
|
2022-12-24 18:26:09 +08:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2023-06-03 16:45:53 +08:00
|
|
|
function M:win_valid()
|
|
|
|
return self.win and vim.api.nvim_win_is_valid(self.win)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:buf_valid()
|
|
|
|
return self.buf and vim.api.nvim_buf_is_valid(self.buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:hide()
|
|
|
|
if self:win_valid() then
|
|
|
|
self:close({ wipe = false })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:toggle()
|
|
|
|
if self:win_valid() then
|
|
|
|
self:hide()
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
self:show()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:show()
|
|
|
|
if self:win_valid() then
|
|
|
|
self:focus()
|
|
|
|
elseif self:buf_valid() then
|
|
|
|
self:mount()
|
|
|
|
else
|
|
|
|
error("LazyFloat: buffer closed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-24 18:26:09 +08:00
|
|
|
function M:focus()
|
|
|
|
vim.api.nvim_set_current_win(self.win)
|
|
|
|
|
|
|
|
-- it seems that setting the current win doesn't work before VimEnter,
|
|
|
|
-- so do that then
|
|
|
|
if vim.v.vim_did_enter ~= 1 then
|
|
|
|
vim.api.nvim_create_autocmd("VimEnter", {
|
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
if self.win and vim.api.nvim_win_is_valid(self.win) then
|
|
|
|
pcall(vim.api.nvim_set_current_win, self.win)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|