feat(win): custom views

This commit is contained in:
Folke Lemaitre 2024-11-04 22:08:56 +01:00
parent dbc1521ee1
commit 12d6f863f7
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
5 changed files with 50 additions and 17 deletions

View file

@ -15,19 +15,20 @@ end
--- Show git log for the current line.
---@param opts? snacks.terminal.Config | {count?: number}
function M.blame_line(opts)
Snacks.config.view("blame_line", {
win = {
width = 0.6,
height = 0.6,
border = "rounded",
title = " Git Blame ",
title_pos = "center",
},
bo = { filetype = "git" },
})
opts = vim.tbl_deep_extend("force", {
count = 5,
interactive = false,
float = {
win = {
width = 0.6,
height = 0.6,
border = "rounded",
title = " Git Blame ",
title_pos = "center",
},
bo = { filetype = "git" },
},
win = { view = "blame_line" },
}, opts or {})
local cursor = vim.api.nvim_win_get_cursor(0)
local line = cursor[1]

View file

@ -33,11 +33,13 @@ _G.Snacks = M
---@field terminal snacks.terminal.Config
---@field lazygit snacks.lazygit.Config
---@field gitbrowse snacks.gitbrowse.Config
---@field views table<string, snacks.win.Config>
local config = {
bigfile = { enabled = true },
quickfile = { enabled = true },
statuscolumn = { enabled = true },
words = { enabled = true },
views = {},
}
---@class snacks.Config: snacks.Opts
@ -50,10 +52,17 @@ M.config = setmetatable({}, {
---@generic T: table
---@param snack string
---@param defaults T
---@param opts? T
---@param ... T[]
---@return T
function M.config.get(snack, defaults, opts)
return vim.tbl_deep_extend("force", {}, vim.deepcopy(defaults), vim.deepcopy(config[snack] or {}), opts or {})
function M.config.get(snack, defaults, ...)
return vim.tbl_deep_extend("force", {}, vim.deepcopy(defaults), vim.deepcopy(config[snack] or {}), ...)
end
--- Register a new window view config.
---@param name string
---@param defaults snacks.win.Config
function M.config.view(name, defaults)
config.views[name] = vim.tbl_deep_extend("force", vim.deepcopy(defaults), config.views[name] or {})
end
---@param opts snacks.Opts?

View file

@ -37,6 +37,9 @@ local defaults = {
selectedLineBgColor = { bg = "Visual" }, -- set to `default` to have no background colour
unstagedChangesColor = { fg = "DiagnosticError" },
},
win = {
view = "lazygit",
},
}
-- re-create config file on startup

View file

@ -56,7 +56,7 @@ local terminals = {}
function M.open(cmd, opts)
local id = vim.v.count1
---@type snacks.terminal.Config
opts = Snacks.config.get("terminal", defaults, opts)
opts = Snacks.config.get("terminal", defaults, { win = Snacks.win.resolve(opts and opts.win) }, opts)
opts.win.position = opts.win.position or (cmd and "float" or "bottom")
opts.win.wo.winbar = opts.win.wo.winbar or (opts.win.position == "float" and "" or (id .. ": %{b:term_title}"))
@ -109,8 +109,7 @@ end
---@param cmd? string | string[]
---@param opts? snacks.terminal.Config
function M.toggle(cmd, opts)
---@type snacks.terminal.Config
opts = vim.tbl_deep_extend("force", {}, defaults, opts or {})
opts = opts or {}
local id = vim.inspect({ cmd = cmd, cwd = opts.cwd, env = opts.env, count = vim.v.count1 })

View file

@ -18,6 +18,7 @@ local M = setmetatable({}, {
---@field mode? string|string[]
---@class snacks.win.Config
---@field view? string merges with config from `Snacks.config.views[view]`
---@field position? "float"|"bottom"|"top"|"left"|"right"
---@field buf? number
---@field file? string
@ -101,13 +102,33 @@ vim.api.nvim_set_hl(0, "SnackFloatBackdrop", { bg = "#000000", default = true })
local id = 0
---@param opts? snacks.win.Config
---@return snacks.win.Config
function M.resolve(opts)
opts = opts or {}
local done = {} ---@type string[]
local views = { opts } ---@type snacks.win.Config[]
local view = opts.view
while view and not vim.tbl_contains(done, view) do
table.insert(done, view)
if not Snacks.config.views[view] then
break
end
table.insert(views, 1, Snacks.config.views[view])
view = Snacks.config.views[view].view
end
local ret = #views == 0 and {} or #views == 1 and views[1] or vim.tbl_deep_extend("force", {}, unpack(views))
ret.view = nil
return ret
end
---@param opts? snacks.win.Config
---@return snacks.win
function M.new(opts)
local self = setmetatable({}, { __index = M })
id = id + 1
self.id = id
opts = Snacks.config.get("win", defaults, opts)
opts = Snacks.config.get("win", defaults, M.resolve(opts))
opts =
vim.tbl_deep_extend("force", {}, vim.deepcopy(opts.position == "float" and defaults_float or defaults_split), opts)
if opts.win.style == "minimal" then