Changed BaseTextEditor::Link to use a pair of offsets (begin, end) instead of position and length.

Done with: Thorbjorn.
This commit is contained in:
Roberto Raggi
2010-02-01 16:13:21 +01:00
parent 460c47f5c6
commit 7c88796002
3 changed files with 19 additions and 21 deletions

View File

@@ -1305,14 +1305,17 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
QTextBlock block; QTextBlock block;
const SimpleToken tk = tokenUnderCursor(tc, &block); const SimpleToken tk = tokenUnderCursor(tc, &block);
const int beginOfToken = block.position() + tk.begin();
const int endOfToken = block.position() + tk.end();
// Handle include directives // Handle include directives
if (tk.is(T_STRING_LITERAL) || tk.is(T_ANGLE_STRING_LITERAL)) { if (tk.is(T_STRING_LITERAL) || tk.is(T_ANGLE_STRING_LITERAL)) {
const unsigned lineno = cursor.blockNumber() + 1; const unsigned lineno = cursor.blockNumber() + 1;
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->includes()) {
if (incl.line() == lineno && incl.resolved()) { if (incl.line() == lineno && incl.resolved()) {
link.fileName = incl.fileName(); link.fileName = incl.fileName();
link.pos = cursor.block().position() + tk.position() + 1; link.begin = beginOfToken + 1;
link.length = tk.length() - 2; link.end = endOfToken - 1;
return link; return link;
} }
} }
@@ -1326,12 +1329,7 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
if (!lastSymbol) if (!lastSymbol)
return link; return link;
const int nameStart = tk.position(); tc.setPosition(endOfToken);
const int nameLength = tk.length();
const int endOfName = block.position() + nameStart + nameLength;
const QString name = block.text().mid(nameStart, nameLength);
tc.setPosition(endOfName);
// Evaluate the type of the expression under the cursor // Evaluate the type of the expression under the cursor
ExpressionUnderCursor expressionUnderCursor; ExpressionUnderCursor expressionUnderCursor;
@@ -1384,8 +1382,8 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
def = findDefinition(symbol); def = findDefinition(symbol);
link = linkToSymbol(def ? def : symbol); link = linkToSymbol(def ? def : symbol);
link.pos = block.position() + nameStart; link.begin = beginOfToken;
link.length = nameLength; link.end = endOfToken;
return link; return link;
// This would jump to the type of a name // This would jump to the type of a name
@@ -1400,13 +1398,13 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
} }
} else { } else {
// Handle macro uses // Handle macro uses
const Document::MacroUse *use = doc->findMacroUseAt(endOfName - 1); const Document::MacroUse *use = doc->findMacroUseAt(endOfToken - 1);
if (use) { if (use) {
const Macro &macro = use->macro(); const Macro &macro = use->macro();
link.fileName = macro.fileName(); link.fileName = macro.fileName();
link.line = macro.line(); link.line = macro.line();
link.pos = use->begin(); link.begin = use->begin();
link.length = use->end() - use->begin(); link.end = use->end();
return link; return link;
} }
} }

View File

@@ -4144,8 +4144,8 @@ void BaseTextEditor::showLink(const Link &link)
QTextEdit::ExtraSelection sel; QTextEdit::ExtraSelection sel;
sel.cursor = textCursor(); sel.cursor = textCursor();
sel.cursor.setPosition(link.pos); sel.cursor.setPosition(link.begin);
sel.cursor.setPosition(link.pos + link.length, QTextCursor::KeepAnchor); sel.cursor.setPosition(link.end, QTextCursor::KeepAnchor);
sel.format = d->m_linkFormat; sel.format = d->m_linkFormat;
sel.format.setFontUnderline(true); sel.format.setFontUnderline(true);
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel); setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel);

View File

@@ -567,21 +567,21 @@ protected:
Link(const QString &fileName = QString(), Link(const QString &fileName = QString(),
int line = 0, int line = 0,
int column = 0) int column = 0)
: pos(-1) : begin(-1)
, length(-1) , end(-1)
, fileName(fileName) , fileName(fileName)
, line(line) , line(line)
, column(column) , column(column)
{} {}
bool isValid() const bool isValid() const
{ return !(pos == -1 || length == -1); } { return begin != end; }
bool operator==(const Link &other) const bool operator==(const Link &other) const
{ return pos == other.pos && length == other.length; } { return begin == other.begin && end == other.end; }
int pos; // Link position int begin; // Link position
int length; // Link length int end; // Link end position
QString fileName; // Target file QString fileName; // Target file
int line; // Target line int line; // Target line