YamlEditor class

An interface for modifying YAML documents while preserving comments and whitespaces.

YAML parsing is supported by package:yaml, and modifications are performed as string operations. An error will be thrown if internal assertions fail - such a situation should be extremely rare, and should only occur with degenerate formatting.

Most modification methods require the user to pass in an Iterable<Object> path that holds the keys/indices to navigate to the element.

Example:

a: 1
b: 2
c:
  - 3
  - 4
  - {e: 5, f: [6, 7]}

To get to 7, our path will be ['c', 2, 'f', 1]. The path for the base object is the empty array []. All modification methods will throw a ArgumentError if the path provided is invalid. Note also that that the order of elements in the path is important, and it should be arranged in order of calling, with the first element being the first key or index to be called.

In most modification methods, users are required to pass in a value to be used for updating the YAML tree. This value is only allowed to either be a valid scalar that is recognizable by YAML (i.e. bool, String, List, Map, num, null) or a YamlNode. Should the user want to specify the style to be applied to the value passed in, the user may wrap the value using wrapAsYamlNode while passing in the appropriate scalarStyle or collectionStyle. While we try to respect the style that is passed in, there will be instances where the formatting will not result in valid YAML, and as such we will fallback to a default formatting while preserving the content.

To dump the YAML after all the modifications have been completed, simply call toString().

Annotations
  • @sealed

Constructors

YamlEditor(String yaml)
factory

Properties

edits List<SourceEdit>
List of SourceEdits that have been applied to _yaml since the creation of this instance, in chronological order. Intended to be compatible with package:analysis_server.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

appendToList(Iterable<Object?> path, Object? value) → void
Appends value to the list at path.
insertIntoList(Iterable<Object?> path, int index, Object? value) → void
Inserts value into the list at path.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
parseAt(Iterable<Object?> path, {YamlNode orElse()?}) → YamlNode
Parses the document to return YamlNode currently present at path.
prependToList(Iterable<Object?> path, Object? value) → void
Prepends value to the list at path.
remove(Iterable<Object?> path) → YamlNode
Removes the node at path. Comments "belonging" to the node will be removed while surrounding comments will be left untouched.
spliceList(Iterable<Object?> path, int index, int deleteCount, Iterable<Object?> values) Iterable<YamlNode>
Changes the contents of the list at path by removing deleteCount items at index, and inserting values in-place. Returns the elements that are deleted.
toString() String
Returns the current YAML string.
override
update(Iterable<Object?> path, Object? value) → void
Sets value in the path.

Operators

operator ==(Object other) bool
The equality operator.
inherited