diff --git a/lua/avante/repo_map.lua b/lua/avante/repo_map.lua index a0d2073..adca68f 100644 --- a/lua/avante/repo_map.lua +++ b/lua/avante/repo_map.lua @@ -1,6 +1,7 @@ local Popup = require("nui.popup") local Utils = require("avante.utils") local event = require("nui.utils.autocmd").event +local fn = vim.fn local filetype_map = { ["javascriptreact"] = "javascript", @@ -27,11 +28,25 @@ function RepoMap.setup() end function RepoMap.get_ts_lang(filepath) - local filetype = vim.filetype.match({ filename = filepath }) + local filetype = RepoMap.get_filetype(filepath) return filetype_map[filetype] or filetype end -function RepoMap.get_filetype(filepath) return vim.filetype.match({ filename = filepath }) end +function RepoMap.get_filetype(filepath) + local filetype = vim.filetype.match({ filename = filepath }) + -- TypeScript files are sometimes not detected correctly + -- https://github.com/neovim/neovim/issues/27265 + if not filetype then + local ext = fn.fnamemodify(filepath, ":e") + if ext == "tsx" then + filetype = "typescriptreact" + end + if ext == "ts" then + filetype = "typescript" + end + end + return filetype +end function RepoMap._build_repo_map(project_root, file_ext) local output = {} @@ -44,8 +59,9 @@ function RepoMap._build_repo_map(project_root, file_ext) Utils.error("Failed to load avante_repo_map") return end - local definitions = - repo_map_lib.stringify_definitions(RepoMap.get_ts_lang(filepath), Utils.file.read_content(filepath) or "") + local filetype = RepoMap.get_ts_lang(filepath) + local definitions = filetype and + repo_map_lib.stringify_definitions(filetype, Utils.file.read_content(filepath) or "") or "" if definitions == "" then return end table.insert(output, { path = Utils.relative_path(filepath), diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 51bc051..ea016ea 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -545,8 +545,8 @@ function M.get_project_root() return M.root.get() end function M.is_same_file_ext(target_ext, filepath) local ext = fn.fnamemodify(filepath, ":e") - if target_ext == "tsx" and ext == "ts" then return true end - if target_ext == "jsx" and ext == "js" then return true end + if (target_ext == "tsx" and ext == "ts") or (target_ext == "ts" and ext == "tsx") then return true end + if (target_ext == "jsx" and ext == "js") or (target_ext == "js" and ext == "jsx") then return true end return ext == target_ext end