fix: get filetype (#1258)
This commit is contained in:
@@ -890,9 +890,19 @@ function M.is_same_file(filepath_a, filepath_b) return M.uniform_path(filepath_a
|
||||
|
||||
function M.trim_think_content(content) return content:gsub("^<think>.-</think>", "", 1) end
|
||||
|
||||
function M.get_filetype(filepath)
|
||||
-- Some files are sometimes not detected correctly when buffer is not included
|
||||
-- https://github.com/neovim/neovim/issues/27265
|
||||
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
local filetype = vim.filetype.match({ filename = filepath, buf = buf })
|
||||
vim.api.nvim_buf_delete(buf, { force = true })
|
||||
|
||||
return filetype
|
||||
end
|
||||
|
||||
---@param file_path string
|
||||
---@return string[]|nil lines
|
||||
---@return string|nil file_type
|
||||
---@return string|nil error
|
||||
function M.read_file_from_buf_or_disk(file_path)
|
||||
--- Lookup if the file is loaded in a buffer
|
||||
@@ -900,8 +910,7 @@ function M.read_file_from_buf_or_disk(file_path)
|
||||
if bufnr ~= -1 and vim.api.nvim_buf_is_loaded(bufnr) then
|
||||
-- If buffer exists and is loaded, get buffer content
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local file_type = vim.api.nvim_get_option_value("filetype", { buf = bufnr })
|
||||
return lines, file_type, nil
|
||||
return lines, nil
|
||||
end
|
||||
|
||||
-- Fallback: read file from disk
|
||||
@@ -909,12 +918,10 @@ function M.read_file_from_buf_or_disk(file_path)
|
||||
if file then
|
||||
local content = file:read("*all")
|
||||
file:close()
|
||||
-- Detect the file type using the specific file's content
|
||||
local file_type = vim.filetype.match({ filename = file_path, contents = { content } }) or "unknown"
|
||||
return vim.split(content, "\n"), file_type, nil
|
||||
return vim.split(content, "\n"), nil
|
||||
else
|
||||
-- M.error("failed to open file: " .. file_path .. " with error: " .. open_err)
|
||||
return {}, nil, open_err
|
||||
return {}, open_err
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user