Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9cd7608

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Consolidate the yaml extensions in one library
Change-Id: Ide0e02145581c1a77c4af2764821c63a333d9dd1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174520 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 1d8d2de commit 9cd7608

File tree

3 files changed

+58
-74
lines changed

3 files changed

+58
-74
lines changed

pkg/analysis_server/lib/src/services/completion/yaml/yaml_completion_generator.dart

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:analysis_server/src/protocol_server.dart';
66
import 'package:analysis_server/src/services/completion/yaml/producer.dart';
7+
import 'package:analysis_server/src/utilities/extensions/yaml.dart';
78
import 'package:analyzer/file_system/file_system.dart';
89
import 'package:yaml/yaml.dart';
910

@@ -182,62 +183,3 @@ class YamlCompletionResults {
182183
replacementOffset = 0,
183184
replacementLength = 0;
184185
}
185-
186-
extension on YamlMap {
187-
/// Return the node representing the key that corresponds to the value
188-
/// represented by the [value] node.
189-
YamlNode keyAtValue(YamlNode value) {
190-
for (var entry in nodes.entries) {
191-
if (entry.value == value) {
192-
return entry.key;
193-
}
194-
}
195-
return null;
196-
}
197-
}
198-
199-
extension on YamlNode {
200-
/// Return the child of this node that contains the given [offset], or `null`
201-
/// if none of the children contains the offset.
202-
YamlNode childContainingOffset(int offset) {
203-
var node = this;
204-
if (node is YamlList) {
205-
for (var element in node.nodes) {
206-
if (element.containsOffset(offset)) {
207-
return element;
208-
}
209-
}
210-
for (var element in node.nodes) {
211-
if (element is YamlScalar && element.value == null) {
212-
// TODO(brianwilkerson) Testing for a null value probably gets
213-
// confused when there are multiple null values.
214-
return element;
215-
}
216-
}
217-
} else if (node is YamlMap) {
218-
for (var entry in node.nodes.entries) {
219-
if ((entry.key as YamlNode).containsOffset(offset)) {
220-
return entry.key;
221-
}
222-
var value = entry.value;
223-
if (value.containsOffset(offset) ||
224-
(value is YamlScalar && value.value == null)) {
225-
// TODO(brianwilkerson) Testing for a null value probably gets
226-
// confused when there are multiple null values or when there is a
227-
// null value before the node that actually contains the offset.
228-
return entry.value;
229-
}
230-
}
231-
}
232-
return null;
233-
}
234-
235-
/// Return `true` if this node contains the given [offset].
236-
bool containsOffset(int offset) {
237-
// TODO(brianwilkerson) Nodes at the end of the file contain any trailing
238-
// whitespace. This needs to be accounted for, here or elsewhere.
239-
var nodeOffset = span.start.offset;
240-
var nodeEnd = nodeOffset + span.length;
241-
return nodeOffset <= offset && offset <= nodeEnd;
242-
}
243-
}

pkg/analysis_server/lib/src/services/correction/fix/data_driven/transform_set_parser.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,18 +1033,3 @@ class TransformSetParser {
10331033
}
10341034
}
10351035
}
1036-
1037-
extension on YamlMap {
1038-
// TODO(brianwilkerson) Copied from YamlCompletionGenerator. Refactor to a
1039-
// utility file that is shared between them.
1040-
/// Return the node representing the key that corresponds to the value
1041-
/// represented by the [value] node.
1042-
YamlNode keyAtValue(YamlNode value) {
1043-
for (var entry in nodes.entries) {
1044-
if (entry.value == value) {
1045-
return entry.key;
1046-
}
1047-
}
1048-
return null;
1049-
}
1050-
}

pkg/analysis_server/lib/src/utilities/extensions/yaml.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
import 'package:yaml/yaml.dart';
66

77
extension YamlMapExtensions on YamlMap {
8+
/// Return the node representing the key that corresponds to the value
9+
/// represented by the [value] node.
10+
YamlNode keyAtValue(YamlNode value) {
11+
for (var entry in nodes.entries) {
12+
if (entry.value == value) {
13+
return entry.key;
14+
}
15+
}
16+
return null;
17+
}
18+
819
/// Return the value associated with the key whose value matches the given
920
/// [key], or `null` if there is no matching key.
1021
YamlNode valueAt(String key) {
@@ -16,3 +27,49 @@ extension YamlMapExtensions on YamlMap {
1627
return null;
1728
}
1829
}
30+
31+
extension YamlNodeExtensions on YamlNode {
32+
/// Return the child of this node that contains the given [offset], or `null`
33+
/// if none of the children contains the offset.
34+
YamlNode childContainingOffset(int offset) {
35+
var node = this;
36+
if (node is YamlList) {
37+
for (var element in node.nodes) {
38+
if (element.containsOffset(offset)) {
39+
return element;
40+
}
41+
}
42+
for (var element in node.nodes) {
43+
if (element is YamlScalar && element.value == null) {
44+
// TODO(brianwilkerson) Testing for a null value probably gets
45+
// confused when there are multiple null values.
46+
return element;
47+
}
48+
}
49+
} else if (node is YamlMap) {
50+
for (var entry in node.nodes.entries) {
51+
if ((entry.key as YamlNode).containsOffset(offset)) {
52+
return entry.key;
53+
}
54+
var value = entry.value;
55+
if (value.containsOffset(offset) ||
56+
(value is YamlScalar && value.value == null)) {
57+
// TODO(brianwilkerson) Testing for a null value probably gets
58+
// confused when there are multiple null values or when there is a
59+
// null value before the node that actually contains the offset.
60+
return entry.value;
61+
}
62+
}
63+
}
64+
return null;
65+
}
66+
67+
/// Return `true` if this node contains the given [offset].
68+
bool containsOffset(int offset) {
69+
// TODO(brianwilkerson) Nodes at the end of the file contain any trailing
70+
// whitespace. This needs to be accounted for, here or elsewhere.
71+
var nodeOffset = span.start.offset;
72+
var nodeEnd = nodeOffset + span.length;
73+
return nodeOffset <= offset && offset <= nodeEnd;
74+
}
75+
}

0 commit comments

Comments
 (0)