forked from qt-creator/qt-creator
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:
@@ -14,10 +14,8 @@
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/manhattanstyle.h>
|
||||
|
||||
#include <cppeditor/cppeditorwidget.h>
|
||||
#include <cppeditor/cppmodelmanager.h>
|
||||
#include <cppeditor/cpprefactoringchanges.h>
|
||||
#include <cppeditor/cppsemanticinfo.h>
|
||||
|
||||
#include <debugger/analyzer/diagnosticlocation.h>
|
||||
|
||||
@@ -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<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);
|
||||
const CppRefactoringFilePtr refactoringFile = changes.cppFile(it.key());
|
||||
Utils::ChangeSet changeSet;
|
||||
|
||||
for (auto it2 = it.value().cbegin(); it2 != it.value().cend(); ++it2) {
|
||||
|
@@ -4,6 +4,8 @@
|
||||
#include "cpprefactoringchanges.h"
|
||||
|
||||
#include "cppeditorconstants.h"
|
||||
#include "cppeditorwidget.h"
|
||||
#include "cppsemanticinfo.h"
|
||||
#include "cppworkingcopy.h"
|
||||
|
||||
#include <projectexplorer/editorconfiguration.h>
|
||||
@@ -19,6 +21,7 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
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<CppEditorWidget *>(editor)) {
|
||||
result->m_data = QSharedPointer<CppRefactoringChangesData>::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<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));
|
||||
}
|
||||
|
||||
|
@@ -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<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);
|
||||
if (!refactoringFile)
|
||||
refactoringFile = state->factory.cppFile(filePath);
|
||||
return refactoringFile;
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
{
|
||||
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<AST *> 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];
|
||||
|
Reference in New Issue
Block a user