diff --git a/context/ProjectUtils.cpp b/context/ProjectUtils.cpp index 6bddd29..7bedd21 100644 --- a/context/ProjectUtils.cpp +++ b/context/ProjectUtils.cpp @@ -70,4 +70,18 @@ QString ProjectUtils::findFileInProject(const QString &filename) return QString(); } +QString ProjectUtils::getProjectRoot() +{ + QList projects = ProjectExplorer::ProjectManager::projects(); + + if (!projects.isEmpty()) { + auto project = projects.first(); + if (project) { + return project->projectDirectory().toFSPathString(); + } + } + + return QString(); +} + } // namespace QodeAssist::Context diff --git a/context/ProjectUtils.hpp b/context/ProjectUtils.hpp index 5c32844..873525a 100644 --- a/context/ProjectUtils.hpp +++ b/context/ProjectUtils.hpp @@ -52,6 +52,16 @@ public: * @return Absolute file path if found, empty string otherwise */ static QString findFileInProject(const QString &filename); + + /** + * @brief Get the project root directory + * + * Returns the root directory of the first open project. + * If multiple projects are open, returns the first one. + * + * @return Absolute path to project root, or empty string if no project is open + */ + static QString getProjectRoot(); }; } // namespace QodeAssist::Context diff --git a/tools/EditFileTool.cpp b/tools/EditFileTool.cpp index 638ab8f..adfb60e 100644 --- a/tools/EditFileTool.cpp +++ b/tools/EditFileTool.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -36,7 +37,6 @@ namespace QodeAssist::Tools { EditFileTool::EditFileTool(QObject *parent) : BaseTool(parent) - , m_ignoreManager(new Context::IgnoreManager(this)) {} QString EditFileTool::name() const @@ -52,11 +52,12 @@ QString EditFileTool::stringName() const QString EditFileTool::description() const { return "Edit a file by replacing old content with new content. " - "Provide the filename (or absolute path), old_content to find and replace, " + "Provide the file path (absolute or relative to project root), old_content to find and replace, " "and new_content to replace it with. Changes are applied immediately if auto-apply " "is enabled in settings. The user can undo or reapply changes at any time. " "\n\nIMPORTANT:" "\n- ALWAYS read the current file content before editing to ensure accuracy." + "\n- Path can be absolute (e.g., /path/to/file.cpp) or relative to project root (e.g., src/main.cpp)." "\n- For EMPTY files: use empty old_content (empty string or omit parameter)." "\n- To append at the END of file: use empty old_content." "\n- To insert at the BEGINNING of a file (e.g., copyright header), you MUST provide " @@ -77,8 +78,8 @@ QJsonObject EditFileTool::getDefinition(LLMCore::ToolSchemaFormat format) const QJsonObject filenameProperty; filenameProperty["type"] = "string"; filenameProperty["description"] - = "The filename or absolute path of the file to edit. If only filename is provided, " - "it will be searched in the project"; + = "The path of the file to edit. Can be an absolute path (e.g., /path/to/file.cpp) " + "or a relative path from the project root (e.g., src/main.cpp)"; properties["filename"] = filenameProperty; QJsonObject oldContentProperty; @@ -139,24 +140,22 @@ QFuture EditFileTool::executeAsync(const QJsonObject &input) } - QString filePath; QFileInfo fileInfo(filename); + QString filePath; - if (fileInfo.isAbsolute() && fileInfo.exists()) { + if (fileInfo.isAbsolute()) { filePath = filename; } else { - FileSearchUtils::FileMatch match = FileSearchUtils::findBestMatch( - filename, QString(), 10, m_ignoreManager); - - if (match.absolutePath.isEmpty()) { + QString projectRoot = Context::ProjectUtils::getProjectRoot(); + if (projectRoot.isEmpty()) { throw ToolRuntimeError( - QString("File '%1' not found in project. " - "Please provide a valid filename or absolute path.") + QString("Cannot resolve relative path '%1': no project is open. " + "Please provide an absolute path or open a project.") .arg(filename)); } - filePath = match.absolutePath; - LOG_MESSAGE(QString("EditFileTool: Found file '%1' at '%2'") + filePath = QDir(projectRoot).absoluteFilePath(filename); + LOG_MESSAGE(QString("EditFileTool: Resolved relative path '%1' to '%2'") .arg(filename, filePath)); } diff --git a/tools/EditFileTool.hpp b/tools/EditFileTool.hpp index bd81486..8f888a0 100644 --- a/tools/EditFileTool.hpp +++ b/tools/EditFileTool.hpp @@ -19,9 +19,6 @@ #pragma once -#include "FileSearchUtils.hpp" - -#include #include namespace QodeAssist::Tools { @@ -39,9 +36,6 @@ public: LLMCore::ToolPermissions requiredPermissions() const override; QFuture executeAsync(const QJsonObject &input = QJsonObject()) override; - -private: - Context::IgnoreManager *m_ignoreManager; }; } // namespace QodeAssist::Tools