feat: add repo map display (#727)

Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
teocns 2024-10-18 00:41:40 -06:00 committed by GitHub
parent 26bf8d67e8
commit 36b23cef16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 2 deletions

View File

@ -61,7 +61,7 @@ jobs:
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
toolchain: "1.80.1"
components: clippy, rustfmt
- name: Run rustfmt
run: make rustlint

View File

@ -150,6 +150,7 @@ Respect and use existing conventions, libraries, etc that are already present in
debug = "<leader>ad",
hint = "<leader>ah",
suggestion = "<leader>as",
repomap = "<leader>ar",
},
sidebar = {
apply_all = "A",

View File

@ -174,6 +174,11 @@ H.keymaps = function()
function() M.toggle.suggestion() end,
{ desc = "avante: toggle suggestion" }
)
Utils.safe_keymap_set("n", Config.mappings.toggle.repomap, function() require("avante.repo_map").show() end, {
desc = "avante: display repo map",
noremap = true,
silent = true,
})
end
if Config.behaviour.auto_suggestions then

View File

@ -1,4 +1,6 @@
local Popup = require("nui.popup")
local Utils = require("avante.utils")
local event = require("nui.utils.autocmd").event
local filetype_map = {
["javascriptreact"] = "javascript",
@ -38,6 +40,10 @@ function RepoMap._build_repo_map(project_root, file_ext)
local filepaths = Utils.scan_directory(project_root, ignore_patterns, negate_patterns)
vim.iter(filepaths):each(function(filepath)
if not Utils.is_same_file_ext(file_ext, filepath) then return end
if not repo_map_lib then
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 "")
if definitions == "" then return end
@ -141,7 +147,8 @@ function RepoMap._get_repo_map(file_ext)
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
callback = function(ev)
vim.defer_fn(function()
local filepath = vim.api.nvim_buf_get_name(ev.buf)
local ok, filepath = pcall(vim.api.nvim_buf_get_name, ev.buf)
if not ok or not filepath then return end
if not vim.startswith(filepath, project_root) then return end
local rel_filepath = Utils.relative_path(filepath)
update_repo_map(rel_filepath)
@ -152,4 +159,53 @@ function RepoMap._get_repo_map(file_ext)
return repo_map
end
function RepoMap.show()
local file_ext = vim.fn.expand("%:e")
local repo_map = RepoMap.get_repo_map(file_ext)
if not repo_map or next(repo_map) == nil then
Utils.warn("The repo map is empty or not supported for this language: " .. file_ext)
return
end
local popup = Popup({
position = "50%",
enter = true,
focusable = true,
border = {
style = "rounded",
padding = { 1, 1 },
text = {
top = " Avante Repo Map ",
top_align = "center",
},
},
size = {
width = math.floor(vim.o.columns * 0.8),
height = math.floor(vim.o.lines * 0.8),
},
})
popup:mount()
popup:map("n", "q", function() popup:unmount() end, { noremap = true, silent = true })
popup:on(event.BufLeave, function() popup:unmount() end)
-- Format the repo map for display
local lines = {}
for _, entry in ipairs(repo_map) do
table.insert(lines, string.format("Path: %s", entry.path))
table.insert(lines, string.format("Lang: %s", entry.lang))
table.insert(lines, "Defs:")
for def_line in entry.defs:gmatch("[^\r\n]+") do
table.insert(lines, def_line)
end
table.insert(lines, "") -- Add an empty line between entries
end
-- Set the buffer content
vim.api.nvim_buf_set_lines(popup.bufnr, 0, -1, false, lines)
end
return RepoMap