Commit 5f68b5b0 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by David Schulz

C++: split lines only once in find usages.

Change-Id: I16f91e81402ecff0039acf1dfc68d9ce7f6d1c81
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent 444c5ae1
......@@ -41,37 +41,6 @@
using namespace CPlusPlus;
namespace {
QString fetchLine(const QByteArray &bytes, const int line)
{
int current = 0;
const char *s = bytes.constData();
if (line) {
while (*s) {
if (*s == '\n') {
++current;
if (line == current) {
++s;
break;
}
}
++s;
}
}
if (current == line) {
const char *e = s;
while (*e && *e != '\n')
++e;
return QString::fromUtf8(s, e - s);
}
return QString();
}
} // Anonymous
FindUsages::FindUsages(const QByteArray &originalSource, Document::Ptr doc, const Snapshot &snapshot)
: ASTVisitor(doc->translationUnit()),
_id(0),
......@@ -85,6 +54,8 @@ FindUsages::FindUsages(const QByteArray &originalSource, Document::Ptr doc, cons
{
_snapshot.insert(_doc);
typeofExpression.init(_doc, _snapshot, _context.bindings());
prepareLines(_originalSource);
}
FindUsages::FindUsages(const LookupContext &context)
......@@ -99,6 +70,8 @@ FindUsages::FindUsages(const LookupContext &context)
_currentScope(0)
{
typeofExpression.init(_doc, _snapshot, _context.bindings());
prepareLines(_originalSource);
}
QList<Usage> FindUsages::usages() const
......@@ -178,8 +151,7 @@ QString FindUsages::matchingLine(const Token &tk) const
break;
}
const QString matchingLine = QString::fromUtf8(cp, lineEnd - cp);
return matchingLine;
return QString::fromUtf8(cp, lineEnd - cp);
}
void FindUsages::reportResult(unsigned tokenIndex)
......@@ -195,9 +167,8 @@ void FindUsages::reportResult(unsigned tokenIndex)
unsigned line, col;
getTokenStartPosition(tokenIndex, &line, &col);
QString lineText;
const int lines = _originalSource.count('\n') + 1;
if (((int) line - 1) < lines)
lineText = fetchLine(_originalSource, line - 1);
if (line < _sourceLineEnds.size())
lineText = fetchLine(line);
else
lineText = matchingLine(tk);
......@@ -2167,3 +2138,28 @@ bool FindUsages::visit(ArrayDeclaratorAST *ast)
// unsigned rbracket_token = ast->rbracket_token;
return false;
}
void FindUsages::prepareLines(const QByteArray &bytes)
{
_sourceLineEnds.reserve(1000);
const char *s = bytes.constData();
_sourceLineEnds.push_back(s - 1); // we start counting at line 1, so line 0 is always empty.
for (; *s; ++s)
if (*s == '\n')
_sourceLineEnds.push_back(s);
if (s != _sourceLineEnds.back() + 1) // no newline at the end of the file
_sourceLineEnds.push_back(s);
}
QString FindUsages::fetchLine(unsigned lineNr) const
{
Q_ASSERT(lineNr < _sourceLineEnds.size());
if (lineNr == 0)
return QString();
const char *start = _sourceLineEnds.at(lineNr - 1) + 1;
const char *end = _sourceLineEnds.at(lineNr);
return QString::fromUtf8(start, end - start);
}
......@@ -283,6 +283,10 @@ protected:
virtual bool visit(FunctionDeclaratorAST *ast);
virtual bool visit(ArrayDeclaratorAST *ast);
private:
void prepareLines(const QByteArray &bytes);
QString fetchLine(unsigned lineNr) const;
private:
const Identifier *_id;
Symbol *_declSymbol;
......@@ -290,7 +294,8 @@ private:
Document::Ptr _doc;
Snapshot _snapshot;
LookupContext _context;
QByteArray _originalSource;
const QByteArray _originalSource;
std::vector<const char *> _sourceLineEnds;
QByteArray _source;
QList<int> _references;
QList<Usage> _usages;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment