Skip to content

Commit 1091aed

Browse files
srawlinsCommit Queue
authored andcommitted
DAS: only attempt to compute fixes for fix-supporting producers
The diff makes this change look bigger than it is. Here's how the code worked before: `compute()` called `_addFromProducers()`. `_addFromProducers()` had a local function, also called `compute()`, which is called per CorrectionProducer. The local `compute()` function would unconditionally create a ChangeBuilder and call each CorrectionProducer's `compute()` method, then call back out to an instance method called `_addFixFromBuilder()`, which would drop the computed change on the floor if `fixKind` was `null`. This CL contains the following changes to the above system: * The local `compute()` function and the instance method, `_addFixFromBuilder()` are combined together, into one instance method, `_addFromProducer()`. * Then, the `fixKind` check is moved to the very top of that method, bailing out _before_ computing changes if `fixKind == null`. Change-Id: I6c61059fc36ec1587b15ca01b43830a5b179da9a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417326 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent bdc01d1 commit 1091aed

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

pkg/analysis_server_plugin/lib/src/correction/fix_processor.dart

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,48 @@ class FixProcessor {
6464
return _fixes;
6565
}
6666

67-
void _addFixFromBuilder(ChangeBuilder builder, CorrectionProducer producer) {
68-
var change = builder.sourceChange;
69-
if (change.edits.isEmpty) {
70-
return;
71-
}
72-
67+
Future<void> _addFromProducer(CorrectionProducer producer) async {
7368
var kind = producer.fixKind;
69+
// If this producer is not actually designed to work as an fix, ignore it.
7470
if (kind == null) {
7571
return;
7672
}
7773

78-
change.id = kind.id;
79-
change.message = formatList(kind.message, producer.fixArguments);
80-
_fixes.add(Fix(kind: kind, change: change));
74+
var builder =
75+
ChangeBuilder(workspace: _fixContext.workspace, eol: producer.eol);
76+
try {
77+
var fixKind = producer.fixKind;
78+
79+
if (_performance != null) {
80+
var startTime = _timer.elapsedMilliseconds;
81+
await producer.compute(builder);
82+
_performance.producerTimings.add((
83+
className: producer.runtimeType.toString(),
84+
elapsedTime: _timer.elapsedMilliseconds - startTime,
85+
));
86+
} else {
87+
await producer.compute(builder);
88+
}
89+
90+
assert(
91+
!producer.canBeAppliedAcrossSingleFile || producer.fixKind == fixKind,
92+
'Producers used in bulk fixes must not modify the FixKind during '
93+
'computation. $producer changed from $fixKind to ${producer.fixKind}.',
94+
);
95+
96+
var change = builder.sourceChange;
97+
if (change.edits.isEmpty) {
98+
return;
99+
}
100+
101+
change.id = kind.id;
102+
change.message = formatList(kind.message, producer.fixArguments);
103+
_fixes.add(Fix(kind: kind, change: change));
104+
} on ConflictingEditException catch (exception, stackTrace) {
105+
// Handle the exception by (a) not adding a fix based on the producer
106+
// and (b) logging the exception.
107+
_fixContext.instrumentationService.logException(exception, stackTrace);
108+
}
81109
}
82110

83111
Future<void> _addFromProducers() async {
@@ -91,37 +119,6 @@ class FixProcessor {
91119
selectionLength: _fixContext.diagnostic.length,
92120
);
93121

94-
Future<void> compute(CorrectionProducer producer) async {
95-
var builder =
96-
ChangeBuilder(workspace: _fixContext.workspace, eol: producer.eol);
97-
try {
98-
var fixKind = producer.fixKind;
99-
100-
if (_performance != null) {
101-
var startTime = _timer.elapsedMilliseconds;
102-
await producer.compute(builder);
103-
_performance.producerTimings.add((
104-
className: producer.runtimeType.toString(),
105-
elapsedTime: _timer.elapsedMilliseconds - startTime,
106-
));
107-
} else {
108-
await producer.compute(builder);
109-
}
110-
111-
assert(
112-
!producer.canBeAppliedAcrossSingleFile || producer.fixKind == fixKind,
113-
'Producers used in bulk fixes must not modify the FixKind during '
114-
'computation. $producer changed from $fixKind to ${producer.fixKind}.',
115-
);
116-
117-
_addFixFromBuilder(builder, producer);
118-
} on ConflictingEditException catch (exception, stackTrace) {
119-
// Handle the exception by (a) not adding a fix based on the producer
120-
// and (b) logging the exception.
121-
_fixContext.instrumentationService.logException(exception, stackTrace);
122-
}
123-
}
124-
125122
var diagnosticCode = diagnostic.diagnosticCode;
126123
List<ProducerGenerator>? generators;
127124
List<MultiProducerGenerator>? multiGenerators;
@@ -137,14 +134,14 @@ class FixProcessor {
137134

138135
if (generators != null) {
139136
for (var generator in generators) {
140-
await compute(generator(context: context));
137+
await _addFromProducer(generator(context: context));
141138
}
142139
}
143140
if (multiGenerators != null) {
144141
for (var multiGenerator in multiGenerators) {
145142
var multiProducer = multiGenerator(context: context);
146143
for (var producer in await multiProducer.producers) {
147-
await compute(producer);
144+
await _addFromProducer(producer);
148145
}
149146
}
150147
}
@@ -163,7 +160,7 @@ class FixProcessor {
163160
continue;
164161
}
165162
}
166-
await compute(producer);
163+
await _addFromProducer(generator(context: context));
167164
}
168165
}
169166
}

0 commit comments

Comments
 (0)