From 27a2ebb0d7cc8328e1383008e36ad0cc01ff0446 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Fri, 30 Sep 2016 21:10:32 +0200 Subject: Autotools: Use foldernode helpers to construct project tree Change-Id: Id0eaebb4035e8bd2fb29cc4f0317151ab00dcce6 Reviewed-by: hjk --- .../autotoolsprojectmanager/autotoolsproject.cpp | 148 ++------------------- .../autotoolsprojectmanager/autotoolsproject.h | 19 --- .../autotoolsprojectnode.cpp | 4 +- .../autotoolsprojectmanager/autotoolsprojectnode.h | 2 +- 4 files changed, 13 insertions(+), 160 deletions(-) (limited to 'src/plugins/autotoolsprojectmanager') diff --git a/src/plugins/autotoolsprojectmanager/autotoolsproject.cpp b/src/plugins/autotoolsprojectmanager/autotoolsproject.cpp index 3470bf15d6..b66b37c106 100644 --- a/src/plugins/autotoolsprojectmanager/autotoolsproject.cpp +++ b/src/plugins/autotoolsprojectmanager/autotoolsproject.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include @@ -74,7 +75,7 @@ AutotoolsProject::AutotoolsProject(AutotoolsManager *manager, const QString &fil setId(Constants::AUTOTOOLS_PROJECT_ID); setProjectManager(manager); setDocument(new AutotoolsProjectFile(fileName)); - setRootProjectNode(new AutotoolsProjectNode(projectFilePath())); + setRootProjectNode(new AutotoolsProjectNode(projectDirectory())); setProjectContext(Core::Context(Constants::PROJECT_CONTEXT)); setProjectLanguages(Core::Context(ProjectExplorer::Constants::LANG_CXX)); @@ -218,7 +219,14 @@ void AutotoolsProject::makefileParsingFinished() m_watchedFiles.append(configureAcFilePath); } - buildFileNodeTree(dir, files); + QList fileNodes = Utils::transform(files, [dir](const QString &f) { + const Utils::FileName path = Utils::FileName::fromString(dir.absoluteFilePath(f)); + return new FileNode(path, + (f == QLatin1String("Makefile.am") || f == QLatin1String("configure.ac")) + ? ProjectFileType : ResourceType, false); + }); + rootProjectNode()->buildTree(fileNodes); + updateCppCodeModel(); m_makefileParserThread->deleteLater(); @@ -239,142 +247,6 @@ QStringList AutotoolsProject::buildTargets() const return targets; } -void AutotoolsProject::buildFileNodeTree(const QDir &directory, - const QStringList &files) -{ - // Get all existing nodes and remember them in a hash table. - // This allows to reuse existing nodes and to remove obsolete - // nodes later. - QHash nodeHash; - foreach (Node *node, nodes(rootProjectNode())) - nodeHash.insert(node->filePath().toString(), node); - - // Add the sources to the filenode project tree. Sources - // inside the same directory are grouped into a folder-node. - const QString baseDir = directory.absolutePath(); - - QList fileNodes; - FolderNode *parentFolder = 0; - FolderNode *oldParentFolder = 0; - - foreach (const QString &file, files) { - if (file.endsWith(QLatin1String(".moc"))) - continue; - - QString subDir = baseDir + QLatin1Char('/') + file; - const int lastSlashPos = subDir.lastIndexOf(QLatin1Char('/')); - if (lastSlashPos != -1) - subDir.truncate(lastSlashPos); - - // Add folder nodes, that are not already available - oldParentFolder = parentFolder; - parentFolder = 0; - if (nodeHash.contains(subDir)) { - QTC_ASSERT(nodeHash[subDir]->nodeType() == FolderNodeType, return); - parentFolder = static_cast(nodeHash[subDir]); - } else { - parentFolder = insertFolderNode(QDir(subDir), nodeHash); - if (parentFolder == 0) { - // No node gets created for the root folder - parentFolder = rootProjectNode(); - } - } - QTC_ASSERT(parentFolder, return); - if (oldParentFolder && (oldParentFolder != parentFolder) && !fileNodes.isEmpty()) { - // AutotoolsProjectNode::addFileNodes() is a very expensive operation. It is - // important to collect as much file nodes of the same parent folder as - // possible before invoking it. - oldParentFolder->addFileNodes(fileNodes); - fileNodes.clear(); - } - - // Add file node - const QString filePath = directory.absoluteFilePath(file); - if (nodeHash.contains(filePath)) { - nodeHash.remove(filePath); - } else if (file == QLatin1String("Makefile.am") || file == QLatin1String("configure.ac")) { - fileNodes.append(new FileNode(Utils::FileName::fromString(filePath), - ProjectFileType, false)); - } else { - fileNodes.append(new FileNode(Utils::FileName::fromString(filePath), - ResourceType, false)); - } - } - - if (parentFolder && !fileNodes.isEmpty()) - parentFolder->addFileNodes(fileNodes); - - // Remove unused file nodes and empty folder nodes - QHash::const_iterator it = nodeHash.constBegin(); - while (it != nodeHash.constEnd()) { - if ((*it)->nodeType() == FileNodeType) { - FileNode *fileNode = static_cast(*it); - FolderNode* parent = fileNode->parentFolderNode(); - parent->removeFileNodes(QList() << fileNode); - - // Remove all empty parent folders - while (parent->subFolderNodes().isEmpty() && parent->fileNodes().isEmpty()) { - FolderNode *grandParent = parent->parentFolderNode(); - grandParent->removeFolderNodes(QList() << parent); - parent = grandParent; - if (parent == rootProjectNode()) - break; - } - } - ++it; - } -} - -FolderNode *AutotoolsProject::insertFolderNode(const QDir &nodeDir, QHash &nodes) -{ - const Utils::FileName nodePath = Utils::FileName::fromString(nodeDir.absolutePath()); - QFileInfo rootInfo = rootProjectNode()->filePath().toFileInfo(); - const Utils::FileName rootPath = Utils::FileName::fromString(rootInfo.absolutePath()); - - // Do not create a folder for the root node - if (rootPath == nodePath) - return 0; - - FolderNode *folder = new FolderNode(nodePath); - QDir dir(nodeDir); - folder->setDisplayName(dir.dirName()); - - // Get parent-folder. If it does not exist, create it recursively. - // Take care that the m_rootNode is considered as top folder. - FolderNode *parentFolder = rootProjectNode(); - if ((rootPath != folder->filePath()) && dir.cdUp()) { - const QString parentDir = dir.absolutePath(); - if (!nodes.contains(parentDir)) { - FolderNode *insertedFolder = insertFolderNode(parentDir, nodes); - if (insertedFolder != 0) - parentFolder = insertedFolder; - } else { - QTC_ASSERT(nodes[parentDir]->nodeType() == FolderNodeType, return 0); - parentFolder = static_cast(nodes[parentDir]); - } - } - - parentFolder->addFolderNodes(QList() << folder); - nodes.insert(nodePath.toString(), folder); - - return folder; -} - -QList AutotoolsProject::nodes(FolderNode *parent) const -{ - QList list; - QTC_ASSERT(parent != 0, return list); - - foreach (FolderNode *folder, parent->subFolderNodes()) { - list.append(nodes(folder)); - list.append(folder); - } - foreach (FileNode *file, parent->fileNodes()) - list.append(file); - - return list; -} - static QStringList filterIncludes(const QString &absSrc, const QString &absBuild, const QStringList &in) { diff --git a/src/plugins/autotoolsprojectmanager/autotoolsproject.h b/src/plugins/autotoolsprojectmanager/autotoolsproject.h index e43ee163e0..d98686a81f 100644 --- a/src/plugins/autotoolsprojectmanager/autotoolsproject.h +++ b/src/plugins/autotoolsprojectmanager/autotoolsproject.h @@ -102,25 +102,6 @@ private: */ void onFileChanged(const QString &file); - /** - * Creates folder-nodes and file-nodes for the project tree. - */ - void buildFileNodeTree(const QDir &directory, - const QStringList &files); - - /** - * Helper function for buildFileNodeTree(): Inserts a new folder-node for - * the directory \p nodeDir and inserts it into \p nodes. If no parent - * folder exists, it will be created recursively. - */ - ProjectExplorer::FolderNode *insertFolderNode(const QDir &nodeDir, - QHash &nodes); - - /** - * @return All nodes (including sub-folder- and file-nodes) for the given parent folder. - */ - QList nodes(ProjectExplorer::FolderNode *parent) const; - /** * This function is in charge of the code completion. */ diff --git a/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.cpp b/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.cpp index cd4c613fa3..e98246ed52 100644 --- a/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.cpp +++ b/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.cpp @@ -34,8 +34,8 @@ using namespace AutotoolsProjectManager; using namespace AutotoolsProjectManager::Internal; using namespace ProjectExplorer; -AutotoolsProjectNode::AutotoolsProjectNode(const Utils::FileName &projectFilePath) : - ProjectNode(projectFilePath) +AutotoolsProjectNode::AutotoolsProjectNode(const Utils::FileName &projectDirectory) : + ProjectNode(projectDirectory) { } bool AutotoolsProjectNode::showInSimpleTree() const diff --git a/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.h b/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.h index f09681d8b4..088e007a4a 100644 --- a/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.h +++ b/src/plugins/autotoolsprojectmanager/autotoolsprojectnode.h @@ -48,7 +48,7 @@ class AutotoolsProject; class AutotoolsProjectNode : public ProjectExplorer::ProjectNode { public: - AutotoolsProjectNode(const Utils::FileName &projectFilePath); + AutotoolsProjectNode(const Utils::FileName &projectDirectory); bool showInSimpleTree() const override; QList supportedActions(Node *node) const override; -- cgit v1.2.1