diff --git a/bin/test/data/log/test_log.txt b/bin/test/data/log/test_log.txt index 5ae4a7c7..5432087a 100644 --- a/bin/test/data/log/test_log.txt +++ b/bin/test/data/log/test_log.txt @@ -21,14 +21,14 @@ NetworkProtocolHelper.cpp ERROR: Failed to parse message, invalid type token: fo NetworkProtocolHelper.cpp ERROR: Failed to parse setActiveToken message, invalid token count Settings.cpp WARNING: File for Settings not found: data/SettingsTestSuite/wrong_settings.xml Settings.cpp WARNING: File for Settings not found: data/SettingsTestSuite/wrong_settings.xml +SourceLocationCollection.cpp ERROR: SourceLocation has wrong boundaries: file.c 2:3 2:1 +SourceLocationCollection.cpp ERROR: SourceLocation has wrong boundaries: file.c 4:1 1:10 TextAccess.cpp ERROR: Could not open file path/to/test.h TextAccess.cpp WARNING: Index 'firstLine' has to be lower or equal index 'lastLine', is 3 > 2 TextAccess.cpp WARNING: Tried to access index 10. Maximum index is 8 TextAccess.cpp WARNING: Tried to access index 10. Maximum index is 8 TextAccess.cpp WARNING: Line numbers start with one, is 0 TextAccess.cpp WARNING: Line numbers start with one, is 0 -TokenLocationCollection.cpp ERROR: TokenLocation has wrong boundaries: file.c 2:3 2:1 -TokenLocationCollection.cpp ERROR: TokenLocation has wrong boundaries: file.c 4:1 1:10 INFO: Indexer - indexing source file: input.cc INFO: Indexer - indexing source file: input.cc INFO: Indexer - indexing source file: input.cc diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 8c141fc8..86aa9af8 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -154,14 +154,12 @@ add_files( data/location/LocationType.cpp data/location/LocationType.h - data/location/TokenLocation.cpp - data/location/TokenLocation.h - data/location/TokenLocationCollection.cpp - data/location/TokenLocationCollection.h - data/location/TokenLocationFile.cpp - data/location/TokenLocationFile.h - data/location/TokenLocationLine.cpp - data/location/TokenLocationLine.h + data/location/SourceLocation.cpp + data/location/SourceLocation.h + data/location/SourceLocationCollection.cpp + data/location/SourceLocationCollection.h + data/location/SourceLocationFile.cpp + data/location/SourceLocationFile.h data/name/NameElement.cpp data/name/NameElement.h @@ -300,7 +298,7 @@ add_files( utility/messaging/type/MessageActivateFile.h utility/messaging/type/MessageActivateLocalSymbols.h utility/messaging/type/MessageActivateNodes.h - utility/messaging/type/MessageActivateTokenLocations.h + utility/messaging/type/MessageActivateSourceLocations.h utility/messaging/type/MessageActivateTokenIds.h utility/messaging/type/MessageActivateTokens.h utility/messaging/type/MessageActivateWindow.h diff --git a/src/lib/component/controller/ActivationController.cpp b/src/lib/component/controller/ActivationController.cpp index a2240d38..f114d348 100644 --- a/src/lib/component/controller/ActivationController.cpp +++ b/src/lib/component/controller/ActivationController.cpp @@ -141,7 +141,7 @@ void ActivationController::handleMessage(MessageActivateTokenIds* message) m.dispatchImmediately(); } -void ActivationController::handleMessage(MessageActivateTokenLocations* message) +void ActivationController::handleMessage(MessageActivateSourceLocations* message) { MessageActivateNodes m; for (Id nodeId : m_storageAccess->getNodeIdsForLocationIds(message->locationIds)) diff --git a/src/lib/component/controller/ActivationController.h b/src/lib/component/controller/ActivationController.h index 75235f2c..36af042f 100644 --- a/src/lib/component/controller/ActivationController.h +++ b/src/lib/component/controller/ActivationController.h @@ -10,7 +10,7 @@ #include "utility/messaging/type/MessageActivateFile.h" #include "utility/messaging/type/MessageActivateNodes.h" #include "utility/messaging/type/MessageActivateTokenIds.h" -#include "utility/messaging/type/MessageActivateTokenLocations.h" +#include "utility/messaging/type/MessageActivateSourceLocations.h" #include "utility/messaging/type/MessageResetZoom.h" #include "utility/messaging/type/MessageSearch.h" #include "utility/messaging/type/MessageZoom.h" @@ -23,7 +23,7 @@ class ActivationController , public MessageListener , public MessageListener , public MessageListener - , public MessageListener + , public MessageListener , public MessageListener , public MessageListener , public MessageListener @@ -40,7 +40,7 @@ private: virtual void handleMessage(MessageActivateNodes* message); virtual void handleMessage(MessageSearch* message); virtual void handleMessage(MessageActivateTokenIds* message); - virtual void handleMessage(MessageActivateTokenLocations* message); + virtual void handleMessage(MessageActivateSourceLocations* message); virtual void handleMessage(MessageResetZoom* message); virtual void handleMessage(MessageZoom* message); diff --git a/src/lib/component/controller/BookmarkController.cpp b/src/lib/component/controller/BookmarkController.cpp index e28daba5..16d5ee1a 100644 --- a/src/lib/component/controller/BookmarkController.cpp +++ b/src/lib/component/controller/BookmarkController.cpp @@ -5,10 +5,6 @@ #include "data/access/StorageAccess.h" #include "data/bookmark/Bookmark.h" -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationCollection.h" -#include "data/location/TokenLocationFile.h" - #include "utility/messaging/type/MessageActivateEdge.h" #include "utility/messaging/type/MessageActivateNodes.h" diff --git a/src/lib/component/controller/CodeController.cpp b/src/lib/component/controller/CodeController.cpp index 815c3fb7..9e8ecce2 100644 --- a/src/lib/component/controller/CodeController.cpp +++ b/src/lib/component/controller/CodeController.cpp @@ -9,10 +9,9 @@ #include "utility/utilityString.h" #include "data/access/StorageAccess.h" -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationCollection.h" -#include "data/location/TokenLocationFile.h" -#include "data/location/TokenLocationLine.h" +#include "data/location/SourceLocation.h" +#include "data/location/SourceLocationCollection.h" +#include "data/location/SourceLocationFile.h" #include "settings/ApplicationSettings.h" #include "Application.h" @@ -20,6 +19,7 @@ CodeController::CodeController(StorageAccess* storageAccess) : m_storageAccess(storageAccess) , m_scrollToDefinition(false) , m_scrollToValue(-1) + , m_scrollToLine(0) { } @@ -49,8 +49,7 @@ void CodeController::handleMessage(MessageActivateAll* message) statsSnippet.reduced = true; - statsSnippet.locationFile = std::make_shared(FilePath()); - statsSnippet.locationFile->isWholeCopy = true; + statsSnippet.locationFile = std::make_shared(FilePath(), true); std::vector description = getProjectDescription(statsSnippet.locationFile.get()); @@ -125,7 +124,7 @@ void CodeController::handleMessage(MessageActivateTokens* message) if (message->isEdge) { - std::shared_ptr collection = m_storageAccess->getTokenLocationsForTokenIds(activeTokenIds); + std::shared_ptr collection = m_storageAccess->getSourceLocationsForTokenIds(activeTokenIds); view->showActiveSnippet(activeTokenIds, collection, message->isLast()); } else if (message->keepContent()) @@ -139,16 +138,16 @@ void CodeController::handleMessage(MessageActivateTokens* message) } else { - m_collection = m_storageAccess->getTokenLocationsForTokenIds(activeTokenIds); + m_collection = m_storageAccess->getSourceLocationsForTokenIds(activeTokenIds); view->showCodeSnippets( - getSnippetsForActiveTokenLocations(m_collection.get(), declarationId), + getSnippetsForActiveSourceLocations(m_collection.get(), declarationId), activeTokenIds, !message->isReplayed() || message->isReplayCleared() ); m_scrollToDefinition = !message->isReplayed() || message->isReplayCleared(); - size_t fileCount = m_collection->getTokenLocationFileCount(); - size_t referenceCount = m_collection->getTokenLocationCount(); + size_t fileCount = m_collection->getSourceLocationFileCount(); + size_t referenceCount = m_collection->getSourceLocationCount(); std::stringstream ss; @@ -201,7 +200,7 @@ void CodeController::handleMessage(MessageChangeFileView* message) case MessageChangeFileView::FILE_SNIPPETS: if (message->needsData && inListMode) { - std::shared_ptr file = m_collection->getTokenLocationFileByPath(message->filePath); + std::shared_ptr file = m_collection->getSourceLocationFileByPath(message->filePath); if (!file) { return; @@ -209,7 +208,7 @@ void CodeController::handleMessage(MessageChangeFileView* message) if (message->showErrors) { - file->isWholeCopy = false; + file->setIsWhole(false); } view->addCodeSnippets(getSnippetsForFile(file, !message->showErrors), false); @@ -231,25 +230,25 @@ void CodeController::handleMessage(MessageChangeFileView* message) if (message->showErrors) { - params.locationFile = m_collection->getTokenLocationFileByPath(message->filePath); + params.locationFile = m_collection->getSourceLocationFileByPath(message->filePath); if (!params.locationFile) { return; } - params.locationFile->isWholeCopy = true; + params.locationFile->setIsWhole(true); } else { - std::shared_ptr file = - m_storageAccess->getTokenLocationsForFile(message->filePath.str()); + std::shared_ptr file = + m_storageAccess->getSourceLocationsForFile(message->filePath.str()); - TokenLocationFile* activeLocations = m_collection->findTokenLocationFileByPath(message->filePath); + SourceLocationFile* activeLocations = m_collection->getSourceLocationFileByPath(message->filePath).get(); if (activeLocations) { - activeLocations->forEachTokenLocation( - [&file](TokenLocation* location) + activeLocations->forEachSourceLocation( + [&file](SourceLocation* location) { - file->addTokenLocationAsPlainCopy(location); + file->addSourceLocationCopy(location); } ); } @@ -316,11 +315,18 @@ void CodeController::handleMessage(MessageCodeViewExpandedInitialFiles* message) getView()->scrollToValue(m_scrollToValue, m_scrollInListMode); m_scrollToValue = -1; } + + if (m_scrollToLine) + { + getView()->scrollToLine(m_scrollToFilePath, m_scrollToLine); + m_scrollToLine = 0; + } } void CodeController::handleMessage(MessageScrollToLine* message) { - getView()->scrollToLine(message->filePath, message->line); + m_scrollToFilePath = message->filePath; + m_scrollToLine = message->line; if (message->isModified) { @@ -355,7 +361,7 @@ void CodeController::handleMessage(MessageShowErrors* message) if (!view->showsErrors() || !message->errorId) { std::vector errors; - m_collection = m_storageAccess->getErrorTokenLocations(&errors); + m_collection = m_storageAccess->getErrorSourceLocations(&errors); std::vector snippets = getSnippetsForCollection(m_collection); view->clear(); @@ -388,18 +394,18 @@ void CodeController::handleMessage(MessageShowScope* message) { TRACE("code scope"); - std::shared_ptr collection = - m_storageAccess->getTokenLocationsForLocationIds(std::vector(1, message->scopeLocationId)); + std::shared_ptr collection = + m_storageAccess->getSourceLocationsForLocationIds({message->scopeLocationId}); - TokenLocation* location = collection->findTokenLocationById(message->scopeLocationId); - if (!location || !location->isScopeTokenLocation() || !location->getOtherTokenLocation()) + SourceLocation* location = collection->getSourceLocationById(message->scopeLocationId); + if (!location || !location->isScopeLocation() || !location->getOtherLocation()) { LOG_ERROR("MessageShowScope did not contain a valid scope location id"); return; } std::vector snippets = - getSnippetsForFile(collection->getTokenLocationFiles().begin()->second, true); + getSnippetsForFile(collection->getSourceLocationFiles().begin()->second, true); if (snippets.size() != 1) { LOG_ERROR("MessageShowScope didn't result in one single snippet to be created"); @@ -408,19 +414,19 @@ void CodeController::handleMessage(MessageShowScope* message) if (message->showErrors) { - snippets[0].locationFile = m_collection->getTokenLocationFileByPath(snippets[0].locationFile->getFilePath()); + snippets[0].locationFile = m_collection->getSourceLocationFileByPath(snippets[0].locationFile->getFilePath()); } else { - TokenLocationFile* activeLocations = - m_collection->findTokenLocationFileByPath(snippets[0].locationFile->getFilePath()); + SourceLocationFile* activeLocations = + m_collection->getSourceLocationFileByPath(snippets[0].locationFile->getFilePath()).get(); if (activeLocations) { - std::shared_ptr file = snippets[0].locationFile; - activeLocations->forEachTokenLocation( - [&file](TokenLocation* location) + std::shared_ptr file = snippets[0].locationFile; + activeLocations->forEachSourceLocation( + [&file](SourceLocation* location) { - file->addTokenLocationAsPlainCopy(location); + file->addSourceLocationCopy(location); } ); } @@ -451,27 +457,32 @@ void CodeController::showContents(MessageBase* message) } } -std::vector CodeController::getSnippetsForActiveTokenLocations( - const TokenLocationCollection* collection, Id declarationId +std::vector CodeController::getSnippetsForActiveSourceLocations( + const SourceLocationCollection* collection, Id declarationId ) const { TRACE(); std::vector snippets; - collection->forEachTokenLocationFile( - [&](std::shared_ptr file) -> void + collection->forEachSourceLocationFile( + [&](std::shared_ptr file) -> void { bool isDeclarationFile = false; bool isDefinitionFile = false; - file->forEachTokenLocation( - [&](TokenLocation* location) + file->forEachSourceLocation( + [&](SourceLocation* location) { - if (location->getTokenId() == declarationId) + for (Id i : location->getTokenIds()) { - isDeclarationFile = true; - - if (location->getType() == LOCATION_SCOPE) + if (i == declarationId) { - isDefinitionFile = true; + isDeclarationFile = true; + + if (location->getType() == LOCATION_SCOPE) + { + isDefinitionFile = true; + } + + break; } } } @@ -479,7 +490,7 @@ std::vector CodeController::getSnippetsForActiveTokenLocation CodeSnippetParams params; params.locationFile = file; - params.refCount = file->getUnscopedStartTokenLocationCount(); + params.refCount = file->getUnscopedStartLocationCount(); params.isDeclaration = isDeclarationFile; params.isDefinition = isDefinitionFile; @@ -497,17 +508,17 @@ std::vector CodeController::getSnippetsForActiveTokenLocation } std::vector CodeController::getSnippetsForCollection( - std::shared_ptr collection, bool addTokenLocations + std::shared_ptr collection, bool addSourceLocations ) const { std::vector snippets; - collection->forEachTokenLocationFile( - [&](std::shared_ptr file) -> void + collection->forEachSourceLocationFile( + [&](std::shared_ptr file) -> void { CodeSnippetParams params; params.locationFile = file; - params.refCount = file->getUnscopedStartTokenLocationCount(); + params.refCount = file->getUnscopedStartLocationCount(); params.isCollapsed = true; snippets.push_back(params); @@ -520,39 +531,19 @@ std::vector CodeController::getSnippetsForCollection( } std::vector CodeController::getSnippetsForFile( - std::shared_ptr activeTokenLocations, bool addTokenLocations + std::shared_ptr activeSourceLocations, bool addSourceLocations ) const { TRACE(); - std::shared_ptr textAccess = m_storageAccess->getFileContent(activeTokenLocations->getFilePath()); - std::shared_ptr scopeLocations - = std::make_shared(activeTokenLocations->getFilePath().str()); - - std::shared_ptr fileLocations = - m_storageAccess->getTokenLocationsForFile(activeTokenLocations->getFilePath().str()); - - fileLocations->forEachStartTokenLocation( - [&](TokenLocation* startLoc) -> void - { - if (startLoc->getType() == LOCATION_SCOPE) - { - TokenLocation* endLoc = startLoc->getOtherTokenLocation(); - TokenLocation* scopeLoc = scopeLocations->addTokenLocation( - startLoc->getId(), - startLoc->getTokenId(), - startLoc->getLineNumber(), - startLoc->getColumnNumber(), - endLoc->getLineNumber(), - endLoc->getColumnNumber()); - scopeLoc->setType(LOCATION_SCOPE); - } - } - ); + std::shared_ptr fileLocations = + m_storageAccess->getSourceLocationsForFile(activeSourceLocations->getFilePath()); + std::shared_ptr scopeLocations = fileLocations->getFilteredByType(LOCATION_SCOPE); + std::shared_ptr textAccess = m_storageAccess->getFileContent(activeSourceLocations->getFilePath()); std::deque ranges; - if (activeTokenLocations->isWholeCopy) + if (activeSourceLocations->isWhole()) { ranges.push_back(SnippetMerger::Range( SnippetMerger::Border(1, true), @@ -563,20 +554,20 @@ std::vector CodeController::getSnippetsForFile( { SnippetMerger fileScopedMerger(1, textAccess->getLineCount()); std::map> mergers; - activeTokenLocations->forEachStartTokenLocation( - [&](TokenLocation* location) + activeSourceLocations->forEachStartSourceLocation( + [&](SourceLocation* location) { buildMergerHierarchy(location, scopeLocations, fileScopedMerger, mergers); } ); std::vector atomicRanges; - m_storageAccess->getCommentLocationsInFile(activeTokenLocations->getFilePath())->forEachStartTokenLocation( - [&](TokenLocation* location) + m_storageAccess->getCommentLocationsInFile(activeSourceLocations->getFilePath())->forEachStartSourceLocation( + [&](SourceLocation* location) { atomicRanges.push_back(SnippetMerger::Range( SnippetMerger::Border(location->getLineNumber(), false), - SnippetMerger::Border(location->getOtherTokenLocation()->getLineNumber(), false) + SnippetMerger::Border(location->getOtherLocation()->getLineNumber(), false) )); } ); @@ -591,57 +582,55 @@ std::vector CodeController::getSnippetsForFile( for (const SnippetMerger::Range& range: ranges) { CodeSnippetParams params; - params.locationFile = activeTokenLocations; - params.refCount = activeTokenLocations->getUnscopedStartTokenLocationCount(); + params.refCount = activeSourceLocations->getUnscopedStartLocationCount(); + params.startLineNumber = std::max(1, range.start.row - (range.start.strong ? 0 : snippetExpandRange)); params.endLineNumber = std::min(textAccess->getLineCount(), range.end.row + (range.end.strong ? 0 : snippetExpandRange)); - std::shared_ptr tempFile = - fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber); - TokenLocationLine* firstUsedLine = nullptr; - for (size_t i = params.startLineNumber; i <= params.endLineNumber && firstUsedLine == nullptr; i++) - { - firstUsedLine = tempFile->findTokenLocationLineByNumber(i); - } - + params.locationFile = activeSourceLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber); params.titleId = 0; - if (firstUsedLine && firstUsedLine->getTokenLocations().size()) + params.footerId = 0; + + std::shared_ptr tempFile = + fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber); + + const SourceLocation* firstSourceLocation = + tempFile->getSourceLocations().size() ? tempFile->getSourceLocations().begin()->get() : nullptr; + + if (firstSourceLocation) { - getTokenLocationOfParentScope( - firstUsedLine->getTokenLocations().begin()->second.get(), - scopeLocations - )->forEachStartTokenLocation( // this TokenLocationFile only contains a single StartTokenLocation. - [&](TokenLocation* location) + // this SourceLocationFile only contains a single StartSourceLocation. + getSourceLocationOfParentScope(firstSourceLocation, scopeLocations)->forEachStartSourceLocation( + [&](SourceLocation* location) { - params.title = m_storageAccess->getNameHierarchyForNodeId(location->getTokenId()).getQualifiedName(); - params.titleId = location->getId(); + if (location->getTokenIds().size()) + { + params.title = m_storageAccess->getNameHierarchyForNodeId(location->getTokenIds()[0]).getQualifiedName(); + params.titleId = location->getLocationId(); + } } ); } - if (!activeTokenLocations->isWholeCopy && params.titleId == 0) + if (!activeSourceLocations->isWhole() && params.titleId == 0) { - params.title = activeTokenLocations->getFilePath().str(); + params.title = activeSourceLocations->getFilePath().str(); } - TokenLocationLine* lastUsedLine = nullptr; - for (size_t i = params.endLineNumber; i >= params.startLineNumber && lastUsedLine == nullptr; i--) + const SourceLocation* lastSourceLocation = + tempFile->getSourceLocations().size() ? tempFile->getSourceLocations().rbegin()->get() : nullptr; + if (lastSourceLocation) { - lastUsedLine = tempFile->findTokenLocationLineByNumber(i); - } - - params.footerId = 0; - if (lastUsedLine && lastUsedLine->getTokenLocations().size()) - { - getTokenLocationOfParentScope( - lastUsedLine->getTokenLocations().begin()->second.get(), - scopeLocations - )->forEachStartTokenLocation( // this TokenLocationFile only contains a single StartTokenLocation. - [&](TokenLocation* location) + // this SourceLocationFile only contains a single StartSourceLocation. + getSourceLocationOfParentScope(lastSourceLocation, scopeLocations)->forEachStartSourceLocation( + [&](SourceLocation* location) { - params.footer = m_storageAccess->getNameHierarchyForNodeId(location->getTokenId()).getQualifiedName(); - params.footerId = location->getId(); + if (location->getTokenIds().size()) + { + params.footer = m_storageAccess->getNameHierarchyForNodeId(location->getTokenIds()[0]).getQualifiedName(); + params.footerId = location->getLocationId(); + } } ); } @@ -654,17 +643,17 @@ std::vector CodeController::getSnippetsForFile( snippets.push_back(params); } - if (addTokenLocations && !activeTokenLocations->isWholeCopy) + if (addSourceLocations && !activeSourceLocations->isWhole()) { for (CodeSnippetParams& params : snippets) { - std::shared_ptr lines = + std::shared_ptr lines = fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber); - params.locationFile->forEachTokenLocation( - [&lines](TokenLocation* location) + params.locationFile->forEachSourceLocation( + [&lines](SourceLocation* location) { - lines->addTokenLocationAsPlainCopy(location); + lines->addSourceLocationCopy(location); } ); @@ -676,34 +665,33 @@ std::vector CodeController::getSnippetsForFile( } std::shared_ptr CodeController::buildMergerHierarchy( - TokenLocation* location, - std::shared_ptr scopeLocations, + SourceLocation* location, + std::shared_ptr scopeLocations, SnippetMerger& fileScopedMerger, std::map>& mergers ) const { - const TokenLocation* currentLocation = location; std::shared_ptr currentMerger = std::make_shared( - currentLocation->getStartTokenLocation()->getLineNumber(), - currentLocation->getEndTokenLocation()->getLineNumber() + location->getStartLocation()->getLineNumber(), + location->getEndLocation()->getLineNumber() ); - std::shared_ptr locationFile = getTokenLocationOfParentScope(currentLocation, scopeLocations); - if (locationFile->getTokenLocationLineCount() == 0) + std::shared_ptr locationFile = getSourceLocationOfParentScope(location, scopeLocations); + if (locationFile->getSourceLocationCount() == 0) { fileScopedMerger.addChild(currentMerger); return currentMerger; } std::shared_ptr nextMerger; - locationFile->forEachStartTokenLocation( // contains just 1 start location - [&](TokenLocation* scopeLocation) + locationFile->forEachStartSourceLocation( // contains just 1 start location + [&](SourceLocation* scopeLocation) { - std::map>::iterator it = mergers.find(scopeLocation->getId()); + std::map>::iterator it = mergers.find(scopeLocation->getLocationId()); if (it == mergers.end()) { nextMerger = buildMergerHierarchy(scopeLocation, scopeLocations, fileScopedMerger, mergers); - mergers[scopeLocation->getId()] = nextMerger; + mergers[scopeLocation->getLocationId()] = nextMerger; } else { @@ -715,44 +703,43 @@ std::shared_ptr CodeController::buildMergerHierarchy( return currentMerger; } -std::shared_ptr CodeController::getTokenLocationOfParentScope( - const TokenLocation* location, - std::shared_ptr scopeLocations +std::shared_ptr CodeController::getSourceLocationOfParentScope( + const SourceLocation* location, + std::shared_ptr scopeLocations ) const { - const TokenLocation* parent = location; - const FilePath filePath = location->getFilePath(); + const SourceLocation* parent = nullptr; - scopeLocations->forEachStartTokenLocation( - [&](TokenLocation* tokenLocation) -> void + scopeLocations->forEachStartSourceLocation( + [&](SourceLocation* scopeLocation) -> void { - if ((*tokenLocation) < *(location->getStartTokenLocation()) && - (*tokenLocation->getEndTokenLocation()) > *(location->getEndTokenLocation())) + if (location->getStartLocation() && *scopeLocation == *location->getStartLocation() && + location->getEndLocation() && *scopeLocation->getEndLocation() == *location->getEndLocation()) { - if (parent == location) - { - parent = tokenLocation; - } - // since tokenLocation is a start location the > location indicates the scope + return; + } + + if (!(*scopeLocation > *location) && + !(*scopeLocation->getEndLocation() < *location) && + // since scopeLocation is a start location the > location indicates the scope // that is closer to the child. - else if ((*tokenLocation) > *parent) - { - parent = tokenLocation; - } + (!parent || *scopeLocation > *parent)) + { + parent = scopeLocation; } } ); - std::shared_ptr file = std::make_shared(filePath); - if (parent != location) + std::shared_ptr file = std::make_shared(location->getFilePath(), false); + if (parent) { - file->addTokenLocationAsPlainCopy(parent); - file->addTokenLocationAsPlainCopy(parent->getOtherTokenLocation()); + file->addSourceLocationCopy(parent); + file->addSourceLocationCopy(parent->getOtherLocation()); } return file; } -std::vector CodeController::getProjectDescription(TokenLocationFile* locationFile) const +std::vector CodeController::getProjectDescription(SourceLocationFile* locationFile) const { Project* currentProject = Application::getInstance()->getCurrentProject().get(); if (!currentProject) @@ -797,8 +784,8 @@ std::vector CodeController::getProjectDescription(TokenLocationFile if (tokenId > 0) { line.replace(posA, posB - posA + 1, nameString); - locationFile->addTokenLocation( - 0, tokenId, + locationFile->addSourceLocation( + LOCATION_TOKEN, 0, {tokenId}, startLineNumber + i, posA + 1, startLineNumber + i, posA + nameString.size() ); diff --git a/src/lib/component/controller/CodeController.h b/src/lib/component/controller/CodeController.h index 2dd5a530..fa5d4178 100644 --- a/src/lib/component/controller/CodeController.h +++ b/src/lib/component/controller/CodeController.h @@ -27,9 +27,9 @@ #include "component/view/CodeView.h" class StorageAccess; -class TokenLocation; -class TokenLocationCollection; -class TokenLocationFile; +class SourceLocation; +class SourceLocationCollection; +class SourceLocationFile; class CodeController : public Controller @@ -78,29 +78,31 @@ private: void showContents(MessageBase* message); - std::vector getSnippetsForActiveTokenLocations( - const TokenLocationCollection* collection, Id declarationId) const; + std::vector getSnippetsForActiveSourceLocations( + const SourceLocationCollection* collection, Id declarationId) const; std::vector getSnippetsForCollection( - std::shared_ptr collection, bool addTokenLocations = false) const; + std::shared_ptr collection, bool addSourceLocations = false) const; std::vector getSnippetsForFile( - std::shared_ptr file, bool addTokenLocations = false) const; + std::shared_ptr file, bool addSourceLocations = false) const; std::shared_ptr buildMergerHierarchy( - TokenLocation* location, std::shared_ptr context, SnippetMerger& fileScopedMerger, + SourceLocation* location, std::shared_ptr context, SnippetMerger& fileScopedMerger, std::map>& mergers) const; - std::shared_ptr getTokenLocationOfParentScope( - const TokenLocation* location, std::shared_ptr context) const; + std::shared_ptr getSourceLocationOfParentScope( + const SourceLocation* location, std::shared_ptr context) const; - std::vector getProjectDescription(TokenLocationFile* locationFile) const; + std::vector getProjectDescription(SourceLocationFile* locationFile) const; void addModificationTimes(std::vector& snippets) const; StorageAccess* m_storageAccess; - mutable std::shared_ptr m_collection; + mutable std::shared_ptr m_collection; bool m_scrollToDefinition; int m_scrollToValue; bool m_scrollInListMode; + FilePath m_scrollToFilePath; + size_t m_scrollToLine; }; #endif // CODE_CONTROLLER_H diff --git a/src/lib/component/controller/IDECommunicationController.cpp b/src/lib/component/controller/IDECommunicationController.cpp index 7fa4636c..4b0555a7 100644 --- a/src/lib/component/controller/IDECommunicationController.cpp +++ b/src/lib/component/controller/IDECommunicationController.cpp @@ -1,12 +1,7 @@ #include "IDECommunicationController.h" -#include "data/access/StorageAccess.h" -#include "data/location/TokenLocationFile.h" -#include "data/location/TokenLocation.h" -#include "settings/ApplicationSettings.h" - #include "utility/file/FileSystem.h" -#include "utility/messaging/type/MessageActivateTokenLocations.h" +#include "utility/messaging/type/MessageActivateSourceLocations.h" #include "utility/messaging/type/MessageActivateWindow.h" #include "utility/messaging/type/MessageDispatchWhenLicenseValid.h" #include "utility/messaging/type/MessageProjectNew.h" @@ -14,6 +9,10 @@ #include "utility/messaging/type/MessageActivateFile.h" #include "utility/messaging/type/MessagePingReceived.h" +#include "data/access/StorageAccess.h" +#include "data/location/SourceLocationFile.h" +#include "settings/ApplicationSettings.h" + IDECommunicationController::IDECommunicationController(StorageAccess* storageAccess) : m_storageAccess(storageAccess) , m_enabled(true) @@ -86,21 +85,22 @@ void IDECommunicationController::handleSetActiveTokenMessage( == m_storageAccess->getFileInfoForFilePath(message.fileLocation).lastWriteTime) { // file was not modified - std::shared_ptr tokenLocationFile = m_storageAccess->getTokenLocationsForLinesInFile( + std::shared_ptr sourceLocationFile = m_storageAccess->getSourceLocationsForLinesInFile( message.fileLocation, message.row, message.row ); std::vector selectedLocationIds; - tokenLocationFile->forEachStartTokenLocation( - [&](TokenLocation* startLocation) + sourceLocationFile->forEachStartSourceLocation( + [&selectedLocationIds, &cursorColumn](SourceLocation* startLocation) { - TokenLocation* endLocation = startLocation->getEndTokenLocation(); + const SourceLocation* endLocation = startLocation->getEndLocation(); - if (!startLocation->isScopeTokenLocation() + if (!startLocation->isScopeLocation() + && startLocation->getLineNumber() == endLocation->getLineNumber() && startLocation->getColumnNumber() <= cursorColumn && endLocation->getColumnNumber() + 1 >= cursorColumn) { - selectedLocationIds.push_back(startLocation->getId()); + selectedLocationIds.push_back(startLocation->getLocationId()); } } ); @@ -110,7 +110,7 @@ void IDECommunicationController::handleSetActiveTokenMessage( MessageStatus("Activating source location from plug-in succeeded: " + message.fileLocation + ", row: " + std::to_string(message.row) + ", col: " + std::to_string(message.column)).dispatch(); - MessageActivateTokenLocations(selectedLocationIds).dispatch(); + MessageActivateSourceLocations(selectedLocationIds).dispatch(); MessageActivateWindow().dispatch(); return; } diff --git a/src/lib/component/view/CodeView.h b/src/lib/component/view/CodeView.h index f884ee2a..bba32597 100644 --- a/src/lib/component/view/CodeView.h +++ b/src/lib/component/view/CodeView.h @@ -10,7 +10,7 @@ #include "component/view/View.h" class CodeController; -class TokenLocationCollection; +class SourceLocationCollection; class CodeView : public View @@ -41,7 +41,7 @@ public: virtual void setFileState(const FilePath filePath, FileState state) = 0; virtual void showActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) = 0; + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) = 0; virtual void showActiveTokenIds(const std::vector& activeTokenIds) = 0; virtual void showActiveLocalSymbolIds(const std::vector& activeLocalSymbolIds) = 0; diff --git a/src/lib/component/view/helper/CodeSnippetParams.cpp b/src/lib/component/view/helper/CodeSnippetParams.cpp index f556e301..b08e9e18 100644 --- a/src/lib/component/view/helper/CodeSnippetParams.cpp +++ b/src/lib/component/view/helper/CodeSnippetParams.cpp @@ -1,6 +1,6 @@ #include "component/view/helper/CodeSnippetParams.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocationFile.h" CodeSnippetParams::CodeSnippetParams() : startLineNumber(0) @@ -39,11 +39,11 @@ bool CodeSnippetParams::sort(const CodeSnippetParams& a, const CodeSnippetParams } // sort whole files - if (a.locationFile->isWholeCopy && !b.locationFile->isWholeCopy) + if (a.locationFile->isWhole() && !b.locationFile->isWhole()) { return true; } - else if (!a.locationFile->isWholeCopy && b.locationFile->isWholeCopy) + else if (!a.locationFile->isWhole() && b.locationFile->isWhole()) { return false; } diff --git a/src/lib/component/view/helper/CodeSnippetParams.h b/src/lib/component/view/helper/CodeSnippetParams.h index 8f42d9c8..2ccc4ff4 100644 --- a/src/lib/component/view/helper/CodeSnippetParams.h +++ b/src/lib/component/view/helper/CodeSnippetParams.h @@ -6,7 +6,7 @@ #include "utility/TimePoint.h" #include "utility/types.h" -class TokenLocationFile; +class SourceLocationFile; struct CodeSnippetParams { @@ -26,7 +26,7 @@ struct CodeSnippetParams Id footerId; TimePoint modificationTime; - std::shared_ptr locationFile; + std::shared_ptr locationFile; int refCount; diff --git a/src/lib/data/PersistentStorage.cpp b/src/lib/data/PersistentStorage.cpp index 75364a44..2d4a2bc4 100644 --- a/src/lib/data/PersistentStorage.cpp +++ b/src/lib/data/PersistentStorage.cpp @@ -20,9 +20,8 @@ #include "data/graph/token_component/TokenComponentFilePath.h" #include "data/graph/token_component/TokenComponentSignature.h" #include "data/graph/Graph.h" -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationFile.h" -#include "data/location/TokenLocationLine.h" +#include "data/location/SourceLocationCollection.h" +#include "data/location/SourceLocationFile.h" #include "data/parser/ParseLocation.h" PersistentStorage::PersistentStorage(const FilePath& dbPath) @@ -530,13 +529,13 @@ bool PersistentStorage::checkEdgeExists(Id edgeId) const return m_sqliteStorage.checkEdgeExists(edgeId); } -std::shared_ptr PersistentStorage::getFullTextSearchLocations( +std::shared_ptr PersistentStorage::getFullTextSearchLocations( const std::string& searchTerm, bool caseSensitive ) const { TRACE(); - std::shared_ptr collection = std::make_shared(); + std::shared_ptr collection = std::make_shared(); if (!searchTerm.size()) { return collection; @@ -554,20 +553,17 @@ std::shared_ptr PersistentStorage::getFullTextSearchLoc ).dispatch(); std::vector hits = m_fullTextSearchIndex.searchForTerm(searchTerm); - int termLength = searchTerm.length(); - FilePath filepath; - std::shared_ptr file; - ParseLocation location; + for (size_t i = 0; i < hits.size(); i++) { - filepath = getFileNodePath(hits[i].fileId); - file = getFileContent(filepath); + FilePath filePath = getFileNodePath(hits[i].fileId); + std::shared_ptr fileContent = getFileContent(filePath); int charsInPreviousLines = 0; int lineNumber = 1; std::string line; - line = file->getLine(lineNumber); + line = fileContent->getLine(lineNumber); for (int pos : hits[i].positions) { @@ -576,8 +572,10 @@ std::shared_ptr PersistentStorage::getFullTextSearchLoc { lineNumber++; charsInPreviousLines += line.length(); - line = file->getLine(lineNumber); + line = fileContent->getLine(lineNumber); } + + ParseLocation location; location.startLineNumber = lineNumber; location.startColumnNumber = pos - charsInPreviousLines + 1; @@ -593,7 +591,7 @@ std::shared_ptr PersistentStorage::getFullTextSearchLoc { lineNumber++; charsInPreviousLines += line.length(); - line = file->getLine(lineNumber); + line = fileContent->getLine(lineNumber); } location.endLineNumber = lineNumber; @@ -602,24 +600,25 @@ std::shared_ptr PersistentStorage::getFullTextSearchLoc if ( addHit ) { // Set first bit to 1 to avoid collisions - Id locationId = ~(~size_t(0) >> 1) + collection->getTokenLocationCount(); + Id locationId = ~(~size_t(0) >> 1) + collection->getSourceLocationCount(); - collection->addTokenLocation( + collection->addSourceLocation( + LOCATION_FULLTEXT, locationId, - 0, - filepath, + std::vector(), + filePath, location.startLineNumber, location.startColumnNumber, location.endLineNumber, location.endColumnNumber - )->setType(LOCATION_FULLTEXT); + ); } } } MessageStatus( - std::to_string(collection->getTokenLocationCount()) + " results in " + - std::to_string(collection->getTokenLocationFileCount()) + " files for fulltext search (case-" + + std::to_string(collection->getSourceLocationCount()) + " results in " + + std::to_string(collection->getSourceLocationFileCount()) + " files for fulltext search (case-" + (caseSensitive ? "sensitive" : "insensitive") + "): " + searchTerm, false, false ).dispatch(); @@ -1085,33 +1084,34 @@ std::vector PersistentStorage::getNodeIdsForLocationIds(const std::vector PersistentStorage::getTokenLocationsForTokenIds( +std::shared_ptr PersistentStorage::getSourceLocationsForTokenIds( const std::vector& tokenIds) const { TRACE(); - std::shared_ptr collection = std::make_shared(); - std::vector fileIds; std::vector nonFileIds; for (const Id tokenId : tokenIds) { - if (!getFileNodePath(tokenId).empty()) - { - fileIds.push_back(tokenId); - } - else + if (getFileNodePath(tokenId).empty()) { nonFileIds.push_back(tokenId); } + else + { + fileIds.push_back(tokenId); + } } - for (StorageFile file: m_sqliteStorage.getAllByIds(fileIds)) + std::shared_ptr collection = std::make_shared(); + + for (const StorageFile& file : m_sqliteStorage.getAllByIds(fileIds)) { - collection->addTokenLocationFile(m_sqliteStorage.getTokenLocationsForFile(file.filePath)); + collection->addSourceLocationFile(m_sqliteStorage.getSourceLocationsForFile(file.filePath)); } + if (nonFileIds.size()) { std::vector locationIds; std::unordered_map locationIdToElementIdMap; @@ -1126,81 +1126,83 @@ std::shared_ptr PersistentStorage::getTokenLocationsFor auto it = locationIdToElementIdMap.find(sourceLocation.id); if (it != locationIdToElementIdMap.end()) { - TokenLocation* tokenLocation = collection->addTokenLocation( + collection->addSourceLocation( + intToLocationType(sourceLocation.type), sourceLocation.id, - it->second, + std::vector(1, it->second), getFileNodePath(sourceLocation.fileNodeId), sourceLocation.startLine, sourceLocation.startCol, sourceLocation.endLine, sourceLocation.endCol ); - - if (tokenLocation) - { - tokenLocation->setType(intToLocationType(sourceLocation.type)); - } } } } + return collection; } -std::shared_ptr PersistentStorage::getTokenLocationsForLocationIds( +std::shared_ptr PersistentStorage::getSourceLocationsForLocationIds( const std::vector& locationIds ) const { TRACE(); - std::shared_ptr collection = std::make_shared(); + std::shared_ptr collection = std::make_shared(); for (StorageSourceLocation location: m_sqliteStorage.getAllByIds(locationIds)) { - for (const StorageOccurrence& occurrences: m_sqliteStorage.getOccurrencesForLocationId(location.id)) + std::vector elementIds; + for (const StorageOccurrence& occurrence: m_sqliteStorage.getOccurrencesForLocationId(location.id)) { - collection->addTokenLocation( - location.id, - occurrences.elementId, - getFileNodePath(location.fileNodeId), - location.startLine, - location.startCol, - location.endLine, - location.endCol - )->setType(intToLocationType(location.type)); + elementIds.push_back(occurrence.elementId); } + + collection->addSourceLocation( + intToLocationType(location.type), + location.id, + elementIds, + getFileNodePath(location.fileNodeId), + location.startLine, + location.startCol, + location.endLine, + location.endCol + ); } return collection; } -std::shared_ptr PersistentStorage::getTokenLocationsForFile(const std::string& filePath) const +std::shared_ptr PersistentStorage::getSourceLocationsForFile(const FilePath& filePath) const { TRACE(); - return m_sqliteStorage.getTokenLocationsForFile(filePath); + return m_sqliteStorage.getSourceLocationsForFile(filePath); } -std::shared_ptr PersistentStorage::getTokenLocationsForLinesInFile( +std::shared_ptr PersistentStorage::getSourceLocationsForLinesInFile( const std::string& filePath, uint firstLineNumber, uint lastLineNumber ) const { TRACE(); - return getTokenLocationsForFile(filePath)->getFilteredByLines(firstLineNumber, lastLineNumber); + return getSourceLocationsForFile(filePath)->getFilteredByLines(firstLineNumber, lastLineNumber); } -std::shared_ptr PersistentStorage::getCommentLocationsInFile(const FilePath& filePath) const +std::shared_ptr PersistentStorage::getCommentLocationsInFile(const FilePath& filePath) const { TRACE(); - std::shared_ptr file = std::make_shared(filePath); + std::shared_ptr file = std::make_shared(filePath, false); std::vector storageLocations = m_sqliteStorage.getCommentLocationsInFile(filePath); for (size_t i = 0; i < storageLocations.size(); i++) { - file->addTokenLocation( + file->addSourceLocation( + LOCATION_TOKEN, storageLocations[i].id, - 0, // comment token location has no element. + std::vector(), // comment token location has no element. storageLocations[i].startLine, storageLocations[i].startCol, storageLocations[i].endLine, @@ -1283,11 +1285,11 @@ std::vector PersistentStorage::getErrors() const return filteredErrors; } -std::shared_ptr PersistentStorage::getErrorTokenLocations(std::vector* errors) const +std::shared_ptr PersistentStorage::getErrorSourceLocations(std::vector* errors) const { TRACE(); - std::shared_ptr errorCollection = std::make_shared(); + std::shared_ptr errorCollection = std::make_shared(); for (const ErrorInfo& error : m_sqliteStorage.getAll()) { if (m_errorFilter.filter(error)) @@ -1297,9 +1299,16 @@ std::shared_ptr PersistentStorage::getErrorTokenLocatio // Set first bit to 1 to avoid collisions Id locationId = ~(~size_t(0) >> 1) + error.id; - errorCollection->addTokenLocation( - locationId, error.id, error.filePath, error.lineNumber, error.columnNumber, error.lineNumber, error.columnNumber - )->setType(LOCATION_ERROR); + errorCollection->addSourceLocation( + LOCATION_ERROR, + locationId, + std::vector(1, error.id), + error.filePath, + error.lineNumber, + error.columnNumber, + error.lineNumber, + error.columnNumber + ); } } diff --git a/src/lib/data/PersistentStorage.h b/src/lib/data/PersistentStorage.h index 0bac34ac..c5465641 100644 --- a/src/lib/data/PersistentStorage.h +++ b/src/lib/data/PersistentStorage.h @@ -9,7 +9,6 @@ #include "data/access/StorageAccess.h" #include "data/fulltextsearch/FullTextSearchIndex.h" #include "data/graph/token_component/TokenComponentAccess.h" -#include "data/location/TokenLocationCollection.h" #include "data/parser/ParserClient.h" #include "data/parser/ParseLocation.h" #include "data/search/SearchIndex.h" @@ -111,8 +110,9 @@ public: virtual StorageEdge getEdgeById(Id edgeId) const; virtual bool checkEdgeExists(Id edgeId) const; - virtual std::shared_ptr getFullTextSearchLocations( - const std::string& searchTerm, bool caseSensitive) const; + virtual std::shared_ptr getFullTextSearchLocations( + const std::string& searchTerm, bool caseSensitive) const; + virtual std::vector getAutocompletionMatches(const std::string& query) const; std::vector getAutocompletionSymbolMatches(const std::string& query, size_t maxResultsCount) const; std::vector getAutocompletionFileMatches(const std::string& query, size_t maxResultsCount) const; @@ -125,18 +125,15 @@ public: virtual std::vector getActiveTokenIdsForId(Id tokenId, Id* declarationId) const; virtual std::vector getNodeIdsForLocationIds(const std::vector& locationIds) const; - virtual std::shared_ptr getTokenLocationsForTokenIds( - const std::vector& tokenIds - ) const; - virtual std::shared_ptr getTokenLocationsForLocationIds( - const std::vector& locationIds - ) const; - virtual std::shared_ptr getTokenLocationsForFile(const std::string& filePath) const; - virtual std::shared_ptr getTokenLocationsForLinesInFile( + virtual std::shared_ptr getSourceLocationsForTokenIds(const std::vector& tokenIds) const; + virtual std::shared_ptr getSourceLocationsForLocationIds(const std::vector& locationIds) const; + + virtual std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const; + virtual std::shared_ptr getSourceLocationsForLinesInFile( const std::string& filePath, uint firstLineNumber, uint lastLineNumber ) const; - virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const; + virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const; virtual std::shared_ptr getFileContent(const FilePath& filePath) const; @@ -148,7 +145,7 @@ public: virtual ErrorCountInfo getErrorCount() const; virtual std::vector getErrors() const; - virtual std::shared_ptr getErrorTokenLocations(std::vector* errors) const; + virtual std::shared_ptr getErrorSourceLocations(std::vector* errors) const; private: Id getFileNodeId(const FilePath& filePath) const; diff --git a/src/lib/data/SqliteStorage.cpp b/src/lib/data/SqliteStorage.cpp index a0e38315..cfae03a3 100644 --- a/src/lib/data/SqliteStorage.cpp +++ b/src/lib/data/SqliteStorage.cpp @@ -3,7 +3,6 @@ #include #include "data/graph/Node.h" -#include "data/location/TokenLocation.h" #include "data/parser/ParseLocation.h" #include "utility/logging/logging.h" #include "utility/text/TextAccess.h" @@ -847,9 +846,9 @@ StorageSourceLocation SqliteStorage::getSourceLocationByAll(const Id fileNodeId, ); } -std::shared_ptr SqliteStorage::getTokenLocationsForFile(const FilePath& filePath) const +std::shared_ptr SqliteStorage::getSourceLocationsForFile(const FilePath& filePath) const { - std::shared_ptr ret = std::make_shared(filePath); + std::shared_ptr ret = std::make_shared(filePath, true); const Id fileNodeId = getFileByPath(filePath.str()).id; if (fileNodeId == 0) // early out @@ -865,25 +864,29 @@ std::shared_ptr SqliteStorage::getTokenLocationsForFile(const sourceLocationIdToData[storageLocation.id] = storageLocation; } + std::map> sourceLocationIdToElementIds; for (const StorageOccurrence& occurrence: getOccurrencesForLocationIds(sourceLocationIds)) { - auto it = sourceLocationIdToData.find(occurrence.sourceLocationId); + sourceLocationIdToElementIds[occurrence.sourceLocationId].push_back(occurrence.elementId); + } + + for (const std::pair>& p : sourceLocationIdToElementIds) + { + auto it = sourceLocationIdToData.find(p.first); if (it != sourceLocationIdToData.end()) { - TokenLocation* loc = ret->addTokenLocation( - it->second.id, //e.first.id, - occurrence.elementId, + ret->addSourceLocation( + intToLocationType(it->second.type), + it->second.id, + p.second, it->second.startLine, it->second.startCol, it->second.endLine, it->second.endCol ); - loc->setType(intToLocationType(it->second.type)); } } - ret->isWholeCopy = true; - return ret; } diff --git a/src/lib/data/SqliteStorage.h b/src/lib/data/SqliteStorage.h index f3c3f6dc..1acae5a8 100644 --- a/src/lib/data/SqliteStorage.h +++ b/src/lib/data/SqliteStorage.h @@ -10,8 +10,7 @@ #include "data/bookmark/BookmarkCategory.h" #include "data/bookmark/EdgeBookmark.h" #include "data/bookmark/NodeBookmark.h" -#include "data/location/TokenLocationFile.h" -#include "data/location/TokenLocationCollection.h" +#include "data/location/SourceLocationFile.h" #include "data/name/NameHierarchy.h" #include "data/StorageTypes.h" #include "data/SqliteIndex.h" @@ -115,7 +114,7 @@ public: void setNodeType(int type, Id nodeId); StorageSourceLocation getSourceLocationByAll(const Id fileNodeId, const uint startLine, const uint startCol, const uint endLine, const uint endCol, const int type) const; - std::shared_ptr getTokenLocationsForFile(const FilePath& filePath) const; + std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const; std::vector getOccurrencesForLocationId(Id locationId) const; std::vector getOccurrencesForLocationIds(const std::vector& locationIds) const; diff --git a/src/lib/data/access/StorageAccess.h b/src/lib/data/access/StorageAccess.h index 1d736f85..46503df4 100644 --- a/src/lib/data/access/StorageAccess.h +++ b/src/lib/data/access/StorageAccess.h @@ -21,10 +21,9 @@ #include "data/StorageStats.h" class Graph; +class SourceLocationCollection; +class SourceLocationFile; class TextAccess; -class TokenLocation; -class TokenLocationCollection; -class TokenLocationFile; class StorageAccess { @@ -46,7 +45,7 @@ public: virtual StorageEdge getEdgeById(Id edgeId) const = 0; virtual bool checkEdgeExists(Id edgeId) const = 0; - virtual std::shared_ptr getFullTextSearchLocations( + virtual std::shared_ptr getFullTextSearchLocations( const std::string& searchTerm, bool caseSensitive) const = 0; virtual std::vector getAutocompletionMatches(const std::string& query) const = 0; virtual std::vector getSearchMatchesForTokenIds(const std::vector& tokenIds) const = 0; @@ -57,15 +56,15 @@ public: virtual std::vector getActiveTokenIdsForId(Id tokenId, Id* declarationId) const = 0; virtual std::vector getNodeIdsForLocationIds(const std::vector& locationIds) const = 0; - virtual std::shared_ptr getTokenLocationsForTokenIds( - const std::vector& tokenIds) const = 0; - virtual std::shared_ptr getTokenLocationsForLocationIds( - const std::vector& locationIds) const = 0; - virtual std::shared_ptr getTokenLocationsForFile(const std::string& filePath) const = 0; - virtual std::shared_ptr getTokenLocationsForLinesInFile( + virtual std::shared_ptr getSourceLocationsForTokenIds( + const std::vector& tokenIds) const = 0; + virtual std::shared_ptr getSourceLocationsForLocationIds( + const std::vector& locationIds) const = 0; + virtual std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const = 0; + virtual std::shared_ptr getSourceLocationsForLinesInFile( const std::string& filePath, uint firstLineNumber, uint lastLineNumber) const = 0; - virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const = 0; + virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const = 0; virtual std::shared_ptr getFileContent(const FilePath& filePath) const = 0; @@ -77,7 +76,7 @@ public: virtual ErrorCountInfo getErrorCount() const = 0; virtual std::vector getErrors() const = 0; - virtual std::shared_ptr getErrorTokenLocations(std::vector* errors) const = 0; + virtual std::shared_ptr getErrorSourceLocations(std::vector* errors) const = 0; virtual void setErrorFilter(const ErrorFilter& filter); diff --git a/src/lib/data/access/StorageAccessProxy.cpp b/src/lib/data/access/StorageAccessProxy.cpp index 76586488..12bde9be 100644 --- a/src/lib/data/access/StorageAccessProxy.cpp +++ b/src/lib/data/access/StorageAccessProxy.cpp @@ -1,8 +1,8 @@ #include "data/access/StorageAccessProxy.h" #include "data/graph/Graph.h" -#include "data/location/TokenLocationCollection.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocationCollection.h" +#include "data/location/SourceLocationFile.h" #include "utility/logging/logging.h" #include "utility/file/FileInfo.h" @@ -133,7 +133,7 @@ bool StorageAccessProxy::checkEdgeExists(Id edgeId) const return false; } -std::shared_ptr StorageAccessProxy::getFullTextSearchLocations( +std::shared_ptr StorageAccessProxy::getFullTextSearchLocations( const std::string &searchTerm, bool caseSensitive) const { if (hasSubject()) @@ -141,7 +141,7 @@ std::shared_ptr StorageAccessProxy::getFullTextSearchLo return m_subject->getFullTextSearchLocations(searchTerm, caseSensitive); } - return std::make_shared(); + return std::make_shared(); } std::vector StorageAccessProxy::getAutocompletionMatches(const std::string& query) const @@ -204,58 +204,58 @@ std::vector StorageAccessProxy::getNodeIdsForLocationIds(const std::vector(); } -std::shared_ptr StorageAccessProxy::getTokenLocationsForTokenIds( +std::shared_ptr StorageAccessProxy::getSourceLocationsForTokenIds( const std::vector& tokenIds) const { if (hasSubject()) { - return m_subject->getTokenLocationsForTokenIds(tokenIds); + return m_subject->getSourceLocationsForTokenIds(tokenIds); } - return std::make_shared(); + return std::make_shared(); } -std::shared_ptr StorageAccessProxy::getTokenLocationsForLocationIds( +std::shared_ptr StorageAccessProxy::getSourceLocationsForLocationIds( const std::vector& locationIds) const { if (hasSubject()) { - return m_subject->getTokenLocationsForLocationIds(locationIds); + return m_subject->getSourceLocationsForLocationIds(locationIds); } - return std::make_shared(); + return std::make_shared(); } -std::shared_ptr StorageAccessProxy::getTokenLocationsForFile(const std::string& filePath) const +std::shared_ptr StorageAccessProxy::getSourceLocationsForFile(const FilePath& filePath) const { if (hasSubject()) { - return m_subject->getTokenLocationsForFile(filePath); + return m_subject->getSourceLocationsForFile(filePath); } - return std::make_shared(""); + return std::make_shared("", false); } -std::shared_ptr StorageAccessProxy::getTokenLocationsForLinesInFile( +std::shared_ptr StorageAccessProxy::getSourceLocationsForLinesInFile( const std::string& filePath, uint firstLineNumber, uint lastLineNumber ) const { if (hasSubject()) { - return m_subject->getTokenLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber); + return m_subject->getSourceLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber); } - return std::make_shared(""); + return std::make_shared("", false); } -std::shared_ptr StorageAccessProxy::getCommentLocationsInFile(const FilePath& filePath) const +std::shared_ptr StorageAccessProxy::getCommentLocationsInFile(const FilePath& filePath) const { if (hasSubject()) { return m_subject->getCommentLocationsInFile(filePath); } - return std::make_shared(""); + return std::make_shared("", false); } std::shared_ptr StorageAccessProxy::getFileContent(const FilePath& filePath) const @@ -319,14 +319,14 @@ std::vector StorageAccessProxy::getErrors() const return std::vector(); } -std::shared_ptr StorageAccessProxy::getErrorTokenLocations(std::vector* errors) const +std::shared_ptr StorageAccessProxy::getErrorSourceLocations(std::vector* errors) const { if (hasSubject()) { - return m_subject->getErrorTokenLocations(errors); + return m_subject->getErrorSourceLocations(errors); } - return std::make_shared(); + return std::make_shared(); } Id StorageAccessProxy::addNodeBookmark(const NodeBookmark& bookmark) diff --git a/src/lib/data/access/StorageAccessProxy.h b/src/lib/data/access/StorageAccessProxy.h index b04ce7c1..bac5a99b 100644 --- a/src/lib/data/access/StorageAccessProxy.h +++ b/src/lib/data/access/StorageAccessProxy.h @@ -33,7 +33,7 @@ public: virtual StorageEdge getEdgeById(Id edgeId) const; virtual bool checkEdgeExists(Id edgeId) const; - virtual std::shared_ptr getFullTextSearchLocations( + virtual std::shared_ptr getFullTextSearchLocations( const std::string& searchTerm, bool caseSensitive) const; virtual std::vector getAutocompletionMatches(const std::string& query) const; virtual std::vector getSearchMatchesForTokenIds(const std::vector& tokenIds) const; @@ -44,18 +44,18 @@ public: virtual std::vector getActiveTokenIdsForId(Id tokenId, Id* declarationId) const; virtual std::vector getNodeIdsForLocationIds(const std::vector& locationIds) const; - virtual std::shared_ptr getTokenLocationsForTokenIds( + virtual std::shared_ptr getSourceLocationsForTokenIds( const std::vector& tokenIds ) const; - virtual std::shared_ptr getTokenLocationsForLocationIds( + virtual std::shared_ptr getSourceLocationsForLocationIds( const std::vector& locationIds ) const; - virtual std::shared_ptr getTokenLocationsForFile(const std::string& filePath) const; - virtual std::shared_ptr getTokenLocationsForLinesInFile( + virtual std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const; + virtual std::shared_ptr getSourceLocationsForLinesInFile( const std::string& filePath, uint firstLineNumber, uint lastLineNumber ) const; - virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const; + virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const; virtual std::shared_ptr getFileContent(const FilePath& filePath) const; @@ -67,7 +67,7 @@ public: virtual ErrorCountInfo getErrorCount() const; virtual std::vector getErrors() const; - virtual std::shared_ptr getErrorTokenLocations(std::vector* errors) const; + virtual std::shared_ptr getErrorSourceLocations(std::vector* errors) const; virtual Id addNodeBookmark(const NodeBookmark& bookmark); virtual Id addEdgeBookmark(const EdgeBookmark& bookmark); diff --git a/src/lib/data/graph/Token.cpp b/src/lib/data/graph/Token.cpp index 69014910..8123632a 100644 --- a/src/lib/data/graph/Token.cpp +++ b/src/lib/data/graph/Token.cpp @@ -1,6 +1,5 @@ #include "data/graph/Token.h" -#include "data/location/TokenLocation.h" #include "utility/logging/logging.h" Token::Token(Id id) diff --git a/src/lib/data/location/SourceLocation.cpp b/src/lib/data/location/SourceLocation.cpp new file mode 100644 index 00000000..7b7521c0 --- /dev/null +++ b/src/lib/data/location/SourceLocation.cpp @@ -0,0 +1,205 @@ +#include "data/location/SourceLocation.h" + +#include "data/location/SourceLocationFile.h" + +SourceLocation::SourceLocation( + SourceLocationFile* file, + LocationType type, + Id locationId, + std::vector tokenIds, + size_t lineNumber, + size_t columnNumber, + bool isStart +) + : m_file(file) + , m_type(type) + , m_locationId(locationId) + , m_tokenIds(tokenIds) + , m_lineNumber(lineNumber) + , m_columnNumber(columnNumber) + , m_other(nullptr) + , m_isStart(isStart) +{ +} + +SourceLocation::SourceLocation(SourceLocation* other, size_t lineNumber, size_t columnNumber) + : m_file(other->m_file) + , m_type(other->m_type) + , m_locationId(other->m_locationId) + , m_tokenIds(other->m_tokenIds) + , m_lineNumber(lineNumber) + , m_columnNumber(columnNumber) + , m_other(other) + , m_isStart(!other->m_isStart) +{ + other->setOtherLocation(this); +} + +SourceLocation::SourceLocation(const SourceLocation* other, SourceLocationFile* file) + : m_file(file) + , m_type(other->m_type) + , m_locationId(other->m_locationId) + , m_tokenIds(other->m_tokenIds) + , m_lineNumber(other->m_lineNumber) + , m_columnNumber(other->m_columnNumber) + , m_other(nullptr) + , m_isStart(other->m_isStart) +{ +} + +SourceLocation::~SourceLocation() +{ +} + +bool SourceLocation::operator==(const SourceLocation& rhs) const +{ + return ( + getLineNumber() == rhs.getLineNumber() && + getColumnNumber() == rhs.getColumnNumber() && + getLocationId() == rhs.getLocationId() && + getType() == rhs.getType() + ); +} + +bool SourceLocation::operator<(const SourceLocation& rhs) const +{ + if (getLineNumber() != rhs.getLineNumber()) + { + return getLineNumber() < rhs.getLineNumber(); + } + + if (getColumnNumber() != rhs.getColumnNumber()) + { + return getColumnNumber() < rhs.getColumnNumber(); + } + + return getLocationId() < rhs.getLocationId(); +} + +bool SourceLocation::operator>(const SourceLocation& rhs) const +{ + if (getLineNumber() != rhs.getLineNumber()) + { + return getLineNumber() > rhs.getLineNumber(); + } + + if (getColumnNumber() != rhs.getColumnNumber()) + { + return getColumnNumber() > rhs.getColumnNumber(); + } + + return getLocationId() > rhs.getLocationId(); +} + +SourceLocationFile* SourceLocation::getSourceLocationFile() const +{ + return m_file; +} + +Id SourceLocation::getLocationId() const +{ + return m_locationId; +} + +const std::vector& SourceLocation::getTokenIds() const +{ + return m_tokenIds; +} + +LocationType SourceLocation::getType() const +{ + return m_type; +} + +size_t SourceLocation::getColumnNumber() const +{ + return m_columnNumber; +} + +size_t SourceLocation::getLineNumber() const +{ + return m_lineNumber; +} + +const FilePath& SourceLocation::getFilePath() const +{ + return m_file->getFilePath(); +} + +const SourceLocation* SourceLocation::getOtherLocation() const +{ + return m_other; +} + +void SourceLocation::setOtherLocation(SourceLocation* other) +{ + m_other = other; +} + +const SourceLocation* SourceLocation::getStartLocation() const +{ + if (m_isStart) + { + return this; + } + else + { + return m_other; + } +} + +const SourceLocation* SourceLocation::getEndLocation() const +{ + if (!m_isStart) + { + return this; + } + else + { + return m_other; + } +} + +bool SourceLocation::isStartLocation() const +{ + return m_isStart; +} + +bool SourceLocation::isEndLocation() const +{ + return !m_isStart; +} + +bool SourceLocation::isScopeLocation() const +{ + return m_type == LOCATION_SCOPE; +} + +bool SourceLocation::isFullTextSearchMatch() const +{ + return m_type == LOCATION_FULLTEXT; +} + +std::ostream& operator<<(std::ostream& ostream, const SourceLocation& location) +{ + if (location.isStartLocation()) + { + ostream << '<'; + } + + ostream << location.getColumnNumber() << ":[ "; + for (Id tokenId : location.getTokenIds()) + { + ostream << '\b' << tokenId << ' '; + } + + ostream << "\b]"; + + if (location.isEndLocation()) + { + ostream << '>'; + } + + ostream << ' '; + return ostream; +} diff --git a/src/lib/data/location/SourceLocation.h b/src/lib/data/location/SourceLocation.h new file mode 100644 index 00000000..70dbaf10 --- /dev/null +++ b/src/lib/data/location/SourceLocation.h @@ -0,0 +1,66 @@ +#ifndef SOURCE_LOCATION_H +#define SOURCE_LOCATION_H + +#include +#include +#include + +#include "data/location/LocationType.h" +#include "utility/types.h" + +class FilePath; +class SourceLocationFile; + +class SourceLocation +{ +public: + SourceLocation(SourceLocationFile* file, LocationType type, Id locationId, std::vector tokenIds, + size_t lineNumber, size_t columnNumber, bool isStart); + SourceLocation(SourceLocation* other, size_t lineNumber, size_t columnNumber); + SourceLocation(const SourceLocation* other, SourceLocationFile* file); + virtual ~SourceLocation(); + + bool operator==(const SourceLocation& rhs) const; + bool operator<(const SourceLocation& rhs) const; + bool operator>(const SourceLocation& rhs) const; + + SourceLocationFile* getSourceLocationFile() const; + + Id getLocationId() const; + const std::vector& getTokenIds() const; + LocationType getType() const; + + size_t getColumnNumber() const; + size_t getLineNumber() const; + const FilePath& getFilePath() const; + + const SourceLocation* getOtherLocation() const; + void setOtherLocation(SourceLocation* other); + + const SourceLocation* getStartLocation() const; + const SourceLocation* getEndLocation() const; + + bool isStartLocation() const; + bool isEndLocation() const; + + bool isScopeLocation() const; + bool isFullTextSearchMatch() const; + +private: + SourceLocationFile* m_file; + + LocationType m_type; + + const Id m_locationId; + const std::vector m_tokenIds; + + const size_t m_lineNumber; + const size_t m_columnNumber; + + SourceLocation* m_other; + const bool m_isStart; +}; + +std::ostream& operator<<(std::ostream& ostream, const SourceLocation& location); + +#endif // SOURCE_LOCATION_H diff --git a/src/lib/data/location/SourceLocationCollection.cpp b/src/lib/data/location/SourceLocationCollection.cpp new file mode 100644 index 00000000..e6c8bb51 --- /dev/null +++ b/src/lib/data/location/SourceLocationCollection.cpp @@ -0,0 +1,129 @@ +#include "data/location/SourceLocationCollection.h" + +#include "data/location/SourceLocationFile.h" +#include "utility/logging/logging.h" + +SourceLocationCollection::SourceLocationCollection() +{ +} + +SourceLocationCollection::~SourceLocationCollection() +{ +} + +const std::map>& SourceLocationCollection::getSourceLocationFiles() const +{ + return m_files; +} + +size_t SourceLocationCollection::getSourceLocationCount() const +{ + size_t count = 0; + for (auto p : m_files) + { + count += p.second->getSourceLocationCount(); + } + return count; +} + +size_t SourceLocationCollection::getSourceLocationFileCount() const +{ + return m_files.size(); +} + +std::shared_ptr SourceLocationCollection::getSourceLocationFileByPath(const FilePath& filePath) const +{ + std::map>::const_iterator it = m_files.find(filePath); + if (it != m_files.end()) + { + return it->second; + } + + return nullptr; +} + +SourceLocation* SourceLocationCollection::getSourceLocationById(Id locationId) const +{ + for (auto p : m_files) + { + SourceLocation* location = p.second->getSourceLocationById(locationId); + if (location) + { + return location; + } + } + + return nullptr; +} + +SourceLocation* SourceLocationCollection::addSourceLocation( + LocationType type, Id locationId, std::vector tokenIds, const FilePath& filePath, + size_t startLineNumber, size_t startColumnNumber, + size_t endLineNumber, size_t endColumnNumber) +{ + if (startLineNumber > endLineNumber || (startLineNumber == endLineNumber && startColumnNumber > endColumnNumber)) + { + LOG_ERROR_STREAM(<< "SourceLocation has wrong boundaries: " << filePath.str() << " " + << startLineNumber << ":" << startColumnNumber << " " + << endLineNumber << ":" << endColumnNumber); + return nullptr; + } + + SourceLocationFile* file = createSourceLocationFile(filePath); + if (file->isWhole()) + { + return nullptr; + } + + return file->addSourceLocation(type, locationId, tokenIds, startLineNumber, startColumnNumber, endLineNumber, endColumnNumber); +} + +SourceLocation* SourceLocationCollection::addSourceLocationCopy(SourceLocation* location) +{ + return createSourceLocationFile(location->getFilePath())->addSourceLocationCopy(location); +} + +void SourceLocationCollection::addSourceLocationFile(std::shared_ptr file) +{ + m_files.emplace(file->getFilePath(), file); +} + +void SourceLocationCollection::forEachSourceLocationFile( + std::function)> func) const +{ + for (auto p : m_files) + { + func(p.second); + } +} + +void SourceLocationCollection::forEachSourceLocation(std::function func) const +{ + for (auto p : m_files) + { + p.second->forEachSourceLocation(func); + } +} + +SourceLocationFile* SourceLocationCollection::createSourceLocationFile(const FilePath& filePath) +{ + SourceLocationFile* file = getSourceLocationFileByPath(filePath).get(); + if (file) + { + return file; + } + + std::shared_ptr filePtr = std::make_shared(filePath, false); + m_files.emplace(filePath, filePtr); + return filePtr.get(); +} + +std::ostream& operator<<(std::ostream& ostream, const SourceLocationCollection& base) +{ + ostream << "Locations:\n"; + base.forEachSourceLocationFile([&ostream](std::shared_ptr f) + { + ostream << *(f.get()); + }); + return ostream; +} diff --git a/src/lib/data/location/SourceLocationCollection.h b/src/lib/data/location/SourceLocationCollection.h new file mode 100644 index 00000000..4366eae6 --- /dev/null +++ b/src/lib/data/location/SourceLocationCollection.h @@ -0,0 +1,49 @@ +#ifndef SOURCE_LOCATION_COLLECTION_H +#define SOURCE_LOCATION_COLLECTION_H + +#include +#include +#include +#include + +#include "data/location/LocationType.h" +#include "utility/types.h" + +class FilePath; +class SourceLocation; +class SourceLocationFile; + +class SourceLocationCollection +{ +public: + SourceLocationCollection(); + virtual ~SourceLocationCollection(); + + const std::map>& getSourceLocationFiles() const; + + size_t getSourceLocationCount() const; + size_t getSourceLocationFileCount() const; + + std::shared_ptr getSourceLocationFileByPath(const FilePath& filePath) const; + SourceLocation* getSourceLocationById(Id locationId) const; + + SourceLocation* addSourceLocation( + LocationType type, Id locationId, std::vector tokenIds, const FilePath& filePath, + size_t startLineNumber, size_t startColumnNumber, + size_t endLineNumber, size_t endColumnNumber); + SourceLocation* addSourceLocationCopy(SourceLocation* location); + + void addSourceLocationFile(std::shared_ptr file); + + void forEachSourceLocationFile(std::function)> func) const; + void forEachSourceLocation(std::function func) const; + +private: + SourceLocationFile* createSourceLocationFile(const FilePath& filePath); + + std::map> m_files; +}; + +std::ostream& operator<<(std::ostream& ostream, const SourceLocationCollection& base); + +#endif // SOURCE_LOCATION_COLLECTION_H diff --git a/src/lib/data/location/SourceLocationFile.cpp b/src/lib/data/location/SourceLocationFile.cpp new file mode 100644 index 00000000..7f0e39b9 --- /dev/null +++ b/src/lib/data/location/SourceLocationFile.cpp @@ -0,0 +1,215 @@ +#include "data/location/SourceLocationFile.h" + +SourceLocationFile::SourceLocationFile(const FilePath& filePath, bool isWhole) + : m_filePath(filePath) + , m_isWhole(isWhole) +{ +} + +SourceLocationFile::~SourceLocationFile() +{ +} + +const FilePath& SourceLocationFile::getFilePath() const +{ + return m_filePath; +} + +void SourceLocationFile::setIsWhole(bool isWhole) +{ + m_isWhole = isWhole; +} + +bool SourceLocationFile::isWhole() const +{ + return m_isWhole; +} + +const std::multiset, SourceLocationFile::LocationComp>& SourceLocationFile::getSourceLocations() const +{ + return m_locations; +} + +size_t SourceLocationFile::getSourceLocationCount() const +{ + return m_locationIndex.size(); +} + +size_t SourceLocationFile::getUnscopedStartLocationCount() const +{ + size_t count = 0; + for (std::shared_ptr location : m_locations) + { + if (location->isStartLocation() && !location->isScopeLocation()) + { + count++; + } + } + return count; +} + +SourceLocation* SourceLocationFile::addSourceLocation( + LocationType type, Id locationId, std::vector tokenIds, + size_t startLineNumber, size_t startColumnNumber, + size_t endLineNumber, size_t endColumnNumber) +{ + std::shared_ptr start = + std::make_shared(this, type, locationId, tokenIds, startLineNumber, startColumnNumber, true); + std::shared_ptr end = std::make_shared(start.get(), endLineNumber, endColumnNumber); + + m_locations.insert(start); + m_locations.insert(end); + + m_locationIndex.emplace(start->getLocationId(), start.get()); + + return start.get(); +} + +SourceLocation* SourceLocationFile::addSourceLocationCopy(const SourceLocation* location) +{ + // Check whether this location was already added or if the other SourceLocation was added. + SourceLocation* oldLocation = getSourceLocationById(location->getLocationId()); + if (oldLocation) + { + if (oldLocation->isStartLocation() == location->isStartLocation()) + { + return oldLocation; + } + + const SourceLocation* otherOldLocation = oldLocation->getOtherLocation(); + if (otherOldLocation && otherOldLocation->isStartLocation() == location->isStartLocation()) + { + return const_cast(otherOldLocation); + } + } + + std::shared_ptr copy = std::make_shared(location, this); + m_locations.insert(copy); + m_locationIndex.emplace(copy->getLocationId(), copy.get()); + + // If the old location was added before, then link them with each other. + if (oldLocation) + { + oldLocation->setOtherLocation(copy.get()); + copy->setOtherLocation(oldLocation); + } + + return copy.get(); +} + +SourceLocation* SourceLocationFile::getSourceLocationById(Id locationId) const +{ + std::map::const_iterator it = m_locationIndex.find(locationId); + + if (it != m_locationIndex.end()) + { + return it->second; + } + + return nullptr; +} + +void SourceLocationFile::forEachSourceLocation(std::function func) const +{ + for (std::shared_ptr location : m_locations) + { + func(location.get()); + } +} + +void SourceLocationFile::forEachStartSourceLocation(std::function func) const +{ + for (std::shared_ptr location : m_locations) + { + if (location->isStartLocation()) + { + func(location.get()); + } + } +} + +void SourceLocationFile::forEachEndSourceLocation(std::function func) const +{ + for (std::shared_ptr location : m_locations) + { + if (location->isEndLocation()) + { + func(location.get()); + } + } +} + +std::shared_ptr SourceLocationFile::getFilteredByLines(size_t firstLineNumber, size_t lastLineNumber) const +{ + std::shared_ptr ret = std::make_shared(getFilePath(), false); + + for (std::shared_ptr location : m_locations) + { + if (location->getLineNumber() >= firstLineNumber && location->getLineNumber() <= lastLineNumber) + { + ret->addSourceLocationCopy(location.get()); + } + } + + if (isWhole() && ret->getSourceLocationCount() == getSourceLocationCount()) + { + ret->setIsWhole(true); + } + + return ret; +} + +std::shared_ptr SourceLocationFile::getFilteredByType(LocationType type) const +{ + std::shared_ptr ret = std::make_shared(getFilePath(), false); + + for (std::shared_ptr location : m_locations) + { + if (location->getType() == type) + { + ret->addSourceLocationCopy(location.get()); + } + } + + if (isWhole() && ret->getSourceLocationCount() == getSourceLocationCount()) + { + ret->setIsWhole(true); + } + + return ret; +} + +std::ostream& operator<<(std::ostream& ostream, const SourceLocationFile& file) +{ + ostream << "file \"" << file.getFilePath().str() << "\""; + + size_t line = 0; + file.forEachSourceLocation( + [&ostream, &line](SourceLocation* location) + { + if (location->getLineNumber() != line) + { + while (line < location->getLineNumber()) + { + if (!line) + { + line = location->getLineNumber(); + } + else + { + line++; + } + + ostream << '\n' << line; + } + + ostream << ": "; + } + + ostream << *location; + } + ); + + ostream << '\n'; + return ostream; +} diff --git a/src/lib/data/location/SourceLocationFile.h b/src/lib/data/location/SourceLocationFile.h new file mode 100644 index 00000000..e41f7c2b --- /dev/null +++ b/src/lib/data/location/SourceLocationFile.h @@ -0,0 +1,62 @@ +#ifndef SOURCE_LOCATION_FILE_H +#define SOURCE_LOCATION_FILE_H + +#include +#include +#include + +#include "data/location/LocationType.h" +#include "data/location/SourceLocation.h" +#include "utility/file/FilePath.h" +#include "utility/types.h" + +class SourceLocationFile +{ +public: + struct LocationComp + { + bool operator()(const std::shared_ptr& lhs, const std::shared_ptr& rhs) const + { + return *(lhs.get()) < *(rhs.get()); + } + }; + + SourceLocationFile(const FilePath& filePath, bool isWhole); + virtual ~SourceLocationFile(); + + const FilePath& getFilePath() const; + + void setIsWhole(bool isWhole); + bool isWhole() const; + + const std::multiset, LocationComp>& getSourceLocations() const; + + size_t getSourceLocationCount() const; + size_t getUnscopedStartLocationCount() const; + + SourceLocation* addSourceLocation( + LocationType type, Id locationId, std::vector tokenIds, + size_t startLineNumber, size_t startColumnNumber, + size_t endLineNumber, size_t endColumnNumber); + SourceLocation* addSourceLocationCopy(const SourceLocation* location); + + SourceLocation* getSourceLocationById(Id locationId) const; + + void forEachSourceLocation(std::function func) const; + void forEachStartSourceLocation(std::function func) const; + void forEachEndSourceLocation(std::function func) const; + + std::shared_ptr getFilteredByLines(size_t firstLineNumber, size_t lastLineNumber) const; + std::shared_ptr getFilteredByType(LocationType type) const; + +private: + const FilePath m_filePath; + bool m_isWhole; + + std::multiset, LocationComp> m_locations; + std::map m_locationIndex; +}; + +std::ostream& operator<<(std::ostream& ostream, const SourceLocationFile& base); + +#endif // SOURCE_LOCATION_FILE_H diff --git a/src/lib/data/location/TokenLocation.cpp b/src/lib/data/location/TokenLocation.cpp deleted file mode 100644 index 0236c5b6..00000000 --- a/src/lib/data/location/TokenLocation.cpp +++ /dev/null @@ -1,206 +0,0 @@ -#include "data/location/TokenLocation.h" - -#include "data/location/TokenLocationLine.h" - -TokenLocation::TokenLocation(Id locationId, Id tokenId, TokenLocationLine* line, unsigned int columnNumber, bool isStart) - : m_id(locationId) - , m_tokenId(tokenId) - , m_type(LOCATION_TOKEN) - , m_line(line) - , m_columnNumber(columnNumber) - , m_other(nullptr) - , m_isStart(isStart) -{ -} - -TokenLocation::TokenLocation(TokenLocation *other, TokenLocationLine* line, unsigned int columnNumber, bool isStart) - : m_id(other->m_id) - , m_tokenId(other->m_tokenId) - , m_type(other->m_type) - , m_line(line) - , m_columnNumber(columnNumber) - , m_other(other) - , m_isStart(isStart) -{ -} - -TokenLocation::TokenLocation(const TokenLocation& other, TokenLocationLine* line) - : m_id(other.m_id) - , m_tokenId(other.m_tokenId) - , m_type(other.m_type) - , m_line(line) - , m_columnNumber(other.m_columnNumber) - , m_other(nullptr) - , m_isStart(other.m_isStart) -{ -} - -TokenLocation::~TokenLocation() -{ -} - -bool TokenLocation::operator<(const TokenLocation& rhs) const -{ - return ( - getLineNumber() < rhs.getLineNumber() || ( - getLineNumber() == rhs.getLineNumber() && - getColumnNumber() < rhs.getColumnNumber() - ) - ); -} - -bool TokenLocation::operator>(const TokenLocation& rhs) const -{ - return ( - getLineNumber() > rhs.getLineNumber() || ( - getLineNumber() == rhs.getLineNumber() && - getColumnNumber() > rhs.getColumnNumber() - ) - ); -} - -Id TokenLocation::getId() const -{ - return m_id; -} - -Id TokenLocation::getTokenId() const -{ - return m_tokenId; -} - -LocationType TokenLocation::getType() const -{ - return m_type; -} - -void TokenLocation::setType(LocationType type) -{ - m_type = type; - - if (m_other) - { - m_other->m_type = type; - } -} - -TokenLocationLine* TokenLocation::getTokenLocationLine() const -{ - return m_line; -} - -TokenLocationFile* TokenLocation::getTokenLocationFile() const -{ - return m_line->getTokenLocationFile(); -} - -unsigned int TokenLocation::getColumnNumber() const -{ - return m_columnNumber; -} - -unsigned int TokenLocation::getLineNumber() const -{ - return m_line->getLineNumber(); -} - -const FilePath& TokenLocation::getFilePath() const -{ - return m_line->getFilePath(); -} - -TokenLocation* TokenLocation::getOtherTokenLocation() const -{ - return m_other; -} - -void TokenLocation::setOtherTokenLocation(TokenLocation* location) -{ - m_other = location; -} - -TokenLocation* TokenLocation::getStartTokenLocation() -{ - if (m_isStart) - { - return this; - } - else - { - return m_other; - } -} - -TokenLocation* TokenLocation::getEndTokenLocation() -{ - if (!m_isStart) - { - return this; - } - else - { - return m_other; - } -} - -const TokenLocation* TokenLocation::getStartTokenLocation() const -{ - if (m_isStart) - { - return this; - } - else - { - return m_other; - } -} - -const TokenLocation* TokenLocation::getEndTokenLocation() const -{ - if (!m_isStart) - { - return this; - } - else - { - return m_other; - } -} - -bool TokenLocation::isStartTokenLocation() const -{ - return m_isStart; -} - -bool TokenLocation::isEndTokenLocation() const -{ - return !m_isStart; -} - -bool TokenLocation::isScopeTokenLocation() const -{ - return m_type == LOCATION_SCOPE; -} - -bool TokenLocation::isFullTextSearchMatch() const -{ - return m_type == LOCATION_FULLTEXT; -} - -std::ostream& operator<<(std::ostream& ostream, const TokenLocation& location) -{ - if ((&location)->isStartTokenLocation()) - { - ostream << "<"; - } - - ostream << location.getColumnNumber() << ":[" << location.getTokenId() << "]"; - - if ((&location)->isEndTokenLocation()) - { - ostream << ">"; - } - - ostream << " "; - return ostream; -} diff --git a/src/lib/data/location/TokenLocation.h b/src/lib/data/location/TokenLocation.h deleted file mode 100644 index 9e7751f3..00000000 --- a/src/lib/data/location/TokenLocation.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef TOKEN_LOCATION_H -#define TOKEN_LOCATION_H - -#include -#include -#include - -#include "data/location/LocationType.h" -#include "utility/file/FilePath.h" -#include "utility/types.h" - -class Token; -class TokenLocationFile; -class TokenLocationLine; - -class TokenLocation -{ -public: - TokenLocation(Id locationId, Id tokenId, TokenLocationLine* line, unsigned int columnNumber, bool isStart); - TokenLocation(TokenLocation* other, TokenLocationLine* line, unsigned int columnNumber, bool isStart); - TokenLocation(const TokenLocation& other, TokenLocationLine* line); - ~TokenLocation(); - - bool operator<(const TokenLocation& rhs) const; - bool operator>(const TokenLocation& rhs) const; - - Id getId() const; - Id getTokenId() const; - - LocationType getType() const; - void setType(LocationType type); - - TokenLocationLine* getTokenLocationLine() const; - TokenLocationFile* getTokenLocationFile() const; - - unsigned int getColumnNumber() const; - unsigned int getLineNumber() const; - const FilePath& getFilePath() const; - - TokenLocation* getOtherTokenLocation() const; - void setOtherTokenLocation(TokenLocation* location); - - TokenLocation* getStartTokenLocation(); - TokenLocation* getEndTokenLocation(); - - const TokenLocation* getStartTokenLocation() const; - const TokenLocation* getEndTokenLocation() const; - - bool isStartTokenLocation() const; - bool isEndTokenLocation() const; - - bool isScopeTokenLocation() const; - bool isFullTextSearchMatch() const; - -private: - const Id m_id; // own id - const Id m_tokenId; - - LocationType m_type; - - TokenLocationLine* const m_line; - const unsigned int m_columnNumber; - - TokenLocation* m_other; - const bool m_isStart; -}; - -std::ostream& operator<<(std::ostream& ostream, const TokenLocation& location); - -#endif // TOKEN_LOCATION_H diff --git a/src/lib/data/location/TokenLocationCollection.cpp b/src/lib/data/location/TokenLocationCollection.cpp deleted file mode 100644 index 7b8334f0..00000000 --- a/src/lib/data/location/TokenLocationCollection.cpp +++ /dev/null @@ -1,233 +0,0 @@ -#include "data/location/TokenLocationCollection.h" - -#include - -#include "utility/file/FileSystem.h" -#include "utility/logging/logging.h" - -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationFile.h" -#include "data/location/TokenLocationLine.h" - -TokenLocationCollection::TokenLocationCollection() -{ -} - -TokenLocationCollection::~TokenLocationCollection() -{ -} - -const TokenLocationCollection::TokenLocationFileMapType& TokenLocationCollection::getTokenLocationFiles() const -{ - return m_files; -} - -const std::map& TokenLocationCollection::getTokenLocations() const -{ - return m_locations; -} - -std::shared_ptr TokenLocationCollection::getTokenLocationFileByPath(const FilePath& filePath) const -{ - std::map>::const_iterator it = m_files.find(filePath); - if (it != m_files.end()) - { - return it->second; - } - - return nullptr; -} - -size_t TokenLocationCollection::getTokenLocationFileCount() const -{ - return m_files.size(); -} - -size_t TokenLocationCollection::getTokenLocationLineCount() const -{ - size_t count = 0; - - for (const TokenLocationFilePairType& file : m_files) - { - count += file.second->getTokenLocationLineCount(); - } - - return count; -} - -size_t TokenLocationCollection::getTokenLocationCount() const -{ - return m_locations.size(); -} - -TokenLocation* TokenLocationCollection::addTokenLocation( - Id locationId, Id tokenId, const FilePath& filePath, - unsigned int startLineNumber, unsigned int startColumnNumber, - unsigned int endLineNumber, unsigned int endColumnNumber) -{ - if (startLineNumber > endLineNumber || (startLineNumber == endLineNumber && startColumnNumber > endColumnNumber)) - { - LOG_ERROR_STREAM(<< "TokenLocation has wrong boundaries: "<< filePath.str() << " " - << startLineNumber << ":" << startColumnNumber << " " - << endLineNumber << ":" << endColumnNumber); - return nullptr; - } - - TokenLocation* location = findTokenLocationById(locationId); - if (location) - { - return location; - } - - TokenLocationFile* file = createTokenLocationFile(filePath); - location = file->addTokenLocation( - locationId, tokenId, startLineNumber, startColumnNumber, endLineNumber, endColumnNumber); - - m_locations.emplace(location->getId(), location); - return location; -} - -void TokenLocationCollection::removeTokenLocation(TokenLocation* location) -{ - if (!location || !findTokenLocationById(location->getId())) - { - LOG_ERROR("TokenLocation is not part of this TokenLocationCollection."); - return; - } - - m_locations.erase(location->getId()); - - TokenLocationFile* file = location->getTokenLocationFile(); - file->removeTokenLocation(location); - - if (!file->getTokenLocationLineCount()) - { - m_files.erase(file->getFilePath()); - } -} - -TokenLocationFile* TokenLocationCollection::addTokenLocationFile(std::shared_ptr locationFile) -{ - TokenLocationFile* file = findTokenLocationFileByPath(locationFile->getFilePath()); - if (file) - { - LOG_ERROR("TokenLocationFile with same path already exists."); - return file; - } - - m_files.emplace(locationFile->getFilePath(), locationFile); - locationFile->forEachTokenLocation( - [this, &file](TokenLocation* tokenLocation) -> void - { - m_locations.emplace(tokenLocation->getId(), tokenLocation); - } - ); - return locationFile.get(); -} - -void TokenLocationCollection::removeTokenLocationFile(TokenLocationFile* file) -{ - file->forEachTokenLocation( - [&](TokenLocation* location) - { - m_locations.erase(location->getId()); - } - ); - - m_files.erase(file->getFilePath()); -} - -TokenLocation* TokenLocationCollection::findTokenLocationById(Id id) const -{ - std::map::const_iterator it = m_locations.find(id); - - if (it != m_locations.end()) - { - return it->second; - } - - return nullptr; -} - -TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const FilePath& filePath) const -{ - return getTokenLocationFileByPath(filePath).get(); -} - -void TokenLocationCollection::forEachTokenLocationFile( - std::function)> func) const -{ - for (const TokenLocationFilePairType& file : m_files) - { - func(file.second); - } -} - -void TokenLocationCollection::forEachTokenLocationLine(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); - } -} - -TokenLocationFile* TokenLocationCollection::addTokenLocationFileAsPlainCopy(const TokenLocationFile* locationFile) -{ - TokenLocationFile* file = createTokenLocationFile(locationFile->getFilePath()); - locationFile->forEachTokenLocation( - [this, &file](TokenLocation* tokenLocation) -> void - { - TokenLocation* copy = file->addTokenLocationAsPlainCopy(tokenLocation); - m_locations.emplace(copy->getId(), copy); - } - ); - return file; -} - -TokenLocation* TokenLocationCollection::addTokenLocationAsPlainCopy(const TokenLocation* location) -{ - const FilePath& filePath = location->getTokenLocationLine()->getTokenLocationFile()->getFilePath(); - TokenLocationFile* file = createTokenLocationFile(filePath); - TokenLocation* copy = file->addTokenLocationAsPlainCopy(location); - - m_locations.emplace(copy->getId(), copy); - return copy; -} - -void TokenLocationCollection::clear() -{ - m_locations.clear(); - m_files.clear(); -} - -TokenLocationFile* TokenLocationCollection::createTokenLocationFile(const FilePath& filePath) -{ - TokenLocationFile* file = findTokenLocationFileByPath(filePath); - - if (file) - { - return file; - } - - std::shared_ptr filePtr = std::make_shared(filePath); - m_files.emplace(filePath, filePtr); - return filePtr.get(); -} - -std::ostream& operator<<(std::ostream& ostream, const TokenLocationCollection& base) -{ - ostream << "Locations:\n"; - base.forEachTokenLocationFile([&ostream](std::shared_ptr f) - { - ostream << *(f.get()); - }); - return ostream; -} diff --git a/src/lib/data/location/TokenLocationCollection.h b/src/lib/data/location/TokenLocationCollection.h deleted file mode 100644 index d5fa81a3..00000000 --- a/src/lib/data/location/TokenLocationCollection.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef TOKEN_LOCATION_COLLECTION_H -#define TOKEN_LOCATION_COLLECTION_H - -#include -#include -#include -#include -#include - -#include "utility/file/FilePath.h" -#include "utility/types.h" - -class TokenLocation; -class TokenLocationFile; -class TokenLocationLine; - -class TokenLocationCollection -{ -public: - typedef std::map > TokenLocationFileMapType; - typedef std::pair > TokenLocationFilePairType; - - TokenLocationCollection(); - ~TokenLocationCollection(); - - const TokenLocationFileMapType& getTokenLocationFiles() const; - const std::map& getTokenLocations() const; - - std::shared_ptr getTokenLocationFileByPath(const FilePath& filePath) const; - - size_t getTokenLocationFileCount() const; - size_t getTokenLocationLineCount() const; - size_t getTokenLocationCount() const; - - TokenLocation* addTokenLocation( - Id locationId, Id tokenId, const FilePath& filePath, - unsigned int startLineNumber, unsigned int startColumnNumber, - unsigned int endLineNumber, unsigned int endColumnNumber); - void removeTokenLocation(TokenLocation* location); - - TokenLocationFile* addTokenLocationFile(std::shared_ptr locationFile); - void removeTokenLocationFile(TokenLocationFile* file); - - TokenLocation* findTokenLocationById(Id id) const; - TokenLocationFile* findTokenLocationFileByPath(const FilePath& filePath) const; - - void forEachTokenLocationFile(std::function)> func) const; - void forEachTokenLocationLine(std::function func) const; - void forEachTokenLocation(std::function func) const; - - TokenLocationFile* addTokenLocationFileAsPlainCopy(const TokenLocationFile* locationFile); - TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location); - - void clear(); - -private: - TokenLocationFile* createTokenLocationFile(const FilePath& filePath); - - TokenLocationFileMapType m_files; - std::map m_locations; -}; - -std::ostream& operator<<(std::ostream& ostream, const TokenLocationCollection& base); - -#endif // TOKEN_LOCATION_COLLECTION_H diff --git a/src/lib/data/location/TokenLocationFile.cpp b/src/lib/data/location/TokenLocationFile.cpp deleted file mode 100644 index 61603d94..00000000 --- a/src/lib/data/location/TokenLocationFile.cpp +++ /dev/null @@ -1,260 +0,0 @@ -#include "data/location/TokenLocationFile.h" - -#include - -#include "utility/logging/logging.h" -#include "utility/types.h" - -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationLine.h" - -TokenLocationFile::TokenLocationFile(const FilePath& filePath) - : isWholeCopy(false) - , m_filePath(filePath) -{ -} - -TokenLocationFile::~TokenLocationFile() -{ -} - -const TokenLocationFile::TokenLocationLineMapType& TokenLocationFile::getTokenLocationLines() const -{ - return m_lines; -} - -size_t TokenLocationFile::getTokenLocationLineCount() const -{ - return m_lines.size(); -} - -size_t TokenLocationFile::getUnscopedStartTokenLocationCount() const -{ - size_t count = 0; - for (const TokenLocationLinePairType& line : m_lines) - { - line.second->forEachStartTokenLocation( - [&count](TokenLocation* location) - { - if (!location->isScopeTokenLocation()) - { - count++; - } - } - ); - } - return count; -} - -const FilePath& TokenLocationFile::getFilePath() const -{ - return m_filePath; -} - -TokenLocation* TokenLocationFile::addTokenLocation( - Id locationId, Id tokenId, - unsigned int startLineNumber, unsigned int startColumnNumber, - unsigned int endLineNumber, unsigned int endColumnNumber) -{ - TokenLocationLine* line = createTokenLocationLine(startLineNumber); - - TokenLocation* start = line->addStartTokenLocation(locationId, tokenId, startColumnNumber); - - if (startLineNumber != endLineNumber) - { - line = createTokenLocationLine(endLineNumber); - } - - line->addEndTokenLocation(start, endColumnNumber); - - return start; -} - -void TokenLocationFile::removeTokenLocation(TokenLocation* location) -{ - TokenLocationLine* line = location->getTokenLocationLine(); - - TokenLocation* otherLocation = location->getOtherTokenLocation(); - TokenLocationLine* otherLine = otherLocation->getTokenLocationLine(); - - line->removeTokenLocation(location); - if (!line->getTokenLocationCount()) - { - m_lines.erase(line->getLineNumber()); - } - - otherLine->removeTokenLocation(otherLocation); - if (!otherLine->getTokenLocationCount()) - { - m_lines.erase(otherLine->getLineNumber()); - } -} - -TokenLocationLine* TokenLocationFile::findTokenLocationLineByNumber(unsigned int lineNumber) const -{ - return findTokenLocationLine(lineNumber); -} - -void TokenLocationFile::forEachTokenLocationLine(std::function func) const -{ - for (const TokenLocationLinePairType& line : m_lines) - { - func(line.second.get()); - } -} - -void TokenLocationFile::forEachTokenLocation(std::function func) const -{ - for (const TokenLocationLinePairType& line : m_lines) - { - line.second->forEachTokenLocation(func); - } -} - -void TokenLocationFile::forEachStartTokenLocation(std::function func) const -{ - for (const TokenLocationLinePairType& line : m_lines) - { - line.second->forEachStartTokenLocation(func); - } -} - -void TokenLocationFile::forEachEndTokenLocation(std::function func) const -{ - for (const TokenLocationLinePairType& line : m_lines) - { - line.second->forEachEndTokenLocation(func); - } -} - -TokenLocation* TokenLocationFile::addTokenLocationAsPlainCopy(const TokenLocation* location) -{ - unsigned int lineNumber = location->getLineNumber(); - TokenLocationLine* line = createTokenLocationLine(lineNumber); - - // Check whether this location was already added or if the other TokenLocation was added. - TokenLocation* otherLocation = line->getTokenLocationById(location->getId()); - if (otherLocation) - { - if (otherLocation->isStartTokenLocation() == location->isStartTokenLocation()) - { - // The location was already added. - return otherLocation; - } - } - else - { - // Look for the other location in it's line. - unsigned int otherLineNumber = location->getOtherTokenLocation()->getLineNumber(); - if (lineNumber != otherLineNumber) - { - TokenLocationLine* otherLine = findTokenLocationLine(otherLineNumber); - if (otherLine) - { - otherLocation = otherLine->getTokenLocationById(location->getId()); - } - } - } - - TokenLocation* copy = line->addTokenLocationAsPlainCopy(location); - - // If the other location was added before, then link them with each other. - if (otherLocation) - { - otherLocation->setOtherTokenLocation(copy); - copy->setOtherTokenLocation(otherLocation); - } - - return copy; -} - -std::shared_ptr TokenLocationFile::getFilteredByLines(unsigned int firstLineNumber, unsigned int lastLineNumber) const -{ - std::shared_ptr ret = std::make_shared(getFilePath().str()); - - if (getTokenLocationLines().size() == 0) - { - return ret; - } - - uint endLineNumber = getTokenLocationLines().rbegin()->first; - std::set addedLocationIds; - for (uint i = firstLineNumber; i <= endLineNumber; i++) - { - TokenLocationLine* locationLine = findTokenLocationLineByNumber(i); - if (!locationLine) - { - continue; - } - - if (locationLine->getLineNumber() <= lastLineNumber) - { - locationLine->forEachTokenLocation( - [&](TokenLocation* tokenLocation) -> void - { - const Id tokenId = tokenLocation->getId(); - if (addedLocationIds.find(tokenId) == addedLocationIds.end()) - { - ret->addTokenLocationAsPlainCopy(tokenLocation->getStartTokenLocation()); - ret->addTokenLocationAsPlainCopy(tokenLocation->getEndTokenLocation()); - addedLocationIds.insert(tokenId); - } - } - ); - } - else - { - // Save start locations of TokenLocations that span accross the line range. - locationLine->forEachTokenLocation( - [&](TokenLocation* tokenLocation) -> void - { - if (tokenLocation->isEndTokenLocation() && - tokenLocation->getStartTokenLocation()->getLineNumber() < firstLineNumber) - { - ret->addTokenLocationAsPlainCopy(tokenLocation->getStartTokenLocation()); - ret->addTokenLocationAsPlainCopy(tokenLocation->getEndTokenLocation()); - } - } - ); - } - } - - return ret; -} - -TokenLocationLine* TokenLocationFile::findTokenLocationLine(unsigned int lineNumber) const -{ - TokenLocationLineMapType::const_iterator it = m_lines.find(lineNumber); - - if (it != m_lines.end()) - { - return it->second.get(); - } - - return nullptr; -} - -TokenLocationLine* TokenLocationFile::createTokenLocationLine(unsigned int lineNumber) -{ - TokenLocationLine* line = findTokenLocationLine(lineNumber); - - if (line) - { - return line; - } - - std::shared_ptr linePtr = std::make_shared(this, lineNumber); - m_lines.emplace(lineNumber, linePtr); - return linePtr.get(); -} - -std::ostream& operator<<(std::ostream& ostream, const TokenLocationFile& file) -{ - ostream << "file \"" << file.getFilePath().str() << "\"\n"; - file.forEachTokenLocationLine([&ostream](TokenLocationLine* l) - { - ostream << *l << '\n'; - }); - return ostream; -} - diff --git a/src/lib/data/location/TokenLocationFile.h b/src/lib/data/location/TokenLocationFile.h deleted file mode 100644 index f3d6af4d..00000000 --- a/src/lib/data/location/TokenLocationFile.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef TOKEN_LOCATION_FILE_H -#define TOKEN_LOCATION_FILE_H - -#include -#include -#include -#include -#include - -#include "utility/file/FilePath.h" -#include "utility/types.h" - -class TokenLocation; -class TokenLocationLine; - -class TokenLocationFile -{ -public: - typedef std::map> TokenLocationLineMapType; - typedef std::pair> TokenLocationLinePairType; - - TokenLocationFile(const FilePath& filePath); - ~TokenLocationFile(); - - const TokenLocationLineMapType& getTokenLocationLines() const; - size_t getTokenLocationLineCount() const; - size_t getUnscopedStartTokenLocationCount() const; - - const FilePath& getFilePath() const; - - TokenLocation* addTokenLocation( - Id locationId, Id tokenId, - unsigned int startLineNumber, unsigned int startColumnNumber, - 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; - void forEachStartTokenLocation(std::function func) const; - void forEachEndTokenLocation(std::function func) const; - - TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location); - - std::shared_ptr getFilteredByLines(unsigned int firstLineNumber, unsigned int lastLineNumber) const; - - bool isWholeCopy; - -private: - TokenLocationLine* findTokenLocationLine(unsigned int lineNumber) const; - TokenLocationLine* createTokenLocationLine(unsigned int lineNumber); - - std::map > m_lines; - FilePath m_filePath; -}; - -std::ostream& operator<<(std::ostream& ostream, const TokenLocationFile& file); - -#endif // TOKEN_LOCATION_FILE_H diff --git a/src/lib/data/location/TokenLocationLine.cpp b/src/lib/data/location/TokenLocationLine.cpp deleted file mode 100644 index aa9b8814..00000000 --- a/src/lib/data/location/TokenLocationLine.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include "data/location/TokenLocationLine.h" - -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationFile.h" -#include "utility/logging/logging.h" - -TokenLocationLine::TokenLocationLine(TokenLocationFile* file, unsigned int lineNumber) - : m_file(file) - , m_lineNumber(lineNumber) -{ -} - -TokenLocationLine::~TokenLocationLine() -{ -} - -const TokenLocationLine::TokenLocationMapType& TokenLocationLine::getTokenLocations() const -{ - return m_locations; -} - -size_t TokenLocationLine::getTokenLocationCount() const -{ - return m_locations.size(); -} - -TokenLocationFile* TokenLocationLine::getTokenLocationFile() const -{ - return m_file; -} - -const FilePath& TokenLocationLine::getFilePath() const -{ - return m_file->getFilePath(); -} - -unsigned int TokenLocationLine::getLineNumber() const -{ - return m_lineNumber; -} - -TokenLocation* TokenLocationLine::addStartTokenLocation(Id locationId, Id tokenId, unsigned int columnNumber) -{ - std::shared_ptr locationPtr = std::make_shared(locationId, tokenId, this, columnNumber, true); - m_locations.emplace(columnNumber, locationPtr); - return locationPtr.get(); -} - -TokenLocation* TokenLocationLine::addEndTokenLocation(TokenLocation* start, unsigned int columnNumber) -{ - std::shared_ptr locationPtr = std::make_shared(start, this, columnNumber, false); - start->setOtherTokenLocation(locationPtr.get()); - m_locations.emplace(columnNumber, locationPtr); - return locationPtr.get(); -} - -void TokenLocationLine::removeTokenLocation(TokenLocation* location) -{ - TokenLocationMapType::iterator it = m_locations.find(location->getColumnNumber()); - - while (it->first == location->getColumnNumber()) - { - if (it->second.get() == location) - { - m_locations.erase(it); - return; - } - it++; - } - - LOG_ERROR("TokenLocation can't be removed, it's not part of the TokenLocationLine."); -} - -TokenLocation* TokenLocationLine::getTokenLocationById(Id id) const -{ - for (const TokenLocationPairType& p : m_locations) - { - if (p.second->getId() == id) - { - return p.second.get(); - } - } - return nullptr; -} - -void TokenLocationLine::forEachTokenLocation(std::function func) const -{ - for (const TokenLocationPairType& location : m_locations) - { - func(location.second.get()); - } -} - -void TokenLocationLine::forEachStartTokenLocation(std::function func) const -{ - for (const TokenLocationPairType& location : m_locations) - { - if (location.second->isStartTokenLocation()) - { - func(location.second.get()); - } - } -} - -void TokenLocationLine::forEachEndTokenLocation(std::function func) const -{ - for (const TokenLocationPairType& location : m_locations) - { - if (location.second->isEndTokenLocation()) - { - func(location.second.get()); - } - } -} - -TokenLocation* TokenLocationLine::addTokenLocationAsPlainCopy(const TokenLocation* location) -{ - std::shared_ptr locationPtr = std::make_shared(*location, this); - m_locations.emplace(location->getColumnNumber(), locationPtr); - return locationPtr.get(); -} - -std::ostream& operator<<(std::ostream& ostream, const TokenLocationLine& line) -{ - ostream << line.getLineNumber() << ": "; - line.forEachTokenLocation([&ostream](TokenLocation* l) - { - ostream << *l; - }); - return ostream; -} diff --git a/src/lib/data/location/TokenLocationLine.h b/src/lib/data/location/TokenLocationLine.h deleted file mode 100644 index 42d63ce3..00000000 --- a/src/lib/data/location/TokenLocationLine.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef TOKEN_LOCATION_LINE_H -#define TOKEN_LOCATION_LINE_H - -#include -#include -#include -#include -#include - -#include "utility/file/FilePath.h" -#include "utility/types.h" - -class TokenLocation; -class TokenLocationFile; - -class TokenLocationLine -{ -public: - typedef std::multimap > TokenLocationMapType; - typedef std::pair > TokenLocationPairType; - - TokenLocationLine(TokenLocationFile* file, unsigned int lineNumber); - ~TokenLocationLine(); - - const TokenLocationMapType& getTokenLocations() const; - size_t getTokenLocationCount() const; - - TokenLocationFile* getTokenLocationFile() const; - const FilePath& getFilePath() const; - - unsigned int getLineNumber() const; - - TokenLocation* addStartTokenLocation(Id locationId, Id tokenId, unsigned int columnNumber); - TokenLocation* addEndTokenLocation(TokenLocation* start, unsigned int columnNumber); - void removeTokenLocation(TokenLocation* location); - - TokenLocation* getTokenLocationById(Id id) const; - - void forEachTokenLocation(std::function func) const; - void forEachStartTokenLocation(std::function func) const; - void forEachEndTokenLocation(std::function func) const; - - TokenLocation* addTokenLocationAsPlainCopy(const TokenLocation* location); - -private: - TokenLocationMapType m_locations; - - TokenLocationFile* const m_file; - const unsigned int m_lineNumber; -}; - -std::ostream& operator<<(std::ostream& ostream, const TokenLocationLine& line); - -#endif // TOKEN_LOCATION_LINE_H diff --git a/src/lib/data/parser/ParserClientImpl.cpp b/src/lib/data/parser/ParserClientImpl.cpp index 4db23d95..6ecce464 100644 --- a/src/lib/data/parser/ParserClientImpl.cpp +++ b/src/lib/data/parser/ParserClientImpl.cpp @@ -1,11 +1,11 @@ #include "data/parser/ParserClientImpl.h" -#include "data/parser/ParseLocation.h" -#include "data/graph/Node.h" #include "data/graph/Edge.h" +#include "data/graph/Node.h" +#include "data/location/LocationType.h" +#include "data/parser/ParseLocation.h" #include "utility/logging/logging.h" #include "utility/utility.h" -#include "data/location/TokenLocation.h" ParserClientImpl::ParserClientImpl() { diff --git a/src/lib/data/parser/ParserClientImpl.h b/src/lib/data/parser/ParserClientImpl.h index c9dba77c..74e62249 100644 --- a/src/lib/data/parser/ParserClientImpl.h +++ b/src/lib/data/parser/ParserClientImpl.h @@ -3,14 +3,14 @@ #include -#include "data/graph/Node.h" -#include "data/parser/ParserClient.h" -#include "data/IntermediateStorage.h" -#include "data/graph/token_component/TokenComponentAccess.h" - #include "data/DefinitionKind.h" +#include "data/graph/Node.h" +#include "data/graph/token_component/TokenComponentAccess.h" +#include "data/IntermediateStorage.h" +#include "data/parser/ParserClient.h" -class ParserClientImpl: public ParserClient +class ParserClientImpl + : public ParserClient { public: ParserClientImpl(); diff --git a/src/lib/utility/messaging/type/MessageActivateSourceLocations.h b/src/lib/utility/messaging/type/MessageActivateSourceLocations.h new file mode 100644 index 00000000..f17d76e5 --- /dev/null +++ b/src/lib/utility/messaging/type/MessageActivateSourceLocations.h @@ -0,0 +1,32 @@ +#ifndef MESSAGE_ACTIVATE_SOURCE_LOCATIONS_H +#define MESSAGE_ACTIVATE_SOURCE_LOCATIONS_H + +#include "utility/messaging/Message.h" +#include "utility/types.h" + +class MessageActivateSourceLocations + : public Message +{ +public: + MessageActivateSourceLocations(const std::vector& locationIds) + : locationIds(locationIds) + { + } + + static const std::string getStaticType() + { + return "MessageActivateSourceLocations"; + } + + virtual void print(std::ostream& os) const + { + for (const Id& id : locationIds) + { + os << id << " "; + } + } + + const std::vector locationIds; +}; + +#endif // MESSAGE_ACTIVATE_SOURCE_LOCATIONS_H diff --git a/src/lib/utility/messaging/type/MessageActivateTokenLocations.h b/src/lib/utility/messaging/type/MessageActivateTokenLocations.h deleted file mode 100644 index e37f42cf..00000000 --- a/src/lib/utility/messaging/type/MessageActivateTokenLocations.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MESSAGE_ACTIVATE_TOKEN_LOCATIONS_H -#define MESSAGE_ACTIVATE_TOKEN_LOCATIONS_H - -#include "utility/messaging/Message.h" -#include "utility/types.h" - -class MessageActivateTokenLocations - : public Message -{ -public: - MessageActivateTokenLocations(const std::vector& locationIds) - : locationIds(locationIds) - { - } - - static const std::string getStaticType() - { - return "MessageActivateTokenLocations"; - } - - virtual void print(std::ostream& os) const - { - for (const Id& id : locationIds) - { - os << id << " "; - } - } - - const std::vector locationIds; -}; - -#endif // MESSAGE_ACTIVATE_TOKEN_LOCATIONS_H diff --git a/src/lib/utility/utility.h b/src/lib/utility/utility.h index 1948d4de..1faa6f7f 100644 --- a/src/lib/utility/utility.h +++ b/src/lib/utility/utility.h @@ -84,6 +84,14 @@ namespace utility return false; } + template + bool shareElement(const std::set& a, const std::set& b) + { + std::set c(a.begin(), a.end()); + c.insert(b.begin(), b.end()); + return a.size() + b.size() != c.size(); + } + bool intersectionPoint(Vec2f a1, Vec2f b1, Vec2f a2, Vec2f b2, Vec2f* i); size_t digits(size_t n); diff --git a/src/lib_gui/qt/element/QtCodeArea.cpp b/src/lib_gui/qt/element/QtCodeArea.cpp index a545da10..50c94b90 100644 --- a/src/lib_gui/qt/element/QtCodeArea.cpp +++ b/src/lib_gui/qt/element/QtCodeArea.cpp @@ -12,7 +12,7 @@ #include #include "utility/messaging/type/MessageActivateLocalSymbols.h" -#include "utility/messaging/type/MessageActivateTokenLocations.h" +#include "utility/messaging/type/MessageActivateSourceLocations.h" #include "utility/messaging/type/MessageActivateTokenIds.h" #include "utility/messaging/type/MessageFocusIn.h" #include "utility/messaging/type/MessageFocusOut.h" @@ -20,8 +20,8 @@ #include "utility/messaging/type/MessageShowErrors.h" #include "utility/utility.h" -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocation.h" +#include "data/location/SourceLocationFile.h" #include "qt/element/QtCodeNavigator.h" #include "qt/utility/QtContextMenu.h" #include "qt/utility/QtHighlighter.h" @@ -83,7 +83,7 @@ void QtCodeArea::clearAnnotationColors() QtCodeArea::QtCodeArea( uint startLineNumber, const std::string& code, - std::shared_ptr locationFile, + std::shared_ptr locationFile, QtCodeNavigator* navigator, QWidget* parent ) @@ -197,7 +197,7 @@ uint QtCodeArea::getEndLineNumber() const return m_startLineNumber + blockCount() - 1; } -std::shared_ptr QtCodeArea::getTokenLocationFile() const +std::shared_ptr QtCodeArea::getSourceLocationFile() const { return m_locationFile; } @@ -301,7 +301,7 @@ uint QtCodeArea::getStartLineNumberOfFirstActiveLocationOfTokenId(Id tokenId) co { if (annotation.locationType == LocationType::LOCATION_TOKEN && annotation.isActive) { - if (annotation.tokenId == tokenId) + if (annotation.tokenIds.find(tokenId) != annotation.tokenIds.end()) { if (!firstActiveLine || firstActiveLine == annotation.startLine) { @@ -326,12 +326,10 @@ Id QtCodeArea::getLocationIdOfFirstActiveLocationOfTokenId(Id tokenId) const { for (const Annotation& annotation : m_annotations) { - if (annotation.locationType == LocationType::LOCATION_TOKEN && annotation.isActive) + if (annotation.locationType == LocationType::LOCATION_TOKEN && annotation.isActive && + annotation.tokenIds.find(tokenId) != annotation.tokenIds.end()) { - if (annotation.tokenId == tokenId) - { - return annotation.locationId; - } + return annotation.locationId; } } @@ -525,7 +523,7 @@ void QtCodeArea::mouseReleaseEvent(QMouseEvent* event) } else { - activateTokenLocations(annotations); + activateSourceLocations(annotations); activateLocalSymbols(annotations); } } @@ -577,9 +575,9 @@ void QtCodeArea::mouseMoveEvent(QMouseEvent* event) setHoveredAnnotations(annotations); - if (m_navigator->hasErrors() && annotations.size() == 1) + if (m_navigator->hasErrors() && annotations.size() == 1 && annotations[0]->tokenIds.size()) { - std::string errorMessage = m_navigator->getErrorMessageForId(annotations[0]->tokenId); + std::string errorMessage = m_navigator->getErrorMessageForId(*annotations[0]->tokenIds.begin()); QToolTip::showText(event->globalPos(), QString::fromStdString(errorMessage)); } } @@ -681,9 +679,9 @@ std::vector QtCodeArea::getInteractiveAnnotations return annotations; } -void QtCodeArea::activateTokenLocations(const std::vector& annotations) +void QtCodeArea::activateSourceLocations(const std::vector& annotations) { - std::vector tokenLocationIds; + std::vector locationIds; std::set tokenIds; bool allActive = true; @@ -698,20 +696,21 @@ void QtCodeArea::activateTokenLocations(const std::vector& an if (annotation->locationId > 0) { - tokenLocationIds.push_back(annotation->locationId); + locationIds.push_back(annotation->locationId); } - if (annotation->tokenId > 0) + + if (annotation->tokenIds.size()) { - tokenIds.insert(annotation->tokenId); + tokenIds.insert(annotation->tokenIds.begin(), annotation->tokenIds.end()); } } } if (!allActive) { - if (tokenLocationIds.size()) + if (locationIds.size()) { - MessageActivateTokenLocations(tokenLocationIds).dispatch(); + MessageActivateSourceLocations(locationIds).dispatch(); } else if (tokenIds.size()) // fallback for links in project description { @@ -734,9 +733,9 @@ void QtCodeArea::activateLocalSymbols(const std::vector& anno allActive = false; } - if (annotation->tokenId > 0) + if (annotation->tokenIds.size()) { - localSymbolIds.push_back(annotation->tokenId); + localSymbolIds.insert(localSymbolIds.end(), annotation->tokenIds.begin(), annotation->tokenIds.end()); } } } @@ -752,9 +751,9 @@ void QtCodeArea::activateErrors(const std::vector& annotation std::vector errorIds; for (const Annotation* annotation : annotations) { - if (annotation->locationType == LOCATION_ERROR && annotation->tokenId > 0) + if (annotation->locationType == LOCATION_ERROR && annotation->tokenIds.size()) { - errorIds.push_back(annotation->tokenId); + errorIds.insert(errorIds.end(), annotation->tokenIds.begin(), annotation->tokenIds.end()); } } @@ -764,58 +763,61 @@ void QtCodeArea::activateErrors(const std::vector& annotation } } -void QtCodeArea::createAnnotations(std::shared_ptr locationFile) +void QtCodeArea::createAnnotations(std::shared_ptr locationFile) { - locationFile->forEachStartTokenLocation( - [&](TokenLocation* startLocation) + uint endLineNumber = getEndLineNumber(); + std::set locationIds; + + locationFile->forEachSourceLocation( + [&](const SourceLocation* location) { + if (locationIds.find(location->getLocationId()) != locationIds.end()) + { + return; + } + locationIds.insert(location->getLocationId()); + Annotation annotation; - uint endLineNumber = getEndLineNumber(); - if (startLocation->getLineNumber() <= endLineNumber) + + const SourceLocation* startLocation = location->getStartLocation(); + if (!startLocation || startLocation->getLineNumber() < m_startLineNumber) { - if (startLocation->getLineNumber() < m_startLineNumber) - { - annotation.start = startTextEditPosition(); - annotation.startLine = m_startLineNumber; - annotation.startCol = 0; - } - else - { - annotation.start = toTextEditPosition(startLocation->getLineNumber(), startLocation->getColumnNumber() - 1); - annotation.startLine = startLocation->getLineNumber(); - annotation.startCol = startLocation->getColumnNumber() - 1; - } + annotation.start = startTextEditPosition(); + annotation.startLine = m_startLineNumber; + annotation.startCol = 0; + } + else if (startLocation->getLineNumber() <= endLineNumber) + { + annotation.start = toTextEditPosition(startLocation->getLineNumber(), startLocation->getColumnNumber() - 1); + annotation.startLine = startLocation->getLineNumber(); + annotation.startCol = startLocation->getColumnNumber() - 1; } else { return; } - TokenLocation* endLocation = startLocation->getEndTokenLocation(); - if (endLocation->getLineNumber() >= m_startLineNumber) + const SourceLocation* endLocation = location->getEndLocation(); + if (!endLocation || endLocation->getLineNumber() > endLineNumber) { - if (endLocation->getLineNumber() > endLineNumber) - { - annotation.end = endTextEditPosition(); - annotation.endLine = endLineNumber; - annotation.endCol = m_lineLengths[document()->blockCount() - 1]; - } - else - { - annotation.end = toTextEditPosition(endLocation->getLineNumber(), endLocation->getColumnNumber()); - annotation.endLine = endLocation->getLineNumber(); - annotation.endCol = endLocation->getColumnNumber(); - } + annotation.end = endTextEditPosition(); + annotation.endLine = endLineNumber; + annotation.endCol = m_lineLengths[document()->blockCount() - 1]; + } + else if (endLocation->getLineNumber() >= m_startLineNumber) + { + annotation.end = toTextEditPosition(endLocation->getLineNumber(), endLocation->getColumnNumber()); + annotation.endLine = endLocation->getLineNumber(); + annotation.endCol = endLocation->getColumnNumber(); } else { return; } - annotation.tokenId = startLocation->getTokenId(); - annotation.locationId = startLocation->getId(); - - annotation.locationType = startLocation->getType(); + annotation.tokenIds.insert(location->getTokenIds().begin(), location->getTokenIds().end()); + annotation.locationId = location->getLocationId(); + annotation.locationType = location->getType(); annotation.isActive = false; annotation.isFocused = false; @@ -827,12 +829,12 @@ void QtCodeArea::createAnnotations(std::shared_ptr locationFi void QtCodeArea::annotateText() { - const std::vector& currentActiveTokenIds = m_navigator->getCurrentActiveTokenIds(); - const std::vector& currentActiveLocationIds = m_navigator->getCurrentActiveLocationIds(); + const std::set& currentActiveTokenIds = m_navigator->getCurrentActiveTokenIds(); + const std::set& currentActiveLocationIds = m_navigator->getCurrentActiveLocationIds(); - const std::vector& activeTokenIds = m_navigator->getActiveTokenIds(); - const std::vector& activeLocalSymbolIds = m_navigator->getActiveLocalSymbolIds(); - const std::vector& focusIds = m_navigator->getFocusedTokenIds(); + const std::set& activeTokenIds = m_navigator->getActiveTokenIds(); + const std::set& activeLocalSymbolIds = m_navigator->getActiveLocalSymbolIds(); + const std::set& focusIds = m_navigator->getFocusedTokenIds(); bool needsUpdate = false; for (Annotation& annotation: m_annotations) @@ -842,15 +844,16 @@ void QtCodeArea::annotateText() const AnnotationColor& oldColor = getAnnotationColorForAnnotation(annotation); annotation.isActive = ( - std::find(currentActiveTokenIds.begin(), currentActiveTokenIds.end(), annotation.tokenId) != currentActiveTokenIds.end() || - std::find(currentActiveLocationIds.begin(), currentActiveLocationIds.end(), annotation.locationId) != currentActiveLocationIds.end() || - std::find(activeLocalSymbolIds.begin(), activeLocalSymbolIds.end(), annotation.tokenId) != activeLocalSymbolIds.end() + utility::shareElement(currentActiveTokenIds, annotation.tokenIds) || + utility::shareElement(activeLocalSymbolIds, annotation.tokenIds) || + currentActiveLocationIds.find(annotation.locationId) != currentActiveLocationIds.end() ); + if (!annotation.isActive) { annotation.isFocused = ( - std::find(focusIds.begin(), focusIds.end(), annotation.tokenId) != focusIds.end() || - std::find(activeTokenIds.begin(), activeTokenIds.end(), annotation.tokenId) != activeTokenIds.end() + utility::shareElement(focusIds, annotation.tokenIds) || + utility::shareElement(activeTokenIds, annotation.tokenIds) ); } @@ -862,7 +865,7 @@ void QtCodeArea::annotateText() bool isDuplicateAnnotation = false; for (Annotation* a : m_colorChangedAnnotations) { - if (a->start == annotation.start && a->end == annotation.end && a->tokenId != annotation.tokenId) + if (a->start == annotation.start && a->end == annotation.end && a->locationId != annotation.locationId) { isDuplicateAnnotation = true; break; @@ -909,7 +912,7 @@ void QtCodeArea::setHoveredAnnotations(const std::vector& ann std::vector tokenIds; for (const Annotation* annotation : m_hoveredAnnotations) { - tokenIds.push_back(annotation->tokenId); + tokenIds.insert(tokenIds.end(), annotation->tokenIds.begin(), annotation->tokenIds.end()); } MessageFocusOut(tokenIds).dispatch(); @@ -922,7 +925,7 @@ void QtCodeArea::setHoveredAnnotations(const std::vector& ann std::vector tokenIds; for (const Annotation* annotation : annotations) { - tokenIds.push_back(annotation->tokenId); + tokenIds.insert(tokenIds.end(), annotation->tokenIds.begin(), annotation->tokenIds.end()); } MessageFocusIn(tokenIds).dispatch(); diff --git a/src/lib_gui/qt/element/QtCodeArea.h b/src/lib_gui/qt/element/QtCodeArea.h index e88f732e..ab6d3ebc 100644 --- a/src/lib_gui/qt/element/QtCodeArea.h +++ b/src/lib_gui/qt/element/QtCodeArea.h @@ -18,8 +18,8 @@ class QSize; class QtCodeNavigator; class QtHighlighter; class QWidget; -class TokenLocation; -class TokenLocationFile; +class SourceLocation; +class SourceLocationFile; class MouseWheelOverScrollbarFilter @@ -62,7 +62,7 @@ public: QtCodeArea( uint startLineNumber, const std::string& code, - std::shared_ptr locationFile, + std::shared_ptr locationFile, QtCodeNavigator* navigator, QWidget* parent = nullptr ); @@ -73,7 +73,7 @@ public: uint getStartLineNumber() const; uint getEndLineNumber() const; - std::shared_ptr getTokenLocationFile() const; + std::shared_ptr getSourceLocationFile() const; void lineNumberAreaPaintEvent(QPaintEvent* event); int lineNumberDigits() const; @@ -127,7 +127,7 @@ private: int start; int end; - Id tokenId; + std::set tokenIds; Id locationId; LocationType locationType; @@ -146,11 +146,11 @@ private: }; std::vector getInteractiveAnnotationsForPosition(int pos) const; - void activateTokenLocations(const std::vector& annotations); + void activateSourceLocations(const std::vector& annotations); void activateLocalSymbols(const std::vector& annotations); void activateErrors(const std::vector& annotations); - void createAnnotations(std::shared_ptr locationFile); + void createAnnotations(std::shared_ptr locationFile); void annotateText(); void setHoveredAnnotations(const std::vector& annotations); @@ -180,7 +180,7 @@ private: const uint m_startLineNumber; const std::string m_code; - std::shared_ptr m_locationFile; + std::shared_ptr m_locationFile; std::vector m_annotations; std::vector m_hoveredAnnotations; diff --git a/src/lib_gui/qt/element/QtCodeFile.cpp b/src/lib_gui/qt/element/QtCodeFile.cpp index d9ae677e..12abf3de 100644 --- a/src/lib_gui/qt/element/QtCodeFile.cpp +++ b/src/lib_gui/qt/element/QtCodeFile.cpp @@ -6,7 +6,7 @@ #include "utility/messaging/type/MessageChangeFileView.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocationFile.h" #include "qt/element/QtCodeFileTitleButton.h" #include "qt/element/QtCodeNavigator.h" #include "qt/element/QtCodeSnippet.h" @@ -120,7 +120,7 @@ QtCodeSnippet* QtCodeFile::addCodeSnippet(const CodeSnippetParams& params) m_snippetLayout->addWidget(snippet.get()); - if (params.locationFile->isWholeCopy) + if (params.locationFile->isWhole()) { snippet->setProperty("isFirst", true); snippet->setProperty("isLast", true); @@ -186,7 +186,6 @@ QtCodeSnippet* QtCodeFile::insertCodeSnippet(const CodeSnippetParams& params) m_snippets.insert(m_snippets.begin() + i, snippet); setSnippets(); - updateRefCount(params.refCount); return snippet.get(); } diff --git a/src/lib_gui/qt/element/QtCodeFileList.cpp b/src/lib_gui/qt/element/QtCodeFileList.cpp index 4d6e6dd3..bddd14e6 100644 --- a/src/lib_gui/qt/element/QtCodeFileList.cpp +++ b/src/lib_gui/qt/element/QtCodeFileList.cpp @@ -5,7 +5,7 @@ #include "utility/file/FileSystem.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocationFile.h" #include "qt/element/QtCodeFile.h" #include "qt/element/QtCodeNavigator.h" #include "qt/element/QtCodeSnippet.h" diff --git a/src/lib_gui/qt/element/QtCodeFileSingle.cpp b/src/lib_gui/qt/element/QtCodeFileSingle.cpp index 424f8612..3e536878 100644 --- a/src/lib_gui/qt/element/QtCodeFileSingle.cpp +++ b/src/lib_gui/qt/element/QtCodeFileSingle.cpp @@ -10,7 +10,7 @@ #include "utility/messaging/type/MessageChangeFileView.h" #include "utility/ResourcePaths.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocationFile.h" #include "qt/element/QtCodeArea.h" #include "qt/element/QtCodeFileTitleButton.h" #include "qt/element/QtCodeNavigator.h" @@ -86,7 +86,7 @@ void QtCodeFileSingle::clearCache() void QtCodeFileSingle::addCodeSnippet(const CodeSnippetParams& params, bool insert) { - if (!params.locationFile->isWholeCopy) + if (!params.locationFile->isWhole()) { LOG_ERROR("Snippet params passed are not for whole file."); return; diff --git a/src/lib_gui/qt/element/QtCodeNavigator.cpp b/src/lib_gui/qt/element/QtCodeNavigator.cpp index 22fa50bf..c562478c 100644 --- a/src/lib_gui/qt/element/QtCodeNavigator.cpp +++ b/src/lib_gui/qt/element/QtCodeNavigator.cpp @@ -12,9 +12,9 @@ #include "utility/messaging/type/MessageScrollCode.h" #include "utility/ResourcePaths.h" -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationCollection.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocation.h" +#include "data/location/SourceLocationCollection.h" +#include "data/location/SourceLocationFile.h" #include "qt/element/QtCodeFile.h" #include "qt/element/QtCodeSnippet.h" #include "qt/utility/QtDeviceScaledPixmap.h" @@ -25,6 +25,7 @@ QtCodeNavigator::QtCodeNavigator(QWidget* parent) : QWidget(parent) , m_mode(MODE_NONE) + , m_activeTokenId(0) , m_value(0) , m_refIndex(0) , m_singleHasNewFile(false) @@ -149,34 +150,33 @@ void QtCodeNavigator::addCodeSnippet(const CodeSnippetParams& params, bool inser } } -void QtCodeNavigator::addFile(std::shared_ptr locationFile, int refCount, TimePoint modificationTime) +void QtCodeNavigator::addFile(std::shared_ptr locationFile, int refCount, TimePoint modificationTime) { - m_list->addFile(locationFile->getFilePath(), locationFile->isWholeCopy, refCount, modificationTime); + m_list->addFile(locationFile->getFilePath(), locationFile->isWhole(), refCount, modificationTime); - if (locationFile->isWholeCopy) + if (locationFile->isWhole()) { Reference ref; ref.filePath = locationFile->getFilePath(); - ref.tokenId = 0; - ref.locationId = 0; - ref.locationType = LOCATION_TOKEN; - m_references.push_back(ref); } else { - locationFile->forEachStartTokenLocation( - [&](TokenLocation* location) + locationFile->forEachStartSourceLocation( + [&](SourceLocation* location) { - if (!location->isScopeTokenLocation()) + if (!location->isScopeLocation()) { - Reference ref; - ref.filePath = location->getFilePath(); - ref.tokenId = location->getTokenId(); - ref.locationId = location->getId(); - ref.locationType = location->getType(); + for (Id i : location->getTokenIds()) + { + Reference ref; + ref.filePath = location->getFilePath(); + ref.tokenId = i; + ref.locationId = location->getLocationId(); + ref.locationType = location->getType(); - m_references.push_back(ref); + m_references.push_back(ref); + } } } ); @@ -207,6 +207,8 @@ void QtCodeNavigator::clearCodeSnippets() m_focusedTokenIds.clear(); m_errorInfos.clear(); + m_activeTokenId = 0; + if (m_references.size() && m_references[0].locationType != LOCATION_TOKEN) { clearCaches(); @@ -224,29 +226,29 @@ void QtCodeNavigator::clearCaches() m_single->clearCache(); } -const std::vector& QtCodeNavigator::getCurrentActiveTokenIds() const +const std::set& QtCodeNavigator::getCurrentActiveTokenIds() const { return m_currentActiveTokenIds; } void QtCodeNavigator::setCurrentActiveTokenIds(const std::vector& currentActiveTokenIds) { - m_currentActiveTokenIds = currentActiveTokenIds; + m_currentActiveTokenIds = std::set(currentActiveTokenIds.begin(), currentActiveTokenIds.end()); m_currentActiveLocationIds.clear(); } -const std::vector& QtCodeNavigator::getCurrentActiveLocationIds() const +const std::set& QtCodeNavigator::getCurrentActiveLocationIds() const { return m_currentActiveLocationIds; } void QtCodeNavigator::setCurrentActiveLocationIds(const std::vector& currentActiveLocationIds) { - m_currentActiveLocationIds = currentActiveLocationIds; + m_currentActiveLocationIds = std::set(currentActiveLocationIds.begin(), currentActiveLocationIds.end()); m_currentActiveTokenIds.clear(); } -const std::vector& QtCodeNavigator::getActiveTokenIds() const +const std::set& QtCodeNavigator::getActiveTokenIds() const { return m_activeTokenIds; } @@ -255,28 +257,30 @@ void QtCodeNavigator::setActiveTokenIds(const std::vector& activeTokenIds) { setCurrentActiveTokenIds(activeTokenIds); - m_activeTokenIds = activeTokenIds; + m_activeTokenIds = std::set(activeTokenIds.begin(), activeTokenIds.end()); + m_activeTokenId = activeTokenIds.size() ? activeTokenIds[0] : 0; + m_activeLocalSymbolIds.clear(); } -const std::vector& QtCodeNavigator::getActiveLocalSymbolIds() const +const std::set& QtCodeNavigator::getActiveLocalSymbolIds() const { return m_activeLocalSymbolIds; } void QtCodeNavigator::setActiveLocalSymbolIds(const std::vector& activeLocalSymbolIds) { - m_activeLocalSymbolIds = activeLocalSymbolIds; + m_activeLocalSymbolIds = std::set(activeLocalSymbolIds.begin(), activeLocalSymbolIds.end()); } -const std::vector& QtCodeNavigator::getFocusedTokenIds() const +const std::set& QtCodeNavigator::getFocusedTokenIds() const { return m_focusedTokenIds; } void QtCodeNavigator::setFocusedTokenIds(const std::vector& focusedTokenIds) { - m_focusedTokenIds = focusedTokenIds; + m_focusedTokenIds = std::set(focusedTokenIds.begin(), focusedTokenIds.end()); } std::string QtCodeNavigator::getErrorMessageForId(Id errorId) const @@ -326,7 +330,7 @@ bool QtCodeNavigator::isInListMode() const } void QtCodeNavigator::showActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) { if (activeTokenIds.size() != 1) { @@ -363,21 +367,31 @@ void QtCodeNavigator::showActiveSnippet( std::set filePathsToExpand; if (!locationIds.size()) { - collection->forEachTokenLocation( - [&](TokenLocation* location) + collection->forEachSourceLocation( + [&](SourceLocation* location) { - if (location->getTokenId() != tokenId) + bool foundId = false; + for (Id i : location->getTokenIds()) + { + if (i == tokenId) + { + foundId = true; + break; + } + } + + if (!foundId) { return; } - locationIds.push_back(location->getId()); + locationIds.push_back(location->getLocationId()); filePathsToExpand.insert(location->getFilePath()); if (!firstReference.tokenId || filePathOrder[location->getFilePath()] < filePathOrder[firstReference.filePath]) { - firstReference.tokenId = location->getTokenId(); - firstReference.locationId = location->getId(); + firstReference.tokenId = tokenId; + firstReference.locationId = location->getLocationId(); firstReference.filePath = location->getFilePath(); } } @@ -534,6 +548,7 @@ void QtCodeNavigator::scrollToValue(int value, bool inListMode) void QtCodeNavigator::scrollToLine(const FilePath& filePath, unsigned int line) { requestScroll(filePath, line, 0, false, false); + emit scrollRequest(); } void QtCodeNavigator::scrollToDefinition(bool ignoreActiveReference) @@ -565,16 +580,14 @@ void QtCodeNavigator::scrollToDefinition(bool ignoreActiveReference) return; } - if (!m_activeTokenIds.size()) + if (!m_activeTokenId) { return; } - Id tokenId = m_activeTokenIds[0]; // The first active tokenId is the one of the active symbol itself. - if (m_mode == MODE_LIST) { - std::pair result = m_list->getFirstSnippetWithActiveLocation(tokenId); + std::pair result = m_list->getFirstSnippetWithActiveLocation(m_activeTokenId); if (result.first != nullptr) { requestScroll(result.first->getFile()->getFilePath(), result.second, 0, false, true); @@ -583,7 +596,7 @@ void QtCodeNavigator::scrollToDefinition(bool ignoreActiveReference) } else { - Id locationId = m_single->getLocationIdOfFirstActiveLocationOfTokenId(tokenId); + Id locationId = m_single->getLocationIdOfFirstActiveLocationOfTokenId(m_activeTokenId); if (locationId) { for (size_t i = 0; i < m_references.size(); i++) @@ -630,6 +643,9 @@ void QtCodeNavigator::requestScroll(const FilePath& filePath, uint lineNumber, I } } + // std::cout << "scroll request: " << req.filePath.str() << " " << req.lineNumber << " " << req.locationId; + // std::cout << " " << req.animated << " " << req.onTop << std::endl; + m_scrollRequest = req; m_singleHasNewFile = false; diff --git a/src/lib_gui/qt/element/QtCodeNavigator.h b/src/lib_gui/qt/element/QtCodeNavigator.h index 1c966207..52c6bffd 100644 --- a/src/lib_gui/qt/element/QtCodeNavigator.h +++ b/src/lib_gui/qt/element/QtCodeNavigator.h @@ -17,8 +17,8 @@ class QLabel; class QPushButton; -class TokenLocationCollection; -class TokenLocationFile; +class SourceLocationCollection; +class SourceLocationFile; class QtCodeNavigator : public QWidget @@ -35,7 +35,7 @@ public: virtual ~QtCodeNavigator(); void addCodeSnippet(const CodeSnippetParams& params, bool insert = false); - void addFile(std::shared_ptr locationFile, int refCount, TimePoint modificationTime); + void addFile(std::shared_ptr locationFile, int refCount, TimePoint modificationTime); void addedFiles(); @@ -43,19 +43,19 @@ public: void clearCodeSnippets(); void clearCaches(); - const std::vector& getCurrentActiveTokenIds() const; + const std::set& getCurrentActiveTokenIds() const; void setCurrentActiveTokenIds(const std::vector& currentActiveTokenIds); - const std::vector& getCurrentActiveLocationIds() const; + const std::set& getCurrentActiveLocationIds() const; void setCurrentActiveLocationIds(const std::vector& currentActiveLocationIds); - const std::vector& getActiveTokenIds() const; + const std::set& getActiveTokenIds() const; void setActiveTokenIds(const std::vector& activeTokenIds); - const std::vector& getActiveLocalSymbolIds() const; + const std::set& getActiveLocalSymbolIds() const; void setActiveLocalSymbolIds(const std::vector& activeLocalSymbolIds); - const std::vector& getFocusedTokenIds() const; + const std::set& getFocusedTokenIds() const; void setFocusedTokenIds(const std::vector& focusedTokenIds); std::string getErrorMessageForId(Id errorId) const; @@ -67,7 +67,7 @@ public: bool isInListMode() const; void showActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo); + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo); void focusTokenIds(const std::vector& focusedTokenIds); void defocusTokenIds(); @@ -119,6 +119,7 @@ private: Reference() : tokenId(0) , locationId(0) + , locationType(LOCATION_TOKEN) { } @@ -163,14 +164,16 @@ private: Mode m_mode; - std::vector m_currentActiveTokenIds; - std::vector m_currentActiveLocationIds; + std::set m_currentActiveTokenIds; + std::set m_currentActiveLocationIds; - std::vector m_activeTokenIds; - std::vector m_activeLocalSymbolIds; - std::vector m_focusedTokenIds; + std::set m_activeTokenIds; + std::set m_activeLocalSymbolIds; + std::set m_focusedTokenIds; std::map m_errorInfos; + Id m_activeTokenId; + int m_value; QPushButton* m_listButton; diff --git a/src/lib_gui/qt/element/QtCodeSnippet.cpp b/src/lib_gui/qt/element/QtCodeSnippet.cpp index 148eee7a..025221d2 100644 --- a/src/lib_gui/qt/element/QtCodeSnippet.cpp +++ b/src/lib_gui/qt/element/QtCodeSnippet.cpp @@ -7,7 +7,7 @@ #include "utility/messaging/type/MessageShowScope.h" #include "utility/text/TextAccess.h" -#include "data/location/TokenLocationFile.h" +#include "data/location/SourceLocationFile.h" #include "qt/element/QtCodeNavigator.h" #include "qt/element/QtCodeFile.h" @@ -17,22 +17,22 @@ std::shared_ptr QtCodeSnippet::merged( QtCodeSnippet* first = a->getStartLineNumber() < b->getStartLineNumber() ? a : b; QtCodeSnippet* second = a->getStartLineNumber() > b->getStartLineNumber() ? a : b; - TokenLocationFile* aFile = a->m_codeArea->getTokenLocationFile().get(); - TokenLocationFile* bFile = b->m_codeArea->getTokenLocationFile().get(); + SourceLocationFile* aFile = a->m_codeArea->getSourceLocationFile().get(); + SourceLocationFile* bFile = b->m_codeArea->getSourceLocationFile().get(); - std::shared_ptr locationFile = std::make_shared(aFile->getFilePath()); + std::shared_ptr locationFile = std::make_shared(aFile->getFilePath(), aFile->isWhole()); - aFile->forEachTokenLocation( - [&locationFile](TokenLocation* loc) + aFile->forEachSourceLocation( + [&locationFile](SourceLocation* loc) { - locationFile->addTokenLocationAsPlainCopy(loc); + locationFile->addSourceLocationCopy(loc); } ); - bFile->forEachTokenLocation( - [&locationFile](TokenLocation* loc) + bFile->forEachSourceLocation( + [&locationFile](SourceLocation* loc) { - locationFile->addTokenLocationAsPlainCopy(loc); + locationFile->addSourceLocationCopy(loc); } ); diff --git a/src/lib_gui/qt/element/QtCodeSnippet.h b/src/lib_gui/qt/element/QtCodeSnippet.h index de3ef39e..a941dc52 100644 --- a/src/lib_gui/qt/element/QtCodeSnippet.h +++ b/src/lib_gui/qt/element/QtCodeSnippet.h @@ -16,7 +16,7 @@ class QBoxLayout; class QPushButton; class QtCodeFile; class QtCodeNavigator; -class TokenLocationFile; +class SourceLocationFile; class QtCodeSnippet : public QFrame diff --git a/src/lib_gui/qt/view/QtCodeView.cpp b/src/lib_gui/qt/view/QtCodeView.cpp index 7bd796df..c1256aa6 100644 --- a/src/lib_gui/qt/view/QtCodeView.cpp +++ b/src/lib_gui/qt/view/QtCodeView.cpp @@ -105,7 +105,7 @@ void QtCodeView::setFileState(const FilePath filePath, FileState state) } void QtCodeView::showActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) { m_doShowActiveSnippetFunctor(activeTokenIds, collection, scrollTo); } @@ -241,7 +241,7 @@ void QtCodeView::doSetFileState(const FilePath filePath, FileState state) } void QtCodeView::doShowActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo) { m_widget->showActiveSnippet(activeTokenIds, collection, scrollTo); } diff --git a/src/lib_gui/qt/view/QtCodeView.h b/src/lib_gui/qt/view/QtCodeView.h index 7449d88a..dd988dbb 100644 --- a/src/lib_gui/qt/view/QtCodeView.h +++ b/src/lib_gui/qt/view/QtCodeView.h @@ -39,7 +39,7 @@ public: virtual void setFileState(const FilePath filePath, FileState state); virtual void showActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo); + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo); virtual void showActiveTokenIds(const std::vector& activeTokenIds); virtual void showActiveLocalSymbolIds(const std::vector& activeLocalSymbolIds); @@ -62,7 +62,7 @@ private: void doSetFileState(const FilePath filePath, FileState state); void doShowActiveSnippet( - const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo); + const std::vector& activeTokenIds, std::shared_ptr collection, bool scrollTo); void doShowActiveTokenIds(const std::vector& activeTokenIds); void doShowActiveLocalSymbolIds(const std::vector& localSymbolIds); @@ -73,7 +73,7 @@ private: QtThreadedFunctor&, const std::vector&, bool> m_showCodeSnippetsFunctor; QtThreadedFunctor&, bool> m_addCodeSnippetsFunctor; QtThreadedFunctor m_setFileStateFunctor; - QtThreadedFunctor&, std::shared_ptr, bool> m_doShowActiveSnippetFunctor; + QtThreadedFunctor&, std::shared_ptr, bool> m_doShowActiveSnippetFunctor; QtThreadedFunctor&> m_doShowActiveTokenIdsFunctor; QtThreadedFunctor&> m_doShowActiveLocalSymbolIdsFunctor; QtThreadedFunctor&> m_focusTokenIdsFunctor; diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0bf1f20b..b0024211 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -23,11 +23,11 @@ add_files( SettingsTestSuite.h SettingsMigratorTestSuite.h SearchIndexTestSuite.h + SourceLocationCollectionTestSuite.h SqliteStorageTestSuite.h StorageTestSuite.h TaskSchedulerTestSuite.h TextAccessTestSuite.h - TokenLocationCollectionTestSuite.h UtilityStringTestSuite.h Vector2TestSuite.h JavaParserTestSuite.h diff --git a/src/test/SourceLocationCollectionTestSuite.h b/src/test/SourceLocationCollectionTestSuite.h new file mode 100644 index 00000000..d133fd49 --- /dev/null +++ b/src/test/SourceLocationCollectionTestSuite.h @@ -0,0 +1,170 @@ +#include "cxxtest/TestSuite.h" + +#include "data/location/SourceLocation.h" +#include "data/location/SourceLocationCollection.h" +#include "data/location/SourceLocationFile.h" + +class SourceLocationCollectionTestSuite : public CxxTest::TestSuite +{ +public: + void test_source_locations_get_created_with_other_end() + { + SourceLocationCollection collection; + const SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5); + + TS_ASSERT(a); + TS_ASSERT(a->isStartLocation()); + TS_ASSERT(!a->isEndLocation()); + + const SourceLocation* b = a->getOtherLocation(); + + TS_ASSERT(b); + TS_ASSERT(!b->isStartLocation()); + TS_ASSERT(b->isEndLocation()); + + TS_ASSERT_EQUALS(a, b->getOtherLocation()); + TS_ASSERT_EQUALS(a, b->getStartLocation()); + TS_ASSERT_EQUALS(a, a->getStartLocation()); + TS_ASSERT_EQUALS(b, a->getEndLocation()); + TS_ASSERT_EQUALS(b, b->getEndLocation()); + } + + void test_source_locations_do_not_get_created_with_wrong_input() + { + SourceLocationCollection collection; + SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 2, 1); + SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {1}, "file.c", 4, 1, 1, 10); + + TS_ASSERT(!a); + TS_ASSERT(!b); + } + + void test_source_locations_get_unique_id_but_both_ends_have_the_same() + { + SourceLocationCollection collection; + SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 1, 1, 1, 1); + SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {2}, "file.c", 1, 1, 1, 1); + SourceLocation* c = collection.addSourceLocation(LOCATION_TOKEN, 3, {3}, "file.c", 1, 1, 1, 1); + + TS_ASSERT_EQUALS(1, collection.getSourceLocationFileCount()); + TS_ASSERT_EQUALS(3, collection.getSourceLocationCount()); + + TS_ASSERT_EQUALS(a->getLocationId(), 1); + TS_ASSERT_EQUALS(b->getLocationId(), 2); + TS_ASSERT_EQUALS(c->getLocationId(), 3); + + TS_ASSERT_DIFFERS(a->getLocationId(), b->getLocationId()); + TS_ASSERT_DIFFERS(b->getLocationId(), c->getLocationId()); + TS_ASSERT_DIFFERS(c->getLocationId(), a->getLocationId()); + + TS_ASSERT_EQUALS(a->getLocationId(), a->getOtherLocation()->getLocationId()); + TS_ASSERT_EQUALS(b->getLocationId(), b->getOtherLocation()->getLocationId()); + TS_ASSERT_EQUALS(c->getLocationId(), c->getOtherLocation()->getLocationId()); + } + + void test_source_locations_have_right_file_path_line_column_and_token_id() + { + SourceLocationCollection collection; + SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5); + + TS_ASSERT_EQUALS(1, a->getTokenIds()[0]); + TS_ASSERT_EQUALS(2, a->getLineNumber()); + TS_ASSERT_EQUALS(3, a->getColumnNumber()); + TS_ASSERT_EQUALS(4, a->getOtherLocation()->getLineNumber()); + TS_ASSERT_EQUALS(5, a->getOtherLocation()->getColumnNumber()); + TS_ASSERT_EQUALS("file.c", a->getFilePath().str()); + } + + void test_finding_source_locations_by_id() + { + SourceLocationCollection collection; + SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5); + SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {6}, "file.c", 7, 8, 9, 10); + + TS_ASSERT_EQUALS(a, collection.getSourceLocationById(a->getLocationId())); + TS_ASSERT_EQUALS(b, collection.getSourceLocationById(b->getLocationId())); + } + + void test_creating_plain_copy_of_all_locations_in_line_range() + { + SourceLocationCollection collection; + SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5); + SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {1}, "file.c", 3, 3, 4, 5); + SourceLocation* c = collection.addSourceLocation(LOCATION_TOKEN, 3, {1}, "file.c", 1, 3, 5, 5); + SourceLocation* d = collection.addSourceLocation(LOCATION_TOKEN, 4, {1}, "file.c", 1, 5, 4, 5); + + Id ida = a->getLocationId(); + Id idb = b->getLocationId(); + Id idc = c->getLocationId(); + Id idd = d->getLocationId(); + + unsigned int fromLine = 2; + unsigned int toLine = 4; + + SourceLocationCollection copy; + SourceLocation* x = collection.getSourceLocationById(ida); + + x->getSourceLocationFile()->forEachSourceLocation( + [©, fromLine, toLine](SourceLocation* location) + { + if (location->getLineNumber() >= fromLine && location->getLineNumber() <= toLine) + { + copy.addSourceLocationCopy(location); + } + } + ); + + TS_ASSERT_EQUALS(1, copy.getSourceLocationFileCount()); + TS_ASSERT_EQUALS(3, copy.getSourceLocationCount()); + + TS_ASSERT(copy.getSourceLocationById(ida)); + TS_ASSERT(copy.getSourceLocationById(idb)); + TS_ASSERT(!copy.getSourceLocationById(idc)); + TS_ASSERT(copy.getSourceLocationById(idd)); + + TS_ASSERT_DIFFERS(a, copy.getSourceLocationById(ida)); + TS_ASSERT_DIFFERS(d, copy.getSourceLocationById(idd)); + + TS_ASSERT(copy.getSourceLocationById(ida)->getStartLocation()); + TS_ASSERT(copy.getSourceLocationById(ida)->getEndLocation()); + + TS_ASSERT(!copy.getSourceLocationById(idd)->getStartLocation()); + TS_ASSERT(copy.getSourceLocationById(idd)->getEndLocation()); + } + + void test_get_source_locations_filtered_by_lines() + { + SourceLocationCollection collection; + SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 1, 3, 1, 5); + SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {1}, "file.c", 1, 3, 2, 5); + SourceLocation* c = collection.addSourceLocation(LOCATION_TOKEN, 3, {1}, "file.c", 2, 3, 2, 5); + SourceLocation* d = collection.addSourceLocation(LOCATION_TOKEN, 4, {1}, "file.c", 3, 3, 4, 5); + SourceLocation* e = collection.addSourceLocation(LOCATION_TOKEN, 5, {1}, "file.c", 3, 5, 5, 5); + SourceLocation* f = collection.addSourceLocation(LOCATION_TOKEN, 6, {1}, "file.c", 1, 5, 5, 5); + SourceLocation* g = collection.addSourceLocation(LOCATION_TOKEN, 7, {1}, "file.c", 5, 5, 5, 5); + + SourceLocationCollection copy; + copy.addSourceLocationFile( + collection.getSourceLocationById(a->getLocationId())->getSourceLocationFile()->getFilteredByLines(2, 4)); + + TS_ASSERT_EQUALS(1, copy.getSourceLocationFileCount()); + TS_ASSERT_EQUALS(4, copy.getSourceLocationCount()); + + TS_ASSERT(!copy.getSourceLocationById(a->getLocationId())); + TS_ASSERT(copy.getSourceLocationById(b->getLocationId())); + TS_ASSERT(copy.getSourceLocationById(c->getLocationId())); + TS_ASSERT(copy.getSourceLocationById(d->getLocationId())); + TS_ASSERT(copy.getSourceLocationById(e->getLocationId())); + TS_ASSERT(!copy.getSourceLocationById(f->getLocationId())); + TS_ASSERT(!copy.getSourceLocationById(g->getLocationId())); + + TS_ASSERT_DIFFERS(b, copy.getSourceLocationById(b->getLocationId())); + TS_ASSERT_DIFFERS(c, copy.getSourceLocationById(c->getLocationId())); + + TS_ASSERT(!copy.getSourceLocationById(b->getLocationId())->getStartLocation()); + TS_ASSERT(copy.getSourceLocationById(b->getLocationId())->getEndLocation()); + + TS_ASSERT(copy.getSourceLocationById(e->getLocationId())->getStartLocation()); + TS_ASSERT(!copy.getSourceLocationById(e->getLocationId())->getEndLocation()); + } +}; diff --git a/src/test/StorageTestSuite.h b/src/test/StorageTestSuite.h index 0365dfa9..d8f8a944 100644 --- a/src/test/StorageTestSuite.h +++ b/src/test/StorageTestSuite.h @@ -5,7 +5,6 @@ #include "data/graph/token_component/TokenComponentAbstraction.h" #include "data/graph/token_component/TokenComponentAccess.h" #include "data/graph/token_component/TokenComponentStatic.h" -#include "data/location/TokenLocation.h" #include "data/parser/ParseLocation.h" #include "data/PersistentStorage.h" @@ -238,13 +237,6 @@ private: clear(); } - std::shared_ptr getLocationCollectionForTokenId(Id id) const - { - std::vector tokenIds; - tokenIds.push_back(id); - return getTokenLocationsForTokenIds(tokenIds); - } - //const size_t getNodeCount() const //{ // return getGraph().getNodeCount(); @@ -268,14 +260,6 @@ private: return ParseLocation(m_filePath, 1, locationId, 1, locationId); } - bool isValidLocation(TokenLocation* location, Id locationId) const - { - return - location->getFilePath() == m_filePath && - location->getLineNumber() == 1 && - location->getColumnNumber() == locationId; - } - NameHierarchy createFunctionNameHierarchy(std::string ret, std::string name, std::string parameters) const { NameHierarchy nameHierarchy = createNameHierarchy(name); diff --git a/src/test/TokenLocationCollectionTestSuite.h b/src/test/TokenLocationCollectionTestSuite.h deleted file mode 100644 index 78e76b9e..00000000 --- a/src/test/TokenLocationCollectionTestSuite.h +++ /dev/null @@ -1,162 +0,0 @@ -#include "cxxtest/TestSuite.h" - -#include "data/location/TokenLocation.h" -#include "data/location/TokenLocationCollection.h" -#include "data/location/TokenLocationFile.h" -#include "data/location/TokenLocationLine.h" - -class TokenLocationCollectionTestSuite : public CxxTest::TestSuite -{ -public: - void test_token_locations_get_created_with_other_end() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5); - - TS_ASSERT(a); - TS_ASSERT(a->isStartTokenLocation()); - TS_ASSERT(!a->isEndTokenLocation()); - - TokenLocation* b = a->getOtherTokenLocation(); - - TS_ASSERT(b); - TS_ASSERT(!b->isStartTokenLocation()); - TS_ASSERT(b->isEndTokenLocation()); - - TS_ASSERT_EQUALS(a, b->getOtherTokenLocation()); - TS_ASSERT_EQUALS(a, b->getStartTokenLocation()); - TS_ASSERT_EQUALS(a, a->getStartTokenLocation()); - TS_ASSERT_EQUALS(b, a->getEndTokenLocation()); - TS_ASSERT_EQUALS(b, b->getEndTokenLocation()); - } - - void test_token_locations_do_not_get_created_with_wrong_input() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 2, 1); - TokenLocation* b = collection.addTokenLocation(2, 1, "file.c", 4, 1, 1, 10); - - TS_ASSERT(!a); - TS_ASSERT(!b); - } - - void test_token_locations_get_unique_id_but_both_ends_have_the_same() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 1, 1, 1, 1); - TokenLocation* b = collection.addTokenLocation(2, 2, "file.c", 1, 1, 1, 1); - TokenLocation* c = collection.addTokenLocation(3, 3, "file.c", 1, 1, 1, 1); - - TS_ASSERT_EQUALS(1, collection.getTokenLocationFileCount()); - TS_ASSERT_EQUALS(3, collection.getTokenLocationCount()); - - TS_ASSERT_DIFFERS(a->getId(), b->getId()); - TS_ASSERT_DIFFERS(b->getId(), c->getId()); - TS_ASSERT_DIFFERS(c->getId(), a->getId()); - - TS_ASSERT_EQUALS(a->getId(), a->getOtherTokenLocation()->getId()); - TS_ASSERT_EQUALS(b->getId(), b->getOtherTokenLocation()->getId()); - TS_ASSERT_EQUALS(c->getId(), c->getOtherTokenLocation()->getId()); - } - - void test_token_locations_have_right_file_path_line_column_and_token_id() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5); - - TS_ASSERT_EQUALS(1, a->getTokenId()); - TS_ASSERT_EQUALS(2, a->getLineNumber()); - TS_ASSERT_EQUALS(3, a->getColumnNumber()); - TS_ASSERT_EQUALS(4, a->getOtherTokenLocation()->getLineNumber()); - TS_ASSERT_EQUALS(5, a->getOtherTokenLocation()->getColumnNumber()); - TS_ASSERT_EQUALS("file.c", a->getFilePath().str()); - } - - void test_finding_token_locations_by_id() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5); - TokenLocation* b = collection.addTokenLocation(2, 6, "file.c", 7, 8, 9, 10); - - TS_ASSERT_EQUALS(a, collection.findTokenLocationById(a->getId())); - TS_ASSERT_EQUALS(b, collection.findTokenLocationById(b->getId())); - } - - void test_removing_token_locations() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5); - TokenLocation* b = collection.addTokenLocation(2, 1, "file.c", 3, 3, 4, 5); - TokenLocation* c = collection.addTokenLocation(3, 1, "file.c", 1, 3, 5, 5); - TokenLocation* d = collection.addTokenLocation(4, 1, "file2.c", 1, 3, 5, 5); - - TS_ASSERT_EQUALS(2, collection.getTokenLocationFileCount()); - TS_ASSERT_EQUALS(4, collection.getTokenLocationCount()); - TS_ASSERT_EQUALS(5, c->getTokenLocationFile()->getTokenLocationLineCount()); - - Id ida = a->getId(); - Id idb = b->getId(); - Id idc = c->getId(); - - collection.removeTokenLocation(a); - collection.removeTokenLocation(b->getOtherTokenLocation()); - collection.removeTokenLocation(d); - - TS_ASSERT(!collection.findTokenLocationById(ida)); - TS_ASSERT(!collection.findTokenLocationById(idb)); - TS_ASSERT_EQUALS(c, collection.findTokenLocationById(idc)); - - TS_ASSERT_EQUALS(1, collection.getTokenLocationFileCount()); - TS_ASSERT_EQUALS(1, collection.getTokenLocationCount()); - TS_ASSERT_EQUALS(2, c->getTokenLocationFile()->getTokenLocationLineCount()); - } - - void test_creating_plain_copy_of_all_locations_in_line_range() - { - TokenLocationCollection collection; - TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5); - TokenLocation* b = collection.addTokenLocation(2, 1, "file.c", 3, 3, 4, 5); - TokenLocation* c = collection.addTokenLocation(3, 1, "file.c", 1, 3, 5, 5); - TokenLocation* d = collection.addTokenLocation(4, 1, "file.c", 1, 5, 4, 5); - - Id ida = a->getId(); - Id idb = b->getId(); - Id idc = c->getId(); - Id idd = d->getId(); - - unsigned int fromLine = 2; - unsigned int toLine = 4; - - TokenLocationCollection copy; - TokenLocation* x = collection.findTokenLocationById(ida); - - x->getTokenLocationFile()->forEachTokenLocationLine([©, fromLine, toLine](TokenLocationLine* line) - { - unsigned int l = line->getLineNumber(); - if (l >= fromLine && l <= toLine) - { - line->forEachTokenLocation([©](TokenLocation* location) - { - copy.addTokenLocationAsPlainCopy(location); - }); - } - }); - - TS_ASSERT_EQUALS(1, copy.getTokenLocationFileCount()); - TS_ASSERT_EQUALS(3, copy.getTokenLocationCount()); - - TS_ASSERT(copy.findTokenLocationById(ida)); - TS_ASSERT(copy.findTokenLocationById(idb)); - TS_ASSERT(!copy.findTokenLocationById(idc)); - TS_ASSERT(copy.findTokenLocationById(idd)); - - TS_ASSERT_DIFFERS(a, copy.findTokenLocationById(ida)); - TS_ASSERT_DIFFERS(d, copy.findTokenLocationById(idd)); - - TS_ASSERT(copy.findTokenLocationById(ida)->getStartTokenLocation()); - TS_ASSERT(copy.findTokenLocationById(ida)->getEndTokenLocation()); - - TS_ASSERT(!copy.findTokenLocationById(idd)->getStartTokenLocation()); - TS_ASSERT(copy.findTokenLocationById(idd)->getEndTokenLocation()); - } -};