From b36c695f216588b444ba511fbe1733fb112ab3d9 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Thu, 1 Jan 2015 15:10:13 +0100 Subject: Added new functions to get the context at a certain byte offset --- src/core/common/CharReader.cpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'src/core/common/CharReader.cpp') diff --git a/src/core/common/CharReader.cpp b/src/core/common/CharReader.cpp index 1a52f53..db9bb2e 100644 --- a/src/core/common/CharReader.cpp +++ b/src/core/common/CharReader.cpp @@ -516,10 +516,11 @@ CharReaderFork CharReader::fork() return CharReaderFork(buffer, readCursor, peekCursor, coherent); } -TextCursor::Context CharReader::getContext(ssize_t maxSize) +SourceContext CharReader::getContextAt(ssize_t maxSize, + Buffer::CursorId referenceCursor) { - // Clone the current read cursor - Buffer::CursorId cur = buffer->createCursor(readCursor.cursor); + // Clone the given read cursor + Buffer::CursorId cur = buffer->createCursor(referenceCursor); // Fetch the start position of the search ssize_t offs = buffer->offset(cur); @@ -612,8 +613,37 @@ TextCursor::Context CharReader::getContext(ssize_t maxSize) // Delete the newly created cursor buffer->deleteCursor(cur); - return TextCursor::Context{ss.str(), relPos, !foundBegin || tStart != start, - !foundEnd || tEnd != end}; + return SourceContext{ss.str(), relPos, !foundBegin || tStart != start, + !foundEnd || tEnd != end}; +} + +SourceContext CharReader::getContextAtOffs(ssize_t maxSize, size_t offs) +{ + // Create a new cursor and calculate how far it has to be moved to reach + // the position specified in the location instance + Buffer::CursorId cur = buffer->createCursor(); + ssize_t moveOffs = buffer->offset(cur) - offs; + + // Try to move the cursor to the specified position and read the context + SourceContext res; + if (buffer->moveCursor(cur, moveOffs) == moveOffs) { + res = getContextAt(60, cur); + } + + // Delete the read cursor + buffer->deleteCursor(cur); + return res; +} + +SourceContext CharReader::getContext(ssize_t maxSize) +{ + return getContextAt(maxSize, readCursor.cursor); +} + +SourceContext CharReader::contextCallback(const SourceLocation &location, + void *data) +{ + return static_cast(data)->getContextAtOffs(60, location.offs); } /* Class CharReaderFork */ -- cgit v1.2.3