Skip to content

Commit 0c2eca6

Browse files
committed
Allow getting tree properties with inheritance
1 parent 47ec404 commit 0c2eca6

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

lib/src/org/model.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ sealed class OrgTree extends OrgParentNode {
219219
.toList(growable: false) ??
220220
const [];
221221

222+
List<String> getPropertiesWithInheritance(String key, OrgTree doc) =>
223+
doc
224+
.find((node) => identical(this, node))
225+
?.path
226+
.whereType<OrgTree>()
227+
.fold<List<String>>(
228+
[], (acc, tree) => acc..addAll(tree.getProperties(key))) ??
229+
const [];
230+
222231
/// Retrieve this section's PROPERTIES drawer, if it exists.
223232
OrgDrawer? get _propertiesDrawer {
224233
OrgDrawer? result;

test/org/model/parser_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,68 @@ content''');
424424
expect(doc.attachDir, (type: OrgAttachDirType.dir, dir: '/bar/'));
425425
});
426426
});
427+
group('properties', () {
428+
test('simple', () {
429+
final result = parser.parse('''
430+
:PROPERTIES:
431+
:FOO: foo
432+
:END:
433+
''');
434+
final doc = result.value as OrgDocument;
435+
expect(doc.getProperties(':FOO:'), ['foo']);
436+
expect(doc.getProperties('FOO'), isEmpty);
437+
expect(doc.getProperties(':BAR:'), isEmpty);
438+
});
439+
test('multiple', () {
440+
final result = parser.parse('''
441+
:PROPERTIES:
442+
:FOO: foo
443+
:BAR: bar
444+
:END:
445+
''');
446+
final doc = result.value as OrgDocument;
447+
expect(doc.getProperties(':FOO:'), ['foo']);
448+
expect(doc.getProperties(':BAR:'), ['bar']);
449+
});
450+
test('duplicate', () {
451+
final result = parser.parse('''
452+
:PROPERTIES:
453+
:FOO: foo
454+
:FOO: bar
455+
:END:
456+
''');
457+
final doc = result.value as OrgDocument;
458+
expect(doc.getProperties(':FOO:'), ['foo', 'bar']);
459+
});
460+
test('inheritance', () {
461+
final result = parser.parse('''
462+
:PROPERTIES:
463+
:FOO: foo
464+
:FOO: bar
465+
:END:
466+
467+
* Section
468+
:PROPERTIES:
469+
:FOO: baz
470+
:BAZINGA: qux
471+
:END:
472+
''');
473+
final doc = result.value as OrgDocument;
474+
expect(doc.getProperties(':FOO:'), ['foo', 'bar']);
475+
expect(doc.getProperties(':BAZINGA:'), isEmpty);
476+
final section = doc.sections[0];
477+
expect(section.getProperties(':FOO:'), ['baz']);
478+
expect(section.getProperties(':BAZINGA:'), ['qux']);
479+
expect(
480+
section.getPropertiesWithInheritance(':FOO:', doc),
481+
['foo', 'bar', 'baz'],
482+
);
483+
expect(
484+
section.getPropertiesWithInheritance(':BAZINGA:', doc),
485+
['qux'],
486+
);
487+
});
488+
});
427489
group('decrypted content', () {
428490
test('verbatim', () {
429491
final cleartext = '''foo

0 commit comments

Comments
 (0)