feat(picker): get filetype from modeline when needed. Closes #987

This commit is contained in:
Folke Lemaitre 2025-02-07 11:35:14 +01:00
parent 33e3f92978
commit 5af04ab667
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 32 additions and 0 deletions

View file

@ -233,6 +233,10 @@ end
function M:highlight(opts)
opts = opts or {}
local ft = opts.ft
if not ft and opts.buf then
local modeline = Snacks.picker.util.modeline(opts.buf)
ft = modeline and modeline.ft
end
if not ft and (opts.file or opts.buf) then
ft = vim.filetype.match({
buf = opts.buf or self.win.buf,

View file

@ -503,4 +503,32 @@ function M.copy_dir(from, to)
end
end
---@param buf number
---@return (vim.bo|vim.wo)?
function M.modeline(buf)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
local lines = vim.api.nvim_buf_get_lines(buf, 0, vim.o.modelines, false)
vim.list_extend(lines, vim.api.nvim_buf_get_lines(buf, -vim.o.modelines, -1, false))
for _, line in ipairs(lines) do
local m, options = line:match("%S*%s+(%w+):%s*(.-)%s*$")
if vim.tbl_contains({ "vi", "vim", "ex" }, m) and options then
local set = vim.split(options, "[:%s]+")
local ret = {} ---@type table<string, any>
for _, v in ipairs(set) do
if v ~= "" then
local k, val = v:match("([^=]+)=(.+)")
if k then
ret[k] = tonumber(val) or val
else
ret[v:gsub("^no", "")] = v:find("^no") and false or true
end
end
end
return ret
end
end
end
return M