ui: scope names in snippet titles

* added title for snippets
* title displays name of parent scope.
* when new TokenLocationFiles are created in the storage they are now returned as shared_ptr to ensure correct linking in the contained data.
* Storage::getTokenLocationsForLinesInFile() now always returns pairs of start and end locations.
* Adjusted code snippet view to regard the change described above.
This commit is contained in:
malte_langkabel
2015-05-11 12:34:27 +02:00
parent 7d48a873f7
commit 97345cc72f
22 changed files with 667 additions and 556 deletions
@@ -1,9 +1,11 @@
#include "component/controller/CodeController.h"
#include <memory>
#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 "settings/ApplicationSettings.h"
#include "utility/text/TextAccess.h"
@@ -54,7 +56,7 @@ void CodeController::handleMessage(MessageFinishedParsing* message)
std::vector<CodeView::CodeSnippetParams> snippets;
errorCollection.forEachTokenLocationFile(
[&](TokenLocationFile* file) -> void
[&](std::shared_ptr<TokenLocationFile> file) -> void
{
std::vector<CodeView::CodeSnippetParams> fileSnippets = getSnippetsForFile(file);
snippets.insert(snippets.end(), fileSnippets.begin(), fileSnippets.end());
@@ -101,7 +103,7 @@ std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForActiveTok
std::vector<CodeView::CodeSnippetParams> snippets;
collection.forEachTokenLocationFile(
[&](TokenLocationFile* file) -> void
[&](std::shared_ptr<TokenLocationFile> file) -> void
{
std::vector<CodeView::CodeSnippetParams> fileSnippets = getSnippetsForFile(file);
@@ -116,7 +118,7 @@ std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForActiveTok
bool isDeclarationFile = false;
for (const CodeView::CodeSnippetParams& snippet : fileSnippets)
{
snippet.locationFile.forEachTokenLocation(
snippet.locationFile->forEachTokenLocation(
[&](TokenLocation* location)
{
if (location->getTokenId() == declarationId)
@@ -142,7 +144,7 @@ std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForActiveTok
return snippets;
}
std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForFile(const TokenLocationFile* file) const
std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForFile(std::shared_ptr<TokenLocationFile> file) const
{
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(file->getFilePath().str());
@@ -174,10 +176,24 @@ std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForFile(cons
for (const SnippetMerger::Range& range: ranges)
{
CodeView::CodeSnippetParams params;
params.locationFile = *file;
params.locationFile = file;
params.startLineNumber = std::max<int>(1, range.start.row - (range.start.strong ? 0 : snippetExpandRange));
params.endLineNumber = std::min<int>(textAccess->getLineCount(), range.end.row + (range.end.strong ? 0 : snippetExpandRange));
std::shared_ptr<TokenLocationFile> tempFile = m_storageAccess->getTokenLocationsForLinesInFile(file->getFilePath().str(), params.startLineNumber, params.endLineNumber);
TokenLocationLine* firstUsedLine = nullptr;
for (rsize_t i = params.startLineNumber; i <= params.endLineNumber, firstUsedLine == nullptr; i++)
{
firstUsedLine = tempFile->findTokenLocationLineByNumber(i);
}
m_storageAccess->getTokenLocationOfParentScope(firstUsedLine->getTokenLocations().begin()->second.get())->forEachStartTokenLocation(
[&](TokenLocation* location)
{
params.title = m_storageAccess->getNameForNodeWithId(location->getTokenId());
}
);
for (const std::string& line: textAccess->getLines(params.startLineNumber, params.endLineNumber))
{
params.code += line;
@@ -4,6 +4,7 @@
#include <string>
#include <map>
#include "component/controller/helper/SnippetMerger.h"
#include "component/controller/Controller.h"
#include "component/view/CodeView.h"
#include "utility/messaging/MessageListener.h"
@@ -14,10 +15,6 @@
#include "utility/messaging/type/MessageShowFile.h"
#include "utility/types.h"
#include "component/controller/helper/SnippetMerger.h"
class StorageAccess;
class TokenLocationFile;
@@ -46,7 +43,7 @@ private:
std::vector<CodeView::CodeSnippetParams> getSnippetsForActiveTokenIds(
const std::vector<Id>& ids, Id declarationId) const;
std::vector<CodeView::CodeSnippetParams> getSnippetsForFile(const TokenLocationFile* file) const;
std::vector<CodeView::CodeSnippetParams> getSnippetsForFile(std::shared_ptr<TokenLocationFile> file) const;
std::shared_ptr<SnippetMerger> buildMergerHierarchy(
TokenLocation* location, SnippetMerger& fileScopedMerger, std::map<int, std::shared_ptr<SnippetMerger>>& mergers) const;
+3 -3
View File
@@ -7,7 +7,7 @@ CodeView::CodeSnippetParams::CodeSnippetParams()
: startLineNumber(0)
, endLineNumber(0)
, lineCount(0)
, locationFile("")
, locationFile(std::make_shared<TokenLocationFile>(""))
, isActive(false)
, isDeclaration(false)
{
@@ -40,8 +40,8 @@ bool CodeView::CodeSnippetParams::sort(const CodeSnippetParams& a, const CodeSni
return false;
}
const FilePath& aFilePath = a.locationFile.getFilePath();
const FilePath& bFilePath = b.locationFile.getFilePath();
const FilePath& aFilePath = a.locationFile->getFilePath();
const FilePath& bFilePath = b.locationFile->getFilePath();
// different files
if (aFilePath != bFilePath)
+4 -1
View File
@@ -1,6 +1,8 @@
#ifndef CODE_VIEW_H
#define CODE_VIEW_H
#include <memory>
#include "component/view/View.h"
#include "data/location/TokenLocationFile.h"
#include "utility/types.h"
@@ -21,9 +23,10 @@ public:
uint endLineNumber;
uint lineCount;
std::string title;
std::string code;
TokenLocationFile locationFile;
std::shared_ptr<TokenLocationFile> locationFile;
bool isActive;
bool isDeclaration;
+15 -9
View File
@@ -976,9 +976,9 @@ TokenLocationCollection Storage::getTokenLocationsForTokenIds(const std::vector<
return ret;
}
TokenLocationFile Storage::getTokenLocationsForFile(const std::string& filePath) const
std::shared_ptr<TokenLocationFile> Storage::getTokenLocationsForFile(const std::string& filePath) const
{
TokenLocationFile ret(filePath);
std::shared_ptr<TokenLocationFile> ret = std::make_shared<TokenLocationFile>(filePath);
TokenLocationFile* locationFile = m_locationCollection.findTokenLocationFileByPath(filePath);
if (!locationFile)
@@ -989,18 +989,18 @@ TokenLocationFile Storage::getTokenLocationsForFile(const std::string& filePath)
locationFile->forEachTokenLocation(
[&](TokenLocation* tokenLocation) -> void
{
ret.addTokenLocationAsPlainCopy(tokenLocation);
ret->addTokenLocationAsPlainCopy(tokenLocation);
}
);
return ret;
}
TokenLocationFile Storage::getTokenLocationsForLinesInFile(
std::shared_ptr<TokenLocationFile> Storage::getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
) const
{
TokenLocationFile ret(filePath);
std::shared_ptr<TokenLocationFile> ret = std::make_shared<TokenLocationFile>(filePath);
TokenLocationFile* locationFile = m_locationCollection.findTokenLocationFileByPath(filePath);
if (!locationFile)
@@ -1009,7 +1009,7 @@ TokenLocationFile Storage::getTokenLocationsForLinesInFile(
}
uint endLineNumber = locationFile->getTokenLocationLines().rbegin()->first;
std::set<int> addedLocationIds;
for (uint i = firstLineNumber; i <= endLineNumber; i++)
{
TokenLocationLine* locationLine = locationFile->findTokenLocationLineByNumber(i);
@@ -1023,7 +1023,13 @@ TokenLocationFile Storage::getTokenLocationsForLinesInFile(
locationLine->forEachTokenLocation(
[&](TokenLocation* tokenLocation) -> void
{
ret.addTokenLocationAsPlainCopy(tokenLocation);
const Id tokenId = tokenLocation->getId();
if (addedLocationIds.find(tokenId) == addedLocationIds.end())
{
ret->addTokenLocationAsPlainCopy(tokenLocation->getStartTokenLocation());
ret->addTokenLocationAsPlainCopy(tokenLocation->getEndTokenLocation());
addedLocationIds.insert(tokenId);
}
}
);
}
@@ -1036,7 +1042,8 @@ TokenLocationFile Storage::getTokenLocationsForLinesInFile(
if (tokenLocation->isEndTokenLocation() &&
tokenLocation->getStartTokenLocation()->getLineNumber() < firstLineNumber)
{
ret.addTokenLocationAsPlainCopy(tokenLocation->getStartTokenLocation());
ret->addTokenLocationAsPlainCopy(tokenLocation->getStartTokenLocation());
ret->addTokenLocationAsPlainCopy(tokenLocation->getEndTokenLocation());
}
}
);
@@ -1062,7 +1069,6 @@ std::shared_ptr<TokenLocationFile> Storage::getTokenLocationOfParentScope(const
[&](TokenLocation* tokenLocation) -> void
{
if (tokenLocation->getType() == TokenLocation::LOCATION_SCOPE &&
tokenLocation->isStartTokenLocation() &&
(*tokenLocation) < *(child->getStartTokenLocation()) &&
(*tokenLocation->getEndTokenLocation()) > *(child->getEndTokenLocation()))
{
+2 -2
View File
@@ -119,8 +119,8 @@ public:
virtual std::vector<Id> getTokenIdsForQuery(std::string query) const;
virtual TokenLocationCollection getTokenLocationsForTokenIds(const std::vector<Id>& tokenIds) const;
virtual TokenLocationFile getTokenLocationsForFile(const std::string& filePath) const;
virtual TokenLocationFile getTokenLocationsForLinesInFile(
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForFile(const std::string& filePath) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
) const;
+2 -2
View File
@@ -34,8 +34,8 @@ public:
virtual std::vector<Id> getTokenIdsForQuery(std::string query) const = 0;
virtual TokenLocationCollection getTokenLocationsForTokenIds(const std::vector<Id>& tokenIds) const = 0;
virtual TokenLocationFile getTokenLocationsForFile(const std::string& filePath) const = 0;
virtual TokenLocationFile getTokenLocationsForLinesInFile(
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForFile(const std::string& filePath) const = 0;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber) const = 0;
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const = 0;
+4 -4
View File
@@ -122,17 +122,17 @@ TokenLocationCollection StorageAccessProxy::getTokenLocationsForTokenIds(const s
return TokenLocationCollection();
}
TokenLocationFile StorageAccessProxy::getTokenLocationsForFile(const std::string& filePath) const
std::shared_ptr<TokenLocationFile> StorageAccessProxy::getTokenLocationsForFile(const std::string& filePath) const
{
if (hasSubject())
{
return m_subject->getTokenLocationsForFile(filePath);
}
return TokenLocationFile("");
return std::make_shared<TokenLocationFile>("");
}
TokenLocationFile StorageAccessProxy::getTokenLocationsForLinesInFile(
std::shared_ptr<TokenLocationFile> StorageAccessProxy::getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
) const
{
@@ -141,7 +141,7 @@ TokenLocationFile StorageAccessProxy::getTokenLocationsForLinesInFile(
return m_subject->getTokenLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber);
}
return TokenLocationFile("");
return std::make_shared<TokenLocationFile>("");
}
TokenLocationCollection StorageAccessProxy::getErrorTokenLocations(std::vector<std::string>* errorMessages) const
+2 -2
View File
@@ -27,8 +27,8 @@ public:
virtual std::vector<Id> getTokenIdsForQuery(std::string query) const;
virtual TokenLocationCollection getTokenLocationsForTokenIds(const std::vector<Id>& tokenIds) const;
virtual TokenLocationFile getTokenLocationsForFile(const std::string& filePath) const;
virtual TokenLocationFile getTokenLocationsForLinesInFile(
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForFile(const std::string& filePath) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
) const;
@@ -105,11 +105,11 @@ TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const Fi
return nullptr;
}
void TokenLocationCollection::forEachTokenLocationFile(std::function<void(TokenLocationFile*)> func) const
void TokenLocationCollection::forEachTokenLocationFile(std::function<void(std::shared_ptr<TokenLocationFile>)> func) const
{
for (const TokenLocationFilePairType& file : m_files)
{
func(file.second.get());
func(file.second);
}
}
@@ -188,9 +188,9 @@ TokenLocationFile* TokenLocationCollection::createTokenLocationFile(const FilePa
std::ostream& operator<<(std::ostream& ostream, const TokenLocationCollection& base)
{
ostream << "Locations:\n";
base.forEachTokenLocationFile([&ostream](TokenLocationFile* f)
base.forEachTokenLocationFile([&ostream](std::shared_ptr<TokenLocationFile> f)
{
ostream << *f;
ostream << *(f.get());
});
return ostream;
}
@@ -38,7 +38,7 @@ public:
TokenLocation* findTokenLocationById(Id id) const;
TokenLocationFile* findTokenLocationFileByPath(const FilePath& filePath) const;
void forEachTokenLocationFile(std::function<void(TokenLocationFile*)> func) const;
void forEachTokenLocationFile(std::function<void(std::shared_ptr<TokenLocationFile>)> func) const;
void forEachTokenLocationLine(std::function<void(TokenLocationLine*)> func) const;
void forEachTokenLocation(std::function<void(TokenLocation*)> func) const;