logic: extended CodeView to show locations of edges to the active Token and merge overlapping snippets
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
#include "component/controller/CodeController.h"
|
||||
|
||||
#include "component/view/CodeView.h"
|
||||
#include "data/access/GraphAccess.h"
|
||||
#include "data/access/LocationAccess.h"
|
||||
#include "data/location/TokenLocation.h"
|
||||
#include "data/location/TokenLocationCollection.h"
|
||||
#include "data/location/TokenLocationFile.h"
|
||||
#include "utility/logging/logging.h"
|
||||
#include "utility/text/TextAccess.h"
|
||||
|
||||
CodeController::CodeController(LocationAccess* locationAccess)
|
||||
: m_locationAccess(locationAccess)
|
||||
CodeController::CodeController(GraphAccess* graphAccess, LocationAccess* locationAccess)
|
||||
: m_graphAccess(graphAccess)
|
||||
, m_locationAccess(locationAccess)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -19,23 +20,25 @@ CodeController::~CodeController()
|
||||
|
||||
void CodeController::setActiveTokenId(Id id)
|
||||
{
|
||||
const unsigned int lineRadius = 2;
|
||||
const uint lineRadius = 2;
|
||||
|
||||
getView()->clearCodeSnippets();
|
||||
|
||||
TokenLocationCollection collection = m_locationAccess->getTokenLocationsForTokenId(id);
|
||||
collection.forEachTokenLocation(
|
||||
[&](TokenLocation* tokenLocation) -> void
|
||||
{
|
||||
if (tokenLocation->isStartTokenLocation())
|
||||
{
|
||||
const std::string filePath = tokenLocation->getFilePath();
|
||||
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(filePath);
|
||||
std::vector<Id> activeTokenIds = m_graphAccess->getActiveTokenIdsForId(id);
|
||||
std::vector<Id> locationIds = m_graphAccess->getLocationIdsForTokenIds(activeTokenIds);
|
||||
|
||||
unsigned int firstLineNumber = std::max<int>(1, tokenLocation->getLineNumber() - lineRadius);
|
||||
unsigned int lastLineNumber = std::min<int>(
|
||||
textAccess->getLineCount(), tokenLocation->getEndTokenLocation()->getLineNumber() + lineRadius
|
||||
);
|
||||
TokenLocationCollection collection = m_locationAccess->getTokenLocationsForLocationIds(locationIds);
|
||||
collection.forEachTokenLocationFile(
|
||||
[&](TokenLocationFile* file) -> void
|
||||
{
|
||||
const std::string filePath = file->getFilePath();
|
||||
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(filePath);
|
||||
|
||||
std::vector<std::pair<uint, uint>> ranges = getSnippetRangesForFile(file, lineRadius);
|
||||
for (const std::pair<uint, uint>& range: ranges)
|
||||
{
|
||||
unsigned int firstLineNumber = std::max<int>(1, range.first - lineRadius);
|
||||
unsigned int lastLineNumber = std::min<int>(textAccess->getLineCount(), range.second + lineRadius);
|
||||
|
||||
CodeView::CodeSnippetParams params;
|
||||
for (const std::string& line: textAccess->getLines(firstLineNumber, lastLineNumber))
|
||||
@@ -46,7 +49,7 @@ void CodeController::setActiveTokenId(Id id)
|
||||
params.startLineNumber = firstLineNumber;
|
||||
params.locationFile =
|
||||
m_locationAccess->getTokenLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber);
|
||||
params.activeTokenId = id;
|
||||
params.activeTokenIds = activeTokenIds;
|
||||
|
||||
getView()->addCodeSnippet(params);
|
||||
}
|
||||
@@ -68,3 +71,40 @@ CodeView* CodeController::getView()
|
||||
{
|
||||
return Controller::getView<CodeView>();
|
||||
}
|
||||
|
||||
std::vector<std::pair<uint, uint>> CodeController::getSnippetRangesForFile(
|
||||
TokenLocationFile* file, const uint lineRadius
|
||||
) const
|
||||
{
|
||||
std::vector<std::pair<uint, uint>> ranges;
|
||||
uint start = 0;
|
||||
uint end = 0;
|
||||
|
||||
file->forEachTokenLocation(
|
||||
[&](TokenLocation* location) -> void
|
||||
{
|
||||
uint lineNumber = location->getLineNumber();
|
||||
|
||||
if (location->isStartTokenLocation())
|
||||
{
|
||||
if (!start)
|
||||
{
|
||||
start = lineNumber;
|
||||
}
|
||||
else if (end && lineNumber > end + 2 * lineRadius + 1)
|
||||
{
|
||||
ranges.push_back(std::make_pair(uint(start), uint(end)));
|
||||
start = lineNumber;
|
||||
end = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
end = lineNumber;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
ranges.push_back(std::make_pair(uint(start), uint(end)));
|
||||
return ranges;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user