Skip to content

Commit 337977d

Browse files
committed
- Improved formatting when specifying source on encodePrioritizedSourceChanges/matcherNormalizedPrioritizedSourceChangeSnapshot
1 parent 897d361 commit 337977d

File tree

8 files changed

+87
-32
lines changed

8 files changed

+87
-32
lines changed

packages/custom_lint_core/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Unreleased patch
2+
3+
- Improved formatting when specifying `source` on `encodePrioritizedSourceChanges`/`matcherNormalizedPrioritizedSourceChangeSnapshot`
4+
15
## 0.5.13 - 2024-02-03
26

37
- Improved formatting when specifying `source` on `encodePrioritizedSourceChanges`/`matcherNormalizedPrioritizedSourceChangeSnapshot`

packages/custom_lint_core/lib/src/matcher.dart

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dart:convert';
22
import 'dart:io';
3+
import 'dart:math';
34

5+
import 'package:analyzer/source/line_info.dart';
46
import 'package:analyzer_plugin/protocol/protocol_common.dart';
57
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
68
import 'package:matcher/matcher.dart';
@@ -19,6 +21,8 @@ String encodePrioritizedSourceChanges(
1921
String? source,
2022
}) {
2123
if (source != null) {
24+
final sourceLineInfo = LineInfo.fromContent(source);
25+
2226
final buffer = StringBuffer();
2327

2428
for (final prioritizedSourceChange in changes) {
@@ -29,6 +33,8 @@ String encodePrioritizedSourceChanges(
2933
prioritizedSourceChange.change.edits.expand((element) => element.edits),
3034
);
3135

36+
final outputLineInfo = LineInfo.fromContent(output);
37+
3238
// Get the offset of the first changed character between output and source.
3339
var firstDiffOffset = 0;
3440
for (; firstDiffOffset < source.length; firstDiffOffset++) {
@@ -49,17 +55,58 @@ String encodePrioritizedSourceChanges(
4955
}
5056
}
5157

52-
buffer.writeln('<<<< start: $firstDiffOffset -- end: $endSourceOffset');
53-
if (firstDiffOffset != endSourceOffset) {
54-
buffer.writeln(source.substring(firstDiffOffset, endSourceOffset));
58+
final firstChangedLine =
59+
sourceLineInfo.getLocation(firstDiffOffset).lineNumber - 1;
60+
61+
void writeDiff({
62+
required String file,
63+
required LineInfo lineInfo,
64+
required int endOffset,
65+
required String token,
66+
required int leadingCount,
67+
required int trailingCount,
68+
}) {
69+
final lastChangedLine = lineInfo.getLocation(endOffset).lineNumber - 1;
70+
final endLine =
71+
min(lastChangedLine + trailingCount, lineInfo.lineCount - 1);
72+
for (var line = max(0, firstChangedLine - leadingCount);
73+
line <= endLine;
74+
line++) {
75+
final changed = line >= firstChangedLine && line <= lastChangedLine;
76+
if (changed) buffer.write(token);
77+
78+
final endOfSource = !(line + 1 < lineInfo.lineCount);
79+
80+
buffer.write(
81+
file.substring(
82+
lineInfo.getOffsetOfLine(line),
83+
endOfSource ? null : lineInfo.getOffsetOfLine(line + 1) - 1,
84+
),
85+
);
86+
if (!endOfSource) buffer.writeln();
87+
}
5588
}
5689

57-
buffer.writeln('==== start: $firstDiffOffset -- end: $endOutputOffset');
58-
if (firstDiffOffset != endOutputOffset) {
59-
buffer.writeln(output.substring(firstDiffOffset, endOutputOffset));
60-
}
90+
buffer.writeln('=== diff (starting at line ${firstChangedLine + 1})');
91+
writeDiff(
92+
file: source,
93+
lineInfo: sourceLineInfo,
94+
endOffset: endSourceOffset,
95+
leadingCount: 2,
96+
trailingCount: 0,
97+
token: '- ',
98+
);
99+
100+
writeDiff(
101+
file: output,
102+
lineInfo: outputLineInfo,
103+
endOffset: endOutputOffset,
104+
leadingCount: 0,
105+
trailingCount: 2,
106+
token: '+ ',
107+
);
61108

62-
buffer.writeln('>>>>');
109+
buffer.writeln('===');
63110
}
64111

65112
return buffer.toString();

packages/custom_lint_core/test/assist_test.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ void main() {
4444
expect(
4545
await changes,
4646
matcherNormalizedPrioritizedSourceChangeSnapshot(
47-
'snapshot.snap',
47+
'snapshot.diff',
4848
source: fileSource,
4949
),
5050
);
5151
expect(
5252
await changes,
5353
isNot(
5454
matcherNormalizedPrioritizedSourceChangeSnapshot(
55-
'snapshot2.snap',
55+
'snapshot2.diff',
5656
source: fileSource,
5757
),
5858
),
@@ -62,15 +62,15 @@ void main() {
6262
await changes2,
6363
isNot(
6464
matcherNormalizedPrioritizedSourceChangeSnapshot(
65-
'snapshot.snap',
65+
'snapshot.diff',
6666
source: fileSource,
6767
),
6868
),
6969
);
7070
expect(
7171
await changes2,
7272
matcherNormalizedPrioritizedSourceChangeSnapshot(
73-
'snapshot2.snap',
73+
'snapshot2.diff',
7474
source: fileSource,
7575
),
7676
);
@@ -137,15 +137,15 @@ void main() {
137137
expect(
138138
await changes,
139139
matcherNormalizedPrioritizedSourceChangeSnapshot(
140-
'snapshot.snap',
140+
'snapshot.diff',
141141
source: fileSource,
142142
),
143143
);
144144
expect(
145145
await changes,
146146
isNot(
147147
matcherNormalizedPrioritizedSourceChangeSnapshot(
148-
'snapshot2.snap',
148+
'snapshot2.diff',
149149
source: fileSource,
150150
),
151151
),
@@ -155,15 +155,15 @@ void main() {
155155
await changes2,
156156
isNot(
157157
matcherNormalizedPrioritizedSourceChangeSnapshot(
158-
'snapshot.snap',
158+
'snapshot.diff',
159159
source: fileSource,
160160
),
161161
),
162162
);
163163
expect(
164164
await changes2,
165165
matcherNormalizedPrioritizedSourceChangeSnapshot(
166-
'snapshot2.snap',
166+
'snapshot2.diff',
167167
source: fileSource,
168168
),
169169
);

packages/custom_lint_core/test/fix_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ void main() {
3333
expect(
3434
await changes,
3535
matcherNormalizedPrioritizedSourceChangeSnapshot(
36-
'snapshot.snap',
36+
'snapshot.diff',
3737
source: fileSource,
3838
),
3939
);
4040
expect(
4141
await changes,
4242
isNot(
4343
matcherNormalizedPrioritizedSourceChangeSnapshot(
44-
'snapshot2.snap',
44+
'snapshot2.diff',
4545
source: fileSource,
4646
),
4747
),
@@ -51,15 +51,15 @@ void main() {
5151
await changes2,
5252
isNot(
5353
matcherNormalizedPrioritizedSourceChangeSnapshot(
54-
'snapshot.snap',
54+
'snapshot.diff',
5555
source: fileSource,
5656
),
5757
),
5858
);
5959
expect(
6060
await changes2,
6161
matcherNormalizedPrioritizedSourceChangeSnapshot(
62-
'snapshot2.snap',
62+
'snapshot2.diff',
6363
source: fileSource,
6464
),
6565
);
@@ -81,15 +81,15 @@ void main() {
8181
expect(
8282
await changes,
8383
matcherNormalizedPrioritizedSourceChangeSnapshot(
84-
'snapshot.snap',
84+
'snapshot.diff',
8585
source: fileSource,
8686
),
8787
);
8888
expect(
8989
await changes,
9090
isNot(
9191
matcherNormalizedPrioritizedSourceChangeSnapshot(
92-
'snapshot2.snap',
92+
'snapshot2.diff',
9393
source: fileSource,
9494
),
9595
),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Message: `MyAssist`
2+
=== diff (starting at line 2)
3+
void main() {
4+
- print('Hello world');
5+
+ Helloprint('Hello world');
6+
}
7+
===

packages/custom_lint_core/test/snapshot.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Message: `Another`
2+
=== diff (starting at line 2)
3+
void main() {
4+
- print('Hello world');
5+
+ Helloprint('Hello world');
6+
}
7+
===

packages/custom_lint_core/test/snapshot2.snap

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)