CMakePM: Add support for "add_subdirectory" operations

This means support for context menu and then "Add Existing Projects..."
and "New Subproject..."

Fixes: QTCREATORBUG-30471
Fixes: QTCREATORBUG-30818
Change-Id: Iffb0c4be744352c8acf1d9ee0bc58602af4426ba
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Cristian Adam
2024-08-21 19:52:10 +02:00
parent abb2236d4b
commit dcc023917a
3 changed files with 81 additions and 0 deletions

View File

@@ -234,6 +234,12 @@ bool CMakeBuildSystem::supportsAction(Node *context, ProjectAction action, const
|| action == ProjectAction::Rename || action == ProjectAction::RemoveFile; || action == ProjectAction::Rename || action == ProjectAction::RemoveFile;
} }
const auto cmakeProject = dynamic_cast<CMakeProjectNode *>(context);
const auto cmakeListsNode = dynamic_cast<CMakeListsNode *>(context);
if (cmakeProject || cmakeListsNode)
return action == ProjectAction::AddSubProject
|| action == ProjectAction::AddExistingProject;
return BuildSystem::supportsAction(context, action, node); return BuildSystem::supportsAction(context, action, node);
} }

View File

@@ -9,6 +9,8 @@
#include <android/androidconstants.h> #include <android/androidconstants.h>
#include <coreplugin/documentmanager.h>
#include <ios/iosconstants.h> #include <ios/iosconstants.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
@@ -16,11 +18,44 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <texteditor/texteditor.h>
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
namespace CMakeProjectManager::Internal { namespace CMakeProjectManager::Internal {
static QString quoteString(const QString &str)
{
return str.contains(QChar::Space) ? ("\"" + str + "\"") : str;
}
static bool addSubdirectory(const Utils::FilePath &projectPathDir, const Utils::FilePath & subProjectFilePath)
{
TextEditor::BaseTextEditor *editor = qobject_cast<TextEditor::BaseTextEditor *>(
Core::EditorManager::openEditorAt(
{projectPathDir.pathAppended(Constants::CMAKE_LISTS_TXT)},
Constants::CMAKE_EDITOR_ID,
Core::EditorManager::DoNotMakeVisible | Core::EditorManager::DoNotChangeCurrentEditor));
if (!editor)
return false;
const QString subDirectory = subProjectFilePath.relativeChildPath(projectPathDir).parentDir().path();
if (subDirectory.isEmpty())
return false;
QTextCursor cursor = editor->textCursor();
cursor.movePosition(QTextCursor::End);
if (!cursor.block().text().isEmpty())
cursor.insertText("\n");
cursor.insertText(QString("add_subdirectory(%1)").arg(quoteString(subDirectory)));
if (!Core::DocumentManager::saveDocument(editor->document()))
return false;
return true;
}
CMakeInputsNode::CMakeInputsNode(const FilePath &cmakeLists) : CMakeInputsNode::CMakeInputsNode(const FilePath &cmakeLists) :
ProjectExplorer::ProjectNode(cmakeLists) ProjectExplorer::ProjectNode(cmakeLists)
{ {
@@ -58,6 +93,22 @@ std::optional<FilePath> CMakeListsNode::visibleAfterAddFileAction() const
return filePath().pathAppended(Constants::CMAKE_LISTS_TXT); return filePath().pathAppended(Constants::CMAKE_LISTS_TXT);
} }
bool CMakeListsNode::canAddSubProject(const Utils::FilePath &subProjectFilePath) const
{
return subProjectFilePath != filePath().pathAppended(Constants::CMAKE_LISTS_TXT)
&& subProjectFilePath.isChildOf(filePath());
}
bool CMakeListsNode::addSubProject(const Utils::FilePath &subProjectFilePath)
{
return addSubdirectory(filePath(), subProjectFilePath);
}
QStringList CMakeListsNode::subProjectFileNamePatterns() const
{
return {Constants::CMAKE_LISTS_TXT};
}
CMakeProjectNode::CMakeProjectNode(const FilePath &directory) : CMakeProjectNode::CMakeProjectNode(const FilePath &directory) :
ProjectExplorer::ProjectNode(directory) ProjectExplorer::ProjectNode(directory)
{ {
@@ -66,6 +117,22 @@ CMakeProjectNode::CMakeProjectNode(const FilePath &directory) :
setListInProject(false); setListInProject(false);
} }
bool CMakeProjectNode::canAddSubProject(const Utils::FilePath &subProjectFilePath) const
{
return subProjectFilePath != filePath().pathAppended(Constants::CMAKE_LISTS_TXT)
&& subProjectFilePath.isChildOf(filePath());
}
bool CMakeProjectNode::addSubProject(const Utils::FilePath &subProjectFilePath)
{
return addSubdirectory(filePath(), subProjectFilePath);
}
QStringList CMakeProjectNode::subProjectFileNamePatterns() const
{
return {Constants::CMAKE_LISTS_TXT};
}
QString CMakeProjectNode::tooltip() const QString CMakeProjectNode::tooltip() const
{ {
return QString(); return QString();

View File

@@ -28,6 +28,10 @@ public:
bool showInSimpleTree() const final; bool showInSimpleTree() const final;
std::optional<Utils::FilePath> visibleAfterAddFileAction() const override; std::optional<Utils::FilePath> visibleAfterAddFileAction() const override;
bool canAddSubProject(const Utils::FilePath &subProjectFilePath) const override;
bool addSubProject(const Utils::FilePath &subProjectFilePath) override;
QStringList subProjectFileNamePatterns() const override;
}; };
class CMakeProjectNode : public ProjectExplorer::ProjectNode class CMakeProjectNode : public ProjectExplorer::ProjectNode
@@ -35,6 +39,10 @@ class CMakeProjectNode : public ProjectExplorer::ProjectNode
public: public:
CMakeProjectNode(const Utils::FilePath &directory); CMakeProjectNode(const Utils::FilePath &directory);
bool canAddSubProject(const Utils::FilePath &subProjectFilePath) const override;
bool addSubProject(const Utils::FilePath &subProjectFilePath) override;
QStringList subProjectFileNamePatterns() const override;
QString tooltip() const final; QString tooltip() const final;
}; };