fix: get filetype (#1258)

This commit is contained in:
yetone 2025-02-12 22:19:55 +08:00 committed by GitHub
parent 3a7277386f
commit f8636315a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 25 deletions

View File

@ -368,9 +368,9 @@ end
function FileSelector:get_selected_files_contents()
local contents = {}
for _, file_path in ipairs(self.selected_filepaths) do
local lines, filetype, error = Utils.read_file_from_buf_or_disk(file_path)
local lines, error = Utils.read_file_from_buf_or_disk(file_path)
lines = lines or {}
filetype = filetype or "unknown"
local filetype = Utils.get_filetype(file_path)
if error ~= nil then
Utils.error("error reading file: " .. error)
else

View File

@ -29,21 +29,10 @@ end
function RepoMap.setup() vim.defer_fn(RepoMap._init_repo_map_lib, 1000) end
function RepoMap.get_ts_lang(filepath)
local filetype = RepoMap.get_filetype(filepath)
local filetype = Utils.get_filetype(filepath)
return filetype_map[filetype] or filetype
end
function RepoMap.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
function RepoMap._build_repo_map(project_root, file_ext)
local output = {}
local gitignore_path = project_root .. "/.gitignore"
@ -70,7 +59,7 @@ function RepoMap._build_repo_map(project_root, file_ext)
if definitions == "" then return end
table.insert(output, {
path = Utils.relative_path(filepath),
lang = RepoMap.get_filetype(filepath),
lang = Utils.get_filetype(filepath),
defs = definitions,
})
end)
@ -142,7 +131,7 @@ function RepoMap._get_repo_map(file_ext)
if not found then
table.insert(repo_map, {
path = Utils.relative_path(abs_filepath),
lang = RepoMap.get_filetype(abs_filepath),
lang = Utils.get_filetype(abs_filepath),
defs = definitions,
})
end

View File

@ -337,8 +337,7 @@ local function transform_result_content(selected_files, result_content, prev_fil
-- can happen if the llm tries to edit or create a file outside of it's context.
if not match_filetype then
local snippet_file_path = current_filepath or prev_filepath
local snippet_file_type = vim.filetype.match({ filename = snippet_file_path }) or "unknown"
match_filetype = snippet_file_type
match_filetype = Utils.get_filetype(snippet_file_path)
end
local search_start_tag_idx_in_transformed_lines = 0

View File

@ -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