Skip to content

Commit

Permalink
properly handle nodes with names that are a prefix of other node names (
Browse files Browse the repository at this point in the history
#35)

Co-authored-by: Dominik <45536968+authaldo@users.noreply.github.com>
  • Loading branch information
ottojo and authaldo authored Feb 8, 2024
1 parent 1b65cdf commit b80672f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/node_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,20 @@ void NodeTree::addNode(const std::shared_ptr<TreeNode> &curNode, const std::stri
} else {
// found an existing node, check whether we have to subdivide it
const auto idx = name.find('/');
if (nextNode->children.empty() && idx != std::string::npos) {

if (nextNode->children.empty() && idx != std::string::npos && nextNode->name.find('/') != std::string::npos) {
// nextNode is leaf with prefix (namespace with single child is collapsed)
auto nextNodePrefix = nextNode->name.substr(0, idx);
auto nextNodeRemainingName = nextNode->name.substr(idx + 1);

nextNode->children.emplace_back(std::make_shared<TreeNode>(TreeNode{nextNodeRemainingName, nextNode->fullName}));

nextNode->name = nextNodePrefix;
nextNode->fullName.clear();
} else if (nextNode->children.empty() && idx != std::string::npos && nextNode->name == prefix) {
// nextNode is a leaf node with same name as next namespace token
// create sibling to nextNode with same name, and add node below that
nextNode = std::make_shared<TreeNode>(TreeNode{prefix, fullName});
curNode->children.emplace_back(nextNode);
}

addNode(nextNode, remainingName, fullName);
Expand All @@ -165,9 +170,13 @@ bool NodeTree::SortComparator::operator()(const std::shared_ptr<TreeNode> &node1
void visualizeNodeTree(const std::shared_ptr<const TreeNode>& root, std::string &selectedNode) {
if (root->children.empty()) {
// leaf node
// push "leaf" to id stack to prevent ID collision between node and namespace with same name
ImGui::PushID("leaf");
if (ImGui::Selectable(root->name.c_str(), selectedNode == root->fullName)) {

selectedNode = root->fullName;
}
ImGui::PopID();
} else {
// inner node
if (!root->name.empty()) {
Expand Down

0 comments on commit b80672f

Please sign in to comment.