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
  • Loading branch information
ottojo committed Feb 5, 2024
1 parent 544fce2 commit 43c8a1c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 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,12 @@ 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;
}
ImGui::PopID();
} else {
// inner node
if (!root->name.empty()) {
Expand Down

0 comments on commit 43c8a1c

Please sign in to comment.