blob: a941bae4eb2714052512147d4c4234a79f3ccf6b [file] [log] [blame]
// Copyright (c) 2021, 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/source.dart';
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_yaml.dart';
import 'package:yaml/yaml.dart';
/// An [EditBuilder] used to build edits in YAML files.
class YamlEditBuilderImpl extends EditBuilderImpl implements YamlEditBuilder {
/// Initialize a newly created builder to build a source edit.
YamlFileEditBuilderImpl sourceFileEditBuilder, int offset, int length)
: super(sourceFileEditBuilder, offset, length);
YamlFileEditBuilderImpl get dartFileEditBuilder =>
fileEditBuilder as YamlFileEditBuilderImpl;
void addLinkedEdit(String groupName,
void Function(YamlLinkedEditBuilder builder) buildLinkedEdit) =>
(builder) => buildLinkedEdit(builder as YamlLinkedEditBuilder));
LinkedEditBuilderImpl createLinkedEditBuilder() {
return YamlLinkedEditBuilderImpl(this);
/// Returns the indentation with the given [level].
String getIndent(int level) => ' ' * level;
/// A [FileEditBuilder] used to build edits for YAML files.
class YamlFileEditBuilderImpl extends FileEditBuilderImpl
implements YamlFileEditBuilder {
/// The document parsed from the file contents.
final YamlDocument document;
/// Initialize a newly created builder to build a source file edit within the
/// change being built by the given [changeBuilder]. The file being edited has
/// the given [filePath], [document], and [timeStamp].
YamlFileEditBuilderImpl(ChangeBuilderImpl changeBuilder, String filePath,
this.document, int timeStamp)
: super(changeBuilder, filePath, timeStamp);
void addInsertion(
int offset, void Function(YamlEditBuilder builder) buildEdit,
{bool insertBeforeExisting = false}) =>
offset, (builder) => buildEdit(builder as YamlEditBuilder),
insertBeforeExisting: insertBeforeExisting);
void addReplacement(SourceRange range,
void Function(YamlEditBuilder builder) buildEdit) =>
range, (builder) => buildEdit(builder as YamlEditBuilder));
YamlFileEditBuilderImpl copyWith(ChangeBuilderImpl changeBuilder,
{Map<YamlFileEditBuilderImpl, YamlFileEditBuilderImpl> editBuilderMap =
const {}}) {
var copy = YamlFileEditBuilderImpl(
changeBuilder, fileEdit.file, document, fileEdit.fileStamp);
return copy;
EditBuilderImpl createEditBuilder(int offset, int length) {
return YamlEditBuilderImpl(this, offset, length);
/// A [LinkedEditBuilder] used to build linked edits for YAML files.
class YamlLinkedEditBuilderImpl extends LinkedEditBuilderImpl
implements YamlLinkedEditBuilder {
/// Initialize a newly created linked edit builder.
YamlLinkedEditBuilderImpl(EditBuilderImpl editBuilder) : super(editBuilder);