Add duplicate (^D), basic search, bugfixes

* Use ^D to duplicate array items
* Use / to search, matching keys will be highlighted
* Don't attempt to load non-existing files when passed on command line
This commit is contained in:
2024-10-08 11:04:26 +02:00
parent 1f74bbfc0d
commit ca61374654
4 changed files with 87 additions and 26 deletions

View File

@@ -43,5 +43,42 @@ class Tree
}
}
public function search(string $search): array
{
return $this->searchNode(null, $this->root, $search);
}
/**
*
*
* @return array<Node>
*/
private function searchNode(?string $key, Node $node, string $search): array
{
$results = [];
if ($node instanceof ArrayNode) {
if ($key && str_contains($key, $search)) {
$results[] = $node;
}
foreach ($node->items as $index=>$item) {
$results = [ ...$results, ...$this->searchNode($index, $item, $search) ];
}
} elseif ($node instanceof ObjectNode) {
if ($key && str_contains($key, $search)) {
$results[] = $node;
}
foreach ($node->properties as $key=>$item) {
$results = [ ...$results, ...$this->searchNode($key, $item, $search) ];
}
} elseif ($node instanceof ValueNode) {
if (str_contains($node->value, $search) || ($key && str_contains($key, $search))) {
$results[] = $node;
}
}
return $results;
}
}