diff --git a/src/lib/component/controller/CodeController.cpp b/src/lib/component/controller/CodeController.cpp index cfafb8e3..ac802506 100644 --- a/src/lib/component/controller/CodeController.cpp +++ b/src/lib/component/controller/CodeController.cpp @@ -468,11 +468,6 @@ std::vector CodeController::getSnippetsForFileWithState( return snippets; } - if (!addSourceLocations) - { - file->setIsWhole(false); - } - snippets = getSnippetsForFile(file, addSourceLocations); } break; @@ -594,45 +589,33 @@ std::vector CodeController::getSnippetsForFile( { TRACE(); - 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 (activeSourceLocations->isWhole()) - { - ranges.push_back(SnippetMerger::Range( - SnippetMerger::Border(1, true), - SnippetMerger::Border(textAccess->getLineCount(), true) - )); - } - else - { - SnippetMerger fileScopedMerger(1, textAccess->getLineCount()); - std::map> mergers; - activeSourceLocations->forEachStartSourceLocation( - [&](SourceLocation* location) - { - buildMergerHierarchy(location, scopeLocations, fileScopedMerger, mergers); - } - ); + SnippetMerger fileScopedMerger(1, textAccess->getLineCount()); + std::map> mergers; - std::vector atomicRanges; - m_storageAccess->getCommentLocationsInFile(activeSourceLocations->getFilePath())->forEachStartSourceLocation( - [&](SourceLocation* location) - { - atomicRanges.push_back(SnippetMerger::Range( - SnippetMerger::Border(location->getLineNumber(), false), - SnippetMerger::Border(location->getOtherLocation()->getLineNumber(), false) - )); - } - ); - atomicRanges = SnippetMerger::Range::mergeAdjacent(atomicRanges); + std::shared_ptr scopeLocations = + m_storageAccess->getSourceLocationsOfTypeInFile(activeSourceLocations->getFilePath(), LOCATION_SCOPE); + activeSourceLocations->forEachStartSourceLocation( + [&](SourceLocation* location) + { + buildMergerHierarchy(location, scopeLocations, fileScopedMerger, mergers); + } + ); - ranges = fileScopedMerger.merge(atomicRanges); - } + std::vector atomicRanges; + m_storageAccess->getCommentLocationsInFile(activeSourceLocations->getFilePath())->forEachStartSourceLocation( + [&](SourceLocation* location) + { + atomicRanges.push_back(SnippetMerger::Range( + SnippetMerger::Border(location->getLineNumber(), false), + SnippetMerger::Border(location->getOtherLocation()->getLineNumber(), false) + )); + } + ); + atomicRanges = SnippetMerger::Range::mergeAdjacent(atomicRanges); + + std::deque ranges = fileScopedMerger.merge(atomicRanges); const int snippetExpandRange = ApplicationSettings::getInstance()->getCodeSnippetExpandRange(); std::vector snippets; @@ -650,8 +633,8 @@ std::vector CodeController::getSnippetsForFile( params.titleId = 0; params.footerId = 0; - std::shared_ptr tempFile = - fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber); + std::shared_ptr tempFile = m_storageAccess->getSourceLocationsForLinesInFile( + activeSourceLocations->getFilePath(), params.startLineNumber, params.endLineNumber); const SourceLocation* firstSourceLocation = tempFile->getSourceLocations().size() ? tempFile->getSourceLocations().begin()->get() : nullptr; @@ -671,7 +654,7 @@ std::vector CodeController::getSnippetsForFile( ); } - if (!activeSourceLocations->isWhole() && params.titleId == 0) + if (params.titleId == 0) { params.title = activeSourceLocations->getFilePath().wstr(); } @@ -698,25 +681,19 @@ std::vector CodeController::getSnippetsForFile( params.code += line; } - snippets.push_back(params); - } - - if (addSourceLocations && !activeSourceLocations->isWhole()) - { - for (CodeSnippetParams& params : snippets) + if (addSourceLocations) { - std::shared_ptr lines = - fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber); - params.locationFile->forEachSourceLocation( - [&lines](SourceLocation* location) + [&tempFile](SourceLocation* location) { - lines->addSourceLocationCopy(location); + tempFile->addSourceLocationCopy(location); } ); - params.locationFile = lines; + params.locationFile = tempFile; } + + snippets.push_back(params); } return snippets; diff --git a/src/lib/data/access/StorageAccess.h b/src/lib/data/access/StorageAccess.h index 3ed7c40d..37e8bbd4 100644 --- a/src/lib/data/access/StorageAccess.h +++ b/src/lib/data/access/StorageAccess.h @@ -11,6 +11,7 @@ #include "data/bookmark/EdgeBookmark.h" #include "data/bookmark/NodeBookmark.h" #include "data/graph/Node.h" +#include "data/location/LocationType.h" #include "data/search/SearchMatch.h" #include "data/storage/type/StorageEdge.h" #include "data/storage/StorageStats.h" @@ -66,9 +67,12 @@ public: 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 FilePath& filePath, uint firstLineNumber, uint lastLineNumber) const = 0; + const FilePath& filePath, size_t startLine, size_t endLine) const = 0; + virtual std::shared_ptr getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type) const = 0; virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const = 0; diff --git a/src/lib/data/access/StorageAccessProxy.cpp b/src/lib/data/access/StorageAccessProxy.cpp index db9f2199..46763a57 100644 --- a/src/lib/data/access/StorageAccessProxy.cpp +++ b/src/lib/data/access/StorageAccessProxy.cpp @@ -250,12 +250,24 @@ std::shared_ptr StorageAccessProxy::getSourceLocationsForFil } std::shared_ptr StorageAccessProxy::getSourceLocationsForLinesInFile( - const FilePath& filePath, uint firstLineNumber, uint lastLineNumber + const FilePath& filePath, size_t startLine, size_t endLine ) const { if (hasSubject()) { - return m_subject->getSourceLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber); + return m_subject->getSourceLocationsForLinesInFile(filePath, startLine, endLine); + } + + return std::make_shared(FilePath(), false, false); +} + +std::shared_ptr StorageAccessProxy::getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type +) const +{ + if (hasSubject()) + { + return m_subject->getSourceLocationsOfTypeInFile(filePath, type); } return std::make_shared(FilePath(), false, false); diff --git a/src/lib/data/access/StorageAccessProxy.h b/src/lib/data/access/StorageAccessProxy.h index 823aa9a7..c6a1d06d 100644 --- a/src/lib/data/access/StorageAccessProxy.h +++ b/src/lib/data/access/StorageAccessProxy.h @@ -52,10 +52,12 @@ public: virtual std::shared_ptr getSourceLocationsForLocationIds( const std::vector& locationIds ) const override; + virtual std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const override; virtual std::shared_ptr getSourceLocationsForLinesInFile( - const FilePath& filePath, uint firstLineNumber, uint lastLineNumber - ) const override; + const FilePath& filePath, size_t startLine, size_t endLine) const override; + virtual std::shared_ptr getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type) const override; virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const override; diff --git a/src/lib/data/storage/PersistentStorage.cpp b/src/lib/data/storage/PersistentStorage.cpp index 0bbcf2af..ddce502f 100644 --- a/src/lib/data/storage/PersistentStorage.cpp +++ b/src/lib/data/storage/PersistentStorage.cpp @@ -1118,21 +1118,23 @@ std::vector PersistentStorage::getActiveTokenIdsForId(Id tokenId, Id* declar std::vector activeTokenIds; - if (!(m_sqliteIndexStorage.isEdge(tokenId) || m_sqliteIndexStorage.isNode(tokenId))) + bool isNode = m_sqliteIndexStorage.isNode(tokenId); + bool isEdge = m_sqliteIndexStorage.isEdge(tokenId); + + if (!isEdge && !isNode) { return activeTokenIds; } activeTokenIds.push_back(tokenId); - if (m_sqliteIndexStorage.isNode(tokenId)) + if (isNode) { *declarationId = tokenId; - const std::vector incomingEdges = m_sqliteIndexStorage.getEdgesByTargetId(tokenId); - for (size_t i = 0; i < incomingEdges.size(); i++) + for (const StorageEdge& edge : m_sqliteIndexStorage.getEdgesByTargetId(tokenId)) { - activeTokenIds.push_back(incomingEdges[i].id); + activeTokenIds.push_back(edge.id); } } @@ -1247,42 +1249,44 @@ std::shared_ptr PersistentStorage::getSourceLocationsF for (const StorageSourceLocation& sourceLocation: m_sqliteIndexStorage.getAllByIds(locationIds)) { - auto it = locationIdToElementIdMap.find(sourceLocation.id); - if (it != locationIdToElementIdMap.end()) + const LocationType type = intToLocationType(sourceLocation.type); + if (type == LOCATION_QUALIFIER) { - const LocationType type = intToLocationType(sourceLocation.type); - if (type == LOCATION_QUALIFIER) - { - continue; - } + continue; + } - FilePath path = getFileNodePath(sourceLocation.fileNodeId); - if (path.empty()) + auto it = locationIdToElementIdMap.find(sourceLocation.id); + if (it == locationIdToElementIdMap.end()) + { + continue; + } + + FilePath path = getFileNodePath(sourceLocation.fileNodeId); + if (path.empty()) + { + const StorageNode fileNode = m_sqliteIndexStorage.getNodeById(sourceLocation.fileNodeId); + if (fileNode.id) { - const StorageNode fileNode = m_sqliteIndexStorage.getNodeById(sourceLocation.fileNodeId); - if (fileNode.id) + const FilePath path2 = FilePath(NameHierarchy::deserialize(fileNode.serializedName).getQualifiedName()); + if (path2.exists()) { - const FilePath path2 = FilePath(NameHierarchy::deserialize(fileNode.serializedName).getQualifiedName()); - if (path2.exists()) - { - path = path2; - } + path = path2; } } + } - if (!path.empty()) - { - collection->addSourceLocation( - type, - sourceLocation.id, - std::vector(1, it->second), - path, - sourceLocation.startLine, - sourceLocation.startCol, - sourceLocation.endLine, - sourceLocation.endCol - ); - } + if (!path.empty()) + { + collection->addSourceLocation( + type, + sourceLocation.id, + std::vector(1, it->second), + path, + sourceLocation.startLine, + sourceLocation.startCol, + sourceLocation.endLine, + sourceLocation.endCol + ); } } } @@ -1333,12 +1337,22 @@ std::shared_ptr PersistentStorage::getSourceLocationsForFile } std::shared_ptr PersistentStorage::getSourceLocationsForLinesInFile( - const FilePath& filePath, uint firstLineNumber, uint lastLineNumber + const FilePath& filePath, size_t startLine, size_t endLine ) const { TRACE(); - return getSourceLocationsForFile(filePath)->getFilteredByLines(firstLineNumber, lastLineNumber); + return m_sqliteIndexStorage.getSourceLocationsForLinesInFile( + filePath, startLine, endLine)->getFilteredByLines(startLine, endLine); +} + +std::shared_ptr PersistentStorage::getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type +) const +{ + TRACE(); + + return m_sqliteIndexStorage.getSourceLocationsOfTypeInFile(filePath, type); } std::shared_ptr PersistentStorage::getCommentLocationsInFile(const FilePath& filePath) const diff --git a/src/lib/data/storage/PersistentStorage.h b/src/lib/data/storage/PersistentStorage.h index 30539f1e..98b24306 100644 --- a/src/lib/data/storage/PersistentStorage.h +++ b/src/lib/data/storage/PersistentStorage.h @@ -109,8 +109,9 @@ public: virtual std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const override; virtual std::shared_ptr getSourceLocationsForLinesInFile( - const FilePath& filePath, uint firstLineNumber, uint lastLineNumber - ) const override; + const FilePath& filePath, size_t startLine, size_t endLine) const override; + virtual std::shared_ptr getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type) const override; virtual std::shared_ptr getCommentLocationsInFile(const FilePath& filePath) const override; diff --git a/src/lib/data/storage/sqlite/SqliteIndexStorage.cpp b/src/lib/data/storage/sqlite/SqliteIndexStorage.cpp index 656846ae..23f15ebe 100644 --- a/src/lib/data/storage/sqlite/SqliteIndexStorage.cpp +++ b/src/lib/data/storage/sqlite/SqliteIndexStorage.cpp @@ -643,7 +643,8 @@ void SqliteIndexStorage::setNodeType(int type, Id nodeId) ); } -std::shared_ptr SqliteIndexStorage::getSourceLocationsForFile(const FilePath& filePath) const +std::shared_ptr SqliteIndexStorage::getSourceLocationsForFile( + const FilePath& filePath, const std::string& query) const { std::shared_ptr ret = std::make_shared(filePath, true, false); @@ -658,7 +659,7 @@ std::shared_ptr SqliteIndexStorage::getSourceLocationsForFil std::vector sourceLocationIds; std::unordered_map sourceLocationIdToData; for (const StorageSourceLocation& storageLocation: - doGetAll("WHERE file_node_id == " + std::to_string(file.id))) + doGetAll("WHERE file_node_id == " + std::to_string(file.id) + " " + query)) { sourceLocationIds.push_back(storageLocation.id); sourceLocationIdToData[storageLocation.id] = storageLocation; @@ -690,6 +691,19 @@ std::shared_ptr SqliteIndexStorage::getSourceLocationsForFil return ret; } +std::shared_ptr SqliteIndexStorage::getSourceLocationsForLinesInFile( + const FilePath& filePath, size_t startLine, size_t endLine) const +{ + return getSourceLocationsForFile(filePath, + "AND start_line <= " + std::to_string(endLine) + " AND end_line >= " + std::to_string(startLine)); +} + +std::shared_ptr SqliteIndexStorage::getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type) const +{ + return getSourceLocationsForFile(filePath, "AND type == " + std::to_string(locationTypeToInt(type))); +} + std::vector SqliteIndexStorage::getOccurrencesForLocationId(Id locationId) const { std::vector locationIds {locationId}; @@ -779,6 +793,10 @@ std::vector> SqliteIndexStorage::getIndices( STORAGE_MODE_READ | STORAGE_MODE_CLEAR, SqliteDatabaseIndex("source_location_file_node_id_index", "source_location(file_node_id)") )); + indices.push_back(std::make_pair( + STORAGE_MODE_READ, + SqliteDatabaseIndex("source_location_file_node_id_type_index", "source_location(file_node_id, type)") + )); indices.push_back(std::make_pair( STORAGE_MODE_WRITE, SqliteDatabaseIndex("source_location_all_data_index", "source_location(file_node_id, start_line, start_column, end_line, end_column, type)") diff --git a/src/lib/data/storage/sqlite/SqliteIndexStorage.h b/src/lib/data/storage/sqlite/SqliteIndexStorage.h index d637ef22..7514d7fd 100644 --- a/src/lib/data/storage/sqlite/SqliteIndexStorage.h +++ b/src/lib/data/storage/sqlite/SqliteIndexStorage.h @@ -88,7 +88,12 @@ public: void setFileComplete(bool complete, Id fileId); void setNodeType(int type, Id nodeId); - std::shared_ptr getSourceLocationsForFile(const FilePath& filePath) const; + std::shared_ptr getSourceLocationsForFile( + const FilePath& filePath, const std::string& query = "") const; + std::shared_ptr getSourceLocationsForLinesInFile( + const FilePath& filePath, size_t startLine, size_t endLine) const; + std::shared_ptr getSourceLocationsOfTypeInFile( + const FilePath& filePath, LocationType type) const; std::vector getOccurrencesForLocationId(Id locationId) const; std::vector getOccurrencesForLocationIds(const std::vector& locationIds) const; diff --git a/src/lib/utility/tracing.cpp b/src/lib/utility/tracing.cpp index 9540d623..89a5d87d 100644 --- a/src/lib/utility/tracing.cpp +++ b/src/lib/utility/tracing.cpp @@ -24,6 +24,7 @@ std::shared_ptr Tracer::startEvent(const std::string& eventName) std::shared_ptr event = std::make_shared(eventName, s_nextTraceId++, m_startedEvents[id].size()); + m_events[id].push_back(event); m_startedEvents[id].push(event.get()); return event; @@ -36,7 +37,6 @@ void Tracer::finishEvent(std::shared_ptr event) const std::thread::id id = std::this_thread::get_id(); m_startedEvents[id].pop(); - m_events[id].push_back(event); } void Tracer::printTraces() diff --git a/src/lib/utility/tracing.h b/src/lib/utility/tracing.h index 61dd0dba..d7d70b31 100644 --- a/src/lib/utility/tracing.h +++ b/src/lib/utility/tracing.h @@ -146,8 +146,8 @@ ScopedTrace::~ScopedTrace() #define PRINT_TRACES() \ Tracer::getInstance()->printTraces() #endif - - + + #else #define TRACE(__name__) #define PRINT_TRACES()