Skip to content

Commit

Permalink
DAS: Simplify CorrectionProducer, et al.
Browse files Browse the repository at this point in the history
Just a lot of tidying in preparation for this code being public API
in the `analysis_server_plugin` package.

A big change:
* Convert `CorrectionProducer.assistArguments`,
  `CorrectionProducer.fixArguments`, and
  `CorrectionProducer.multiFixArguments` each from a `List<Object>?`
  to a `List<String>?`. This should be a no-op.

Smaller changes:
* Make private: `AssistProcessor.generators`, `.multiGenerators`,
  `.assistContext`, `.assists`,
* Make private: `CorrectionProducerContext.selectionOffset`,
  `.selectionLength`, `.utils`, `.sessionHelper`, `.unitResult`,
  `.applyingBulkFixes`, `.diagnostic`, `.node`, `.token`.
* Make private: `FixProcessor.fixContext`.
* Remove `CorrectionProducerContext.selectionEnd`, `.unit`, `.file`,
  `.session`, `.workspace`, `.typeProvider`; these can be accessed via
  getters.
* Remove unused `FixProcessor.computeFix()`.
* Make doc comments more idiomatic.

Change-Id: I100fe81aad612967191568fe207ff0b807f131b3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/364420
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Apr 25, 2024
1 parent b0535bd commit f6828dd
Show file tree
Hide file tree
Showing 62 changed files with 243 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import 'package:analyzer_plugin/utilities/change_builder/conflicting_edit_except
/// The computer for Dart assists.
class AssistProcessor {
/// A list of the generators used to produce assists.
static const List<ProducerGenerator> generators = [
static const List<ProducerGenerator> _generators = [
AddDiagnosticPropertyReference.new,
AddReturnType.new,
AddTypeAnnotation.bulkFixable,
Expand Down Expand Up @@ -155,23 +155,23 @@ class AssistProcessor {
];

/// A list of the multi-generators used to produce assists.
static const List<MultiProducerGenerator> multiGenerators = [
static const List<MultiProducerGenerator> _multiGenerators = [
FlutterWrap.new,
SurroundWith.new,
];

final DartAssistContext assistContext;
final DartAssistContext _assistContext;

final List<Assist> assists = <Assist>[];
final List<Assist> _assists = [];

AssistProcessor(this.assistContext);
AssistProcessor(this._assistContext);

Future<List<Assist>> compute() async {
if (isMacroGenerated(assistContext.resolveResult.file.path)) {
return assists;
if (isMacroGenerated(_assistContext.resolveResult.file.path)) {
return _assists;
}
await _addFromProducers();
return assists;
return _assists;
}

void _addAssistFromBuilder(ChangeBuilder builder, AssistKind kind,
Expand All @@ -182,24 +182,23 @@ class AssistProcessor {
}
change.id = kind.id;
change.message = formatList(kind.message, args);
assists.add(Assist(kind, change));
_assists.add(Assist(kind, change));
}

Future<void> _addFromProducers() async {
var context = CorrectionProducerContext.createResolved(
selectionOffset: assistContext.selectionOffset,
selectionLength: assistContext.selectionLength,
resolvedResult: assistContext.resolveResult,
workspace: assistContext.workspace,
selectionOffset: _assistContext.selectionOffset,
selectionLength: _assistContext.selectionLength,
resolvedResult: _assistContext.resolveResult,
);
if (context == null) {
return;
}

Future<void> compute(CorrectionProducer producer) async {
producer.configure(context);
var builder = ChangeBuilder(
workspace: context.workspace, eol: context.utils.endOfLine);
var builder =
ChangeBuilder(workspace: _assistContext.workspace, eol: producer.eol);
try {
await producer.compute(builder);
var assistKind = producer.assistKind;
Expand All @@ -210,21 +209,21 @@ class AssistProcessor {
} on ConflictingEditException catch (exception, stackTrace) {
// Handle the exception by (a) not adding an assist based on the
// producer and (b) logging the exception.
assistContext.instrumentationService
_assistContext.instrumentationService
.logException(exception, stackTrace);
}
}

for (var generator in generators) {
for (var generator in _generators) {
if (!_generatorAppliesToAnyLintRule(
generator,
assistContext.producerGeneratorsForLintRules[generator] ?? {},
_assistContext.producerGeneratorsForLintRules[generator] ?? {},
)) {
var producer = generator();
await compute(producer);
}
}
for (var multiGenerator in multiGenerators) {
for (var multiGenerator in _multiGenerators) {
var multiProducer = multiGenerator();
multiProducer.configure(context);
for (var producer in await multiProducer.producers) {
Expand All @@ -240,17 +239,17 @@ class AssistProcessor {
Set<String> errorCodes,
) {
var selectionEnd =
assistContext.selectionOffset + assistContext.selectionLength;
var locator = NodeLocator(assistContext.selectionOffset, selectionEnd);
var node = locator.searchWithin(assistContext.resolveResult.unit);
_assistContext.selectionOffset + _assistContext.selectionLength;
var locator = NodeLocator(_assistContext.selectionOffset, selectionEnd);
var node = locator.searchWithin(_assistContext.resolveResult.unit);
if (node == null) {
return false;
}

var fileOffset = node.offset;
for (var error in assistContext.resolveResult.errors) {
for (var error in _assistContext.resolveResult.errors) {
var errorSource = error.source;
if (assistContext.resolveResult.path == errorSource.fullName) {
if (_assistContext.resolveResult.path == errorSource.fullName) {
if (fileOffset >= error.offset &&
fileOffset <= error.offset + error.length) {
if (errorCodes.contains(error.errorCode.name)) {
Expand All @@ -263,7 +262,7 @@ class AssistProcessor {
}

static Map<ProducerGenerator, Set<String>> computeLintRuleMap() => {
for (var generator in generators)
for (var generator in _generators)
generator: {
for (var MapEntry(key: lintName, value: generators)
in FixProcessor.lintProducerMap.entries)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ class BulkFixProcessor {
resolvedResult: unit,
selectionOffset: diagnostic.offset,
selectionLength: diagnostic.length,
workspace: workspace,
);
}

Expand Down Expand Up @@ -721,7 +720,6 @@ class BulkFixProcessor {
resolvedResult: result,
selectionOffset: diagnostic.offset,
selectionLength: diagnostic.length,
workspace: workspace,
);
if (context == null) {
return;
Expand Down Expand Up @@ -784,7 +782,6 @@ class BulkFixProcessor {
resolvedResult: result,
selectionOffset: diagnostic.offset,
selectionLength: diagnostic.length,
workspace: workspace,
);

var errorCode = diagnostic.errorCode;
Expand Down Expand Up @@ -854,7 +851,7 @@ class BulkFixProcessor {
await _applyProducer(context, producer);
var newHash = computeChangeHash();
if (newHash != oldHash) {
changeMap.add(context.unitResult.path, code.toLowerCase());
changeMap.add(context.path, code.toLowerCase());
}
}

Expand Down
Loading

0 comments on commit f6828dd

Please sign in to comment.