fix: Improve TS support for @codebase (#735)
This commit is contained in:
parent
134cf40096
commit
bf366f1b73
@ -1,6 +1,7 @@
|
|||||||
local Popup = require("nui.popup")
|
local Popup = require("nui.popup")
|
||||||
local Utils = require("avante.utils")
|
local Utils = require("avante.utils")
|
||||||
local event = require("nui.utils.autocmd").event
|
local event = require("nui.utils.autocmd").event
|
||||||
|
local fn = vim.fn
|
||||||
|
|
||||||
local filetype_map = {
|
local filetype_map = {
|
||||||
["javascriptreact"] = "javascript",
|
["javascriptreact"] = "javascript",
|
||||||
@ -27,11 +28,25 @@ function RepoMap.setup()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function RepoMap.get_ts_lang(filepath)
|
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
|
return filetype_map[filetype] or filetype
|
||||||
end
|
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)
|
function RepoMap._build_repo_map(project_root, file_ext)
|
||||||
local output = {}
|
local output = {}
|
||||||
@ -44,8 +59,9 @@ function RepoMap._build_repo_map(project_root, file_ext)
|
|||||||
Utils.error("Failed to load avante_repo_map")
|
Utils.error("Failed to load avante_repo_map")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local definitions =
|
local filetype = RepoMap.get_ts_lang(filepath)
|
||||||
repo_map_lib.stringify_definitions(RepoMap.get_ts_lang(filepath), Utils.file.read_content(filepath) or "")
|
local definitions = filetype and
|
||||||
|
repo_map_lib.stringify_definitions(filetype, Utils.file.read_content(filepath) or "") or ""
|
||||||
if definitions == "" then return end
|
if definitions == "" then return end
|
||||||
table.insert(output, {
|
table.insert(output, {
|
||||||
path = Utils.relative_path(filepath),
|
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)
|
function M.is_same_file_ext(target_ext, filepath)
|
||||||
local ext = fn.fnamemodify(filepath, ":e")
|
local ext = fn.fnamemodify(filepath, ":e")
|
||||||
if target_ext == "tsx" and ext == "ts" 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" 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
|
return ext == target_ext
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user