forked from qt-creator/qt-creator
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:
@@ -80,25 +80,24 @@ public:
|
||||
"Add %1 Declaration").arg(type));
|
||||
}
|
||||
|
||||
void performChanges(CppRefactoringFile *, CppRefactoringChanges *refactoring)
|
||||
void performChanges(const CppRefactoringFilePtr &,
|
||||
const CppRefactoringChanges &refactoring)
|
||||
{
|
||||
InsertionPointLocator locator(refactoring);
|
||||
const InsertionLocation loc = locator.methodDeclarationInClass(
|
||||
m_targetFileName, m_targetSymbol, m_xsSpec);
|
||||
Q_ASSERT(loc.isValid());
|
||||
|
||||
CppRefactoringFile targetFile = refactoring->file(m_targetFileName);
|
||||
int targetPosition1 = targetFile.position(loc.line(), loc.column());
|
||||
int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1);
|
||||
CppRefactoringFilePtr targetFile = refactoring.file(m_targetFileName);
|
||||
int targetPosition1 = targetFile->position(loc.line(), loc.column());
|
||||
int targetPosition2 = qMax(0, targetFile->position(loc.line(), 1) - 1);
|
||||
|
||||
Utils::ChangeSet target;
|
||||
target.insert(targetPosition1, loc.prefix() + m_decl);
|
||||
targetFile.change(target);
|
||||
targetFile.indent(Utils::ChangeSet::Range(targetPosition2, targetPosition1));
|
||||
|
||||
const int prefixLineCount = loc.prefix().count(QLatin1Char('\n'));
|
||||
refactoring->activateEditor(m_targetFileName, loc.line() + prefixLineCount,
|
||||
qMax(((int) loc.column()) - 1, 0));
|
||||
targetFile->setChangeSet(target);
|
||||
targetFile->appendIndentRange(Utils::ChangeSet::Range(targetPosition2, targetPosition1));
|
||||
targetFile->setOpenEditor(true, targetPosition1);
|
||||
targetFile->apply();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -114,7 +113,7 @@ QList<CppQuickFixOperation::Ptr> DeclFromDef::match(
|
||||
const QSharedPointer<const Internal::CppQuickFixAssistInterface> &interface)
|
||||
{
|
||||
const QList<AST *> &path = interface->path();
|
||||
const CppRefactoringFile &file = interface->currentFile();
|
||||
CppRefactoringFilePtr file = interface->currentFile();
|
||||
|
||||
FunctionDefinitionAST *funDef = 0;
|
||||
int idx = 0;
|
||||
@@ -122,7 +121,7 @@ QList<CppQuickFixOperation::Ptr> DeclFromDef::match(
|
||||
AST *node = path.at(idx);
|
||||
if (idx > 1) {
|
||||
if (DeclaratorIdAST *declId = node->asDeclaratorId()) {
|
||||
if (file.isCursorOn(declId)) {
|
||||
if (file->isCursorOn(declId)) {
|
||||
if (FunctionDefinitionAST *candidate = path.at(idx - 2)->asFunctionDefinition()) {
|
||||
if (funDef) {
|
||||
return noResult();
|
||||
@@ -230,12 +229,12 @@ public:
|
||||
.arg(dir.relativeFilePath(m_loc.fileName())));
|
||||
}
|
||||
|
||||
void performChanges(CppRefactoringFile *,
|
||||
CppRefactoringChanges *refactoring)
|
||||
void performChanges(const CppRefactoringFilePtr &,
|
||||
const CppRefactoringChanges &refactoring)
|
||||
{
|
||||
Q_ASSERT(m_loc.isValid());
|
||||
|
||||
CppRefactoringFile targetFile = refactoring->file(m_loc.fileName());
|
||||
CppRefactoringFilePtr targetFile = refactoring.file(m_loc.fileName());
|
||||
|
||||
Overview oo;
|
||||
oo.setShowFunctionSignatures(true);
|
||||
@@ -243,7 +242,7 @@ public:
|
||||
oo.setShowArgumentNames(true);
|
||||
|
||||
// make target lookup context
|
||||
Document::Ptr targetDoc = targetFile.cppDocument();
|
||||
Document::Ptr targetDoc = targetFile->cppDocument();
|
||||
Scope *targetScope = targetDoc->scopeAt(m_loc.line(), m_loc.column());
|
||||
LookupContext targetContext(targetDoc, assistInterface()->snapshot());
|
||||
ClassOrNamespace *targetCoN = targetContext.lookupType(targetScope);
|
||||
@@ -272,18 +271,15 @@ public:
|
||||
|
||||
QString defText = oo.prettyType(tn, name) + "\n{\n}";
|
||||
|
||||
int targetPos = targetFile.position(m_loc.line(), m_loc.column());
|
||||
int targetPos2 = qMax(0, targetFile.position(m_loc.line(), 1) - 1);
|
||||
int targetPos = targetFile->position(m_loc.line(), m_loc.column());
|
||||
int targetPos2 = qMax(0, targetFile->position(m_loc.line(), 1) - 1);
|
||||
|
||||
Utils::ChangeSet target;
|
||||
target.insert(targetPos, m_loc.prefix() + defText + m_loc.suffix());
|
||||
targetFile.change(target);
|
||||
targetFile.indent(Utils::ChangeSet::Range(targetPos2, targetPos));
|
||||
|
||||
const int prefixLineCount = m_loc.prefix().count(QLatin1Char('\n'));
|
||||
refactoring->activateEditor(m_loc.fileName(),
|
||||
m_loc.line() + prefixLineCount,
|
||||
0);
|
||||
targetFile->setChangeSet(target);
|
||||
targetFile->appendIndentRange(Utils::ChangeSet::Range(targetPos2, targetPos));
|
||||
targetFile->setOpenEditor(true, targetPos);
|
||||
targetFile->apply();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -297,7 +293,7 @@ QList<CppQuickFixOperation::Ptr> DefFromDecl::match(
|
||||
const QSharedPointer<const Internal::CppQuickFixAssistInterface> &interface)
|
||||
{
|
||||
const QList<AST *> &path = interface->path();
|
||||
const CppRefactoringFile &file = interface->currentFile();
|
||||
CppRefactoringFilePtr file = interface->currentFile();
|
||||
|
||||
int idx = path.size() - 1;
|
||||
for (; idx >= 0; --idx) {
|
||||
@@ -311,9 +307,9 @@ QList<CppQuickFixOperation::Ptr> DefFromDecl::match(
|
||||
&& decl->enclosingScope()
|
||||
&& decl->enclosingScope()->isClass()) {
|
||||
DeclaratorAST *declarator = simpleDecl->declarator_list->value;
|
||||
if (file.isCursorOn(declarator->core_declarator)) {
|
||||
if (file->isCursorOn(declarator->core_declarator)) {
|
||||
CppRefactoringChanges refactoring(interface->snapshot());
|
||||
InsertionPointLocator locator(&refactoring);
|
||||
InsertionPointLocator locator(refactoring);
|
||||
QList<CppQuickFixOperation::Ptr> results;
|
||||
foreach (const InsertionLocation &loc, locator.methodDefinition(decl)) {
|
||||
if (loc.isValid())
|
||||
|
||||
Reference in New Issue
Block a user