feat(win): allow setting ft just for highlighting without actually changing the filetype

This commit is contained in:
Folke Lemaitre 2024-11-07 00:17:38 +01:00
parent d62faa35b6
commit cad236f9bb
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 6 additions and 7 deletions

View file

@ -8,7 +8,7 @@ Snacks.config.style("blame_line", {
border = "rounded", border = "rounded",
title = " Git Blame ", title = " Git Blame ",
title_pos = "center", title_pos = "center",
bo = { filetype = "git" }, ft = "git",
}) })
--- Gets the git root for a buffer or path. --- Gets the git root for a buffer or path.

View file

@ -60,11 +60,12 @@ local M = setmetatable({}, {
Snacks.config.style("notification", { Snacks.config.style("notification", {
border = "rounded", border = "rounded",
zindex = 100, zindex = 100,
ft = "markdown",
wo = { wo = {
winblend = 5, winblend = 5,
wrap = false, wrap = false,
}, },
bo = { filetype = "markdown" }, bo = { filetype = "snacks_notif" },
}) })
---@class snacks.notifier.Config ---@class snacks.notifier.Config
@ -309,7 +310,7 @@ function N:render(notif)
style = "notification", style = "notification",
enter = false, enter = false,
backdrop = false, backdrop = false,
bo = { filetype = notif.ft }, ft = notif.ft,
noautocmd = true, noautocmd = true,
wo = { wo = {
winhighlight = table.concat({ winhighlight = table.concat({

View file

@ -28,6 +28,7 @@ local M = setmetatable({}, {
---@field backdrop? number|false Opacity of the backdrop (default: 60) ---@field backdrop? number|false Opacity of the backdrop (default: 60)
---@field wo? vim.wo window options ---@field wo? vim.wo window options
---@field bo? vim.bo buffer options ---@field bo? vim.bo buffer options
---@field ft? string filetype to use for treesitter/syntax highlighting. Won't override existing filetype
---@field keys? table<string, false|string|fun(self: snacks.win)|snacks.win.Keys> Key mappings ---@field keys? table<string, false|string|fun(self: snacks.win)|snacks.win.Keys> Key mappings
---@field on_buf? fun(self: snacks.win) Callback after opening the buffer ---@field on_buf? fun(self: snacks.win) Callback after opening the buffer
---@field on_win? fun(self: snacks.win) Callback after opening the window ---@field on_win? fun(self: snacks.win) Callback after opening the window
@ -338,7 +339,7 @@ function M:show()
self.opts.on_win(self) self.opts.on_win(self)
end end
local ft = vim.bo[self.buf].filetype local ft = self.opts.ft or vim.bo[self.buf].filetype
if ft then if ft then
local lang = ft and vim.treesitter.language.get_lang(ft) local lang = ft and vim.treesitter.language.get_lang(ft)
if lang and not vim.b[self.buf].ts_highlight and not pcall(vim.treesitter.start, self.buf, lang) then if lang and not vim.b[self.buf].ts_highlight and not pcall(vim.treesitter.start, self.buf, lang) then
@ -492,8 +493,6 @@ end
---@private ---@private
---@param type "win" | "buf" ---@param type "win" | "buf"
function M:set_options(type) function M:set_options(type)
local ei = vim.o.eventignore
vim.o.eventignore = "all"
local opts = type == "win" and self.opts.wo or self.opts.bo local opts = type == "win" and self.opts.wo or self.opts.bo
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
for k, v in pairs(opts or {}) do for k, v in pairs(opts or {}) do
@ -509,7 +508,6 @@ function M:set_options(type)
) )
end end
end end
vim.o.eventignore = ei
end end
function M:buf_valid() function M:buf_valid()