fix: thrown error when trying to use codebase with no buffer opened (#1541)

* Fix - Fix thrown error when trying to use @codebase with no buffer opened

* Style - Fix stylua formatting in repo_map and sidebar

* Fix - Restore code selection handling in sidebar
This was a mistake on my part while rushing to make the PR. I deleted an
unrelated commit that got sent by mistake, and during the second review,
I didn't notice that this important code selection functionality was
removed. The code has now been properly restored.
This commit is contained in:
Canopus
2025-03-09 16:20:43 +00:00
committed by GitHub
parent 05b3b843c8
commit 4976807a33
2 changed files with 27 additions and 5 deletions

View File

@@ -62,6 +62,12 @@ end
local cache = {}
function RepoMap.get_repo_map(file_ext)
-- Add safety check for file_ext
if not file_ext then
Utils.warn("No file extension available - please open a file first")
return {}
end
local repo_map = RepoMap._get_repo_map(file_ext) or {}
if not repo_map or next(repo_map) == nil then
Utils.warn("The repo map is empty. Maybe do not support this language: " .. file_ext)
@@ -70,7 +76,15 @@ function RepoMap.get_repo_map(file_ext)
end
function RepoMap._get_repo_map(file_ext)
file_ext = file_ext or vim.fn.expand("%:e")
-- Add safety check at the start of the function
if not file_ext then
local current_buf = vim.api.nvim_get_current_buf()
local buf_name = vim.api.nvim_buf_get_name(current_buf)
if buf_name and buf_name ~= "" then file_ext = vim.fn.fnamemodify(buf_name, ":e") end
if not file_ext or file_ext == "" then return {} end
end
local project_root = Utils.root.get()
local cache_key = project_root .. "." .. file_ext
local cached = cache[cache_key]

View File

@@ -2356,6 +2356,11 @@ function Sidebar:create_input_container(opts)
---@param cb fun(opts: AvanteGeneratePromptsOptions): nil
local function get_generate_prompts_options(request, summarize_memory, cb)
local filetype = api.nvim_get_option_value("filetype", { buf = self.code.bufnr })
local file_ext = nil
-- Get file extension safely
local buf_name = api.nvim_buf_get_name(self.code.bufnr)
if buf_name and buf_name ~= "" then file_ext = vim.fn.fnamemodify(buf_name, ":e") end
---@type AvanteSelectedCode | nil
local selected_code = nil
@@ -2370,11 +2375,9 @@ function Sidebar:create_input_container(opts)
local mentions = Utils.extract_mentions(request)
request = mentions.new_content
local file_ext = api.nvim_buf_get_name(self.code.bufnr):match("^.+%.(.+)$")
local project_context = mentions.enable_project_context and file_ext and RepoMap.get_repo_map(file_ext) or nil
local project_context = mentions.enable_project_context and RepoMap.get_repo_map(file_ext) or nil
local selected_files_contents = self.file_selector:get_selected_files_contents()
local selected_files_contents = self.file_selector:get_selected_files_contents() or {}
local diagnostics = nil
if mentions.enable_diagnostics then
@@ -2428,6 +2431,11 @@ function Sidebar:create_input_container(opts)
---@param request string
local function handle_submit(request)
if request:match("@codebase") and not vim.fn.expand("%:e") then
self:update_content("Please open a file first before using @codebase", { focus = false, scroll = false })
return
end
local model = Config.has_provider(Config.provider) and Config.get_provider_config(Config.provider).model
or "default"