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:
@@ -62,6 +62,12 @@ end
|
|||||||
local cache = {}
|
local cache = {}
|
||||||
|
|
||||||
function RepoMap.get_repo_map(file_ext)
|
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 {}
|
local repo_map = RepoMap._get_repo_map(file_ext) or {}
|
||||||
if not repo_map or next(repo_map) == nil then
|
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)
|
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
|
end
|
||||||
|
|
||||||
function RepoMap._get_repo_map(file_ext)
|
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 project_root = Utils.root.get()
|
||||||
local cache_key = project_root .. "." .. file_ext
|
local cache_key = project_root .. "." .. file_ext
|
||||||
local cached = cache[cache_key]
|
local cached = cache[cache_key]
|
||||||
|
|||||||
@@ -2356,6 +2356,11 @@ function Sidebar:create_input_container(opts)
|
|||||||
---@param cb fun(opts: AvanteGeneratePromptsOptions): nil
|
---@param cb fun(opts: AvanteGeneratePromptsOptions): nil
|
||||||
local function get_generate_prompts_options(request, summarize_memory, cb)
|
local function get_generate_prompts_options(request, summarize_memory, cb)
|
||||||
local filetype = api.nvim_get_option_value("filetype", { buf = self.code.bufnr })
|
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
|
---@type AvanteSelectedCode | nil
|
||||||
local selected_code = nil
|
local selected_code = nil
|
||||||
@@ -2370,11 +2375,9 @@ function Sidebar:create_input_container(opts)
|
|||||||
local mentions = Utils.extract_mentions(request)
|
local mentions = Utils.extract_mentions(request)
|
||||||
request = mentions.new_content
|
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() or {}
|
||||||
|
|
||||||
local selected_files_contents = self.file_selector:get_selected_files_contents()
|
|
||||||
|
|
||||||
local diagnostics = nil
|
local diagnostics = nil
|
||||||
if mentions.enable_diagnostics then
|
if mentions.enable_diagnostics then
|
||||||
@@ -2428,6 +2431,11 @@ function Sidebar:create_input_container(opts)
|
|||||||
|
|
||||||
---@param request string
|
---@param request string
|
||||||
local function handle_submit(request)
|
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
|
local model = Config.has_provider(Config.provider) and Config.get_provider_config(Config.provider).model
|
||||||
or "default"
|
or "default"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user