1
0
mirror of synced 2025-01-30 19:43:43 +01:00

impr: Return early to remove nested code (#1253)

<!--
Please provide as much information as possible about what your PR aims
to do.
PRs with no description will most likely be closed until more
information is provided.
If you're planing on changing fundamental behaviour or add big new
features, please open a GitHub Issue first before starting to work on
it.
If it's not something big and you still want to contact us about it,
feel free to do so !
-->

### Problem description
<!-- Describe the bug that you fixed/feature request that you
implemented, or link to an existing issue describing it -->
This will simplify the codebase in the pattern drawer. It returns early
on conditional statements and reduces the amount of nested code making
it easier to read and track while developing.
This commit is contained in:
Paul Sorensen 2023-09-20 04:49:09 -04:00 committed by GitHub
parent c577a42f62
commit be8c679d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -109,9 +109,12 @@ namespace hex::plugin::builtin::ui {
} }
void drawOffsetColumn(const pl::ptrn::Pattern& pattern) { void drawOffsetColumn(const pl::ptrn::Pattern& pattern) {
if (auto *bitfieldMember = dynamic_cast<pl::ptrn::PatternBitfieldMember const*>(&pattern); bitfieldMember != nullptr && bitfieldMember->getParentBitfield() != nullptr) auto *bitfieldMember = dynamic_cast<pl::ptrn::PatternBitfieldMember const*>(&pattern);
if (bitfieldMember != nullptr && bitfieldMember->getParentBitfield() != nullptr) {
drawOffsetColumnForBitfieldMember(*bitfieldMember); drawOffsetColumnForBitfieldMember(*bitfieldMember);
else { return;
}
if (pattern.isPatternLocal()) { if (pattern.isPatternLocal()) {
ImGui::TextFormatted("[{}]", "hex.builtin.pattern_drawer.local"_lang); ImGui::TextFormatted("[{}]", "hex.builtin.pattern_drawer.local"_lang);
} else { } else {
@ -128,7 +131,6 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
} }
}
inline void drawSizeColumnForBitfieldMember(const pl::ptrn::PatternBitfieldMember &pattern) { inline void drawSizeColumnForBitfieldMember(const pl::ptrn::PatternBitfieldMember &pattern) {
if (pattern.getBitSize() == 1) if (pattern.getBitSize() == 1)
@ -155,7 +157,10 @@ namespace hex::plugin::builtin::ui {
std::vector<std::string> parseRValueFilter(const std::string &filter) { std::vector<std::string> parseRValueFilter(const std::string &filter) {
std::vector<std::string> result; std::vector<std::string> result;
if (!filter.empty()) { if (filter.empty()) {
return result;
}
result.emplace_back(); result.emplace_back();
for (char c : filter) { for (char c : filter) {
if (c == '.') if (c == '.')
@ -167,7 +172,6 @@ namespace hex::plugin::builtin::ui {
result.back() += c; result.back() += c;
} }
} }
}
return result; return result;
} }
@ -203,7 +207,11 @@ namespace hex::plugin::builtin::ui {
} }
void PatternDrawer::drawFavoriteColumn(const pl::ptrn::Pattern& pattern) { void PatternDrawer::drawFavoriteColumn(const pl::ptrn::Pattern& pattern) {
if (this->m_showFavoriteStars) { if (!this->m_showFavoriteStars) {
ImGui::TableNextColumn();
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
if (this->m_favorites.contains(this->m_currPatternPath)) { if (this->m_favorites.contains(this->m_currPatternPath)) {
@ -218,7 +226,6 @@ namespace hex::plugin::builtin::ui {
} }
ImGui::PopStyleVar(); ImGui::PopStyleVar();
}
ImGui::TableNextColumn(); ImGui::TableNextColumn();
} }
@ -305,7 +312,7 @@ namespace hex::plugin::builtin::ui {
ImGui::Unindent(); ImGui::Unindent();
return false; return false;
} }
else {
return highlightWhenSelected(pattern, [&]{ return highlightWhenSelected(pattern, [&]{
switch (this->m_treeStyle) { switch (this->m_treeStyle) {
using enum TreeStyle; using enum TreeStyle;
@ -319,7 +326,6 @@ namespace hex::plugin::builtin::ui {
} }
}); });
} }
}
void PatternDrawer::makeSelectable(const pl::ptrn::Pattern &pattern) { void PatternDrawer::makeSelectable(const pl::ptrn::Pattern &pattern) {
ImGui::PushID(static_cast<int>(pattern.getOffset())); ImGui::PushID(static_cast<int>(pattern.getOffset()));
@ -389,7 +395,11 @@ namespace hex::plugin::builtin::ui {
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "bits"); ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "bits");
ImGui::TableNextColumn(); ImGui::TableNextColumn();
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@ -423,9 +433,6 @@ namespace hex::plugin::builtin::ui {
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternBitfieldArray& pattern) { void PatternDrawer::visit(pl::ptrn::PatternBitfieldArray& pattern) {
@ -455,7 +462,10 @@ namespace hex::plugin::builtin::ui {
drawValueColumn(pattern); drawValueColumn(pattern);
} }
if (open) { if (!open) {
return;
}
int id = 1; int id = 1;
pattern.forEachEntry(0, pattern.getEntryCount(), [&] (u64, auto *field) { pattern.forEachEntry(0, pattern.getEntryCount(), [&] (u64, auto *field) {
ImGui::PushID(id); ImGui::PushID(id);
@ -467,12 +477,15 @@ namespace hex::plugin::builtin::ui {
closeTreeNode(pattern.isInlined()); closeTreeNode(pattern.isInlined());
} }
}
void PatternDrawer::visit(pl::ptrn::PatternBoolean& pattern) { void PatternDrawer::visit(pl::ptrn::PatternBoolean& pattern) {
createDefaultEntry(pattern); createDefaultEntry(pattern);
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@ -483,15 +496,16 @@ namespace hex::plugin::builtin::ui {
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternCharacter& pattern) { void PatternDrawer::visit(pl::ptrn::PatternCharacter& pattern) {
createDefaultEntry(pattern); createDefaultEntry(pattern);
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = hex::encodeByteString(pattern.getBytes()); auto value = hex::encodeByteString(pattern.getBytes());
@ -506,9 +520,6 @@ namespace hex::plugin::builtin::ui {
} }
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternEnum& pattern) { void PatternDrawer::visit(pl::ptrn::PatternEnum& pattern) {
@ -524,7 +535,11 @@ namespace hex::plugin::builtin::ui {
drawSizeColumn(pattern); drawSizeColumn(pattern);
drawTypenameColumn(pattern, "enum"); drawTypenameColumn(pattern, "enum");
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@ -547,15 +562,16 @@ namespace hex::plugin::builtin::ui {
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternFloat& pattern) { void PatternDrawer::visit(pl::ptrn::PatternFloat& pattern) {
createDefaultEntry(pattern); createDefaultEntry(pattern);
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@ -571,9 +587,6 @@ namespace hex::plugin::builtin::ui {
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternPadding& pattern) { void PatternDrawer::visit(pl::ptrn::PatternPadding& pattern) {
@ -610,7 +623,11 @@ namespace hex::plugin::builtin::ui {
void PatternDrawer::visit(pl::ptrn::PatternSigned& pattern) { void PatternDrawer::visit(pl::ptrn::PatternSigned& pattern) {
createDefaultEntry(pattern); createDefaultEntry(pattern);
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@ -626,16 +643,17 @@ namespace hex::plugin::builtin::ui {
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternString& pattern) { void PatternDrawer::visit(pl::ptrn::PatternString& pattern) {
if (pattern.getSize() > 0) { if (pattern.getSize() > 0) {
createDefaultEntry(pattern); createDefaultEntry(pattern);
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@ -647,9 +665,6 @@ namespace hex::plugin::builtin::ui {
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
} }
@ -688,7 +703,10 @@ namespace hex::plugin::builtin::ui {
} }
if (open) { if (!open) {
return;
}
int id = 1; int id = 1;
pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member){ pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member){
ImGui::PushID(id); ImGui::PushID(id);
@ -699,7 +717,6 @@ namespace hex::plugin::builtin::ui {
closeTreeNode(pattern.isInlined()); closeTreeNode(pattern.isInlined());
} }
}
void PatternDrawer::visit(pl::ptrn::PatternUnion& pattern) { void PatternDrawer::visit(pl::ptrn::PatternUnion& pattern) {
bool open = true; bool open = true;
@ -735,7 +752,10 @@ namespace hex::plugin::builtin::ui {
} }
} }
if (open) { if (!open) {
return;
}
int id = 1; int id = 1;
pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member) { pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member) {
ImGui::PushID(id); ImGui::PushID(id);
@ -747,12 +767,15 @@ namespace hex::plugin::builtin::ui {
closeTreeNode(pattern.isInlined()); closeTreeNode(pattern.isInlined());
} }
}
void PatternDrawer::visit(pl::ptrn::PatternUnsigned& pattern) { void PatternDrawer::visit(pl::ptrn::PatternUnsigned& pattern) {
createDefaultEntry(pattern); createDefaultEntry(pattern);
if (this->isEditingPattern(pattern)) { if (!this->isEditingPattern(pattern)) {
drawValueColumn(pattern);
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = pattern.toString(); auto value = pattern.toString();
@ -766,9 +789,6 @@ namespace hex::plugin::builtin::ui {
} }
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} else {
drawValueColumn(pattern);
}
} }
void PatternDrawer::visit(pl::ptrn::PatternWideCharacter& pattern) { void PatternDrawer::visit(pl::ptrn::PatternWideCharacter& pattern) {
@ -828,7 +848,10 @@ namespace hex::plugin::builtin::ui {
drawValueColumn(pattern); drawValueColumn(pattern);
} }
if (open) { if (!open) {
return;
}
u64 chunkCount = 0; u64 chunkCount = 0;
for (u64 i = 0; i < iterable.getEntryCount(); i += ChunkSize) { for (u64 i = 0; i < iterable.getEntryCount(); i += ChunkSize) {
chunkCount++; chunkCount++;
@ -843,7 +866,8 @@ namespace hex::plugin::builtin::ui {
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
displayEnd += DisplayEndStep; displayEnd += DisplayEndStep;
break; break;
} else { }
auto endIndex = std::min<u64>(iterable.getEntryCount(), i + ChunkSize); auto endIndex = std::min<u64>(iterable.getEntryCount(), i + ChunkSize);
bool chunkOpen = true; bool chunkOpen = true;
@ -881,7 +905,10 @@ namespace hex::plugin::builtin::ui {
} }
if (chunkOpen) { if (!chunkOpen) {
continue;
}
int id = 1; int id = 1;
iterable.forEachEntry(i, endIndex, [&](u64, auto *entry){ iterable.forEachEntry(i, endIndex, [&](u64, auto *entry){
ImGui::PushID(id); ImGui::PushID(id);
@ -894,12 +921,9 @@ namespace hex::plugin::builtin::ui {
if (iterable.getEntryCount() > ChunkSize) if (iterable.getEntryCount() > ChunkSize)
ImGui::TreePop(); ImGui::TreePop();
} }
}
}
closeTreeNode(isInlined); closeTreeNode(isInlined);
} }
}
u64& PatternDrawer::getDisplayEnd(const pl::ptrn::Pattern& pattern) { u64& PatternDrawer::getDisplayEnd(const pl::ptrn::Pattern& pattern) {
auto it = this->m_displayEnd.find(&pattern); auto it = this->m_displayEnd.find(&pattern);
@ -953,7 +977,10 @@ namespace hex::plugin::builtin::ui {
} }
bool PatternDrawer::beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) { bool PatternDrawer::beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) {
if (ImGui::BeginTable("##Patterntable", 8, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, ImVec2(0, height))) { if (!ImGui::BeginTable("##Patterntable", 8, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, ImVec2(0, height))) {
return false;
}
ImGui::TableSetupScrollFreeze(0, 1); ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("##favorite", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder | ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_IndentDisable, ImGui::GetTextLineHeight(), ImGui::GetID("favorite")); ImGui::TableSetupColumn("##favorite", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder | ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_IndentDisable, ImGui::GetTextLineHeight(), ImGui::GetID("favorite"));
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.var_name"_lang, ImGuiTableColumnFlags_PreferSortAscending | ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_IndentEnable, 0, ImGui::GetID("name")); ImGui::TableSetupColumn("hex.builtin.pattern_drawer.var_name"_lang, ImGuiTableColumnFlags_PreferSortAscending | ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_IndentEnable, 0, ImGui::GetID("name"));
@ -966,10 +993,15 @@ namespace hex::plugin::builtin::ui {
auto sortSpecs = ImGui::TableGetSortSpecs(); auto sortSpecs = ImGui::TableGetSortSpecs();
if (patterns.empty()) if (patterns.empty()) {
sortedPatterns.clear(); sortedPatterns.clear();
return true;
}
if (!sortSpecs->SpecsDirty && !sortedPatterns.empty()) {
return true;
}
if (!patterns.empty() && (sortSpecs->SpecsDirty || sortedPatterns.empty())) {
sortedPatterns.clear(); sortedPatterns.clear();
std::transform(patterns.begin(), patterns.end(), std::back_inserter(sortedPatterns), [](const std::shared_ptr<pl::ptrn::Pattern> &pattern) { std::transform(patterns.begin(), patterns.end(), std::back_inserter(sortedPatterns), [](const std::shared_ptr<pl::ptrn::Pattern> &pattern) {
return pattern.get(); return pattern.get();
@ -985,14 +1017,10 @@ namespace hex::plugin::builtin::ui {
}); });
sortSpecs->SpecsDirty = false; sortSpecs->SpecsDirty = false;
}
return true; return true;
} }
return false;
}
void PatternDrawer::traversePatternTree(pl::ptrn::Pattern &pattern, std::vector<std::string> &patternPath, const std::function<void(pl::ptrn::Pattern&)> &callback) { void PatternDrawer::traversePatternTree(pl::ptrn::Pattern &pattern, std::vector<std::string> &patternPath, const std::function<void(pl::ptrn::Pattern&)> &callback) {
patternPath.push_back(pattern.getVariableName()); patternPath.push_back(pattern.getVariableName());
ON_SCOPE_EXIT { patternPath.pop_back(); }; ON_SCOPE_EXIT { patternPath.pop_back(); };