C++ function links: Use line+column instead of offset for target.

It's more robust if text before the target is changed in a minor way.

Change-Id: I48e27c5d194dd2dcff4b064bf59538b4660015d7
Reviewed-on: http://codereview.qt.nokia.com/3097
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
This commit is contained in:
Christian Kamm
2011-08-17 14:11:35 +02:00
parent 8a6d767a8f
commit 6235ec5120
2 changed files with 25 additions and 10 deletions

View File

@@ -174,7 +174,7 @@ static QSharedPointer<FunctionDeclDefLink> findLinkHelper(QSharedPointer<Functio
targetFile->startOf(targetParent), targetFile->startOf(targetParent),
targetEnd); targetEnd);
link->targetOffset = targetStart; targetFile->lineAndColumn(targetStart, &link->targetLine, &link->targetColumn);
link->targetInitial = targetInitial; link->targetInitial = targetInitial;
link->targetFile = targetFile; link->targetFile = targetFile;
@@ -228,7 +228,8 @@ void FunctionDeclDefLinkFinder::startFindLinkAt(
FunctionDeclDefLink::FunctionDeclDefLink() FunctionDeclDefLink::FunctionDeclDefLink()
{ {
hasMarker = false; hasMarker = false;
targetOffset = 0; targetLine = 0;
targetColumn = 0;
sourceFunction = 0; sourceFunction = 0;
targetFunction = 0; targetFunction = 0;
targetDeclaration = 0; targetDeclaration = 0;
@@ -276,12 +277,13 @@ void FunctionDeclDefLink::apply(CPPEditorWidget *editor, bool jumpToMatch)
CppTools::CppRefactoringFilePtr newTargetFile = refactoringChanges.file(targetFile->fileName()); CppTools::CppRefactoringFilePtr newTargetFile = refactoringChanges.file(targetFile->fileName());
if (!newTargetFile->isValid()) if (!newTargetFile->isValid())
return; return;
const int targetEnd = targetOffset + targetInitial.size(); const int targetStart = newTargetFile->position(targetLine, targetColumn);
if (targetInitial == newTargetFile->textOf(targetOffset, targetEnd)) { const int targetEnd = targetStart + targetInitial.size();
const Utils::ChangeSet changeset = changes(snapshot); if (targetInitial == newTargetFile->textOf(targetStart, targetEnd)) {
const Utils::ChangeSet changeset = changes(snapshot, targetStart);
newTargetFile->setChangeSet(changeset); newTargetFile->setChangeSet(changeset);
if (jumpToMatch) if (jumpToMatch)
newTargetFile->setOpenEditor(true, targetOffset); newTargetFile->setOpenEditor(true, targetStart);
newTargetFile->apply(); newTargetFile->apply();
} else { } else {
TextEditor::ToolTip::instance()->show( TextEditor::ToolTip::instance()->show(
@@ -354,7 +356,7 @@ static int declaredArgumentCount(Function *function)
return c; return c;
} }
Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot) Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targetOffset)
{ {
Utils::ChangeSet changes; Utils::ChangeSet changes;
@@ -379,7 +381,7 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot)
Function *newFunction = newDef->symbol; Function *newFunction = newDef->symbol;
QTC_ASSERT(newFunction, return changes); // check() should always create this symbol QTC_ASSERT(newFunction, return changes); // check() should always create this symbol
LookupContext sourceContext(sourceDocument, snapshot); const LookupContext &sourceContext = typeOfExpression.context();
LookupContext targetContext(targetFile->cppDocument(), snapshot); LookupContext targetContext(targetFile->cppDocument(), snapshot);
Overview overview; Overview overview;
@@ -573,5 +575,16 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot)
} }
} }
if (targetOffset != -1) {
// move all change operations to have the right start offset
const int moveAmount = targetOffset - targetFile->startOf(targetDeclaration);
QList<Utils::ChangeSet::EditOp> ops = changes.operationList();
for (int i = 0; i < ops.size(); ++i) {
ops[i].pos1 += moveAmount;
ops[i].pos2 += moveAmount;
}
changes = Utils::ChangeSet(ops);
}
return changes; return changes;
} }

View File

@@ -89,10 +89,12 @@ public:
void apply(CPPEditorWidget *editor, bool jumpToMatch); void apply(CPPEditorWidget *editor, bool jumpToMatch);
void hideMarker(CPPEditorWidget *editor); void hideMarker(CPPEditorWidget *editor);
void showMarker(CPPEditorWidget *editor); void showMarker(CPPEditorWidget *editor);
Utils::ChangeSet changes(const CPlusPlus::Snapshot &snapshot); Utils::ChangeSet changes(const CPlusPlus::Snapshot &snapshot, int targetOffset = -1);
QTextCursor linkSelection; QTextCursor linkSelection;
int targetOffset; // 1-based line and column
unsigned targetLine;
unsigned targetColumn;
QString targetInitial; QString targetInitial;
CPlusPlus::Document::Ptr sourceDocument; CPlusPlus::Document::Ptr sourceDocument;