mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-05 11:18:26 +00:00
feat(picker): get filetype from modeline when needed. Closes #987
This commit is contained in:
parent
33e3f92978
commit
5af04ab667
2 changed files with 32 additions and 0 deletions
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue