logic: Codeview Snippet order

Sorting the Snippets in. the Codeview. Active Token first then declaration,
filename and extension
This commit is contained in:
Andreas Stallinger
2014-10-24 13:37:06 +02:00
parent a1b874c520
commit 0a4c65d198
17 changed files with 157 additions and 42 deletions
+42 -16
View File
@@ -8,6 +8,8 @@
#include "data/location/TokenLocationFile.h"
#include "utility/text/TextAccess.h"
const uint CodeController::s_lineRadius = 2;
CodeController::CodeController(GraphAccess* graphAccess, LocationAccess* locationAccess)
: m_graphAccess(graphAccess)
, m_locationAccess(locationAccess)
@@ -18,26 +20,29 @@ CodeController::~CodeController()
{
}
void CodeController::setActiveTokenIds(const std::vector<Id>& ids)
void CodeController::setActiveTokenIds(const std::vector<Id>& ids, Id activeId, Id declarationId)
{
const uint lineRadius = 2;
getView()->clearCodeSnippets();
getView()->setActiveTokenIds(ids);
std::vector<Id> locationIds = m_graphAccess->getLocationIdsForTokenIds(ids);
std::vector<CodeView::CodeSnippetParams> snippets;
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);
std::vector<std::pair<uint, uint>> ranges = getSnippetRangesForFile(file, s_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);
unsigned int firstLineNumber = std::max<int>(1, range.first - s_lineRadius);
unsigned int lastLineNumber = std::min<int>(textAccess->getLineCount(), range.second + s_lineRadius);
CodeView::CodeSnippetParams params;
for (const std::string& line: textAccess->getLines(firstLineNumber, lastLineNumber))
@@ -47,31 +52,52 @@ void CodeController::setActiveTokenIds(const std::vector<Id>& ids)
params.startLineNumber = firstLineNumber;
params.locationFile =
m_locationAccess->getTokenLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber);
params.activeTokenIds = ids;
m_locationAccess->getTokenLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber);
params.locationFile.forEachTokenLocation(
[&](TokenLocation* location)
{
if(location->getTokenId() == activeId && activeId != 0)
{
params.isActive = true;
}
if(location->getTokenId() == declarationId && declarationId != 0)
{
params.isDeclaration = true;
}
}
);
getView()->addCodeSnippet(params);
snippets.push_back(params);
}
}
);
std::sort(snippets.begin(), snippets.end(), CodeView::CodeSnippetParams::sort);
for( CodeView::CodeSnippetParams p : snippets)
{
getView()->addCodeSnippet(p);
}
}
void CodeController::handleMessage(MessageActivateToken* message)
{
std::vector<Id> activeTokenIds = m_graphAccess->getActiveTokenIdsForId(message->tokenId);
setActiveTokenIds(activeTokenIds);
Id declarationId;
std::vector<Id> activeTokenIds = m_graphAccess->getActiveTokenIdsForId(message->tokenId, declarationId);
setActiveTokenIds(activeTokenIds, message->tokenId, declarationId);
}
void CodeController::handleMessage(MessageActivateTokens* message)
{
if (message->tokenIds.size() == 1)
if (message->tokenIds.size () == 1)
{
std::vector<Id> activeTokenIds = m_graphAccess->getActiveTokenIdsForId(message->tokenIds[0]);
setActiveTokenIds(activeTokenIds);
Id declarationId;
std::vector<Id> activeTokenIds = m_graphAccess->getActiveTokenIdsForId(message->tokenIds[0], declarationId);
setActiveTokenIds(activeTokenIds, message->tokenIds[0], declarationId);
}
else
{
setActiveTokenIds(message->tokenIds);
setActiveTokenIds(message->tokenIds, 0, 0);
}
}
@@ -85,7 +111,7 @@ void CodeController::handleMessage(MessageShowFile* message)
CodeView::CodeSnippetParams params;
params.startLineNumber = message->startLineNumber;
params.endLineNumber = message->endLineNumber;
params.activeTokenIds = message->activeTokenIds;
getView()->setActiveTokenIds(message->activeTokenIds);
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(message->filePath);
params.lineCount = textAccess->getLineCount();
@@ -33,7 +33,7 @@ public:
CodeController(GraphAccess* graphAccess, LocationAccess* locationAccess);
~CodeController();
void setActiveTokenIds(const std::vector<Id>& ids);
void setActiveTokenIds(const std::vector<Id>& ids, Id activeId, Id declarationId);
private:
virtual void handleMessage(MessageActivateToken* message);
@@ -43,6 +43,8 @@ private:
CodeView* getView();
static const uint s_lineRadius;
std::vector<std::pair<uint, uint>> getSnippetRangesForFile(TokenLocationFile* file, const uint lineRadius) const;
GraphAccess* m_graphAccess;
+45 -1
View File
@@ -1,12 +1,56 @@
#include "component/view/CodeView.h"
#include "component/controller/CodeController.h"
#include "utility/FileSystem.h"
CodeView::CodeSnippetParams::CodeSnippetParams()
: locationFile("")
: startLineNumber(0)
, endLineNumber(0)
, lineCount(0)
, locationFile("")
, isActive(false)
, isDeclaration(false)
{
}
bool CodeView::CodeSnippetParams::sort( CodeSnippetParams a, CodeSnippetParams b )
{
// sort active snippet first
if(a.isActive)
{
return true;
}
if(b.isActive)
{
return false;
}
// sort declarations
if(a.isDeclaration && !b.isDeclaration)
{
return true;
}
else if (!a.isDeclaration && b.isDeclaration)
{
return false;
}
else
{
// first header
if( FileSystem::filePathWithoutExtension(a.locationFile.getFilePath())
== FileSystem::filePathWithoutExtension(b.locationFile.getFilePath()) )
{
return FileSystem::extension(a.locationFile.getFilePath())
> FileSystem::extension(b.locationFile.getFilePath());
}
// alphabetical filepath without extension
else
{
return FileSystem::filePathWithoutExtension(a.locationFile.getFilePath())
< FileSystem::filePathWithoutExtension(b.locationFile.getFilePath());
}
}
}
CodeView::CodeView(ViewLayout* viewLayout)
: View(viewLayout, Vec2i(100, 100))
{
+9 -2
View File
@@ -19,8 +19,14 @@ public:
uint lineCount;
std::string code;
TokenLocationFile locationFile;
std::vector<Id> activeTokenIds;
TokenLocationFile locationFile;
bool isActive;
bool isDeclaration;
//comparefunctions for snippetsorting
static bool sort(CodeSnippetParams a, CodeSnippetParams b);
};
CodeView(ViewLayout* viewLayout);
@@ -31,6 +37,7 @@ public:
virtual void showCodeFile(const CodeSnippetParams& params) = 0;
virtual void addCodeSnippet(const CodeSnippetParams& params) = 0;
virtual void clearCodeSnippets() = 0;
virtual void setActiveTokenIds(std::vector<Id> ids) = 0;
private:
CodeController* getController();
+8 -3
View File
@@ -392,7 +392,7 @@ std::shared_ptr<Graph> Storage::getGraphForActiveTokenIds(const std::vector<Id>&
return graph;
}
std::vector<Id> Storage::getActiveTokenIdsForId(Id tokenId) const
std::vector<Id> Storage::getActiveTokenIdsForId(Id tokenId, Id& declarationId) const
{
std::vector<Id> ret;
Token* token = m_graph.getTokenById(tokenId);
@@ -401,17 +401,22 @@ std::vector<Id> Storage::getActiveTokenIdsForId(Id tokenId) const
return ret;
}
ret.push_back(token->getId());
Node* node;
if (token->isEdge())
{
node = dynamic_cast<Edge*>(token)->getTo();
declarationId = node->getId();
ret.push_back(node->getId());
}
else
{
{
node = dynamic_cast<Node*>(token);
declarationId = node->getId();
}
ret.push_back(node->getId());
//ret.push_back(node->getId());
node->forEachEdge(
[&node, &ret](Edge* e)
+1 -1
View File
@@ -75,7 +75,7 @@ public:
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId) const;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId, Id& declarationId) const;
virtual std::vector<Id> getLocationIdsForTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::vector<Id> getTokenIdsForQuery(std::string query) const;
+1 -1
View File
@@ -20,7 +20,7 @@ public:
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const = 0;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId) const = 0;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId, Id& declarationId) const = 0;
virtual std::vector<Id> getLocationIdsForTokenIds(const std::vector<Id>& tokenIds) const = 0;
virtual std::vector<Id> getTokenIdsForQuery(std::string query) const = 0;
+2 -2
View File
@@ -67,11 +67,11 @@ std::shared_ptr<Graph> GraphAccessProxy::getGraphForActiveTokenIds(const std::ve
return std::make_shared<Graph>();
}
std::vector<Id> GraphAccessProxy::getActiveTokenIdsForId(Id tokenId) const
std::vector<Id> GraphAccessProxy::getActiveTokenIdsForId(Id tokenId, Id& delcarationId) const
{
if (hasSubject())
{
return m_subject->getActiveTokenIdsForId(tokenId);
return m_subject->getActiveTokenIdsForId(tokenId, delcarationId);
}
return std::vector<Id>();
+1 -1
View File
@@ -19,7 +19,7 @@ public:
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId) const;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId, Id& declarationId) const;
virtual std::vector<Id> getLocationIdsForTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::vector<Id> getTokenIdsForQuery(std::string query) const;
+10
View File
@@ -55,3 +55,13 @@ std::string FileSystem::fileName(const std::string& path)
{
return boost::filesystem::path(path).filename().generic_string();
}
std::string FileSystem::extension(const std::string& path)
{
return boost::filesystem::path(path).extension().generic_string();
}
std::string FileSystem::filePathWithoutExtension(const std::string& path)
{
return boost::filesystem::path(path).replace_extension().generic_string();
}
+2
View File
@@ -17,6 +17,8 @@ public:
static bool exists(const std::string& path);
static std::string fileName(const std::string& path);
static std::string extension(const std::string& path);
static std::string filePathWithoutExtension(const std::string& path);
private:
static bool isValidExtension(const std::string& filepath, const std::vector<std::string>& extensions);