Add a mixin for assists similar to that for fixes

R=scheglov@google.com

Review-Url: https://codereview.chromium.org/2956353002 .
This commit is contained in:
Brian Wilkerson 2017-06-28 11:44:21 -07:00
parent 37a5c124ee
commit 7bd9e6c443
7 changed files with 83 additions and 36 deletions

View file

@ -8,6 +8,7 @@ import 'package:analyzer/src/dart/analysis/driver.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart'
show SourceChange;
import 'package:analyzer_plugin/utilities/assist/assist.dart';
/**
* A description of a single proposed assist.
@ -26,7 +27,7 @@ class Assist {
*/
static final Comparator<Assist> SORT_BY_RELEVANCE =
(Assist firstAssist, Assist secondAssist) =>
firstAssist.kind.relevance - secondAssist.kind.relevance;
firstAssist.kind.priority - secondAssist.kind.priority;
/**
* A description of the assist being proposed.
@ -87,37 +88,3 @@ abstract class AssistContributor {
*/
Future<List<Assist>> computeAssists(AssistContext context);
}
/**
* A description of a class of assists. Instances are intended to hold the
* information that is common across a number of assists and to be shared by
* those assists.
*
* Clients may not extend, implement or mix-in this class.
*/
class AssistKind {
/**
* The name of this kind of assist, used for debugging.
*/
final String name;
/**
* The relevance of this kind of assist for the kind of error being addressed.
*/
final int relevance;
/**
* A human-readable description of the changes that will be applied by this
* kind of assist.
*/
final String message;
/**
* Initialize a newly created kind of assist to have the given [name],
* [relevance] and [message].
*/
const AssistKind(this.name, this.relevance, this.message);
@override
String toString() => name;
}

View file

@ -5,6 +5,7 @@
import 'package:analysis_server/plugin/edit/assist/assist_core.dart';
import 'package:analyzer/src/dart/analysis/driver.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
/**
* The implementation of [AssistContext].

View file

@ -26,6 +26,8 @@ import 'package:analyzer/src/generated/java_core.dart';
import 'package:analyzer/src/generated/resolver.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
import 'package:analyzer_plugin/utilities/assist/assist.dart'
hide AssistContributor;
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
import 'package:path/path.dart';

View file

@ -20,6 +20,7 @@ import 'package:analyzer/src/dart/element/ast_provider.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:plugin/manager.dart';
import 'package:plugin/plugin.dart';
import 'package:test/test.dart';

View file

@ -74,6 +74,42 @@ class AssistGenerator {
}
}
/**
* A description of a class of assists. Instances are intended to hold the
* information that is common across a number of assists and to be shared by
* those assists.
*
* Clients may not extend, implement or mix-in this class.
*/
class AssistKind {
/**
* The name of this kind of assist, used for debugging.
*/
final String name;
/**
* The priority of this kind of assist for the kind of error being addressed.
*/
final int priority;
/**
* A human-readable description of the changes that will be applied by this
* kind of assist. The message can contain parameters, where each parameter is
* represented by a zero-based index inside curly braces. For example, the
* message `"Create a component named '{0}' in '{1}'"` contains two parameters.
*/
final String message;
/**
* Initialize a newly created kind of assist to have the given [name],
* [relevance] and [message].
*/
const AssistKind(this.name, this.priority, this.message);
@override
String toString() => name;
}
/**
* The information about a requested set of assists.
*

View file

@ -0,0 +1,40 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/generated/java_core.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
/**
* A partial implementation of an [AssistContributor] that provides a utility
* method to make it easier to add assists.
*
* Clients may not extend or implement this class, but are allowed to use it as
* a mix-in when creating a subclass of [AssistContributor].
*/
abstract class AssistContributorMixin implements AssistContributor {
/**
* The collector to which assists should be added.
*/
AssistCollector get collector;
/**
* Add an assist. Use the [kind] of the assist to get the message and priority,
* and use the change [builder] to get the edits that comprise the assist. If
* the message has parameters, then use the list of [args] to populate the
* message.
*/
void addAssistFromBuilder(ChangeBuilder builder, AssistKind kind,
{List<Object> args}) {
SourceChange change = builder.sourceChange;
if (change.edits.isEmpty) {
return;
}
change.message = formatList(kind.message, args);
collector.addAssist(
new PrioritizedSourceChange(kind.priority, builder.sourceChange));
}
}

View file

@ -35,7 +35,7 @@ abstract class FixContributorMixin implements FixContributor {
* [args] to populate the message.
*/
void addFix(AnalysisError error, FixKind kind, ChangeBuilder builder,
{List<Object> args: null}) {
{List<Object> args}) {
SourceChange change = builder.sourceChange;
if (change.edits.isEmpty) {
return;