CppEditor: Consider open editors in CppRefactoringChanges::cppFile()

... and get rid of the three copies of that code.

Change-Id: Iee95c1332fc782812f1181ee11f58d0ff4a67c6c
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-22 16:51:31 +02:00
parent ee8ad68f5b
commit fbdc246ea8
4 changed files with 25 additions and 54 deletions

View File

@@ -14,10 +14,8 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/manhattanstyle.h> #include <coreplugin/manhattanstyle.h>
#include <cppeditor/cppeditorwidget.h>
#include <cppeditor/cppmodelmanager.h> #include <cppeditor/cppmodelmanager.h>
#include <cppeditor/cpprefactoringchanges.h> #include <cppeditor/cpprefactoringchanges.h>
#include <cppeditor/cppsemanticinfo.h>
#include <debugger/analyzer/diagnosticlocation.h> #include <debugger/analyzer/diagnosticlocation.h>
@@ -256,19 +254,7 @@ void DiagnosticView::suppressCurrentDiagnosticInline()
CppRefactoringChanges changes(CppModelManager::snapshot()); CppRefactoringChanges changes(CppModelManager::snapshot());
for (auto it = diagnosticsPerFileAndLine.cbegin(); it != diagnosticsPerFileAndLine.cend(); ++it) { for (auto it = diagnosticsPerFileAndLine.cbegin(); it != diagnosticsPerFileAndLine.cend(); ++it) {
const Utils::FilePath filePath = it.key(); const CppRefactoringFilePtr refactoringFile = changes.cppFile(it.key());
CppEditorWidget *editorWidget = nullptr;
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(filePath);
for (Core::IEditor *editor : editors) {
const auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor);
if (textEditor)
editorWidget = qobject_cast<CppEditorWidget *>(textEditor->editorWidget());
if (editorWidget)
break;
}
CppRefactoringFilePtr refactoringFile
= editorWidget ? changes.file(editorWidget, editorWidget->semanticInfo().doc)
: changes.cppFile(filePath);
Utils::ChangeSet changeSet; Utils::ChangeSet changeSet;
for (auto it2 = it.value().cbegin(); it2 != it.value().cend(); ++it2) { for (auto it2 = it.value().cbegin(); it2 != it.value().cend(); ++it2) {

View File

@@ -4,6 +4,8 @@
#include "cpprefactoringchanges.h" #include "cpprefactoringchanges.h"
#include "cppeditorconstants.h" #include "cppeditorconstants.h"
#include "cppeditorwidget.h"
#include "cppsemanticinfo.h"
#include "cppworkingcopy.h" #include "cppworkingcopy.h"
#include <projectexplorer/editorconfiguration.h> #include <projectexplorer/editorconfiguration.h>
@@ -19,6 +21,7 @@
#include <utility> #include <utility>
using namespace Core;
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace Utils; 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)); CppRefactoringFilePtr result(new CppRefactoringFile(editor));
result->setCppDocument(document); result->setCppDocument(document);
if (const auto cppEditorWidget = qobject_cast<CppEditorWidget *>(editor)) {
result->m_data = QSharedPointer<CppRefactoringChangesData>::create(
cppEditorWidget->semanticInfo().snapshot);
}
return result; return result;
} }
@@ -55,6 +63,18 @@ TextEditor::RefactoringFilePtr CppRefactoringChanges::file(const FilePath &fileP
CppRefactoringFilePtr CppRefactoringChanges::cppFile(const Utils::FilePath &filePath) const 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<IEditor *> editors = DocumentModel::editorsForFilePath(filePath);
for (IEditor *editor : editors) {
if (const auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor)) {
if (const auto editorWidget = qobject_cast<CppEditorWidget *>(
textEditor->editorWidget())) {
return file(editorWidget, editorWidget->semanticInfo().doc);
}
}
}
return CppRefactoringFilePtr(new CppRefactoringFile(filePath, m_data)); return CppRefactoringFilePtr(new CppRefactoringFile(filePath, m_data));
} }

View File

@@ -223,20 +223,8 @@ private:
static CppRefactoringFilePtr getRefactoringFile(const FilePath &filePath, const State::Ptr &state) static CppRefactoringFilePtr getRefactoringFile(const FilePath &filePath, const State::Ptr &state)
{ {
CppRefactoringFilePtr &refactoringFile = state->perFileState[filePath].refactoringFile; CppRefactoringFilePtr &refactoringFile = state->perFileState[filePath].refactoringFile;
if (refactoringFile) if (!refactoringFile)
return refactoringFile; refactoringFile = state->factory.cppFile(filePath);
CppEditorWidget *editorWidget = nullptr;
const QList<IEditor *> editors = DocumentModel::editorsForFilePath(filePath);
for (IEditor *editor : editors) {
const auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor);
if (textEditor)
editorWidget = qobject_cast<CppEditorWidget *>(textEditor->editorWidget());
if (editorWidget)
break;
}
refactoringFile = editorWidget
? state->factory.file(editorWidget, editorWidget->semanticInfo().doc)
: state->factory.cppFile(filePath);
return refactoringFile; return refactoringFile;
} }

View File

@@ -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<IEditor *> editors = DocumentModel::editorsForFilePath(filePath);
for (IEditor *editor : editors) {
const auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor);
if (textEditor)
editorWidget = qobject_cast<CppEditorWidget *>(textEditor->editorWidget());
if (editorWidget)
return editorWidget;
}
return nullptr;
}
static void finish(const State::Ptr &state) static void finish(const State::Ptr &state)
{ {
CppRefactoringChanges factory{CppModelManager::snapshot()}; 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 auto findAstRange = [](const CppRefactoringFile &file, const Link &pos) {
const QList<AST *> astPath = ASTPath( const QList<AST *> astPath = ASTPath(
file.cppDocument())(pos.targetLine, pos.targetColumn + 1); file.cppDocument())(pos.targetLine, pos.targetColumn + 1);
@@ -180,7 +157,7 @@ private:
if (defLocsExpectedOrder == defLocsActualOrder) if (defLocsExpectedOrder == defLocsActualOrder)
continue; continue;
CppRefactoringFilePtr file = createRefactoringFile(it.key()); CppRefactoringFilePtr file = factory.cppFile(it.key());
ChangeSet changes; ChangeSet changes;
for (int i = 0; i < defLocsActualOrder.size(); ++i) { for (int i = 0; i < defLocsActualOrder.size(); ++i) {
const DefLocation &actualLoc = defLocsActualOrder[i]; const DefLocation &actualLoc = defLocsActualOrder[i];