diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2024-03-14 10:24:46 +0100 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2024-04-09 08:18:30 +0000 |
commit | 42edb0dd61a05702370a02d3efcbd5ba32e05e6b (patch) | |
tree | b7bc939e01bb51180e8ccbabb3415841d3cc93d0 /src/plugins/cppeditor/cppmodelmanager.cpp | |
parent | dd876dc4054b06638567afbdc3a849d56e99669d (diff) |
CppEditor: Make renameIncludes() also work for moved files
Task-number: QTCREATORBUG-26545
Change-Id: I0bfe203af8f091562cdd91411dbe502fc5a76956
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/plugins/cppeditor/cppmodelmanager.cpp')
-rw-r--r-- | src/plugins/cppeditor/cppmodelmanager.cpp | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/src/plugins/cppeditor/cppmodelmanager.cpp b/src/plugins/cppeditor/cppmodelmanager.cpp index 5ea954add4..d5d6842e00 100644 --- a/src/plugins/cppeditor/cppmodelmanager.cpp +++ b/src/plugins/cppeditor/cppmodelmanager.cpp @@ -1878,16 +1878,15 @@ void CppModelManager::renameIncludes(const QList<std::pair<FilePath, FilePath>> if (oldFilePath.isEmpty() || newFilePath.isEmpty()) continue; - // We just want to handle renamings so return when the file was actually moved. - if (oldFilePath.absolutePath() != newFilePath.absolutePath()) - continue; - const TextEditor::PlainRefactoringFileFactory changes; QString oldFileName = oldFilePath.fileName(); QString newFileName = newFilePath.fileName(); const bool isUiFile = oldFilePath.suffix() == "ui" && newFilePath.suffix() == "ui"; + const bool moved = oldFilePath.absolutePath() != newFilePath.absolutePath(); if (isUiFile) { + if (moved) + return; // This is out of scope. oldFileName = "ui_" + oldFilePath.baseName() + ".h"; newFileName = "ui_" + newFilePath.baseName() + ".h"; } @@ -1925,12 +1924,26 @@ void CppModelManager::renameIncludes(const QList<std::pair<FilePath, FilePath>> TextEditor::RefactoringFilePtr file = changes.file(includingFileNew); const QTextBlock &block = file->document()->findBlockByNumber(loc.second - 1); - const int replaceStart = block.text().indexOf(oldFileName); - if (replaceStart > -1) { + const FilePath relPathOld = FilePath::fromString(FilePath::calcRelativePath( + oldFilePath.toString(), includingFileOld.parentDir().toString())); + const FilePath relPathNew = FilePath::fromString(FilePath::calcRelativePath( + newFilePath.toString(), includingFileNew.parentDir().toString())); + int replaceStart = block.text().indexOf(relPathOld.toString()); + QString oldString; + QString newString; + if (isUiFile || replaceStart == -1) { + replaceStart = block.text().indexOf(oldFileName); + oldString = oldFileName; + newString = newFileName; + } else { + oldString = relPathOld.toString(); + newString = relPathNew.toString(); + } + if (replaceStart > -1 && oldString != newString) { ChangeSet changeSet; changeSet.replace(block.position() + replaceStart, - block.position() + replaceStart + oldFileName.length(), - newFileName); + block.position() + replaceStart + oldString.length(), + newString); file->setChangeSet(changeSet); file->apply(); } |