diff --git a/bin/app/data/src/main.cpp b/bin/app/data/src/main.cpp index 04f24455..20d59a79 100644 --- a/bin/app/data/src/main.cpp +++ b/bin/app/data/src/main.cpp @@ -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"; } \ No newline at end of file diff --git a/src/app/qt/utility/QtThreadedFunctor.h b/src/app/qt/utility/QtThreadedFunctor.h index 0fe5ec23..7b275a97 100644 --- a/src/app/qt/utility/QtThreadedFunctor.h +++ b/src/app/qt/utility/QtThreadedFunctor.h @@ -3,6 +3,7 @@ #include #include +#include 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 callback) { + m_freeCallbacks.acquire(); m_callback = callback; signalExecution(); } private: std::function m_callback; + QSemaphore m_freeCallbacks; }; template diff --git a/src/app/qt/view/QtCodeView.cpp b/src/app/qt/view/QtCodeView.cpp index 55041f57..1c6e2f70 100644 --- a/src/app/qt/view/QtCodeView.cpp +++ b/src/app/qt/view/QtCodeView.cpp @@ -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) diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index adf2cdde..e5cf7798 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -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 diff --git a/src/lib/Project.cpp b/src/lib/Project.cpp index aebad895..03f96826 100644 --- a/src/lib/Project.cpp +++ b/src/lib/Project.cpp @@ -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::create( @@ -46,7 +46,7 @@ void Project::parseCode() m_storage->logGraph(); m_storage->logLocations(); - MessageActivateTokenLocation message; + MessageActivateToken message(1); message.dispatch(); } } diff --git a/src/lib/component/controller/CodeController.cpp b/src/lib/component/controller/CodeController.cpp index ab983f16..22053c64 100644 --- a/src/lib/component/controller/CodeController.cpp +++ b/src/lib/component/controller/CodeController.cpp @@ -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::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::createFromFile(filePath); + + unsigned int firstLineNumber = std::max(1, tokenLocation->getLineNumber() - lineRadius); + unsigned int lastLineNumber = std::min( + 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() diff --git a/src/lib/component/controller/CodeController.h b/src/lib/component/controller/CodeController.h index 7c6040c9..8c0b40d9 100644 --- a/src/lib/component/controller/CodeController.h +++ b/src/lib/component/controller/CodeController.h @@ -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 +class CodeController: public Controller, public MessageListener { public: CodeController(std::shared_ptr 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 m_locationAccess; diff --git a/src/lib/data/Storage.cpp b/src/lib/data/Storage.cpp index 6af6bd8f..53040772 100644 --- a/src/lib/data/Storage.cpp +++ b/src/lib/data/Storage.cpp @@ -3,6 +3,8 @@ #include #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 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 diff --git a/src/lib/data/Storage.h b/src/lib/data/Storage.h index 6cb61c84..65533277 100644 --- a/src/lib/data/Storage.h +++ b/src/lib/data/Storage.h @@ -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; diff --git a/src/lib/data/access/GraphAccess.h b/src/lib/data/access/GraphAccess.h index dea85aae..6c3d35bc 100644 --- a/src/lib/data/access/GraphAccess.h +++ b/src/lib/data/access/GraphAccess.h @@ -10,7 +10,7 @@ class GraphAccess public: virtual ~GraphAccess(); - virtual Token* getToken(Id tokenId) = 0; + virtual Token* getToken(Id tokenId) const = 0; }; diff --git a/src/lib/data/access/LocationAccess.h b/src/lib/data/access/LocationAccess.h index 91c316cc..c7f2f736 100644 --- a/src/lib/data/access/LocationAccess.h +++ b/src/lib/data/access/LocationAccess.h @@ -1,16 +1,21 @@ #ifndef LOCATION_ACCESS_H #define LOCATION_ACCESS_H +#include + #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; }; diff --git a/src/lib/data/location/TokenLocationCollection.cpp b/src/lib/data/location/TokenLocationCollection.cpp index fea44849..a83c63b3 100644 --- a/src/lib/data/location/TokenLocationCollection.cpp +++ b/src/lib/data/location/TokenLocationCollection.cpp @@ -103,6 +103,22 @@ void TokenLocationCollection::forEachTokenLocationFile(std::function func) const +{ + for (const TokenLocationFilePairType& file : m_files) + { + file.second->forEachTokenLocationLine(func); + } +} + +void TokenLocationCollection::forEachTokenLocation(std::function 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(); diff --git a/src/lib/data/location/TokenLocationCollection.h b/src/lib/data/location/TokenLocationCollection.h index 84211388..d498b150 100644 --- a/src/lib/data/location/TokenLocationCollection.h +++ b/src/lib/data/location/TokenLocationCollection.h @@ -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 func) const; + void forEachTokenLocationLine(std::function func) const; + void forEachTokenLocation(std::function func) const; TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location); diff --git a/src/lib/data/location/TokenLocationFile.cpp b/src/lib/data/location/TokenLocationFile.cpp index e8d98bc6..ebe24d23 100644 --- a/src/lib/data/location/TokenLocationFile.cpp +++ b/src/lib/data/location/TokenLocationFile.cpp @@ -66,6 +66,11 @@ void TokenLocationFile::removeTokenLocation(TokenLocation* location) } } +TokenLocationLine* TokenLocationFile::findTokenLocationLineByNumber(unsigned int lineNumber) const +{ + return findTokenLocationLine(lineNumber); +} + void TokenLocationFile::forEachTokenLocationLine(std::function func) const { for (const TokenLocationLinePairType& line : m_lines) @@ -74,6 +79,14 @@ void TokenLocationFile::forEachTokenLocationLine(std::function func) const +{ + for (const TokenLocationLinePairType& line : m_lines) + { + line.second->forEachTokenLocation(func); + } +} + TokenLocation* TokenLocationFile::addTokenLocationAsPlainCopy(const TokenLocation* location) { unsigned int lineNumber = location->getTokenLocationLine()->getLineNumber(); diff --git a/src/lib/data/location/TokenLocationFile.h b/src/lib/data/location/TokenLocationFile.h index dd7cb09b..5757483c 100644 --- a/src/lib/data/location/TokenLocationFile.h +++ b/src/lib/data/location/TokenLocationFile.h @@ -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 func) const; + void forEachTokenLocation(std::function func) const; TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location); diff --git a/src/lib/utility/messaging/type/MessageActivateToken.h b/src/lib/utility/messaging/type/MessageActivateToken.h new file mode 100644 index 00000000..1f4c9e98 --- /dev/null +++ b/src/lib/utility/messaging/type/MessageActivateToken.h @@ -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 +{ +public: + MessageActivateToken(Id tokenId) + : tokenId(tokenId) + { + } + + static const std::string getStaticType() + { + return "MessageActivateToken"; + } + +public: + const Id tokenId; +}; + +#endif // MESSAGE_ACTIVATE_TOKEN_LOCATION_H diff --git a/src/lib/utility/messaging/type/MessageActivateTokenLocation.h b/src/lib/utility/messaging/type/MessageActivateTokenLocation.h deleted file mode 100644 index 85dcaf86..00000000 --- a/src/lib/utility/messaging/type/MessageActivateTokenLocation.h +++ /dev/null @@ -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 -{ -public: - static const std::string getStaticType() - { - return "MessageActivateTokenLocation"; - } -}; - -#endif // MESSAGE_ACTIVATE_TOKEN_LOCATION_H