yerba 0.7.2-arm-linux-gnu → 0.7.3-arm-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.
data/rust/src/syntax.rs CHANGED
@@ -1,7 +1,7 @@
1
1
  use rowan::ast::AstNode;
2
2
  use rowan::{TextRange, TextSize};
3
3
 
4
- use yaml_parser::ast::{BlockMap, BlockMapEntry};
4
+ use yaml_parser::ast::{BlockMap, BlockMapEntry, BlockSeq};
5
5
  use yaml_parser::{SyntaxKind, SyntaxNode, SyntaxToken};
6
6
 
7
7
  #[derive(Debug, Clone, PartialEq)]
@@ -31,25 +31,27 @@ pub fn detect_yaml_type(scalar: &ScalarValue) -> YerbaValueType {
31
31
  detect_yaml_type_from_plain(&scalar.text)
32
32
  }
33
33
 
34
- pub fn extract_scalar(node: &SyntaxNode) -> Option<ScalarValue> {
35
- if let Some(token) = find_scalar_token(node) {
36
- let text = match token.kind() {
37
- SyntaxKind::PLAIN_SCALAR => token.text().to_string(),
34
+ pub fn raw_scalar_value(token: &SyntaxToken) -> Option<String> {
35
+ match token.kind() {
36
+ SyntaxKind::PLAIN_SCALAR => Some(token.text().to_string()),
38
37
 
39
- SyntaxKind::DOUBLE_QUOTED_SCALAR => {
40
- let raw = token.text();
41
- unescape_double_quoted(&raw[1..raw.len() - 1])
42
- }
38
+ SyntaxKind::DOUBLE_QUOTED_SCALAR => {
39
+ let text = token.text();
40
+ Some(unescape_double_quoted(&text[1..text.len() - 1]))
41
+ }
43
42
 
44
- SyntaxKind::SINGLE_QUOTED_SCALAR => {
45
- let raw = token.text();
46
- unescape_single_quoted(&raw[1..raw.len() - 1])
47
- }
43
+ SyntaxKind::SINGLE_QUOTED_SCALAR => {
44
+ let text = token.text();
45
+ Some(unescape_single_quoted(&text[1..text.len() - 1]))
46
+ }
48
47
 
49
- _ => return None,
50
- };
48
+ _ => None,
49
+ }
50
+ }
51
51
 
52
- return Some(ScalarValue {
52
+ pub fn extract_scalar(node: &SyntaxNode) -> Option<ScalarValue> {
53
+ if let Some(token) = find_scalar_token(node) {
54
+ return raw_scalar_value(&token).map(|text| ScalarValue {
53
55
  text,
54
56
  kind: token.kind(),
55
57
  file_path: None,
@@ -72,6 +74,35 @@ pub fn extract_scalar(node: &SyntaxNode) -> Option<ScalarValue> {
72
74
  })
73
75
  }
74
76
 
77
+ pub fn find_block_map(node: &SyntaxNode) -> Option<BlockMap> {
78
+ node.descendants().find_map(BlockMap::cast)
79
+ }
80
+
81
+ pub fn find_block_sequence(node: &SyntaxNode) -> Option<BlockSeq> {
82
+ node.descendants().find_map(BlockSeq::cast)
83
+ }
84
+
85
+ pub enum FirstCollection {
86
+ Map(BlockMap),
87
+ Sequence(BlockSeq),
88
+ }
89
+
90
+ pub fn first_collection(node: &SyntaxNode) -> Option<FirstCollection> {
91
+ match (find_block_map(node), find_block_sequence(node)) {
92
+ (Some(map), Some(sequence)) => {
93
+ if sequence.syntax().text_range().start() <= map.syntax().text_range().start() {
94
+ Some(FirstCollection::Sequence(sequence))
95
+ } else {
96
+ Some(FirstCollection::Map(map))
97
+ }
98
+ }
99
+
100
+ (Some(map), None) => Some(FirstCollection::Map(map)),
101
+ (None, Some(sequence)) => Some(FirstCollection::Sequence(sequence)),
102
+ (None, None) => None,
103
+ }
104
+ }
105
+
75
106
  pub fn is_map_key(token: &SyntaxToken) -> bool {
76
107
  token.parent_ancestors().any(|ancestor| ancestor.kind() == SyntaxKind::BLOCK_MAP_KEY)
77
108
  }
@@ -120,34 +151,7 @@ pub fn quote_if_needed(value: &str) -> String {
120
151
  }
121
152
 
122
153
  pub fn extract_scalar_text(node: &SyntaxNode) -> Option<String> {
123
- if let Some(token) = find_scalar_token(node) {
124
- return match token.kind() {
125
- SyntaxKind::PLAIN_SCALAR => Some(token.text().to_string()),
126
-
127
- SyntaxKind::DOUBLE_QUOTED_SCALAR => {
128
- let text = token.text();
129
- let inner = &text[1..text.len() - 1];
130
-
131
- Some(unescape_double_quoted(inner))
132
- }
133
-
134
- SyntaxKind::SINGLE_QUOTED_SCALAR => {
135
- let text = token.text();
136
- let inner = &text[1..text.len() - 1];
137
-
138
- Some(unescape_single_quoted(inner))
139
- }
140
-
141
- _ => None,
142
- };
143
- }
144
-
145
- let block_scalar_text = node
146
- .descendants_with_tokens()
147
- .filter_map(|element| element.into_token())
148
- .find(|token| token.kind() == SyntaxKind::BLOCK_SCALAR_TEXT)?;
149
-
150
- Some(dedent_block_scalar(block_scalar_text.text()))
154
+ extract_scalar(node).map(|scalar| scalar.text)
151
155
  }
152
156
 
153
157
  pub fn dedent_block_scalar(text: &str) -> String {
@@ -207,6 +211,18 @@ pub fn unescape_single_quoted(text: &str) -> String {
207
211
  text.replace("''", "'")
208
212
  }
209
213
 
214
+ pub fn line_at(source: &str, offset: usize) -> usize {
215
+ source[..offset].matches('\n').count() + 1
216
+ }
217
+
218
+ pub fn line_start_at(source: &str, offset: usize) -> usize {
219
+ source[..offset].rfind('\n').map(|position| position + 1).unwrap_or(0)
220
+ }
221
+
222
+ pub fn column_at(source: &str, offset: usize) -> usize {
223
+ offset - line_start_at(source, offset)
224
+ }
225
+
210
226
  pub fn preceding_whitespace_indent(node: &SyntaxNode) -> String {
211
227
  if let Some(token) = preceding_whitespace_token(node) {
212
228
  let text = token.text();
@@ -221,10 +237,10 @@ pub fn preceding_whitespace_indent(node: &SyntaxNode) -> String {
221
237
  let source = root.text().to_string();
222
238
 
223
239
  if start_offset > 0 {
224
- let before = &source[..start_offset];
240
+ let line_start = line_start_at(&source, start_offset);
225
241
 
226
- if let Some(newline_position) = before.rfind('\n') {
227
- return before[newline_position + 1..].to_string();
242
+ if line_start > 0 {
243
+ return source[line_start..start_offset].to_string();
228
244
  }
229
245
  }
230
246
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yerba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: arm-linux-gnu
6
6
  authors:
7
7
  - Marco Roth
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-06-22 00:00:00.000000000 Z
11
+ date: 2026-07-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A CLI tool for editing YAML while preserving structure, comments, and
14
14
  format.