ui: Refactored code component to handle all state changes in controller

This commit is contained in:
Eberhard Graether
2019-11-08 16:20:43 +01:00
parent 9d556599ab
commit 9281c237a7
32 changed files with 1542 additions and 1985 deletions
+1
View File
@@ -65,6 +65,7 @@ add_files(
component/controller/UndoRedoController.cpp
component/controller/UndoRedoController.h
component/view/helper/CodeScrollParams.h
component/view/helper/CodeSnippetParams.cpp
component/view/helper/CodeSnippetParams.h
component/view/BookmarkButtonsView.cpp
@@ -60,8 +60,7 @@ void ActivationController::handleMessage(MessageActivateFile* message)
message->filePath,
MessageChangeFileView::FILE_MAXIMIZED,
MessageChangeFileView::VIEW_CURRENT,
true,
true
CodeScrollParams::toFile(message->filePath, CodeScrollParams::Target::VISIBLE)
);
msg.setSchedulerId(message->getSchedulerId());
msg.dispatchImmediately();
File diff suppressed because it is too large Load Diff
+55 -10
View File
@@ -5,8 +5,10 @@
#include <string>
#include "FilePath.h"
#include "LocationType.h"
#include "MessageListener.h"
#include "MessageActivateLegend.h"
#include "MessageCodeReference.h"
#include "MessageCodeShowDefinition.h"
#include "MessageActivateErrors.h"
#include "MessageErrorCountClear.h"
@@ -24,6 +26,7 @@
#include "MessageFocusOut.h"
#include "MessageScrollCode.h"
#include "MessageScrollToLine.h"
#include "MessageShowReference.h"
#include "MessageShowScope.h"
#include "types.h"
@@ -47,6 +50,7 @@ class CodeController
, public MessageListener<MessageActivateTrail>
, public MessageListener<MessageActivateTrailEdge>
, public MessageListener<MessageChangeFileView>
, public MessageListener<MessageCodeReference>
, public MessageListener<MessageCodeShowDefinition>
, public MessageListener<MessageDeactivateEdge>
, public MessageListener<MessageErrorCountClear>
@@ -56,6 +60,7 @@ class CodeController
, public MessageListener<MessageScrollCode>
, public MessageListener<MessageScrollToLine>
, public MessageListener<MessageShowError>
, public MessageListener<MessageShowReference>
, public MessageListener<MessageShowScope>
{
public:
@@ -65,6 +70,15 @@ public:
Id getSchedulerId() const override;
private:
struct Reference
{
FilePath filePath;
Id tokenId = 0;
Id locationId = 0;
Id scopeLocationId = 0;
LocationType locationType = LOCATION_TOKEN;
};
void handleMessage(MessageActivateErrors* message) override;
void handleMessage(MessageActivateFullTextSearch* message) override;
void handleMessage(MessageActivateLegend* message) override;
@@ -74,6 +88,7 @@ private:
void handleMessage(MessageActivateTrail* message) override;
void handleMessage(MessageActivateTrailEdge* message) override;
void handleMessage(MessageChangeFileView* message) override;
void handleMessage(MessageCodeReference* message) override;
void handleMessage(MessageCodeShowDefinition* message) override;
void handleMessage(MessageDeactivateEdge* message) override;
void handleMessage(MessageErrorCountClear* message) override;
@@ -83,17 +98,18 @@ private:
void handleMessage(MessageScrollCode* message) override;
void handleMessage(MessageScrollToLine* message) override;
void handleMessage(MessageShowError* message) override;
void handleMessage(MessageShowReference* message) override;
void handleMessage(MessageShowScope* message) override;
CodeView* getView() const;
void clear() override;
std::vector<CodeSnippetParams> getSnippetsForFileWithState(const FilePath& filePath, CodeView::FileState state) const;
std::vector<CodeSnippetParams> getSnippetsForActiveSourceLocations(
std::vector<CodeFileParams> getFilesForActiveSourceLocations(
const SourceLocationCollection* collection, Id declarationId) const;
std::vector<CodeSnippetParams> getSnippetsForCollection(std::shared_ptr<SourceLocationCollection> collection) const;
std::vector<CodeSnippetParams> getSnippetsForFile(std::shared_ptr<SourceLocationFile> file) const;
std::vector<CodeFileParams> getFilesForCollection(std::shared_ptr<SourceLocationCollection> collection) const;
CodeSnippetParams getSnippetParamsForWholeFile(std::shared_ptr<SourceLocationFile> locationFile, bool useSingleFileCache) const;
std::vector<CodeSnippetParams> getSnippetsForFile(std::shared_ptr<SourceLocationFile> activeSourceLocations) const;
std::shared_ptr<SnippetMerger> buildMergerHierarchy(
const SourceLocation* location, const SourceLocationFile* scopeLocations, SnippetMerger& fileScopedMerger,
@@ -103,19 +119,48 @@ private:
std::vector<std::string> getProjectDescription(SourceLocationFile* locationFile) const;
void expandVisibleSnippets(std::vector<CodeSnippetParams>* snippets, bool useSingleFileCache) const;
void addAllSourceLocations(std::vector<CodeSnippetParams>* snippets) const;
void addModificationTimes(std::vector<CodeSnippetParams>* snippets) const;
void clearReferences();
void createReferences();
void clearLocalReferences();
void createLocalReferences(const std::set<Id>& localSymbolIds);
void iterateReference(bool next);
void iterateLocalReference(bool next, bool updateView);
void expandVisibleFiles(bool useSingleFileCache);
CodeFileParams* addSourceLocations(std::shared_ptr<SourceLocationFile> locationFile);
void setFileState(const FilePath& filePath, MessageChangeFileView::FileState state, bool useSingleFileCache);
void setFileState(CodeFileParams& file, MessageChangeFileView::FileState state, bool useSingleFileCache);
bool addAllSourceLocations();
void addModificationTimes();
CodeScrollParams firstReferenceScrollParams() const;
CodeScrollParams definitionReferenceScrollParams(const std::vector<Id>& activeTokenIds) const;
CodeScrollParams toReferenceScrollParams(const Reference& ref) const;
void saveOrRestoreViewMode(MessageBase* message);
void showCodeSnippets(
std::vector<CodeSnippetParams> snippets, const CodeView::CodeParams params, bool addSourceLocations = true);
void showFirstActiveReference(Id tokenId, bool updateView);
void showFiles(CodeView::CodeParams params, CodeScrollParams scrollParams, bool updateView);
StorageAccess* m_storageAccess;
mutable std::shared_ptr<SourceLocationCollection> m_collection;
std::shared_ptr<SourceLocationCollection> m_collection;
std::vector<CodeFileParams> m_files;
FilePath m_currentFilePath;
CodeView::CodeParams m_codeParams;
CodeScrollParams m_scrollParams;
std::map<Id, bool> m_messageIdToViewModeMap;
std::vector<Reference> m_references;
int m_referenceIndex = -1;
std::vector<Reference> m_localReferences;
int m_localReferenceIndex = -1;
};
#endif // CODE_CONTROLLER_H
+22 -44
View File
@@ -4,9 +4,10 @@
#include <memory>
#include "ErrorInfo.h"
#include "ScreenSearchInterfaces.h"
#include "LocationType.h"
#include "CodeScrollParams.h"
#include "CodeSnippetParams.h"
#include "ScreenSearchInterfaces.h"
#include "View.h"
class CodeController;
@@ -30,42 +31,22 @@ public:
struct CodeParams
{
bool clearSnippets = false;
bool showContents = false;
bool useSingleFileCache = true;
size_t referenceCount = 0;
size_t referenceIndex = 0;
size_t localReferenceCount = 0;
size_t localReferenceIndex = 0;
std::vector<Id> activeTokenIds;
std::vector<Id> activeLocationIds;
std::vector<Id> activeLocalSymbolIds;
LocationType activeLocalSymbolType = LOCATION_TOKEN;
std::vector<Id> currentActiveLocalLocationIds;
std::vector<ErrorInfo> errorInfos;
};
struct ScrollParams
{
enum ScrollType
{
SCROLL_NONE,
SCROLL_TO_DEFINITION,
SCROLL_TO_LINE,
SCROLL_TO_VALUE
} type;
ScrollParams(ScrollType type = SCROLL_NONE)
: type(type)
, line(0)
, value(0)
, animated(false)
, ignoreActiveReference(false)
, inListMode(false)
{}
FilePath filePath;
size_t line;
size_t value;
bool animated;
bool ignoreActiveReference;
bool inListMode;
};
CodeView(ViewLayout* viewLayout);
virtual ~CodeView();
@@ -73,24 +54,21 @@ public:
virtual void clear() = 0;
virtual void showCodeSnippets(const std::vector<CodeSnippetParams>& snippets, const CodeParams params) = 0;
virtual void updateCodeSnippets(const std::vector<CodeSnippetParams>& snippets) = 0;
virtual void scrollTo(const ScrollParams params) = 0;
virtual void showSnippets(
const std::vector<CodeFileParams> files, const CodeParams params, const CodeScrollParams scrollParams) = 0;
virtual void showSingleFile(
const CodeFileParams file, const CodeParams params, const CodeScrollParams scrollParams) = 0;
virtual void updateSourceLocations(const std::vector<CodeFileParams> files) = 0;
virtual void scrollTo(const CodeScrollParams params, bool animated) = 0;
virtual bool showsErrors() const = 0;
virtual void setFileState(const FilePath filePath, FileState state) = 0;
virtual void showActiveSnippet(
const std::vector<Id>& activeTokenIds, std::shared_ptr<SourceLocationCollection> collection, bool scrollTo) = 0;
virtual void showActiveTokenIds(const std::vector<Id>& activeTokenIds) = 0;
virtual void showActiveLocalSymbolIds(const std::vector<Id>& activeLocalSymbolIds) = 0;
virtual void focusTokenIds(const std::vector<Id>& focusedTokenIds) = 0;
virtual void defocusTokenIds() = 0;
virtual void showContents() = 0;
virtual bool isInListMode() const = 0;
virtual void setMode(bool listMode) = 0;
@@ -0,0 +1,61 @@
#ifndef CODE_SCROLL_PARAMS_H
#define CODE_SCROLL_PARAMS_H
#include "FilePath.h"
#include "types.h"
struct CodeScrollParams
{
enum class Type
{
NONE,
TO_REFERENCE,
TO_FILE,
TO_LINE,
TO_VALUE
};
enum class Target
{
VISIBLE,
CENTER,
TOP
};
static CodeScrollParams toReference(const FilePath& filePath, Id locationId, Target target)
{
return CodeScrollParams { Type::TO_REFERENCE, target, filePath, locationId, 0, 0, false };
}
static CodeScrollParams toFile(const FilePath& filePath, Target target)
{
return CodeScrollParams { Type::TO_LINE, target, filePath, 0, 0, 0, false };
}
static CodeScrollParams toLine(const FilePath& filePath, size_t line, Target target)
{
return CodeScrollParams { Type::TO_LINE, target, filePath, 0, line, 0, false };
}
static CodeScrollParams toValue(size_t value, bool inListMode)
{
return CodeScrollParams { Type::TO_VALUE, Target::VISIBLE, FilePath(), 0, 0, value, inListMode };
}
Type type = Type::NONE;
Target target = Target::VISIBLE;
FilePath filePath;
// Reference
Id locationId = 0;
// Line
size_t line = 0;
// Value
size_t value = 0;
bool inListMode = false;
};
#endif // CODE_SCROLL_PARAMS_H
@@ -2,21 +2,55 @@
#include "SourceLocationFile.h"
CodeSnippetParams::CodeSnippetParams()
: startLineNumber(0)
, endLineNumber(0)
, titleId(0)
, footerId(0)
, refCount(0)
, isCollapsed(false)
, isDeclaration(false)
, isDefinition(false)
, insertSnippet(false)
, reduced(false)
CodeSnippetParams CodeSnippetParams::merge(const CodeSnippetParams& a, const CodeSnippetParams& b)
{
const CodeSnippetParams* first = a.startLineNumber < b.startLineNumber ? &a : &b;
const CodeSnippetParams* second = a.startLineNumber > b.startLineNumber ? &a : &b;
SourceLocationFile* aFile = a.locationFile.get();
SourceLocationFile* bFile = b.locationFile.get();
std::shared_ptr<SourceLocationFile> locationFile = std::make_shared<SourceLocationFile>(
aFile->getFilePath(), aFile->getLanguage(), aFile->isWhole(), aFile->isComplete(), aFile->isIndexed());
aFile->forEachSourceLocation(
[&locationFile](SourceLocation* loc)
{
locationFile->addSourceLocationCopy(loc);
}
);
bFile->forEachSourceLocation(
[&locationFile](SourceLocation* loc)
{
locationFile->addSourceLocationCopy(loc);
}
);
std::string code = first->code;
std::string secondCode = second->code;
int secondCodeStartIndex = 0;
for (size_t i = second->startLineNumber; i <= first->endLineNumber; i++)
{
secondCodeStartIndex = secondCode.find("\n", secondCodeStartIndex) + 1;
}
code += secondCode.substr(secondCodeStartIndex, secondCode.npos);
CodeSnippetParams params;
params.startLineNumber = first->startLineNumber;
params.endLineNumber = second->endLineNumber;
params.title = first->title;
params.titleId = first->titleId;
params.footer = second->footer;
params.footerId = second->footerId;
params.code = code;
params.locationFile = locationFile;
return params;
}
bool CodeSnippetParams::sort(const CodeSnippetParams& a, const CodeSnippetParams& b)
bool CodeFileParams::sort(const CodeFileParams& a, const CodeFileParams& b)
{
// sort definitions
if (a.isDefinition && !b.isDefinition)
@@ -51,25 +85,19 @@ bool CodeSnippetParams::sort(const CodeSnippetParams& a, const CodeSnippetParams
const FilePath& aFilePath = a.locationFile->getFilePath();
const FilePath& bFilePath = b.locationFile->getFilePath();
// different files
if (aFilePath != bFilePath)
// first header
if (aFilePath.withoutExtension() == bFilePath.withoutExtension())
{
// first header
if (aFilePath.withoutExtension() == bFilePath.withoutExtension())
{
return aFilePath.extension() > bFilePath.extension();
}
// alphabetical filepath without extension
else
{
return aFilePath.withoutExtension() < bFilePath.withoutExtension();
}
return aFilePath.extension() > bFilePath.extension();
}
// alphabetical filepath without extension
else
{
return aFilePath.withoutExtension() < bFilePath.withoutExtension();
}
return a.startLineNumber < b.startLineNumber;
}
bool CodeSnippetParams::sortById(const CodeSnippetParams& a, const CodeSnippetParams& b)
bool CodeFileParams::sortById(const CodeFileParams& a, const CodeFileParams& b)
{
return a.locationFile->getSourceLocations().begin()->get()->getLocationId() <
b.locationFile->getSourceLocations().begin()->get()->getLocationId();
@@ -10,34 +10,38 @@ class SourceLocationFile;
struct CodeSnippetParams
{
CodeSnippetParams();
static CodeSnippetParams merge(const CodeSnippetParams& a, const CodeSnippetParams& b);
// comparefunction for snippetsorting
static bool sort(const CodeSnippetParams& a, const CodeSnippetParams& b);
static bool sortById(const CodeSnippetParams& a, const CodeSnippetParams& b);
size_t startLineNumber;
size_t endLineNumber;
size_t startLineNumber = 0;
size_t endLineNumber = 0;
std::wstring title;
std::wstring footer;
std::string code;
Id titleId;
Id footerId;
TimeStamp modificationTime;
Id titleId = 0;
Id footerId = 0;
std::shared_ptr<SourceLocationFile> locationFile;
bool hasAllSourceLocations = false;
bool isOverview = false;
};
int refCount;
struct CodeFileParams
{
static bool sort(const CodeFileParams& a, const CodeFileParams& b);
static bool sortById(const CodeFileParams& a, const CodeFileParams& b);
bool isCollapsed;
std::shared_ptr<SourceLocationFile> locationFile;
TimeStamp modificationTime;
size_t referenceCount = 0;
bool isDeclaration;
bool isDefinition;
bool isMinimized = true;
bool isDeclaration = false;
bool isDefinition = false;
bool insertSnippet;
bool reduced;
std::vector<CodeSnippetParams> snippetParams;
std::shared_ptr<CodeSnippetParams> fileParams; // TODO: replace with std::optional
};
#endif // CODE_SNIPPET_PARAMS_H
@@ -91,16 +91,21 @@ void SourceLocationCollection::addSourceLocationCopies(const SourceLocationColle
other->forEachSourceLocationFile(
[this](std::shared_ptr<SourceLocationFile> otherFile)
{
SourceLocationFile* file = createSourceLocationFile(
otherFile->getFilePath(), otherFile->getLanguage(), otherFile->isWhole(), otherFile->isComplete(),
otherFile->isIndexed());
addSourceLocationCopies(otherFile.get());
}
);
}
otherFile->forEachSourceLocation(
[file](SourceLocation* otherLocation)
{
file->addSourceLocationCopy(otherLocation);
}
);
void SourceLocationCollection::addSourceLocationCopies(const SourceLocationFile* otherFile)
{
SourceLocationFile* file = createSourceLocationFile(
otherFile->getFilePath(), otherFile->getLanguage(), otherFile->isWhole(), otherFile->isComplete(),
otherFile->isIndexed());
otherFile->forEachSourceLocation(
[file](SourceLocation* otherLocation)
{
file->addSourceLocationCopy(otherLocation);
}
);
}
@@ -35,6 +35,7 @@ public:
SourceLocation* addSourceLocationCopy(const SourceLocation* location);
void addSourceLocationCopies(const SourceLocationCollection* other);
void addSourceLocationCopies(const SourceLocationFile* otherFile);
void addSourceLocationFile(std::shared_ptr<SourceLocationFile> file);
+1 -1
View File
@@ -233,7 +233,7 @@ std::shared_ptr<SourceLocationFile> SourceLocationFile::getFilteredByTypes(const
}
std::shared_ptr<SourceLocationFile> ret =
std::make_shared<SourceLocationFile>(getFilePath(), getLanguage(), false, isComplete(), isIndexed());
std::make_shared<SourceLocationFile>(getFilePath(), getLanguage(), isWhole(), isComplete(), isIndexed());
for (const std::shared_ptr<SourceLocation>& location : m_locations)
{
@@ -1,6 +1,8 @@
#ifndef MESSAGE_CHANGE_FILE_VIEW_H
#define MESSAGE_CHANGE_FILE_VIEW_H
#include "CodeScrollParams.h"
#include "CodeSnippetParams.h"
#include "FilePath.h"
#include "Message.h"
#include "TabId.h"
@@ -27,15 +29,13 @@ public:
const FilePath& filePath,
FileState state,
ViewMode viewMode,
bool needsData,
bool showErrors,
CodeScrollParams scrollParams,
bool switchesViewMode = false
)
: filePath(filePath)
, state(state)
, viewMode(viewMode)
, needsData(needsData)
, showErrors(showErrors)
, scrollParams(scrollParams)
, switchesViewMode(switchesViewMode)
{
setSchedulerId(TabId::currentTab());
@@ -63,18 +63,12 @@ public:
case VIEW_SINGLE: os << L", single"; break;
case VIEW_CURRENT: os << L", current"; break;
}
if (needsData)
{
os << L", needs data";
}
}
const FilePath filePath;
const FileState state;
const ViewMode viewMode;
const bool needsData;
const bool showErrors;
const CodeScrollParams scrollParams;
const bool switchesViewMode;
};
@@ -21,6 +21,11 @@ public:
return "MessageScrollToLine";
}
void print(std::wostream& os) const override
{
os << filePath.wstr() << L":" << line;
}
const FilePath filePath;
size_t line;
};