blob: cf173141479e035cd80827895f696e261ec32b12 [file] [log] [blame]
// Copyright (c) 2020, 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:analysis_server/src/services/correction/fix/data_driven/value_generator.dart';
import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
/// An object used to generate code to be inserted.
class CodeTemplate {
/// The kind of code that will be generated by this template.
final CodeTemplateKind kind;
/// The components of the template.
final List<TemplateComponent> components;
/// Initialize a newly generated code template with the given [kind] and
/// [components].
CodeTemplate(this.kind, this.components);
/// Use the [context] to validate that this template will be able to generate
/// a value.
bool validate(TemplateContext context) {
for (var component in components) {
if (!component.validate(context)) {
return false;
}
}
return true;
}
void writeOn(DartEditBuilder builder, TemplateContext context) {
for (var component in components) {
component.writeOn(builder, context);
}
}
}
/// The kinds of code that can be generated by a template.
enum CodeTemplateKind {
expression,
statements,
}
/// An object used to compute some portion of a template.
abstract class TemplateComponent {
/// Use the [context] to validate that this component will be able to generate
/// a value.
bool validate(TemplateContext context);
/// Write the text contributed by this component to the given [builder], using
/// the [context] to access needed information that isn't already known to
/// this component.
void writeOn(DartEditBuilder builder, TemplateContext context);
}
/// The context in which a template is being evaluated.
class TemplateContext {
/// The node in the AST that is being transformed.
final AstNode node;
/// The utilities used to help extract the code associated with various nodes.
final CorrectionUtils utils;
/// Initialize a newly created variable support.
TemplateContext(this.node, this.utils);
}
/// Literal text within a template.
class TemplateText extends TemplateComponent {
/// The literal text to be included in the resulting code.
final String text;
/// Initialize a newly create template text with the given [text].
TemplateText(this.text);
@override
bool validate(TemplateContext context) {
return true;
}
@override
void writeOn(DartEditBuilder builder, TemplateContext context) {
builder.write(text);
}
}
/// A reference to a variable within a template.
class TemplateVariable extends TemplateComponent {
/// The generator used to compute the value of the variable.
final ValueGenerator generator;
/// Initialize a newly created template variable with the given [generator].
TemplateVariable(this.generator);
@override
bool validate(TemplateContext context) {
return generator.validate(context);
}
@override
void writeOn(DartEditBuilder builder, TemplateContext context) {
generator.writeOn(builder, context);
}
}