From f8636315a5629cbd6606fb770824c2da8fa2ec4a Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 12 Feb 2025 22:19:55 +0800 Subject: [PATCH] fix: get filetype (#1258) --- lua/avante/file_selector.lua | 4 ++-- lua/avante/repo_map.lua | 17 +++-------------- lua/avante/sidebar.lua | 3 +-- lua/avante/utils/init.lua | 21 ++++++++++++++------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/lua/avante/file_selector.lua b/lua/avante/file_selector.lua index 2b703d1..6eece15 100644 --- a/lua/avante/file_selector.lua +++ b/lua/avante/file_selector.lua @@ -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 diff --git a/lua/avante/repo_map.lua b/lua/avante/repo_map.lua index c639922..42334d8 100644 --- a/lua/avante/repo_map.lua +++ b/lua/avante/repo_map.lua @@ -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 diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 1835d3c..6c63040 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -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 diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 7c1bf2d..d20c2cb 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -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("^.-", "", 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