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

@@ -43,65 +43,82 @@
using namespace QmlJS;
using namespace QmlJSTools;
class QmlJSTools::QmlJSRefactoringChangesData : public TextEditor::RefactoringChangesData
{
public:
QmlJSRefactoringChangesData(ModelManagerInterface *modelManager,
const Snapshot &snapshot)
: m_modelManager(modelManager)
, m_snapshot(snapshot)
{}
virtual void indentSelection(const QTextCursor &selection,
const QString &fileName,
const TextEditor::BaseTextEditorWidget *textEditor) const
{
// ### shares code with QmlJSTextEditor::indent
QTextDocument *doc = selection.document();
QTextBlock block = doc->findBlock(selection.selectionStart());
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textEditor);
QtStyleCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block);
do {
tabSettings.indentLine(block, codeFormatter.indentFor(block));
codeFormatter.updateLineStateChange(block);
block = block.next();
} while (block.isValid() && block != end);
}
virtual void fileChanged(const QString &fileName)
{
m_modelManager->updateSourceFiles(QStringList(fileName), true);
}
QmlJS::ModelManagerInterface *m_modelManager;
QmlJS::Snapshot m_snapshot;
};
QmlJSRefactoringChanges::QmlJSRefactoringChanges(ModelManagerInterface *modelManager,
const Snapshot &snapshot)
: m_modelManager(modelManager)
, m_snapshot(snapshot)
: RefactoringChanges(new QmlJSRefactoringChangesData(modelManager, snapshot))
{
Q_ASSERT(modelManager);
}
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file(const QString &fileName) const
{
return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(fileName, m_data));
}
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file(
TextEditor::BaseTextEditorWidget *editor, const Document::Ptr &document)
{
return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(editor, document));
}
const Snapshot &QmlJSRefactoringChanges::snapshot() const
{
return m_snapshot;
return data()->m_snapshot;
}
QmlJSRefactoringFile QmlJSRefactoringChanges::file(const QString &fileName)
QmlJSRefactoringChangesData *QmlJSRefactoringChanges::data() const
{
return QmlJSRefactoringFile(fileName, this);
return static_cast<QmlJSRefactoringChangesData *>(m_data.data());
}
void QmlJSRefactoringChanges::indentSelection(const QTextCursor &selection,
const QString &fileName,
const TextEditor::BaseTextEditorWidget *textEditor) const
{
// ### shares code with QmlJSTextEditor::indent
QTextDocument *doc = selection.document();
QTextBlock block = doc->findBlock(selection.selectionStart());
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textEditor);
QtStyleCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block);
do {
tabSettings.indentLine(block, codeFormatter.indentFor(block));
codeFormatter.updateLineStateChange(block);
block = block.next();
} while (block.isValid() && block != end);
}
void QmlJSRefactoringChanges::fileChanged(const QString &fileName)
{
m_modelManager->updateSourceFiles(QStringList(fileName), true);
}
QmlJSRefactoringFile::QmlJSRefactoringFile()
{ }
QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, QmlJSRefactoringChanges *refactoringChanges)
: RefactoringFile(fileName, refactoringChanges)
QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, const QSharedPointer<TextEditor::RefactoringChangesData> &data)
: RefactoringFile(fileName, data)
{ }
QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::BaseTextEditorWidget *editor, QmlJS::Document::Ptr document)
: RefactoringFile()
: RefactoringFile(editor)
, m_qmljsDocument(document)
{
m_fileName = document->fileName();
m_editor = editor;
}
Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
@@ -109,7 +126,7 @@ Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
if (!m_qmljsDocument) {
const QString source = document()->toPlainText();
const QString name = fileName();
const Snapshot &snapshot = refactoringChanges()->snapshot();
const Snapshot &snapshot = data()->m_snapshot;
m_qmljsDocument = snapshot.documentFromSource(source, name);
m_qmljsDocument->parse();
@@ -145,7 +162,13 @@ bool QmlJSRefactoringFile::isCursorOn(AST::UiQualifiedId *ast) const
return pos <= ast->identifierToken.end();
}
QmlJSRefactoringChanges *QmlJSRefactoringFile::refactoringChanges() const
QmlJSRefactoringChangesData *QmlJSRefactoringFile::data() const
{
return static_cast<QmlJSRefactoringChanges *>(m_refactoringChanges);
return static_cast<QmlJSRefactoringChangesData *>(m_data.data());
}
void QmlJSRefactoringFile::fileChanged()
{
m_qmljsDocument.clear();
RefactoringFile::fileChanged();
}