feat: add project root option in commands Ask/Chat/ChatNew (#2659)

This commit is contained in:
brook hong
2025-08-31 22:10:29 +08:00
committed by GitHub
parent e63427fb9a
commit 604fdcc4b1
3 changed files with 153 additions and 44 deletions

View File

@@ -31,7 +31,17 @@ local History = {}
function History.get_history_dir(bufnr)
local dirname = generate_project_dirname_in_storage(bufnr)
local history_dir = Path:new(Config.history.storage_path):joinpath(dirname):joinpath("history")
if not history_dir:exists() then history_dir:mkdir({ parents = true }) end
if not history_dir:exists() then
history_dir:mkdir({ parents = true })
local metadata_filepath = history_dir:joinpath("metadata.json")
local metadata = {
project_root = Utils.root.get({
buf = bufnr,
}),
}
metadata_filepath:write(vim.json.encode(metadata), "w")
end
return history_dir
end
@@ -105,13 +115,14 @@ end
function History.save_latest_filename(bufnr, filename)
local metadata_filepath = History.get_metadata_filepath(bufnr)
local metadata
if not metadata_filepath:exists() then
metadata = {}
else
local metadata = {}
if metadata_filepath:exists() then
local metadata_content = metadata_filepath:read()
metadata = vim.json.decode(metadata_content)
end
if metadata.project_root == nil then metadata.project_root = Utils.root.get({
buf = bufnr,
}) end
metadata.latest_filename = filename
metadata_filepath:write(vim.json.encode(metadata), "w")
end
@@ -187,6 +198,56 @@ end
P.history = History
---@return table[] List of projects with their information
function P.list_projects()
local projects_dir = Path:new(Config.history.storage_path):joinpath("projects")
if not projects_dir:exists() then return {} end
local projects = {}
local dirs = Scan.scan_dir(tostring(projects_dir), { depth = 1, add_dirs = true, only_dirs = true })
for _, dir_path in ipairs(dirs) do
local project_dir = Path:new(dir_path)
local history_dir = project_dir:joinpath("history")
local metadata_file = history_dir:joinpath("metadata.json")
local project_root = ""
if metadata_file:exists() then
local content = metadata_file:read()
if content then
local metadata = vim.json.decode(content)
if metadata and metadata.project_root then project_root = metadata.project_root end
end
end
-- Skip if project_root is empty
if project_root == "" then goto continue end
-- Count history files
local history_count = 0
if history_dir:exists() then
local history_files = vim.fn.glob(tostring(history_dir:joinpath("*.json")), true, true)
for _, file in ipairs(history_files) do
if not file:match("metadata.json") then history_count = history_count + 1 end
end
end
table.insert(projects, {
name = filepath_to_filename(project_dir),
root = project_root,
history_count = history_count,
directory = tostring(project_dir),
})
::continue::
end
-- Sort by history count (most active projects first)
table.sort(projects, function(a, b) return a.history_count > b.history_count end)
return projects
end
-- Prompt path
local Prompt = {}

View File

@@ -1607,6 +1607,44 @@ function M.uuid()
end)
end
---Parse command arguments (fargs) into a structured format
---@param fargs string[] Command arguments
---@param options? {collect_remaining?: boolean, boolean_keys?: string[]} Options for parsing
---@return table parsed_args Key-value pairs from arguments
---@return string|nil remaining_text Concatenated remaining arguments (if collect_remaining is true)
function M.parse_args(fargs, options)
options = options or {}
local parsed_args = {}
local remaining_parts = {}
local boolean_keys = options.boolean_keys or {}
-- Create a lookup table for boolean keys for faster access
local boolean_keys_lookup = {}
for _, key in ipairs(boolean_keys) do
boolean_keys_lookup[key] = true
end
for _, arg in ipairs(fargs) do
local key, value = arg:match("([%w_]+)=(.+)")
if key and value then
-- Convert "true"/"false" string values to boolean for specified keys
if boolean_keys_lookup[key] or value == "true" or value == "false" then
parsed_args[key] = (value == "true")
else
parsed_args[key] = value
end
elseif options.collect_remaining then
table.insert(remaining_parts, arg)
end
end
-- Return the parsed arguments and optionally the concatenated remaining text
if options.collect_remaining and #remaining_parts > 0 then return parsed_args, table.concat(remaining_parts, " ") end
return parsed_args
end
---@param tool_use AvanteLLMToolUse
function M.tool_use_to_xml(tool_use)
local tool_use_json = vim.json.encode({