diff --git a/src/plugins/clangtools/clangtoolsdiagnosticview.cpp b/src/plugins/clangtools/clangtoolsdiagnosticview.cpp index b8c516ad1a9..47d891ae3bc 100644 --- a/src/plugins/clangtools/clangtoolsdiagnosticview.cpp +++ b/src/plugins/clangtools/clangtoolsdiagnosticview.cpp @@ -14,10 +14,8 @@ #include #include -#include #include #include -#include #include @@ -256,19 +254,7 @@ void DiagnosticView::suppressCurrentDiagnosticInline() CppRefactoringChanges changes(CppModelManager::snapshot()); for (auto it = diagnosticsPerFileAndLine.cbegin(); it != diagnosticsPerFileAndLine.cend(); ++it) { - const Utils::FilePath filePath = it.key(); - CppEditorWidget *editorWidget = nullptr; - const QList editors = Core::DocumentModel::editorsForFilePath(filePath); - for (Core::IEditor *editor : editors) { - const auto textEditor = qobject_cast(editor); - if (textEditor) - editorWidget = qobject_cast(textEditor->editorWidget()); - if (editorWidget) - break; - } - CppRefactoringFilePtr refactoringFile - = editorWidget ? changes.file(editorWidget, editorWidget->semanticInfo().doc) - : changes.cppFile(filePath); + const CppRefactoringFilePtr refactoringFile = changes.cppFile(it.key()); Utils::ChangeSet changeSet; for (auto it2 = it.value().cbegin(); it2 != it.value().cend(); ++it2) { diff --git a/src/plugins/cppeditor/cpprefactoringchanges.cpp b/src/plugins/cppeditor/cpprefactoringchanges.cpp index 053daeb245f..3c979d9cc2b 100644 --- a/src/plugins/cppeditor/cpprefactoringchanges.cpp +++ b/src/plugins/cppeditor/cpprefactoringchanges.cpp @@ -4,6 +4,8 @@ #include "cpprefactoringchanges.h" #include "cppeditorconstants.h" +#include "cppeditorwidget.h" +#include "cppsemanticinfo.h" #include "cppworkingcopy.h" #include @@ -19,6 +21,7 @@ #include +using namespace Core; using namespace CPlusPlus; using namespace Utils; @@ -41,10 +44,15 @@ CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot) { } -CppRefactoringFilePtr CppRefactoringChanges::file(TextEditor::TextEditorWidget *editor, const Document::Ptr &document) +CppRefactoringFilePtr CppRefactoringChanges::file( + TextEditor::TextEditorWidget *editor, const Document::Ptr &document) { CppRefactoringFilePtr result(new CppRefactoringFile(editor)); result->setCppDocument(document); + if (const auto cppEditorWidget = qobject_cast(editor)) { + result->m_data = QSharedPointer::create( + cppEditorWidget->semanticInfo().snapshot); + } return result; } @@ -55,6 +63,18 @@ TextEditor::RefactoringFilePtr CppRefactoringChanges::file(const FilePath &fileP CppRefactoringFilePtr CppRefactoringChanges::cppFile(const Utils::FilePath &filePath) const { + // Prefer documents from editors, as these are already parsed and up to date with regards to + // unsaved changes. + const QList editors = DocumentModel::editorsForFilePath(filePath); + for (IEditor *editor : editors) { + if (const auto textEditor = qobject_cast(editor)) { + if (const auto editorWidget = qobject_cast( + textEditor->editorWidget())) { + return file(editorWidget, editorWidget->semanticInfo().doc); + } + } + } + return CppRefactoringFilePtr(new CppRefactoringFile(filePath, m_data)); } diff --git a/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp b/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp index 3d1ec64c134..e556f80300b 100644 --- a/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp +++ b/src/plugins/cppeditor/quickfixes/moveclasstoownfile.cpp @@ -223,20 +223,8 @@ private: static CppRefactoringFilePtr getRefactoringFile(const FilePath &filePath, const State::Ptr &state) { CppRefactoringFilePtr &refactoringFile = state->perFileState[filePath].refactoringFile; - if (refactoringFile) - return refactoringFile; - CppEditorWidget *editorWidget = nullptr; - const QList editors = DocumentModel::editorsForFilePath(filePath); - for (IEditor *editor : editors) { - const auto textEditor = qobject_cast(editor); - if (textEditor) - editorWidget = qobject_cast(textEditor->editorWidget()); - if (editorWidget) - break; - } - refactoringFile = editorWidget - ? state->factory.file(editorWidget, editorWidget->semanticInfo().doc) - : state->factory.cppFile(filePath); + if (!refactoringFile) + refactoringFile = state->factory.cppFile(filePath); return refactoringFile; } diff --git a/src/plugins/cppeditor/quickfixes/synchronizememberfunctionorder.cpp b/src/plugins/cppeditor/quickfixes/synchronizememberfunctionorder.cpp index 400988bb659..eddc4d6e5ba 100644 --- a/src/plugins/cppeditor/quickfixes/synchronizememberfunctionorder.cpp +++ b/src/plugins/cppeditor/quickfixes/synchronizememberfunctionorder.cpp @@ -127,33 +127,10 @@ private: } } - // TODO: Move to some central place for re-use - static CppEditorWidget *getEditorWidget(const FilePath &filePath) - { - CppEditorWidget *editorWidget = nullptr; - const QList editors = DocumentModel::editorsForFilePath(filePath); - for (IEditor *editor : editors) { - const auto textEditor = qobject_cast(editor); - if (textEditor) - editorWidget = qobject_cast(textEditor->editorWidget()); - if (editorWidget) - return editorWidget; - } - return nullptr; - } - static void finish(const State::Ptr &state) { CppRefactoringChanges factory{CppModelManager::snapshot()}; - // TODO: Move to some central place for re-use. - const auto createRefactoringFile = [&factory](const FilePath &filePath) - { - CppEditorWidget * const editorWidget = getEditorWidget(filePath); - return editorWidget ? factory.file(editorWidget, editorWidget->semanticInfo().doc) - : factory.cppFile(filePath); - }; - const auto findAstRange = [](const CppRefactoringFile &file, const Link &pos) { const QList astPath = ASTPath( file.cppDocument())(pos.targetLine, pos.targetColumn + 1); @@ -180,7 +157,7 @@ private: if (defLocsExpectedOrder == defLocsActualOrder) continue; - CppRefactoringFilePtr file = createRefactoringFile(it.key()); + CppRefactoringFilePtr file = factory.cppFile(it.key()); ChangeSet changes; for (int i = 0; i < defLocsActualOrder.size(); ++i) { const DefLocation &actualLoc = defLocsActualOrder[i];