From 6b2f66ac47175d58fc03f0f446b2b4ddf8fe6f3f Mon Sep 17 00:00:00 2001 From: Aaron Madlon-Kay Date: Sat, 7 Sep 2024 18:24:59 +0900 Subject: [PATCH] Add optional `where` predicate to OrgTree.findContainingTree --- lib/src/org/model.dart | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/src/org/model.dart b/lib/src/org/model.dart index df963bd..bcbbf25 100644 --- a/lib/src/org/model.dart +++ b/lib/src/org/model.dart @@ -186,11 +186,16 @@ sealed class OrgTree extends OrgParentNode { /// Find the immediate parent [OrgSection] or [OrgDocument] of the specified /// [node]. - OrgTree? findContainingTree(T node) { + OrgTree? findContainingTree(T node, + {bool Function(OrgTree)? where}) { + where ??= (_) => true; final found = find((n) => identical(node, n)); if (found == null) return null; final (node: _, path: path) = found; - return path.reversed.firstWhere((node) => node is OrgTree) as OrgTree; + for (final node in path.reversed) { + if (node is OrgTree && where(node)) return node; + } + return null; } /// Get the directory in which attachments are expected to be found for this