Skip to content

Commit e3b451f

Browse files
committed
Add ensureTrailingNewLine to OrgContent, OrgHeadline, OrgSection
1 parent 0a31507 commit e3b451f

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

lib/src/org/model/org_content.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ class OrgContent extends OrgParentNode {
3030

3131
OrgContent copyWith({List<OrgNode>? children, String? id}) =>
3232
OrgContent(children ?? this.children, id ?? this.id);
33+
34+
OrgContent ensureTrailingNewLine() {
35+
if (children.isNotEmpty) {
36+
final [...rest, last] = children;
37+
if (last is OrgElement) {
38+
return copyWith(
39+
children: [...rest, last.ensureTrailingNewLine()],
40+
);
41+
}
42+
}
43+
return copyWith(
44+
children: [...children, OrgParagraph('', OrgContent([]), '\n')],
45+
);
46+
}
3347
}

lib/src/org/model/org_headline.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,7 @@ class OrgHeadline extends OrgParentNode {
166166
?.isEndState(keyword!.value) ==
167167
true;
168168
}
169+
170+
OrgHeadline ensureTrailingNewLine() =>
171+
trailing.contains('\n') ? this : copyWith(trailing: '$trailing\n');
169172
}

lib/src/org/model/org_section.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,8 @@ class OrgSection extends OrgTree {
210210
);
211211

212212
@override
213-
OrgSection _ensureContent({required OrgContent content}) => copyWith(
214-
headline: headline.trailing?.contains('\n') == true
215-
? headline
216-
: headline.copyWith(trailing: '\n'),
217-
content: content);
213+
OrgSection _ensureContent({required OrgContent content}) =>
214+
copyWith(headline: headline.ensureTrailingNewLine(), content: content);
218215

219216
@override
220217
bool contains(Pattern pattern, {bool includeChildren = true}) =>
@@ -223,4 +220,10 @@ class OrgSection extends OrgTree {
223220

224221
@override
225222
String toString() => 'OrgSection';
223+
224+
OrgSection ensureTrailingNewLine() {
225+
return content == null
226+
? copyWith(headline: headline.ensureTrailingNewLine())
227+
: copyWith(content: content!.ensureTrailingNewLine());
228+
}
226229
}

0 commit comments

Comments
 (0)