logic: code controller

- Renamed MessageActivateTokenLocation to MessageActivateToken, since it uses the Token id (not the TokenLocation id).
- Implemented CodeController to add one snippet for each location where the activated token occurred.
- Added Semaphore to QtThreadedFunctor to prevent an evil race-condition.
- CodeView dispatches message when a token is clicked.
This commit is contained in:
malte_langkabel
2014-07-03 16:03:06 +02:00
parent 4c7bbbe28e
commit 4ae6f4a04a
17 changed files with 183 additions and 44 deletions
+8
View File
@@ -1,7 +1,15 @@
int main();
void foo();
int main()
{
int a = 6;
int b = 7;
int c = a * b;
return 0;
}
void foo()
{
std::string foo = "bar";
}
+5
View File
@@ -3,6 +3,7 @@
#include <functional>
#include <QObject>
#include <QSemaphore>
class QtThreadedFunctorHelper: public QObject
{
@@ -15,22 +16,26 @@ private slots:
void execute()
{
m_callback();
m_freeCallbacks.release();
}
public:
QtThreadedFunctorHelper()
: m_freeCallbacks(1)
{
QObject::connect(this, SIGNAL(signalExecution()), this, SLOT(execute()));
}
void operator()(std::function<void(void)> callback)
{
m_freeCallbacks.acquire();
m_callback = callback;
signalExecution();
}
private:
std::function<void(void)> m_callback;
QSemaphore m_freeCallbacks;
};
template <typename T1 = void, typename T2 = void, typename T3 = void>
+3 -1
View File
@@ -8,6 +8,7 @@
#include "qt/QtWidgetWrapper.h"
#include "qt/utility/QtHighLighter.h"
#include "qt/utility/utilityQt.h"
#include "utility/messaging/type/MessageActivateToken.h"
QtCodeView::QtCodeView(ViewLayout* viewLayout)
: CodeView(viewLayout)
@@ -52,7 +53,8 @@ void QtCodeView::clearCodeSnippets()
void QtCodeView::activateToken(Id tokenId) const
{
std::cout << "TODO: activate Token: " << tokenId << std::endl;
MessageActivateToken message(tokenId);
message.dispatch();
}
void QtCodeView::doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber)
+1 -1
View File
@@ -108,7 +108,7 @@ add_files(
utility/math/Vector4.h
utility/math/VectorBase.h
utility/messaging/type/MessageActivateTokenLocation.h
utility/messaging/type/MessageActivateToken.h
utility/messaging/Message.h
utility/messaging/MessageBase.h
+2 -2
View File
@@ -5,7 +5,7 @@
#include "data/parser/cxx/CxxParser.h"
#include "utility/FileSystem.h"
#include "utility/messaging/type/MessageActivateTokenLocation.h"
#include "utility/messaging/type/MessageActivateToken.h"
#include "utility/logging/logging.h"
std::shared_ptr<Project> Project::create(
@@ -46,7 +46,7 @@ void Project::parseCode()
m_storage->logGraph();
m_storage->logLocations();
MessageActivateTokenLocation message;
MessageActivateToken message(1);
message.dispatch();
}
}
+31 -10
View File
@@ -3,6 +3,8 @@
#include "component/view/CodeView.h"
#include "data/access/LocationAccess.h"
#include "data/location/TokenLocation.h"
#include "data/location/TokenLocationCollection.h"
#include "data/location/TokenLocationFile.h"
#include "utility/text/TextAccess.h"
#include "utility/logging/logging.h"
@@ -17,20 +19,39 @@ CodeController::~CodeController()
{
}
void CodeController::setActiveTokenLocationId(Id id)
void CodeController::setActiveTokenId(Id id)
{
TokenLocation* tokenLocation = m_locationAccess->getTokenLocation(id);
if (tokenLocation)
{
getView()->clearCodeSnippets();
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(tokenLocation->getFilePath());
// getView()->addCodeSnippet(textAccess->getText());
}
const unsigned int 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);
unsigned int firstLineNumber = std::max<int>(1, tokenLocation->getLineNumber() - lineRadius);
unsigned int lastLineNumber = std::min<int>(
textAccess->getLineCount(), tokenLocation->getEndTokenLocation()->getLineNumber() + lineRadius
);
std::string text;
for (std::string line: textAccess->getLines(firstLineNumber, lastLineNumber))
{
text += line;
}
TokenLocationFile tokenLocationFile = m_locationAccess->getTokenLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber);
getView()->addCodeSnippet(text, tokenLocationFile, firstLineNumber);
}
});
}
void CodeController::handleMessage(MessageActivateTokenLocation* message)
void CodeController::handleMessage(MessageActivateToken* message)
{
setActiveTokenLocationId(1);
setActiveTokenId(message->tokenId);
}
CodeView* CodeController::getView()
@@ -5,7 +5,7 @@
#include "component/controller/Controller.h"
#include "utility/messaging/MessageListener.h"
#include "utility/messaging/type/MessageActivateTokenLocation.h"
#include "utility/messaging/type/MessageActivateToken.h"
#include "utility/types.h"
class CodeView;
@@ -17,16 +17,16 @@ struct AnnotatedText
Id tokenId;
};
class CodeController: public Controller, public MessageListener<MessageActivateTokenLocation>
class CodeController: public Controller, public MessageListener<MessageActivateToken>
{
public:
CodeController(std::shared_ptr<LocationAccess> locationAccess);
~CodeController();
void setActiveTokenLocationId(Id id);
void setActiveTokenId(Id id);
private:
virtual void handleMessage(MessageActivateTokenLocation* message);
virtual void handleMessage(MessageActivateToken* message);
CodeView* getView();
std::shared_ptr<LocationAccess> m_locationAccess;
+51 -4
View File
@@ -3,6 +3,8 @@
#include <sstream>
#include "data/location/TokenLocation.h"
#include "data/location/TokenLocationFile.h"
#include "data/location/TokenLocationLine.h"
#include "data/parser/ParseLocation.h"
#include "data/parser/ParseVariable.h"
#include "utility/logging/logging.h"
@@ -173,14 +175,59 @@ void Storage::logLocations() const
LOG_INFO(str.str());
}
Token* Storage::getToken(Id tokenId)
Token* Storage::getToken(Id tokenId) const
{
return m_graph.getTokenById(tokenId);
return m_graph.getTokenById(tokenId); // Todo: copy information.
}
TokenLocation* Storage::getTokenLocation(Id locationId)
TokenLocationCollection Storage::getTokenLocationsForTokenId(Id id) const
{
return m_locationCollection.findTokenLocationById(locationId);
TokenLocationCollection ret;
std::vector<Id> locationIds = m_graph.getTokenById(id)->getLocationIds();
for (Id locationId: locationIds)
{
TokenLocation* location = m_locationCollection.findTokenLocationById(locationId);
if (location->getOtherTokenLocation())
{
ret.addTokenLocationAsPlainCopy(location);
ret.addTokenLocationAsPlainCopy(location->getOtherTokenLocation());
}
}
return ret;
}
TokenLocationFile Storage::getTokenLocationsForLinesInFile(
const std::string& fileName, unsigned int firstLineNumber, unsigned int lastLineNumber
) const
{
TokenLocationFile ret(fileName);
TokenLocationFile* locationFile = m_locationCollection.findTokenLocationFileByPath(fileName);
if (locationFile)
{
for (unsigned int i = firstLineNumber; i <= lastLineNumber; i++)
{
TokenLocationLine* locationLine = locationFile->findTokenLocationLineByNumber(i);
if (locationLine)
{
locationLine->forEachTokenLocation([&] (TokenLocation* tokenLocation) -> void {
if (tokenLocation->getOtherTokenLocation() &&
tokenLocation->isStartTokenLocation() &&
tokenLocation->getLineNumber() >= firstLineNumber &&
tokenLocation->getOtherTokenLocation()->isEndTokenLocation() &&
tokenLocation->getOtherTokenLocation()->getLineNumber() <= lastLineNumber)
{
ret.addTokenLocationAsPlainCopy(tokenLocation);
ret.addTokenLocationAsPlainCopy(tokenLocation->getOtherTokenLocation());
}
});
}
}
}
return ret;
}
Edge::AccessType Storage::convertAccessType(ParserClient::AccessType access) const
+10 -3
View File
@@ -10,7 +10,10 @@
#include "data/location/TokenLocationCollection.h"
#include "data/parser/ParserClient.h"
class Storage: public ParserClient, public GraphAccess, public LocationAccess
class Storage
: public ParserClient
, public GraphAccess
, public LocationAccess
{
public:
Storage();
@@ -42,8 +45,12 @@ public:
void logGraph() const;
void logLocations() const;
virtual Token* getToken(Id tokenId);
virtual TokenLocation* getTokenLocation(Id locationId);
virtual Token* getToken(Id tokenId) const;
virtual TokenLocationCollection getTokenLocationsForTokenId(Id locationId) const;
virtual TokenLocationFile getTokenLocationsForLinesInFile(
const std::string& fileName, unsigned int firstLineNumber, unsigned int lastLineNumber
) const;
private:
Edge::AccessType convertAccessType(ParserClient::AccessType access) const;
+1 -1
View File
@@ -10,7 +10,7 @@ class GraphAccess
public:
virtual ~GraphAccess();
virtual Token* getToken(Id tokenId) = 0;
virtual Token* getToken(Id tokenId) const = 0;
};
+8 -3
View File
@@ -1,16 +1,21 @@
#ifndef LOCATION_ACCESS_H
#define LOCATION_ACCESS_H
#include <string>
#include "utility/types.h"
class TokenLocation;
class TokenLocationCollection;
class TokenLocationFile;
class LocationAccess
{
public:
virtual ~LocationAccess();
virtual TokenLocation* getTokenLocation(Id locationId) = 0;
virtual TokenLocationCollection getTokenLocationsForTokenId(Id id) const = 0;
virtual TokenLocationFile getTokenLocationsForLinesInFile(
const std::string& fileName, unsigned int firstLineNumber, unsigned int lastLineNumber
) const = 0;
};
@@ -103,6 +103,22 @@ void TokenLocationCollection::forEachTokenLocationFile(std::function<void(TokenL
}
}
void TokenLocationCollection::forEachTokenLocationLine(std::function<void(TokenLocationLine*)> func) const
{
for (const TokenLocationFilePairType& file : m_files)
{
file.second->forEachTokenLocationLine(func);
}
}
void TokenLocationCollection::forEachTokenLocation(std::function<void(TokenLocation*)> func) const
{
for (const TokenLocationFilePairType& file : m_files)
{
file.second->forEachTokenLocation(func);
}
}
TokenLocation* TokenLocationCollection::addTokenLocationAsPlainCopy(const TokenLocation* location)
{
const std::string& filePath = location->getTokenLocationLine()->getTokenLocationFile()->getFilePath();
@@ -11,6 +11,7 @@
class TokenLocation;
class TokenLocationFile;
class TokenLocationLine;
class TokenLocationCollection
{
@@ -37,6 +38,8 @@ public:
TokenLocationFile* findTokenLocationFileByPath(const std::string& filePath) const;
void forEachTokenLocationFile(std::function<void(TokenLocationFile*)> func) const;
void forEachTokenLocationLine(std::function<void(TokenLocationLine*)> func) const;
void forEachTokenLocation(std::function<void(TokenLocation*)> func) const;
TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location);
@@ -66,6 +66,11 @@ void TokenLocationFile::removeTokenLocation(TokenLocation* location)
}
}
TokenLocationLine* TokenLocationFile::findTokenLocationLineByNumber(unsigned int lineNumber) const
{
return findTokenLocationLine(lineNumber);
}
void TokenLocationFile::forEachTokenLocationLine(std::function<void(TokenLocationLine*)> func) const
{
for (const TokenLocationLinePairType& line : m_lines)
@@ -74,6 +79,14 @@ void TokenLocationFile::forEachTokenLocationLine(std::function<void(TokenLocatio
}
}
void TokenLocationFile::forEachTokenLocation(std::function<void(TokenLocation*)> func) const
{
for (const TokenLocationLinePairType& line : m_lines)
{
line.second->forEachTokenLocation(func);
}
}
TokenLocation* TokenLocationFile::addTokenLocationAsPlainCopy(const TokenLocation* location)
{
unsigned int lineNumber = location->getTokenLocationLine()->getLineNumber();
@@ -32,7 +32,10 @@ public:
unsigned int endLineNumber, unsigned int endColumnNumber);
void removeTokenLocation(TokenLocation* location);
TokenLocationLine* findTokenLocationLineByNumber(unsigned int lineNumber) const;
void forEachTokenLocationLine(std::function<void(TokenLocationLine*)> func) const;
void forEachTokenLocation(std::function<void(TokenLocation*)> func) const;
TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location);
@@ -0,0 +1,24 @@
#ifndef MESSAGE_ACTIVATE_TOKEN_H
#define MESSAGE_ACTIVATE_TOKEN_H
#include "utility/messaging/Message.h"
#include "utility/types.h"
class MessageActivateToken: public Message<MessageActivateToken>
{
public:
MessageActivateToken(Id tokenId)
: tokenId(tokenId)
{
}
static const std::string getStaticType()
{
return "MessageActivateToken";
}
public:
const Id tokenId;
};
#endif // MESSAGE_ACTIVATE_TOKEN_LOCATION_H
@@ -1,15 +0,0 @@
#ifndef MESSAGE_ACTIVATE_TOKEN_LOCATION_H
#define MESSAGE_ACTIVATE_TOKEN_LOCATION_H
#include "utility/messaging/Message.h"
class MessageActivateTokenLocation: public Message<MessageActivateTokenLocation>
{
public:
static const std::string getStaticType()
{
return "MessageActivateTokenLocation";
}
};
#endif // MESSAGE_ACTIVATE_TOKEN_LOCATION_H