fix: Improve TS support for @codebase ()

This commit is contained in:
Ivan Sorokin 2024-10-20 21:43:27 +02:00 committed by GitHub
parent 134cf40096
commit bf366f1b73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions
lua/avante

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

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