yerba 0.7.6-aarch64-linux-gnu → 0.8.1-aarch64-linux-gnu
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +66 -2
- data/exe/aarch64-linux-gnu/yerba +0 -0
- data/ext/yerba/include/yerba.h +8 -0
- data/ext/yerba/yerba.c +70 -8
- data/lib/yerba/3.2/yerba.so +0 -0
- data/lib/yerba/3.3/yerba.so +0 -0
- data/lib/yerba/3.4/yerba.so +0 -0
- data/lib/yerba/4.0/yerba.so +0 -0
- data/lib/yerba/map.rb +15 -10
- data/lib/yerba/node.rb +2 -2
- data/lib/yerba/sequence.rb +13 -1
- data/lib/yerba/version.rb +1 -1
- data/rust/Cargo.lock +2 -342
- data/rust/Cargo.toml +15 -6
- data/rust/src/commands/blank_lines.rs +2 -2
- data/rust/src/commands/directives.rs +4 -4
- data/rust/src/commands/get.rs +17 -10
- data/rust/src/commands/init.rs +4 -4
- data/rust/src/commands/insert.rs +6 -6
- data/rust/src/commands/location.rs +2 -2
- data/rust/src/commands/mate.rs +7 -7
- data/rust/src/commands/mod.rs +119 -90
- data/rust/src/commands/move_key.rs +9 -6
- data/rust/src/commands/quote_style.rs +4 -5
- data/rust/src/commands/rename.rs +3 -3
- data/rust/src/commands/schema.rs +6 -6
- data/rust/src/commands/selectors.rs +2 -2
- data/rust/src/commands/set.rs +12 -1
- data/rust/src/commands/sort.rs +36 -42
- data/rust/src/commands/sort_keys.rs +2 -2
- data/rust/src/commands/ui.rs +157 -0
- data/rust/src/commands/unique.rs +7 -7
- data/rust/src/commands/version.rs +13 -3
- data/rust/src/document/condition.rs +89 -32
- data/rust/src/document/delete.rs +6 -1
- data/rust/src/document/get.rs +94 -26
- data/rust/src/document/insert.rs +26 -0
- data/rust/src/document/mod.rs +73 -20
- data/rust/src/document/set.rs +91 -2
- data/rust/src/error.rs +17 -0
- data/rust/src/ffi.rs +76 -9
- data/rust/src/json.rs +10 -1
- data/rust/src/lib.rs +23 -7
- data/rust/src/main.rs +1 -0
- data/rust/src/quote_style.rs +12 -11
- data/rust/src/selector.rs +19 -3
- data/rust/src/syntax.rs +178 -26
- data/rust/src/yaml_writer.rs +4 -4
- data/rust/src/yerbafile.rs +30 -6
- metadata +3 -2
data/rust/src/document/get.rs
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
use super::*;
|
|
2
2
|
|
|
3
|
+
fn key_of(node: &SyntaxNode, source: &str) -> (Option<String>, Location) {
|
|
4
|
+
node
|
|
5
|
+
.parent()
|
|
6
|
+
.and_then(|parent| {
|
|
7
|
+
use yaml_parser::ast::{BlockMapEntry, FlowMapEntry};
|
|
8
|
+
|
|
9
|
+
let key_node = match BlockMapEntry::cast(parent.clone()) {
|
|
10
|
+
Some(entry) => entry.key().map(|key| key.syntax().clone()),
|
|
11
|
+
None => FlowMapEntry::cast(parent).and_then(|entry| entry.key().map(|key| key.syntax().clone())),
|
|
12
|
+
}?;
|
|
13
|
+
|
|
14
|
+
let key_text = extract_scalar_text(&key_node)?;
|
|
15
|
+
let key_range = key_node.text_range();
|
|
16
|
+
let key_location = compute_location(source, key_range.start().into(), key_range.end().into());
|
|
17
|
+
|
|
18
|
+
Some((key_text, key_location))
|
|
19
|
+
})
|
|
20
|
+
.map(|(name, location)| (Some(name), location))
|
|
21
|
+
.unwrap_or((None, Location::default()))
|
|
22
|
+
}
|
|
23
|
+
|
|
3
24
|
impl Document {
|
|
4
25
|
pub fn is_valid_selector(&self, dot_path: &str) -> bool {
|
|
5
26
|
if dot_path.is_empty() {
|
|
@@ -12,6 +33,10 @@ impl Document {
|
|
|
12
33
|
return true;
|
|
13
34
|
}
|
|
14
35
|
|
|
36
|
+
if crate::selector::Selector::parse(dot_path).has_wildcard() && !self.navigate_all_compact(dot_path).is_empty() {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
15
40
|
let wildcard_version = dot_path.replace(|c: char| c.is_ascii_digit(), "").replace("[.", "[");
|
|
16
41
|
|
|
17
42
|
let mut normalized = String::new();
|
|
@@ -82,6 +107,49 @@ impl Document {
|
|
|
82
107
|
self.navigate_all_compact(dot_path).iter().map(node_selector).collect()
|
|
83
108
|
}
|
|
84
109
|
|
|
110
|
+
pub fn keys_at(&self, dot_path: &str) -> Vec<String> {
|
|
111
|
+
let node = if dot_path.is_empty() {
|
|
112
|
+
self.root.clone()
|
|
113
|
+
} else {
|
|
114
|
+
match self.navigate_all_compact(dot_path).into_iter().next() {
|
|
115
|
+
Some(node) => node,
|
|
116
|
+
None => return Vec::new(),
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
let collection = node.descendants().find(|descendant| {
|
|
121
|
+
matches!(
|
|
122
|
+
descendant.kind(),
|
|
123
|
+
SyntaxKind::BLOCK_MAP | SyntaxKind::BLOCK_SEQ | SyntaxKind::FLOW_MAP | SyntaxKind::FLOW_SEQ
|
|
124
|
+
)
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
let Some(collection) = collection else {
|
|
128
|
+
return Vec::new();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
if let Some(map) = BlockMap::cast(collection.clone()) {
|
|
132
|
+
return map
|
|
133
|
+
.entries()
|
|
134
|
+
.filter_map(|entry| entry.key().and_then(|key| extract_scalar_text(key.syntax())))
|
|
135
|
+
.collect();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if let Some(map) = yaml_parser::ast::FlowMap::cast(collection) {
|
|
139
|
+
return map
|
|
140
|
+
.entries()
|
|
141
|
+
.map(|entries| {
|
|
142
|
+
entries
|
|
143
|
+
.entries()
|
|
144
|
+
.filter_map(|entry| entry.key().and_then(|key| extract_scalar_text(key.syntax())))
|
|
145
|
+
.collect()
|
|
146
|
+
})
|
|
147
|
+
.unwrap_or_default();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
Vec::new()
|
|
151
|
+
}
|
|
152
|
+
|
|
85
153
|
pub fn get_all_located(&self, dot_path: &str) -> Vec<LocatedNode> {
|
|
86
154
|
let source = self.source_text();
|
|
87
155
|
let file_path = self.path.as_ref().map(|p| p.to_string_lossy().to_string());
|
|
@@ -116,6 +184,8 @@ impl Document {
|
|
|
116
184
|
}
|
|
117
185
|
};
|
|
118
186
|
|
|
187
|
+
let (key_name, key_location) = key_of(node, &source);
|
|
188
|
+
|
|
119
189
|
LocatedNode {
|
|
120
190
|
node_type: node_type.to_string(),
|
|
121
191
|
text,
|
|
@@ -124,6 +194,8 @@ impl Document {
|
|
|
124
194
|
selector,
|
|
125
195
|
line,
|
|
126
196
|
location,
|
|
197
|
+
key_name,
|
|
198
|
+
key_location,
|
|
127
199
|
}
|
|
128
200
|
})
|
|
129
201
|
.collect()
|
|
@@ -214,23 +286,7 @@ impl Document {
|
|
|
214
286
|
let range = node.text_range();
|
|
215
287
|
let location = compute_location(source, range.start().into(), range.end().into());
|
|
216
288
|
|
|
217
|
-
let (key_name, key_location) = node
|
|
218
|
-
.parent()
|
|
219
|
-
.and_then(|parent| {
|
|
220
|
-
use yaml_parser::ast::BlockMapEntry;
|
|
221
|
-
|
|
222
|
-
BlockMapEntry::cast(parent).and_then(|entry| {
|
|
223
|
-
entry.key().and_then(|key_node| {
|
|
224
|
-
let key_text = extract_scalar_text(key_node.syntax())?;
|
|
225
|
-
let key_range = key_node.syntax().text_range();
|
|
226
|
-
let key_location = compute_location(source, key_range.start().into(), key_range.end().into());
|
|
227
|
-
|
|
228
|
-
Some((key_text, key_location))
|
|
229
|
-
})
|
|
230
|
-
})
|
|
231
|
-
})
|
|
232
|
-
.map(|(name, location)| (Some(name), location))
|
|
233
|
-
.unwrap_or((None, Location::default()));
|
|
289
|
+
let (key_name, key_location) = key_of(&node, source);
|
|
234
290
|
|
|
235
291
|
(location, key_name, key_location)
|
|
236
292
|
}
|
|
@@ -238,15 +294,15 @@ impl Document {
|
|
|
238
294
|
}
|
|
239
295
|
}
|
|
240
296
|
|
|
241
|
-
pub fn find_items(&self, dot_path: &str, condition: Option<&str>, select: Option<&str>) -> Vec<serde_json::Value> {
|
|
297
|
+
pub fn find_items(&self, dot_path: &str, condition: Option<&str>, select: Option<&str>) -> Result<Vec<serde_json::Value>, YerbaError> {
|
|
242
298
|
let values = match condition {
|
|
243
|
-
Some(cond) => self.filter(dot_path, cond)
|
|
299
|
+
Some(cond) => self.filter(dot_path, cond)?,
|
|
244
300
|
None => self.get_values(dot_path),
|
|
245
301
|
};
|
|
246
302
|
|
|
247
303
|
let select_fields: Option<Vec<&str>> = select.map(|s| s.split(',').collect());
|
|
248
304
|
|
|
249
|
-
values
|
|
305
|
+
let items = values
|
|
250
306
|
.iter()
|
|
251
307
|
.map(|value| match &select_fields {
|
|
252
308
|
Some(fields) => {
|
|
@@ -262,7 +318,9 @@ impl Document {
|
|
|
262
318
|
}
|
|
263
319
|
None => crate::json::yaml_to_json(value),
|
|
264
320
|
})
|
|
265
|
-
.collect()
|
|
321
|
+
.collect();
|
|
322
|
+
|
|
323
|
+
Ok(items)
|
|
266
324
|
}
|
|
267
325
|
|
|
268
326
|
pub fn get_value(&self, dot_path: &str) -> Option<yaml_serde::Value> {
|
|
@@ -337,7 +395,7 @@ impl Document {
|
|
|
337
395
|
}
|
|
338
396
|
|
|
339
397
|
pub fn exists(&self, dot_path: &str) -> bool {
|
|
340
|
-
if dot_path.
|
|
398
|
+
if crate::selector::Selector::parse(dot_path).has_wildcard() {
|
|
341
399
|
if !self.navigate_all_compact(dot_path).is_empty() {
|
|
342
400
|
return true;
|
|
343
401
|
}
|
|
@@ -455,11 +513,13 @@ pub(crate) fn node_selector(node: &SyntaxNode) -> String {
|
|
|
455
513
|
let mut parts: Vec<String> = Vec::new();
|
|
456
514
|
let mut current = node.clone();
|
|
457
515
|
|
|
458
|
-
if current.kind()
|
|
516
|
+
if matches!(current.kind(), SyntaxKind::BLOCK_SEQ_ENTRY | SyntaxKind::FLOW_SEQ_ENTRY) {
|
|
459
517
|
if let Some(parent) = current.parent() {
|
|
518
|
+
let kind = current.kind();
|
|
519
|
+
|
|
460
520
|
let index = parent
|
|
461
521
|
.children()
|
|
462
|
-
.filter(|child| child.kind() ==
|
|
522
|
+
.filter(|child| child.kind() == kind)
|
|
463
523
|
.position(|child| child == current)
|
|
464
524
|
.unwrap_or(0);
|
|
465
525
|
|
|
@@ -474,11 +534,11 @@ pub(crate) fn node_selector(node: &SyntaxNode) -> String {
|
|
|
474
534
|
};
|
|
475
535
|
|
|
476
536
|
match parent.kind() {
|
|
477
|
-
SyntaxKind::BLOCK_SEQ_ENTRY => {
|
|
537
|
+
SyntaxKind::BLOCK_SEQ_ENTRY | SyntaxKind::FLOW_SEQ_ENTRY => {
|
|
478
538
|
if let Some(grandparent) = parent.parent() {
|
|
479
539
|
let index = grandparent
|
|
480
540
|
.children()
|
|
481
|
-
.filter(|child| child.kind() ==
|
|
541
|
+
.filter(|child| child.kind() == parent.kind())
|
|
482
542
|
.position(|child| child == parent)
|
|
483
543
|
.unwrap_or(0);
|
|
484
544
|
|
|
@@ -494,6 +554,14 @@ pub(crate) fn node_selector(node: &SyntaxNode) -> String {
|
|
|
494
554
|
}
|
|
495
555
|
}
|
|
496
556
|
|
|
557
|
+
SyntaxKind::FLOW_MAP_ENTRY => {
|
|
558
|
+
if let Some(key_node) = parent.children().find(|child| child.kind() == SyntaxKind::FLOW_MAP_KEY) {
|
|
559
|
+
if let Some(key_text) = extract_scalar_text(&key_node) {
|
|
560
|
+
parts.push(key_text);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
497
565
|
_ => {}
|
|
498
566
|
}
|
|
499
567
|
|
data/rust/src/document/insert.rs
CHANGED
|
@@ -90,6 +90,28 @@ impl Document {
|
|
|
90
90
|
|
|
91
91
|
pub fn insert_into(&mut self, dot_path: &str, value: &str, position: InsertPosition) -> Result<(), YerbaError> {
|
|
92
92
|
Self::validate_path(dot_path)?;
|
|
93
|
+
self.refuse_flow_target(dot_path)?;
|
|
94
|
+
|
|
95
|
+
if let Some((parent_path, _)) = dot_path.rsplit_once('.') {
|
|
96
|
+
if let Ok(parent) = self.navigate(parent_path) {
|
|
97
|
+
let occupied_flow = find_flow_map(&parent).map(|map| !flow_map_entries(&map).is_empty()).unwrap_or(false)
|
|
98
|
+
|| find_flow_sequence(&parent)
|
|
99
|
+
.map(|sequence| !flow_sequence_entries(&sequence).is_empty())
|
|
100
|
+
.unwrap_or(false);
|
|
101
|
+
|
|
102
|
+
if occupied_flow {
|
|
103
|
+
return Err(YerbaError::FlowCollectionNotWritable(dot_path.to_string()));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if crate::selector::Selector::parse(dot_path)
|
|
109
|
+
.segments()
|
|
110
|
+
.iter()
|
|
111
|
+
.any(|segment| matches!(segment, crate::selector::SelectorSegment::AllKeys))
|
|
112
|
+
{
|
|
113
|
+
return Err(YerbaError::SelectorNotFound(dot_path.to_string()));
|
|
114
|
+
}
|
|
93
115
|
|
|
94
116
|
if let Ok(current_node) = self.navigate(dot_path) {
|
|
95
117
|
if find_block_sequence(¤t_node).is_some()
|
|
@@ -548,6 +570,10 @@ impl Document {
|
|
|
548
570
|
let is_block_value = value.contains('\n') || value.starts_with("- ");
|
|
549
571
|
|
|
550
572
|
if !is_block_value {
|
|
573
|
+
if !crate::syntax::is_valid_inline_value(value) {
|
|
574
|
+
return format!("{}: {}", key, crate::syntax::quote_if_needed(value));
|
|
575
|
+
}
|
|
576
|
+
|
|
551
577
|
return format!("{}: {}", key, value);
|
|
552
578
|
}
|
|
553
579
|
|
data/rust/src/document/mod.rs
CHANGED
|
@@ -2,11 +2,16 @@ mod condition;
|
|
|
2
2
|
mod delete;
|
|
3
3
|
mod get;
|
|
4
4
|
mod insert;
|
|
5
|
-
mod schema;
|
|
6
5
|
mod set;
|
|
7
6
|
mod sort;
|
|
8
|
-
pub mod style;
|
|
9
7
|
mod unique;
|
|
8
|
+
|
|
9
|
+
#[cfg(feature = "schema")]
|
|
10
|
+
mod schema;
|
|
11
|
+
|
|
12
|
+
pub mod style;
|
|
13
|
+
|
|
14
|
+
pub use condition::{validate_condition, validate_item_condition};
|
|
10
15
|
pub use unique::DuplicateInfo;
|
|
11
16
|
|
|
12
17
|
use crate::syntax::YerbaValueType;
|
|
@@ -20,6 +25,8 @@ pub struct LocatedNode {
|
|
|
20
25
|
pub selector: String,
|
|
21
26
|
pub line: usize,
|
|
22
27
|
pub location: Location,
|
|
28
|
+
pub key_name: Option<String>,
|
|
29
|
+
pub key_location: Location,
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
use std::fs;
|
|
@@ -35,9 +42,10 @@ use crate::error::YerbaError;
|
|
|
35
42
|
use crate::QuoteStyle;
|
|
36
43
|
|
|
37
44
|
use crate::syntax::{
|
|
38
|
-
column_at, dedent_block_scalar, extract_scalar, extract_scalar_text, find_block_map, find_block_sequence, find_entry_by_key,
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
column_at, dedent_block_scalar, extract_scalar, extract_scalar_text, find_block_map, find_block_sequence, find_entry_by_key, find_flow_entry_by_key,
|
|
46
|
+
find_flow_map, find_flow_sequence, find_scalar_token, first_collection, flow_map_entries, flow_sequence_entries, format_scalar_value, in_flow_collection,
|
|
47
|
+
is_map_key, is_yaml_non_string, line_at, line_start_at, preceding_whitespace_indent, preceding_whitespace_token, raw_scalar_value, removal_range,
|
|
48
|
+
FirstCollection, ScalarValue,
|
|
41
49
|
};
|
|
42
50
|
|
|
43
51
|
#[derive(Debug, Clone)]
|
|
@@ -190,6 +198,14 @@ impl Document {
|
|
|
190
198
|
self.navigate_all(dot_path).into_iter().flatten().collect()
|
|
191
199
|
}
|
|
192
200
|
|
|
201
|
+
pub(crate) fn refuse_flow_target(&self, dot_path: &str) -> Result<(), YerbaError> {
|
|
202
|
+
if self.navigate_all_compact(dot_path).iter().any(in_flow_collection) {
|
|
203
|
+
return Err(YerbaError::FlowCollectionNotWritable(dot_path.to_string()));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
Ok(())
|
|
207
|
+
}
|
|
208
|
+
|
|
193
209
|
pub fn navigate_all(&self, dot_path: &str) -> Vec<Option<SyntaxNode>> {
|
|
194
210
|
if Document::validate_path(dot_path).is_err() {
|
|
195
211
|
return Vec::new();
|
|
@@ -220,7 +236,7 @@ impl Document {
|
|
|
220
236
|
let segments = parsed.segments();
|
|
221
237
|
|
|
222
238
|
for (i, segment) in segments.iter().enumerate() {
|
|
223
|
-
let is_wildcard = matches!(segment, crate::selector::SelectorSegment::AllItems);
|
|
239
|
+
let is_wildcard = matches!(segment, crate::selector::SelectorSegment::AllItems | crate::selector::SelectorSegment::AllKeys);
|
|
224
240
|
let has_remaining = i + 1 < segments.len();
|
|
225
241
|
let mut next_nodes: Vec<Option<SyntaxNode>> = Vec::new();
|
|
226
242
|
|
|
@@ -335,22 +351,28 @@ impl Document {
|
|
|
335
351
|
return self.remove_node(entry_node);
|
|
336
352
|
}
|
|
337
353
|
|
|
338
|
-
let
|
|
339
|
-
next_sibling.text_range().start()
|
|
340
|
-
} else {
|
|
341
|
-
entry_node.parent().map(|parent| parent.text_range().end()).unwrap_or(entry_range.end())
|
|
342
|
-
};
|
|
354
|
+
let source = self.source_text();
|
|
343
355
|
|
|
344
|
-
let start
|
|
345
|
-
|
|
346
|
-
|
|
356
|
+
let (start, end) = match preceding_whitespace_token(entry_node) {
|
|
357
|
+
Some(whitespace_token) => {
|
|
358
|
+
let whitespace_text = whitespace_token.text();
|
|
359
|
+
let whitespace_start = whitespace_token.text_range().start();
|
|
347
360
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
361
|
+
let start = whitespace_text
|
|
362
|
+
.rfind('\n')
|
|
363
|
+
.map(|offset| whitespace_start + TextSize::from(offset as u32))
|
|
364
|
+
.unwrap_or(whitespace_start);
|
|
365
|
+
|
|
366
|
+
(start, entry_range.end())
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
None => {
|
|
370
|
+
let entry_end: usize = entry_range.end().into();
|
|
371
|
+
let line_start = line_start_at(&source, entry_range.start().into());
|
|
372
|
+
let after_entry = source[entry_end..].find('\n').map(|offset| entry_end + offset + 1).unwrap_or(entry_end);
|
|
373
|
+
|
|
374
|
+
(TextSize::from(line_start as u32), TextSize::from(after_entry as u32))
|
|
375
|
+
}
|
|
354
376
|
};
|
|
355
377
|
|
|
356
378
|
self.apply_edit(TextRange::new(start, end), "")
|
|
@@ -673,6 +695,8 @@ fn resolve_segment(node: &SyntaxNode, segment: &crate::selector::SelectorSegment
|
|
|
673
695
|
SelectorSegment::AllItems => {
|
|
674
696
|
if let Some(sequence) = find_block_sequence(node) {
|
|
675
697
|
sequence.entries().map(|entry| entry.syntax().clone()).collect()
|
|
698
|
+
} else if let Some(sequence) = find_flow_sequence(node) {
|
|
699
|
+
flow_sequence_entries(&sequence)
|
|
676
700
|
} else {
|
|
677
701
|
Vec::new()
|
|
678
702
|
}
|
|
@@ -681,6 +705,25 @@ fn resolve_segment(node: &SyntaxNode, segment: &crate::selector::SelectorSegment
|
|
|
681
705
|
SelectorSegment::Index(index) => {
|
|
682
706
|
if let Some(sequence) = find_block_sequence(node) {
|
|
683
707
|
sequence.entries().nth(*index).map(|entry| vec![entry.syntax().clone()]).unwrap_or_default()
|
|
708
|
+
} else if let Some(sequence) = find_flow_sequence(node) {
|
|
709
|
+
flow_sequence_entries(&sequence)
|
|
710
|
+
.into_iter()
|
|
711
|
+
.nth(*index)
|
|
712
|
+
.map(|entry| vec![entry])
|
|
713
|
+
.unwrap_or_default()
|
|
714
|
+
} else {
|
|
715
|
+
Vec::new()
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
SelectorSegment::AllKeys => {
|
|
720
|
+
if let Some(map) = find_block_map(node) {
|
|
721
|
+
map.entries().filter_map(|entry| entry.value().map(|value| value.syntax().clone())).collect()
|
|
722
|
+
} else if let Some(map) = find_flow_map(node) {
|
|
723
|
+
flow_map_entries(&map)
|
|
724
|
+
.into_iter()
|
|
725
|
+
.filter_map(|entry| entry.value().map(|value| value.syntax().clone()))
|
|
726
|
+
.collect()
|
|
684
727
|
} else {
|
|
685
728
|
Vec::new()
|
|
686
729
|
}
|
|
@@ -693,6 +736,16 @@ fn resolve_segment(node: &SyntaxNode, segment: &crate::selector::SelectorSegment
|
|
|
693
736
|
return vec![value.syntax().clone()];
|
|
694
737
|
}
|
|
695
738
|
}
|
|
739
|
+
|
|
740
|
+
return Vec::new();
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if let Some(map) = find_flow_map(node) {
|
|
744
|
+
if let Some(entry) = find_flow_entry_by_key(&map, key) {
|
|
745
|
+
if let Some(value) = entry.value() {
|
|
746
|
+
return vec![value.syntax().clone()];
|
|
747
|
+
}
|
|
748
|
+
}
|
|
696
749
|
}
|
|
697
750
|
|
|
698
751
|
Vec::new()
|
data/rust/src/document/set.rs
CHANGED
|
@@ -1,5 +1,78 @@
|
|
|
1
1
|
use super::*;
|
|
2
2
|
|
|
3
|
+
fn scalar_replacement_text(value: &str, kind: SyntaxKind) -> String {
|
|
4
|
+
if kind == SyntaxKind::PLAIN_SCALAR && !crate::syntax::is_inline_scalar_safe(value) {
|
|
5
|
+
return format_scalar_value(value, SyntaxKind::DOUBLE_QUOTED_SCALAR);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
format_scalar_value(value, kind)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fn holds_collection(node: &SyntaxNode) -> bool {
|
|
12
|
+
node.descendants().any(|descendant| {
|
|
13
|
+
matches!(
|
|
14
|
+
descendant.kind(),
|
|
15
|
+
SyntaxKind::BLOCK_MAP | SyntaxKind::BLOCK_SEQ | SyntaxKind::FLOW_MAP | SyntaxKind::FLOW_SEQ
|
|
16
|
+
)
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
struct ValueSpan {
|
|
21
|
+
range: TextRange,
|
|
22
|
+
separated: bool,
|
|
23
|
+
comment: Option<String>,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn value_span(node: &SyntaxNode) -> Option<ValueSpan> {
|
|
27
|
+
let (container, separator_kind) = match node.kind() {
|
|
28
|
+
SyntaxKind::BLOCK_MAP_VALUE | SyntaxKind::FLOW_MAP_VALUE => (node.parent()?, SyntaxKind::COLON),
|
|
29
|
+
SyntaxKind::BLOCK_SEQ_ENTRY => (node.clone(), SyntaxKind::MINUS),
|
|
30
|
+
|
|
31
|
+
SyntaxKind::FLOW_SEQ_ENTRY => {
|
|
32
|
+
return Some(ValueSpan {
|
|
33
|
+
range: node.text_range(),
|
|
34
|
+
separated: false,
|
|
35
|
+
comment: None,
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_ => return None,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
let mut elements = container
|
|
43
|
+
.children_with_tokens()
|
|
44
|
+
.skip_while(|element| element.as_token().map(|token| token.kind() != separator_kind).unwrap_or(true));
|
|
45
|
+
|
|
46
|
+
let separator = elements.next()?.into_token()?;
|
|
47
|
+
let mut comment = None;
|
|
48
|
+
|
|
49
|
+
for element in elements {
|
|
50
|
+
match element.into_token() {
|
|
51
|
+
Some(token) if token.kind() == SyntaxKind::COMMENT => comment = Some(token.text().trim_end().to_string()),
|
|
52
|
+
Some(token) if token.kind() == SyntaxKind::WHITESPACE && token.text().contains('\n') => break,
|
|
53
|
+
Some(_) => {}
|
|
54
|
+
None => break,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
Some(ValueSpan {
|
|
59
|
+
range: TextRange::new(separator.text_range().end(), node.text_range().end()),
|
|
60
|
+
separated: true,
|
|
61
|
+
comment,
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn replacement_text(span: &ValueSpan, value: &str) -> String {
|
|
66
|
+
let mut text = if span.separated { format!(" {}", value) } else { value.to_string() };
|
|
67
|
+
|
|
68
|
+
if let Some(comment) = &span.comment {
|
|
69
|
+
text.push(' ');
|
|
70
|
+
text.push_str(comment);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
text
|
|
74
|
+
}
|
|
75
|
+
|
|
3
76
|
impl Document {
|
|
4
77
|
pub fn set(&mut self, dot_path: &str, value: &str) -> Result<(), YerbaError> {
|
|
5
78
|
let current_node = self.navigate(dot_path)?;
|
|
@@ -11,9 +84,15 @@ impl Document {
|
|
|
11
84
|
return self.apply_edit(block_scalar.text_range(), &new_text);
|
|
12
85
|
}
|
|
13
86
|
|
|
87
|
+
if holds_collection(¤t_node) {
|
|
88
|
+
let span = value_span(¤t_node).ok_or_else(|| YerbaError::SelectorNotFound(dot_path.to_string()))?;
|
|
89
|
+
|
|
90
|
+
return self.apply_edit(span.range, &replacement_text(&span, value));
|
|
91
|
+
}
|
|
92
|
+
|
|
14
93
|
let scalar_token = find_scalar_token(¤t_node).ok_or_else(|| YerbaError::SelectorNotFound(dot_path.to_string()))?;
|
|
15
94
|
|
|
16
|
-
let new_text =
|
|
95
|
+
let new_text = scalar_replacement_text(value, scalar_token.kind());
|
|
17
96
|
|
|
18
97
|
self.replace_token(&scalar_token, &new_text)
|
|
19
98
|
}
|
|
@@ -31,8 +110,12 @@ impl Document {
|
|
|
31
110
|
for node in nodes {
|
|
32
111
|
if let Some(block_scalar) = node.descendants().find(|child| child.kind() == SyntaxKind::BLOCK_SCALAR) {
|
|
33
112
|
edits.push((block_scalar.text_range(), Self::block_scalar_replacement(&source, &block_scalar, value)));
|
|
113
|
+
} else if holds_collection(&node) {
|
|
114
|
+
if let Some(span) = value_span(&node) {
|
|
115
|
+
edits.push((span.range, replacement_text(&span, value)));
|
|
116
|
+
}
|
|
34
117
|
} else if let Some(scalar_token) = find_scalar_token(&node) {
|
|
35
|
-
edits.push((scalar_token.text_range(),
|
|
118
|
+
edits.push((scalar_token.text_range(), scalar_replacement_text(value, scalar_token.kind())));
|
|
36
119
|
}
|
|
37
120
|
}
|
|
38
121
|
|
|
@@ -68,6 +151,12 @@ impl Document {
|
|
|
68
151
|
return self.apply_edit(range, value);
|
|
69
152
|
}
|
|
70
153
|
|
|
154
|
+
if holds_collection(¤t_node) {
|
|
155
|
+
let span = value_span(¤t_node).ok_or_else(|| YerbaError::SelectorNotFound(dot_path.to_string()))?;
|
|
156
|
+
|
|
157
|
+
return self.apply_edit(span.range, &replacement_text(&span, value));
|
|
158
|
+
}
|
|
159
|
+
|
|
71
160
|
let scalar_token = find_scalar_token(¤t_node).ok_or_else(|| YerbaError::SelectorNotFound(dot_path.to_string()))?;
|
|
72
161
|
|
|
73
162
|
self.replace_token(&scalar_token, value)
|
data/rust/src/error.rs
CHANGED
|
@@ -4,10 +4,13 @@ pub enum YerbaError {
|
|
|
4
4
|
IoError(std::io::Error),
|
|
5
5
|
SelectorNotFound(String),
|
|
6
6
|
AmbiguousSelector(String, usize),
|
|
7
|
+
InvalidCondition(String, String),
|
|
7
8
|
NotASequence(String),
|
|
9
|
+
FlowCollectionNotWritable(String),
|
|
8
10
|
IndexOutOfBounds(usize, usize),
|
|
9
11
|
UnknownKeys(Vec<String>),
|
|
10
12
|
DuplicateValues(Vec<crate::DuplicateInfo>),
|
|
13
|
+
#[cfg(feature = "schema")]
|
|
11
14
|
SchemaValidation(Vec<crate::schema::ValidationError>),
|
|
12
15
|
DuplicateKey {
|
|
13
16
|
key: String,
|
|
@@ -25,6 +28,14 @@ impl std::fmt::Display for YerbaError {
|
|
|
25
28
|
YerbaError::SelectorNotFound(selector) => write!(f, "selector not found: {}", selector),
|
|
26
29
|
YerbaError::NotASequence(path) => write!(f, "not a sequence: {}", path),
|
|
27
30
|
|
|
31
|
+
YerbaError::FlowCollectionNotWritable(path) => {
|
|
32
|
+
write!(
|
|
33
|
+
f,
|
|
34
|
+
"cannot write to \"{}\": flow collections are not writable yet. Convert it to block style first, with `collection_style` or `yerba collection-style`",
|
|
35
|
+
path
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
28
39
|
YerbaError::AmbiguousSelector(selector, count) => {
|
|
29
40
|
write!(
|
|
30
41
|
f,
|
|
@@ -33,6 +44,10 @@ impl std::fmt::Display for YerbaError {
|
|
|
33
44
|
)
|
|
34
45
|
}
|
|
35
46
|
|
|
47
|
+
YerbaError::InvalidCondition(condition, reason) => {
|
|
48
|
+
write!(f, "invalid condition \"{}\": {}", condition, reason)
|
|
49
|
+
}
|
|
50
|
+
|
|
36
51
|
YerbaError::DuplicateKey {
|
|
37
52
|
key,
|
|
38
53
|
first_line,
|
|
@@ -64,6 +79,7 @@ impl std::fmt::Display for YerbaError {
|
|
|
64
79
|
write!(f, "index {} out of bounds (length {})", index, length)
|
|
65
80
|
}
|
|
66
81
|
|
|
82
|
+
#[cfg(feature = "schema")]
|
|
67
83
|
YerbaError::SchemaValidation(errors) => {
|
|
68
84
|
let details: Vec<String> = errors.iter().map(|error| error.to_string()).collect();
|
|
69
85
|
|
|
@@ -123,6 +139,7 @@ impl GitHubAnnotations for YerbaError {
|
|
|
123
139
|
})
|
|
124
140
|
.collect(),
|
|
125
141
|
|
|
142
|
+
#[cfg(feature = "schema")]
|
|
126
143
|
YerbaError::SchemaValidation(errors) => errors
|
|
127
144
|
.iter()
|
|
128
145
|
.map(|error| GitHubAnnotation {
|