Refactoring changes: Cleanup and improvements.

Previously RefactoringFiles were usually passed around by value.
However, since a RefactoringFile may sometimes own a QTextDocument
(when it was read from a file), that's not great and caused the
file to be reread after every copy.

With this change RefactoringFile becomes noncopyable and is always
owned by a shared pointer.

This change also allowed having const RefactoringFiles which is
useful because they can be safely used from other threads. See
CppRefactoringChanges::fileNoEditor.

Change-Id: I9045921d6d0f6349f9558ff2a3d8317ea172193b
Reviewed-on: http://codereview.qt.nokia.com/3084
Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
This commit is contained in:
Christian Kamm
2011-08-17 11:35:57 +02:00
parent a07acad516
commit 8a6d767a8f
30 changed files with 609 additions and 500 deletions

View File

@@ -114,7 +114,8 @@ public:
}
}
virtual void performChanges(QmlJSRefactoringFile *currentFile, QmlJSRefactoringChanges *refactoring)
virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
const QmlJSRefactoringChanges &refactoring)
{
QString componentName = m_componentName;
QString path = QFileInfo(fileName()).path();
@@ -142,7 +143,7 @@ public:
+ QLatin1String("}\n");
// stop if we can't create the new file
if (!refactoring->createFile(newFileName, txt))
if (!refactoring.createFile(newFileName, txt))
return;
QString replacement = componentName + QLatin1String(" {\n");
@@ -152,8 +153,9 @@ public:
Utils::ChangeSet changes;
changes.replace(start, end, replacement);
currentFile->change(changes);
currentFile->indent(Range(start, end + 1));
currentFile->setChangeSet(changes);
currentFile->appendIndentRange(Range(start, end + 1));
currentFile->apply();
}
};
@@ -163,13 +165,13 @@ public:
QList<QmlJSQuickFixOperation::Ptr> ComponentFromObjectDef::match(
const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface)
{
const int pos = interface->currentFile().cursor().position();
const int pos = interface->currentFile()->cursor().position();
QList<Node *> path = interface->semanticInfo().rangePath(pos);
for (int i = path.size() - 1; i >= 0; --i) {
Node *node = path.at(i);
if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) {
if (!interface->currentFile().isCursorOn(objDef->qualifiedTypeNameId))
if (!interface->currentFile()->isCursorOn(objDef->qualifiedTypeNameId))
return noResult();
// check that the node is not the root node
if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) {

View File

@@ -66,11 +66,10 @@ QmlJSQuickFixOperation::~QmlJSQuickFixOperation()
void QmlJSQuickFixOperation::perform()
{
QmlJSRefactoringChanges refactoring(ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>(),
//_state.snapshot());
m_interface->semanticInfo().snapshot);
QmlJSRefactoringFile current = refactoring.file(fileName());
QmlJSRefactoringFilePtr current = refactoring.file(fileName());
performChanges(&current, &refactoring);
performChanges(current, refactoring);
}
const QmlJSQuickFixAssistInterface *QmlJSQuickFixOperation::assistInterface() const

View File

@@ -79,8 +79,8 @@ public:
protected:
typedef Utils::ChangeSet::Range Range;
virtual void performChanges(QmlJSTools::QmlJSRefactoringFile *currentFile,
QmlJSTools::QmlJSRefactoringChanges *refactoring) = 0;
virtual void performChanges(QmlJSTools::QmlJSRefactoringFilePtr currentFile,
const QmlJSTools::QmlJSRefactoringChanges &refactoring) = 0;
const Internal::QmlJSQuickFixAssistInterface *assistInterface() const;

View File

@@ -51,6 +51,7 @@ QmlJSQuickFixAssistInterface::QmlJSQuickFixAssistInterface(QmlJSTextEditorWidget
: DefaultAssistInterface(editor->document(), editor->position(), editor->file(), reason)
, m_editor(editor)
, m_semanticInfo(editor->semanticInfo())
, m_currentFile(QmlJSRefactoringChanges::file(m_editor, m_semanticInfo.document))
{}
QmlJSQuickFixAssistInterface::~QmlJSQuickFixAssistInterface()
@@ -61,9 +62,9 @@ const SemanticInfo &QmlJSQuickFixAssistInterface::semanticInfo() const
return m_semanticInfo;
}
const QmlJSTools::QmlJSRefactoringFile QmlJSQuickFixAssistInterface::currentFile() const
QmlJSRefactoringFilePtr QmlJSQuickFixAssistInterface::currentFile() const
{
return QmlJSRefactoringFile(m_editor, m_semanticInfo.document);
return m_currentFile;
}
QWidget *QmlJSQuickFixAssistInterface::widget() const

View File

@@ -51,12 +51,13 @@ public:
virtual ~QmlJSQuickFixAssistInterface();
const SemanticInfo &semanticInfo() const;
const QmlJSTools::QmlJSRefactoringFile currentFile() const;
QmlJSTools::QmlJSRefactoringFilePtr currentFile() const;
QWidget *widget() const;
private:
QmlJSTextEditorWidget *m_editor;
SemanticInfo m_semanticInfo;
QmlJSTools::QmlJSRefactoringFilePtr m_currentFile;
};

View File

@@ -70,7 +70,7 @@ public:
{
UiObjectInitializer *objectInitializer = 0;
const int pos = interface->currentFile().cursor().position();
const int pos = interface->currentFile()->cursor().position();
if (QmlJS::AST::Node *member = interface->semanticInfo().rangeAt(pos)) {
if (QmlJS::AST::UiObjectBinding *b = QmlJS::AST::cast<QmlJS::AST::UiObjectBinding *>(member)) {
@@ -104,7 +104,8 @@ private:
"Split initializer"));
}
virtual void performChanges(QmlJSRefactoringFile *currentFile, QmlJSRefactoringChanges *)
virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
const QmlJSRefactoringChanges &)
{
Q_ASSERT(_objectInitializer != 0);
@@ -123,9 +124,10 @@ private:
changes.insert(currentFile->startOf(_objectInitializer->rbraceToken),
QLatin1String("\n"));
currentFile->change(changes);
currentFile->indent(Range(currentFile->startOf(_objectInitializer->lbraceToken),
currentFile->setChangeSet(changes);
currentFile->appendIndentRange(Range(currentFile->startOf(_objectInitializer->lbraceToken),
currentFile->startOf(_objectInitializer->rbraceToken)));
currentFile->apply();
}
};
};

View File

@@ -775,11 +775,12 @@ void QmlOutlineModel::reparentNodes(QmlOutlineItem *targetItem, int row, QList<Q
}
QmlJSRefactoringChanges refactoring(ModelManagerInterface::instance(), m_semanticInfo.snapshot);
TextEditor::RefactoringFile file = refactoring.file(m_semanticInfo.document->fileName());
file.change(changeSet);
TextEditor::RefactoringFilePtr file = refactoring.file(m_semanticInfo.document->fileName());
file->setChangeSet(changeSet);
foreach (const Utils::ChangeSet::Range &range, changedRanges) {
file.indent(range);
file->appendIndentRange(range);
}
file->apply();
}
void QmlOutlineModel::moveObjectMember(AST::UiObjectMember *toMove,