rlsp-yaml 0.9.0

A fast, lightweight YAML language server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// SPDX-License-Identifier: MIT

use tower_lsp::lsp_types::{CodeAction, CodeActionKind, Position, Range, TextEdit};

use rlsp_yaml_parser::node::Node;
use rlsp_yaml_parser::{Chomp, CollectionStyle, Document, ScalarStyle, Span};

use crate::editing::formatter::{YamlFormatOptions, format_subtree};

use super::{block_to_flow::node_loc, make_action};

pub(super) fn string_to_block_scalar(
    docs: &[Document<Span>],
    _text: &str,
    line_idx: usize,
    uri: &tower_lsp::lsp_types::Url,
) -> Option<CodeAction> {
    let parser_line = line_idx + 1;
    let (scalar, key_col, scalar_loc) = find_block_scalar_candidate(docs, parser_line)?;

    let base_indent = key_col;
    let mut block_scalar = scalar.clone();
    if let Node::Scalar { style, .. } = &mut block_scalar {
        *style = ScalarStyle::Literal(Chomp::Clip);
    }

    let new_text = format_subtree(&block_scalar, &YamlFormatOptions::default(), base_indent);

    if new_text.trim().is_empty() {
        return None;
    }

    #[expect(
        clippy::cast_possible_truncation,
        reason = "LSP line/col are u32; always fits"
    )]
    let edit_range = Range::new(
        Position::new(
            scalar_loc.start.line.saturating_sub(1) as u32,
            scalar_loc.start.column as u32,
        ),
        Position::new(
            scalar_loc.end.line.saturating_sub(1) as u32,
            scalar_loc.end.column as u32,
        ),
    );

    Some(make_action(
        "Convert to block scalar".to_string(),
        uri,
        vec![TextEdit {
            range: edit_range,
            new_text,
        }],
        CodeActionKind::REFACTOR_REWRITE,
        None,
    ))
}

/// Walk the AST to find a qualifying scalar mapping value on the given parser line.
///
/// Returns `(scalar_node, key_start_column, scalar_loc)` when found.
fn find_block_scalar_candidate(
    docs: &[Document<Span>],
    parser_line: usize,
) -> Option<(&Node<Span>, usize, &Span)> {
    for doc in docs {
        if let Some(result) = find_block_scalar_in_node(&doc.root, parser_line) {
            return Some(result);
        }
    }
    None
}

fn find_block_scalar_in_node(
    node: &Node<Span>,
    parser_line: usize,
) -> Option<(&Node<Span>, usize, &Span)> {
    match node {
        Node::Mapping { entries, style, .. } => {
            let is_block = matches!(style, CollectionStyle::Block);
            for (k, v) in entries {
                if is_block {
                    if let Node::Scalar {
                        style: scalar_style,
                        value,
                        loc,
                        ..
                    } = v
                    {
                        if loc.start.line == parser_line
                            && matches!(
                                scalar_style,
                                ScalarStyle::Plain
                                    | ScalarStyle::SingleQuoted
                                    | ScalarStyle::DoubleQuoted
                            )
                            && value.chars().count() >= 40
                        {
                            let key_col = node_loc(k).start.column;
                            return Some((v, key_col, loc));
                        }
                    }
                }
                if let Some(result) = find_block_scalar_in_node(k, parser_line) {
                    return Some(result);
                }
                if let Some(result) = find_block_scalar_in_node(v, parser_line) {
                    return Some(result);
                }
            }
            None
        }
        Node::Sequence { items, .. } => {
            for item in items {
                if let Some(result) = find_block_scalar_in_node(item, parser_line) {
                    return Some(result);
                }
            }
            None
        }
        Node::Scalar { .. } | Node::Alias { .. } => None,
    }
}

#[cfg(test)]
#[expect(
    clippy::indexing_slicing,
    clippy::unwrap_used,
    clippy::expect_used,
    reason = "test code"
)]
mod tests {
    use super::super::code_actions;
    use super::super::test_helpers::{apply_block_scalar_edit, cursor_range, docs_for};
    use crate::test_utils::test_uri;

    #[test]
    fn should_convert_long_string_to_block_scalar() {
        let long_value = "a".repeat(50);
        let text = format!("description: \"{long_value}\"\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );

        let action = actions
            .iter()
            .find(|a| a.title.contains("block scalar"))
            .unwrap();
        let edit = action.edit.as_ref().unwrap();
        let changes = edit.changes.as_ref().unwrap();
        let edits = &changes[&test_uri()];
        assert!(
            edits[0].range.start.character > 0,
            "edit range must start at the scalar, not at column 0"
        );
        assert!(edits[0].new_text.contains("|\n"));
        assert!(edits[0].new_text.contains(&long_value));
    }

    #[test]
    fn should_not_offer_block_scalar_for_short_string() {
        let text = "key: \"short\"\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());

        assert!(actions.iter().all(|a| !a.title.contains("block scalar")));
    }

    #[test]
    fn should_not_offer_block_scalar_for_flow_collection() {
        let long_value = format!("{{{}:1}}", "a".repeat(50));
        let text = format!("key: {long_value}\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );

        assert!(actions.iter().all(|a| !a.title.contains("block scalar")));
    }

    #[test]
    fn should_not_offer_block_scalar_for_scalar_in_flow_mapping_value() {
        let long = "a".repeat(50);
        let text = format!("{{key: \"{long}\"}}\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar for a value inside a flow mapping"
        );
    }

    #[test]
    fn should_not_offer_block_scalar_for_scalar_in_nested_flow_mapping() {
        let long = "a".repeat(50);
        let text = format!("outer: {{key: \"{long}\"}}\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar for a value inside a flow mapping nested in a block mapping"
        );
    }

    // ---- qualifying criteria ----

    #[test]
    fn should_offer_block_scalar_for_plain_scalar_mapping_value() {
        let text = "description: this is a very long plain scalar value that exceeds forty chars\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().any(|a| a.title.contains("block scalar")),
            "expected block-scalar action for plain mapping value"
        );
    }

    #[test]
    fn should_offer_block_scalar_for_single_quoted_mapping_value() {
        let text =
            "description: 'this is a very long single-quoted scalar that exceeds forty chars'\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().any(|a| a.title.contains("block scalar")),
            "expected block-scalar action for single-quoted mapping value"
        );
    }

    #[test]
    fn should_not_offer_block_scalar_for_already_literal_block_scalar() {
        let text =
            "description: |\n  this is a very long literal block scalar that exceeds forty chars\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar action for already-literal value"
        );
    }

    #[test]
    fn should_not_offer_block_scalar_for_already_folded_block_scalar() {
        let text =
            "description: >\n  this is a very long folded block scalar that exceeds forty chars\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar action for already-folded value"
        );
    }

    #[test]
    fn should_not_offer_block_scalar_for_value_below_char_threshold() {
        let text = "key: \"short string under forty\"\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar action for value below 40 char threshold"
        );
    }

    #[test]
    fn should_use_char_count_not_byte_length_for_threshold() {
        let text = "key: \"αβγδεζηθ\"\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar for value with 8 chars (< 40), even if byte length > 8"
        );
    }

    #[test]
    fn should_offer_block_scalar_when_char_count_meets_threshold_with_multibyte() {
        let value = "α".repeat(40);
        let text = format!("key: \"{value}\"\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        assert!(
            actions.iter().any(|a| a.title.contains("block scalar")),
            "must offer block-scalar when char count meets 40-char threshold"
        );
    }

    #[test]
    fn block_scalar_not_offered_when_cursor_on_different_line() {
        let long = "x".repeat(50);
        let text = format!("key: short\nother: \"{long}\"\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar when cursor is on a different line from the long scalar"
        );
    }

    #[test]
    fn block_scalar_indentation_follows_key_column() {
        let long = "a".repeat(50);
        let text = format!("  key: \"{long}\"\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        let action = actions
            .iter()
            .find(|a| a.title == "Convert to block scalar")
            .expect("expected block-scalar action");
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        let new_text = &edits[0].new_text;
        let body_line = new_text
            .lines()
            .nth(1)
            .expect("new_text must have a second line (the scalar body)");
        assert!(
            body_line.starts_with("    ") && !body_line.starts_with("     "),
            "scalar body must be indented by exactly 4 spaces (key_col 2 + tab_width 2), got: {body_line:?}"
        );
    }

    #[test]
    fn block_scalar_title_is_convert_to_block_scalar() {
        let long = "a".repeat(50);
        let text = format!("key: \"{long}\"\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        let action = actions
            .iter()
            .find(|a| a.title == "Convert to block scalar")
            .expect("action with exact title 'Convert to block scalar' must be present");
        assert_eq!(
            action.title, "Convert to block scalar",
            "title must be exactly 'Convert to block scalar'"
        );
    }

    // ---- defect class regressions ----

    #[test]
    fn should_resolve_escape_sequences_in_double_quoted_value() {
        let text = "summary: \"line one\\nline two\\ttabbed\\\\backslash\\\"quote and more padding here\"\n";
        let (result, _edit) = apply_block_scalar_edit(text, 0);
        assert!(
            !result.contains("\\n"),
            "block scalar must not contain literal \\n escape: {result:?}"
        );
        assert!(
            !result.contains("\\t"),
            "block scalar must not contain literal \\t escape: {result:?}"
        );
        assert!(
            result.contains('\n'),
            "block scalar must contain actual newline: {result:?}"
        );
        assert!(
            result.contains('\t'),
            "block scalar must contain actual tab: {result:?}"
        );
        assert!(
            result.contains('\\'),
            "block scalar must contain literal backslash: {result:?}"
        );
        assert!(
            result.contains('"'),
            "block scalar must contain literal double-quote: {result:?}"
        );
    }

    #[test]
    fn block_scalar_double_quoted_backslash_and_quote_resolved() {
        let text = "key: \"contains \\\\backslash and \\\"quote\\\" here plus some extra padding chars\"\n";
        let (result, _edit) = apply_block_scalar_edit(text, 0);
        assert!(
            result.contains('\\'),
            "block scalar must contain a literal backslash: {result:?}"
        );
        assert!(
            result.contains('"'),
            "block scalar must contain a literal double-quote: {result:?}"
        );
    }

    #[test]
    fn block_scalar_uses_char_count_not_byte_count_for_threshold() {
        let val: String = "é".repeat(39);
        let text = format!("key: \"{val}\"\n");
        let actions = code_actions(
            &docs_for(&text),
            &text,
            cursor_range(0, 0),
            &[],
            &test_uri(),
        );
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar for 39 chars (< threshold), even though byte len = 78"
        );
    }

    #[test]
    fn should_resolve_single_quoted_escape_to_literal_apostrophe() {
        let text = "note: 'it''s a long string that should exceed the forty character threshold'\n";
        let (result, _edit) = apply_block_scalar_edit(text, 0);
        assert!(
            result.contains("it's"),
            "block scalar must contain resolved apostrophe: {result:?}"
        );
        assert!(
            !result.contains("it''s"),
            "block scalar must not contain the '' escape sequence: {result:?}"
        );
    }

    #[test]
    fn should_not_be_fooled_by_colon_in_url_value() {
        let text = "homepage: \"https://example.com/very-long-path-that-exceeds-forty-chars\"\n";
        let (result, _edit) = apply_block_scalar_edit(text, 0);
        assert!(
            result.contains("https://example.com/very-long-path-that-exceeds-forty-chars"),
            "full URL must be preserved in block scalar output: {result:?}"
        );
        assert!(
            result.contains("|\n"),
            "output must be a literal block scalar: {result:?}"
        );
    }

    #[test]
    fn should_preserve_trailing_comment_when_converting_to_block_scalar() {
        let text = "description: \"this is a long string that exceeds forty chars\"  # keep me\n";
        let (result, edit) = apply_block_scalar_edit(text, 0);
        let scalar_end_col =
            "description: \"this is a long string that exceeds forty chars\"".len();
        assert_eq!(
            edit.range.end.character as usize, scalar_end_col,
            "edit end column must equal the exclusive end of the scalar span: range={:?}",
            edit.range
        );
        assert!(
            !edit.new_text.contains("# keep me"),
            "new_text must not contain the trailing comment: {:?}",
            edit.new_text
        );
        assert!(
            result.contains("# keep me"),
            "trailing comment must survive in the final edited text: {result:?}"
        );
    }

    #[test]
    fn should_not_be_fooled_by_colon_in_quoted_key() {
        let text = "\"foo:bar\": \"this is a long mapping value that exceeds forty characters\"\n";
        let (result, _edit) = apply_block_scalar_edit(text, 0);
        assert!(
            result.contains("this is a long mapping value that exceeds forty characters"),
            "actual value must be preserved: {result:?}"
        );
        assert!(
            result.contains("|\n"),
            "output must be literal block scalar: {result:?}"
        );
    }

    #[test]
    fn should_not_offer_block_scalar_for_sequence_item() {
        let text = "- \"this is a very long sequence item value that exceeds forty characters\"\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar for sequence item (only mapping values qualify)"
        );
    }

    #[test]
    fn should_not_offer_block_scalar_for_flow_sequence_value() {
        let text = "key: [one, two, three, four, five, six, seven, eight, nine, ten]\n";
        let actions = code_actions(&docs_for(text), text, cursor_range(0, 0), &[], &test_uri());
        assert!(
            actions.iter().all(|a| !a.title.contains("block scalar")),
            "must not offer block-scalar when mapping value is a flow sequence"
        );
    }

    #[test]
    fn should_preserve_anchor_when_converting_to_block_scalar() {
        let text =
            "description: &myanchor \"this is a long string that exceeds forty characters\"\n";
        let (result, _edit) = apply_block_scalar_edit(text, 0);
        assert!(
            result.contains("&myanchor"),
            "anchor must be preserved in block scalar output: {result:?}"
        );
    }
}