diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2019-08-15 16:59:12 +0200 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2019-08-16 12:28:23 +0000 |
commit | 087396fe002462c236a6c76a20803b63183fe77d (patch) | |
tree | d0f821b7e873eef142971e2b5beffbd6c0b2e45b /src/plugins/projectexplorer/projectmodels.cpp | |
parent | 4e45f713397da8c88d150beab68b7fc862f8f6ea (diff) | |
download | qt-creator-087396fe002462c236a6c76a20803b63183fe77d.tar.gz |
ProjectExplorer: Support bulk renamings in project tree
If a user changes the name of a file abc.cpp to def.cpp, and there is a
file abc.h in the same directory, it's likely that the user wants to
rename that file to def.h. Detect such circumstances and offer the user
to automatically rename the sibling files.
Fixes: QTCREATORBUG-21738
Change-Id: Ib3ece08698a3341ef4087066d2289048f6b0fa61
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/projectexplorer/projectmodels.cpp')
-rw-r--r-- | src/plugins/projectexplorer/projectmodels.cpp | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/src/plugins/projectexplorer/projectmodels.cpp b/src/plugins/projectexplorer/projectmodels.cpp index 3e99eba356..015c260fdb 100644 --- a/src/plugins/projectexplorer/projectmodels.cpp +++ b/src/plugins/projectexplorer/projectmodels.cpp @@ -59,6 +59,8 @@ #include <QVBoxLayout> #include <functional> +#include <tuple> +#include <vector> using namespace Utils; @@ -221,11 +223,53 @@ bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int rol Node *node = nodeForIndex(index); QTC_ASSERT(node, return false); + std::vector<std::tuple<Node *, FilePath, FilePath>> toRename; const Utils::FilePath orgFilePath = node->filePath(); const Utils::FilePath newFilePath = orgFilePath.parentDir().pathAppended(value.toString()); + const QFileInfo orgFileInfo = orgFilePath.toFileInfo(); + toRename.emplace_back(std::make_tuple(node, orgFilePath, newFilePath)); + + // The base name of the file was changed. Go look for other files with the same base name + // and offer to rename them as well. + if (orgFilePath != newFilePath && orgFileInfo.suffix() == newFilePath.toFileInfo().suffix()) { + ProjectNode *productNode = node->parentProjectNode(); + while (productNode && !productNode->isProduct()) + productNode = productNode->parentProjectNode(); + if (productNode) { + const auto filter = [&orgFilePath, &orgFileInfo](const Node *n) { + return n->asFileNode() + && n->filePath().toFileInfo().dir() == orgFileInfo.dir() + && n->filePath().toFileInfo().completeBaseName() + == orgFileInfo.completeBaseName() + && n->filePath() != orgFilePath; + }; + const QList<Node *> candidateNodes = productNode->findNodes(filter); + if (!candidateNodes.isEmpty()) { + const QMessageBox::StandardButton reply = QMessageBox::question( + Core::ICore::mainWindow(), tr("Rename More Files?"), + tr("Would you like to rename these files as well?\n %1") + .arg(transform<QStringList>(candidateNodes, [](const Node *n) { + return n->filePath().toFileInfo().fileName(); + }).join("\n "))); + if (reply == QMessageBox::Yes) { + for (Node * const n : candidateNodes) { + QString targetFilePath = orgFileInfo.absolutePath() + '/' + + newFilePath.toFileInfo().completeBaseName(); + const QString suffix = n->filePath().toFileInfo().suffix(); + if (!suffix.isEmpty()) + targetFilePath.append('.').append(suffix); + toRename.emplace_back(std::make_tuple(n, n->filePath(), + FilePath::fromString(targetFilePath))); + } + } + } + } + } - ProjectExplorerPlugin::renameFile(node, newFilePath.toString()); - emit renamed(orgFilePath, newFilePath); + for (const auto &f : toRename) { + ProjectExplorerPlugin::renameFile(std::get<0>(f), std::get<2>(f).toString()); + emit renamed(std::get<1>(f), std::get<2>(f)); + } return true; } |