Skip to content

Commit 77dd008

Browse files
CatHood0CatHood0
andauthored
Fix: issue where mergers does not works as expected (#3)
* Fix: issue where mergers does not works as expected * Chore: reinserted a missed test --------- Co-authored-by: CatHood0 <santiagowmar@gmail.com>
1 parent 23d7354 commit 77dd008

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

lib/easy_parser/block_merger_builder.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ class BlockMergerBuilder extends MergerBuilder {
1515
final Paragraph curParagraph = paragraphs.elementAt(i);
1616
final Paragraph? nextParagraph = paragraphs.elementAtOrNull(i + 1);
1717
if (indexsIgnore.contains(i)) {
18+
if (nextParagraph != null) {
19+
if (canMergeBothParagraphs(
20+
paragraph: curParagraph, nextParagraph: nextParagraph)) {
21+
final Paragraph lastParagraph = result.last;
22+
final Paragraph paragraphResult = Paragraph(
23+
lines: <Line>[
24+
...lastParagraph.lines,
25+
...nextParagraph.lines,
26+
],
27+
blockAttributes: curParagraph.blockAttributes,
28+
type: curParagraph.type,
29+
);
30+
result[result.length - 1] = paragraphResult;
31+
indexsIgnore.add(i + 1);
32+
}
33+
}
1834
continue;
1935
}
2036
// check if the current iteration is the last

lib/easy_parser/common_merger_builder.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ class CommonMergerBuilder extends MergerBuilder {
1818
final Paragraph curParagraph = paragraphs.elementAt(i);
1919
final Paragraph? nextParagraph = paragraphs.elementAtOrNull(i + 1);
2020
if (indexsIgnore.contains(i)) {
21+
if (nextParagraph != null) {
22+
if (canMergeBothParagraphs(
23+
paragraph: curParagraph, nextParagraph: nextParagraph)) {
24+
final Paragraph lastParagraph = result.last;
25+
final Paragraph paragraphResult = Paragraph(
26+
lines: <Line>[
27+
...lastParagraph.lines,
28+
...nextParagraph.lines,
29+
],
30+
blockAttributes: curParagraph.blockAttributes,
31+
type: curParagraph.type,
32+
);
33+
result[result.length - 1] = paragraphResult;
34+
indexsIgnore.add(i + 1);
35+
}
36+
}
2137
continue;
2238
}
2339
// check if the current iteration is the last

test/flutter_quill_delta_easy_parser_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,34 @@ void main() {
206206
_execExpects(parsedDocument, expectedDocument);
207207
});
208208

209+
test(
210+
'Should merge different paragraphs with similar attributes into a same one',
211+
() {
212+
final Delta delta = Delta()
213+
..insert('void main() {')
214+
..insert('\n', {'code-block': true})
215+
..insert(' print("hello world!");')
216+
..insert('\n', {'code-block': true})
217+
..insert('}')
218+
..insert('\n', {'code-block': true})
219+
..insert('\n');
220+
221+
final Document expectedDocument = Document(paragraphs: [
222+
Paragraph.sealed(
223+
lines: [
224+
Line.fromData(data: 'void main() {'),
225+
Line.fromData(data: ' print("hello world!");'),
226+
Line.fromData(data: '}'),
227+
],
228+
blockAttributes: {"code-block": true},
229+
type: ParagraphType.block,
230+
),
231+
Paragraph.newLine(),
232+
]);
233+
final Document? parsedDocument = DocumentParser().parseDelta(delta: delta);
234+
_execExpects(parsedDocument, expectedDocument);
235+
});
236+
209237
test('Should handle empty Delta', () {
210238
final Delta emptyDelta = Delta();
211239

0 commit comments

Comments
 (0)