Skip to content

Commit 6d5d1c3

Browse files
committed
Add back support for diff attribute to code block processing
1 parent 2b2a3ef commit 6d5d1c3

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

site/lib/src/extensions/code_block_processor.dart

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,23 @@ final class CodeBlockProcessor implements PageExtension {
8080
}
8181
}
8282

83-
final codeLines = _removeHighlights(lines, skipHighlighting);
83+
final isDiff = metadata.containsKey('diff');
84+
if (isDiff && showLineNumbers) {
85+
throw ArgumentError(
86+
'showLineNumbers and diff are not supported on the same '
87+
'code block yet.',
88+
);
89+
}
90+
91+
final diffResult = isDiff ? _processDiffLines(lines) : null;
92+
final linesWithDiffRemoved = diffResult?.lines ?? lines;
93+
final addedLines = diffResult?.addedLines ?? const <int>{};
94+
final removedLines = diffResult?.removedLines ?? const <int>{};
95+
96+
final codeLines = _removeHighlights(
97+
linesWithDiffRemoved,
98+
skipHighlighting,
99+
);
84100
final processedContent = _highlightCode(
85101
codeLines,
86102
language: language,
@@ -101,6 +117,8 @@ final class CodeBlockProcessor implements PageExtension {
101117
},
102118
title: title,
103119
highlightLines: _parseNumbersAndRanges(rawHighlightLines),
120+
addedLines: addedLines,
121+
removedLines: removedLines,
104122
tag: tag != null ? CodeBlockTag.parse(tag) : null,
105123
initialLineNumber: initialLineNumber ?? 1,
106124
showLineNumbers: showLineNumbers,
@@ -387,6 +405,42 @@ final class CodeBlockProcessor implements PageExtension {
387405
),
388406
];
389407
}
408+
409+
/// Processes lines for diff mode, extracting added/removed line markers.
410+
///
411+
/// Lines starting with '+' are marked as added lines.
412+
/// Lines starting with '-' are marked as removed lines.
413+
/// The first two characters (marker and space) are removed from each line.
414+
({
415+
List<String> lines,
416+
Set<int> addedLines,
417+
Set<int> removedLines,
418+
})
419+
_processDiffLines(List<String> lines) {
420+
final addedLines = <int>{};
421+
final removedLines = <int>{};
422+
final processedLines = <String>[];
423+
424+
for (var lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
425+
final line = lines[lineIndex];
426+
427+
switch (line.isNotEmpty ? line[0] : '') {
428+
case '+':
429+
addedLines.add(lineIndex + 1);
430+
case '-':
431+
removedLines.add(lineIndex + 1);
432+
}
433+
434+
// Remove the first 2 characters (marker and space).
435+
processedLines.add(line.length >= 2 ? line.substring(2) : '');
436+
}
437+
438+
return (
439+
lines: processedLines,
440+
addedLines: addedLines,
441+
removedLines: removedLines,
442+
);
443+
}
390444
}
391445

392446
@immutable

src/content/release/breaking-changes/integration-test-default-golden-comparator.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ In cases where users wrote custom test infrastructure and comparators, consider
5454
instead removing the [`goldenFileComparator`][] overrides, and instead rely on
5555
the (new) default which should work as expected:
5656

57-
```diff
58-
import 'package:integration_test/integration_test.dart';
59-
-import 'package:my_integration_test/custom_golden_file_comparator.dart';
57+
```dart diff
58+
import 'package:integration_test/integration_test.dart';
59+
- import 'package:my_integration_test/custom_golden_file_comparator.dart';
6060
61-
void main() {
62-
- goldenFileComparator = CustomGoldenFileComparatorThatWorks();
61+
void main() {
62+
- goldenFileComparator = CustomGoldenFileComparatorThatWorks();
6363
64-
// ...
65-
}
64+
// ...
65+
}
6666
```
6767

6868
_Fun fact_: The existing code that was used for

0 commit comments

Comments
 (0)