mirror of https://github.com/folke/lazy.nvim.git
refactor: easier to pass window options for floats
This commit is contained in:
parent
d3b0d3e851
commit
13af39b83e
|
@ -5,18 +5,22 @@ function M.file_exists(file)
|
||||||
return vim.loop.fs_stat(file) ~= nil
|
return vim.loop.fs_stat(file) ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param opts? LazyViewOptions
|
---@param opts? LazyFloatOptions
|
||||||
function M.float(opts)
|
function M.float(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
if require("lazy.view").visible() then
|
||||||
opts = vim.tbl_deep_extend("force", {
|
opts = vim.tbl_deep_extend("force", {
|
||||||
win_opts = { zindex = 60, border = "none" },
|
zindex = 60,
|
||||||
|
border = "none",
|
||||||
margin = { top = 3, left = 2, right = 2 },
|
margin = { top = 3, left = 2, right = 2 },
|
||||||
}, opts or {})
|
}, opts)
|
||||||
|
end
|
||||||
return require("lazy.view.float")(opts)
|
return require("lazy.view.float")(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.open(uri)
|
function M.open(uri)
|
||||||
if M.file_exists(uri) then
|
if M.file_exists(uri) then
|
||||||
return M.float({ win_opts = { style = "" }, file = uri })
|
return M.float({ style = "", file = uri })
|
||||||
end
|
end
|
||||||
local Config = require("lazy.core.config")
|
local Config = require("lazy.core.config")
|
||||||
local cmd
|
local cmd
|
||||||
|
@ -89,8 +93,13 @@ function M.throttle(ms, fn)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@class LazyCmdOpts
|
||||||
|
---@field cwd? string
|
||||||
|
---@field env? table<string,string>
|
||||||
|
---@field float? LazyFloatOptions
|
||||||
|
|
||||||
---@param cmd string[]
|
---@param cmd string[]
|
||||||
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean, float?:LazyViewOptions}
|
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean, float?:LazyFloatOptions}
|
||||||
function M.open_cmd(cmd, opts)
|
function M.open_cmd(cmd, opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local float = M.float(opts.float)
|
local float = M.float(opts.float)
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
local Config = require("lazy.core.config")
|
local Config = require("lazy.core.config")
|
||||||
local ViewConfig = require("lazy.view.config")
|
local ViewConfig = require("lazy.view.config")
|
||||||
|
|
||||||
---@class LazyViewOptions
|
---@class LazyFloatOptions
|
||||||
---@field buf? number
|
---@field buf? number
|
||||||
---@field file? string
|
---@field file? string
|
||||||
---@field margin? {top?:number, right?:number, bottom?:number, left?:number}
|
---@field margin? {top?:number, right?:number, bottom?:number, left?:number}
|
||||||
---@field win_opts LazyViewWinOpts
|
|
||||||
---@field size? {width:number, height:number}
|
---@field size? {width:number, height:number}
|
||||||
local defaults = {
|
---@field zindex? number
|
||||||
win_opts = {},
|
---@field style? "" | "minimal"
|
||||||
}
|
---@field border? "none" | "single" | "double" | "rounded" | "solid" | "shadow"
|
||||||
|
|
||||||
---@class LazyFloat
|
---@class LazyFloat
|
||||||
---@field buf number
|
---@field buf number
|
||||||
---@field win number
|
---@field win number
|
||||||
---@field opts LazyViewOptions
|
---@field opts LazyFloatOptions
|
||||||
---@overload fun(opts?:LazyViewOptions):LazyFloat
|
---@field win_opts LazyWinOpts
|
||||||
|
---@overload fun(opts?:LazyFloatOptions):LazyFloat
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
setmetatable(M, {
|
setmetatable(M, {
|
||||||
|
@ -24,16 +24,33 @@ setmetatable(M, {
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
---@param opts? LazyViewOptions
|
---@param opts? LazyFloatOptions
|
||||||
function M.new(opts)
|
function M.new(opts)
|
||||||
local self = setmetatable({}, { __index = M })
|
local self = setmetatable({}, { __index = M })
|
||||||
return self:init(opts)
|
return self:init(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param opts? LazyViewOptions
|
---@param opts? LazyFloatOptions
|
||||||
function M:init(opts)
|
function M:init(opts)
|
||||||
self.opts = vim.tbl_deep_extend("force", defaults, opts or {})
|
self.opts = vim.tbl_deep_extend("force", {
|
||||||
self.opts.size = vim.tbl_extend("keep", self.opts.size or {}, Config.options.ui.size)
|
size = Config.options.ui.size,
|
||||||
|
style = "minimal",
|
||||||
|
border = Config.options.ui.border,
|
||||||
|
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,
|
||||||
|
noautocmd = true,
|
||||||
|
}
|
||||||
self:mount()
|
self:mount()
|
||||||
self:on_key(ViewConfig.keys.close, self.close)
|
self:on_key(ViewConfig.keys.close, self.close)
|
||||||
self:on({ "BufDelete", "BufLeave", "BufHidden" }, self.close, { once = true })
|
self:on({ "BufDelete", "BufLeave", "BufHidden" }, self.close, { once = true })
|
||||||
|
@ -44,25 +61,25 @@ function M:layout()
|
||||||
local function size(max, value)
|
local function size(max, value)
|
||||||
return value > 1 and math.min(value, max) or math.floor(max * value)
|
return value > 1 and math.min(value, max) or math.floor(max * value)
|
||||||
end
|
end
|
||||||
self.opts.win_opts.width = size(vim.o.columns, self.opts.size.width)
|
self.win_opts.width = size(vim.o.columns, self.opts.size.width)
|
||||||
self.opts.win_opts.height = size(vim.o.lines, self.opts.size.height)
|
self.win_opts.height = size(vim.o.lines, self.opts.size.height)
|
||||||
self.opts.win_opts.row = math.floor((vim.o.lines - self.opts.win_opts.height) / 2)
|
self.win_opts.row = math.floor((vim.o.lines - self.win_opts.height) / 2)
|
||||||
self.opts.win_opts.col = math.floor((vim.o.columns - self.opts.win_opts.width) / 2)
|
self.win_opts.col = math.floor((vim.o.columns - self.win_opts.width) / 2)
|
||||||
|
|
||||||
if self.opts.margin then
|
if self.opts.margin then
|
||||||
if self.opts.margin.top then
|
if self.opts.margin.top then
|
||||||
self.opts.win_opts.height = self.opts.win_opts.height - self.opts.margin.top
|
self.win_opts.height = self.win_opts.height - self.opts.margin.top
|
||||||
self.opts.win_opts.row = self.opts.win_opts.row + self.opts.margin.top
|
self.win_opts.row = self.win_opts.row + self.opts.margin.top
|
||||||
end
|
end
|
||||||
if self.opts.margin.right then
|
if self.opts.margin.right then
|
||||||
self.opts.win_opts.width = self.opts.win_opts.width - self.opts.margin.right
|
self.win_opts.width = self.win_opts.width - self.opts.margin.right
|
||||||
end
|
end
|
||||||
if self.opts.margin.bottom then
|
if self.opts.margin.bottom then
|
||||||
self.opts.win_opts.height = self.opts.win_opts.height - self.opts.margin.bottom
|
self.win_opts.height = self.win_opts.height - self.opts.margin.bottom
|
||||||
end
|
end
|
||||||
if self.opts.margin.left then
|
if self.opts.margin.left then
|
||||||
self.opts.win_opts.width = self.opts.win_opts.width - self.opts.margin.left
|
self.win_opts.width = self.win_opts.width - self.opts.margin.left
|
||||||
self.opts.win_opts.col = self.opts.win_opts.col + self.opts.margin.left
|
self.win_opts.col = self.win_opts.col + self.opts.margin.left
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -78,25 +95,8 @@ function M:mount()
|
||||||
self.buf = vim.api.nvim_create_buf(false, false)
|
self.buf = vim.api.nvim_create_buf(false, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class LazyViewWinOpts
|
|
||||||
---@field width number
|
|
||||||
---@field height number
|
|
||||||
---@field row number
|
|
||||||
---@field col number
|
|
||||||
local win_opts = {
|
|
||||||
relative = "editor",
|
|
||||||
style = "minimal",
|
|
||||||
border = Config.options.ui.border,
|
|
||||||
noautocmd = true,
|
|
||||||
zindex = 50,
|
|
||||||
}
|
|
||||||
self.opts.win_opts = vim.tbl_extend("force", win_opts, self.opts.win_opts)
|
|
||||||
if self.opts.win_opts.style == "" then
|
|
||||||
self.opts.win_opts.style = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
self:layout()
|
self:layout()
|
||||||
self.win = vim.api.nvim_open_win(self.buf, true, self.opts.win_opts)
|
self.win = vim.api.nvim_open_win(self.buf, true, self.win_opts)
|
||||||
self:focus()
|
self:focus()
|
||||||
|
|
||||||
vim.bo[self.buf].buftype = "nofile"
|
vim.bo[self.buf].buftype = "nofile"
|
||||||
|
@ -118,7 +118,7 @@ function M:mount()
|
||||||
local config = {}
|
local config = {}
|
||||||
for _, key in ipairs({ "relative", "width", "height", "col", "row" }) do
|
for _, key in ipairs({ "relative", "width", "height", "col", "row" }) do
|
||||||
---@diagnostic disable-next-line: no-unknown
|
---@diagnostic disable-next-line: no-unknown
|
||||||
config[key] = self.opts.win_opts[key]
|
config[key] = self.win_opts[key]
|
||||||
end
|
end
|
||||||
vim.api.nvim_win_set_config(self.win, config)
|
vim.api.nvim_win_set_config(self.win, config)
|
||||||
vim.cmd([[do User LazyFloatResized]])
|
vim.cmd([[do User LazyFloatResized]])
|
||||||
|
|
|
@ -25,13 +25,17 @@ local M = {}
|
||||||
---@type LazyView
|
---@type LazyView
|
||||||
M.view = nil
|
M.view = nil
|
||||||
|
|
||||||
|
function M.visible()
|
||||||
|
return M.view and M.view.win and vim.api.nvim_win_is_valid(M.view.win)
|
||||||
|
end
|
||||||
|
|
||||||
---@param mode? string
|
---@param mode? string
|
||||||
function M.show(mode)
|
function M.show(mode)
|
||||||
if Config.headless then
|
if Config.headless then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
M.view = (M.view and M.view.win and vim.api.nvim_win_is_valid(M.view.win)) and M.view or M.create()
|
M.view = M.visible() and M.view or M.create()
|
||||||
if mode then
|
if mode then
|
||||||
M.view.state.mode = mode
|
M.view.state.mode = mode
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,7 +26,7 @@ function M.new(view)
|
||||||
local self = setmetatable({}, { __index = setmetatable(M, { __index = Text }) })
|
local self = setmetatable({}, { __index = setmetatable(M, { __index = Text }) })
|
||||||
self.view = view
|
self.view = view
|
||||||
self.padding = 2
|
self.padding = 2
|
||||||
self.wrap = view.opts.win_opts.width
|
self.wrap = view.win_opts.width
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue