aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristian Kamm <christian.d.kamm@nokia.com>2011-08-17 14:11:35 +0200
committerChristian Kamm <christian.d.kamm@nokia.com>2011-08-19 11:12:17 +0200
commit6235ec51201976e08b86591969e3b9b94011076e (patch)
tree388a897421e6a188f39787208e87da8e4847d646 /src
parent8a6d767a8f2f98ea4e04847f92cff40d661b806f (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/plugins/cppeditor/cppfunctiondecldeflink.cpp29
-rw-r--r--src/plugins/cppeditor/cppfunctiondecldeflink.h6
2 files changed, 25 insertions, 10 deletions
diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp
index 9f33c62ffd..545ae54af4 100644
--- a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp
+++ b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp
@@ -174,7 +174,7 @@ static QSharedPointer<FunctionDeclDefLink> findLinkHelper(QSharedPointer<Functio
targetFile->startOf(targetParent),
targetEnd);
- link->targetOffset = targetStart;
+ targetFile->lineAndColumn(targetStart, &link->targetLine, &link->targetColumn);
link->targetInitial = targetInitial;
link->targetFile = targetFile;
@@ -228,7 +228,8 @@ void FunctionDeclDefLinkFinder::startFindLinkAt(
FunctionDeclDefLink::FunctionDeclDefLink()
{
hasMarker = false;
- targetOffset = 0;
+ targetLine = 0;
+ targetColumn = 0;
sourceFunction = 0;
targetFunction = 0;
targetDeclaration = 0;
@@ -276,12 +277,13 @@ void FunctionDeclDefLink::apply(CPPEditorWidget *editor, bool jumpToMatch)
CppTools::CppRefactoringFilePtr newTargetFile = refactoringChanges.file(targetFile->fileName());
if (!newTargetFile->isValid())
return;
- const int targetEnd = targetOffset + targetInitial.size();
- if (targetInitial == newTargetFile->textOf(targetOffset, targetEnd)) {
- const Utils::ChangeSet changeset = changes(snapshot);
+ const int targetStart = newTargetFile->position(targetLine, targetColumn);
+ const int targetEnd = targetStart + targetInitial.size();
+ if (targetInitial == newTargetFile->textOf(targetStart, targetEnd)) {
+ const Utils::ChangeSet changeset = changes(snapshot, targetStart);
newTargetFile->setChangeSet(changeset);
if (jumpToMatch)
- newTargetFile->setOpenEditor(true, targetOffset);
+ newTargetFile->setOpenEditor(true, targetStart);
newTargetFile->apply();
} else {
TextEditor::ToolTip::instance()->show(
@@ -354,7 +356,7 @@ static int declaredArgumentCount(Function *function)
return c;
}
-Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot)
+Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targetOffset)
{
Utils::ChangeSet changes;
@@ -379,7 +381,7 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot)
Function *newFunction = newDef->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);
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;
}
diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.h b/src/plugins/cppeditor/cppfunctiondecldeflink.h
index a75e71f3ef..d27833bced 100644
--- a/src/plugins/cppeditor/cppfunctiondecldeflink.h
+++ b/src/plugins/cppeditor/cppfunctiondecldeflink.h
@@ -89,10 +89,12 @@ public:
void apply(CPPEditorWidget *editor, bool jumpToMatch);
void hideMarker(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;
- int targetOffset;
+ // 1-based line and column
+ unsigned targetLine;
+ unsigned targetColumn;
QString targetInitial;
CPlusPlus::Document::Ptr sourceDocument;