Skip to content

Commit

Permalink
Add OrgNode.find
Browse files Browse the repository at this point in the history
  • Loading branch information
amake committed Sep 15, 2023
1 parent 133f47e commit 141fba5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/src/org.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ String parseOrgIdUrl(String url) {
return url.substring(3);
}

typedef OrgPath = List<OrgNode>;

/// The base type of all Org AST objects
abstract class OrgNode {
/// The children of this node. May be empty (no children) or null (an object
Expand Down Expand Up @@ -68,6 +70,25 @@ abstract class OrgNode {
return true;
}

({T node, OrgPath path})? find<T extends OrgNode>(
bool Function(T) predicate, [
OrgPath path = const [],
]) {
final self = this;
if (self is T && predicate(self)) {
return (node: self, path: path);
}
if (children != null) {
for (final child in children!) {
final result = child.find<T>(predicate, [...path, child]);
if (result != null) {
return result;
}
}
}
return null;
}

String toMarkup() {
final buf = StringBuffer();
_toMarkupImpl(buf);
Expand Down
45 changes: 45 additions & 0 deletions test/ast_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,51 @@ content''');
expect(sections, ['Foobar', 'Bizzbazz']);
});
});
group('find', () {
final result = parser.parse('''* Foobar
** Bizzbazz
/boo/
*** Bingbang
*blah*''');
final doc = result.value as OrgDocument;
test('find deep with type', () {
var visited = 0;
final found = doc.find<OrgMarkup>((node) {
visited += 1;
return node.style == OrgStyle.bold;
});
expect(found, isNotNull);
expect(found!.node.content, 'blah');
expect(visited, 2);
expect(found.path.map((n) => n.toString()), [
'OrgSection',
'OrgSection',
'OrgSection',
'OrgContent',
'OrgParagraph',
'OrgContent',
'OrgMarkup'
]);
});
test('find shallow with type', () {
var visited = 0;
final found = doc.find<OrgMarkup>((node) {
visited += 1;
return node.style == OrgStyle.italic;
});
expect(found, isNotNull);
expect(found!.node.content, 'boo');
expect(visited, 1);
expect(found.path.map((n) => n.toString()), [
'OrgSection',
'OrgSection',
'OrgContent',
'OrgParagraph',
'OrgContent',
'OrgMarkup'
]);
});
});
group('section ids', () {
test('has ids', () {
final result = parser.parse('''* Foobar
Expand Down

0 comments on commit 141fba5

Please sign in to comment.