fix: Improve TS support for @codebase (#735)
This commit is contained in:
parent
134cf40096
commit
bf366f1b73
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user