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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// SPDX-License-Identifier: MIT

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

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

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

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

pub(super) fn yaml11_octal_actions(
    docs: &[Document<Span>],
    diag: &Diagnostic,
    uri: &tower_lsp::lsp_types::Url,
) -> Vec<CodeAction> {
    let Some((scalar, loc, base_indent)) = find_yaml11_octal_scalar(docs, diag) else {
        return vec![];
    };
    let Node::Scalar { value, .. } = scalar else {
        return vec![];
    };

    let mut quoted = scalar.clone();
    if let Node::Scalar { style, .. } = &mut quoted {
        *style = ScalarStyle::DoubleQuoted;
    }
    let mut converted = scalar.clone();
    if let Node::Scalar {
        style, value: v, ..
    } = &mut converted
    {
        *style = ScalarStyle::Plain;
        *v = format!("0o{}", &value[1..]);
    }

    let quote_opts = YamlFormatOptions {
        preserve_quotes: true,
        ..YamlFormatOptions::default()
    };
    let quoted_text = format_subtree(&quoted, &quote_opts, base_indent);
    let converted_text = format_subtree(&converted, &YamlFormatOptions::default(), base_indent);

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

    vec![
        make_action(
            "Quote as string".to_string(),
            uri,
            vec![TextEdit {
                range: edit_range,
                new_text: quoted_text,
            }],
            CodeActionKind::QUICKFIX,
            Some(vec![diag.clone()]),
        ),
        make_action(
            "Convert to YAML 1.2 octal".to_string(),
            uri,
            vec![TextEdit {
                range: edit_range,
                new_text: converted_text,
            }],
            CodeActionKind::QUICKFIX,
            Some(vec![diag.clone()]),
        ),
    ]
}

fn find_yaml11_octal_scalar<'a>(
    docs: &'a [Document<Span>],
    diag: &Diagnostic,
) -> Option<(&'a Node<Span>, &'a Span, usize)> {
    let col_match = diagnostic_code(diag) == Some("yaml11Octal");
    let parser_line = diag.range.start.line as usize + 1;
    if !col_match {
        let count: usize = docs
            .iter()
            .map(|doc| count_yaml11_octal_on_line(&doc.root, parser_line))
            .sum();
        if count != 1 {
            return None;
        }
    }
    for doc in docs {
        if let Some(result) = find_yaml11_octal_in_node(&doc.root, parser_line, diag, col_match) {
            return Some(result);
        }
    }
    None
}

fn count_yaml11_octal_on_line(node: &Node<Span>, parser_line: usize) -> usize {
    match node {
        Node::Mapping { entries, .. } => entries
            .iter()
            .map(|(k, v)| {
                let v_count = if let Node::Scalar {
                    style: ScalarStyle::Plain,
                    value,
                    loc,
                    ..
                } = v
                {
                    usize::from(
                        loc.start.line == parser_line
                            && crate::scalar_helpers::is_yaml11_octal(value),
                    )
                } else {
                    count_yaml11_octal_on_line(v, parser_line)
                };
                count_yaml11_octal_on_line(k, parser_line) + v_count
            })
            .sum(),
        Node::Sequence { items, .. } => items
            .iter()
            .map(|item| {
                if let Node::Scalar {
                    style: ScalarStyle::Plain,
                    value,
                    loc,
                    ..
                } = item
                {
                    usize::from(
                        loc.start.line == parser_line
                            && crate::scalar_helpers::is_yaml11_octal(value),
                    )
                } else {
                    count_yaml11_octal_on_line(item, parser_line)
                }
            })
            .sum(),
        Node::Scalar { .. } | Node::Alias { .. } => 0,
    }
}

const fn yaml11_octal_col_matches_diag(loc: &Span, diag: &Diagnostic) -> bool {
    diag.range.start.character as usize == loc.start.column
        && diag.range.end.character as usize == loc.end.column
}

fn find_yaml11_octal_in_node<'a>(
    node: &'a Node<Span>,
    parser_line: usize,
    diag: &Diagnostic,
    col_match: bool,
) -> Option<(&'a Node<Span>, &'a Span, usize)> {
    match node {
        Node::Mapping { entries, .. } => {
            for (k, v) in entries {
                if let Node::Scalar {
                    style: ScalarStyle::Plain,
                    value,
                    loc,
                    ..
                } = v
                {
                    if loc.start.line == parser_line
                        && crate::scalar_helpers::is_yaml11_octal(value)
                        && (!col_match || yaml11_octal_col_matches_diag(loc, diag))
                    {
                        let key_col = node_loc(k).start.column;
                        return Some((v, loc, key_col));
                    }
                }
                if let Some(result) = find_yaml11_octal_in_node(k, parser_line, diag, col_match) {
                    return Some(result);
                }
                if let Some(result) = find_yaml11_octal_in_node(v, parser_line, diag, col_match) {
                    return Some(result);
                }
            }
            None
        }
        Node::Sequence { items, .. } => {
            for item in items {
                if let Node::Scalar {
                    style: ScalarStyle::Plain,
                    value,
                    loc,
                    ..
                } = item
                {
                    if loc.start.line == parser_line
                        && crate::scalar_helpers::is_yaml11_octal(value)
                        && (!col_match || yaml11_octal_col_matches_diag(loc, diag))
                    {
                        return Some((item, loc, loc.start.column));
                    }
                }
                if let Some(result) = find_yaml11_octal_in_node(item, parser_line, diag, col_match)
                {
                    return Some(result);
                }
            }
            None
        }
        Node::Scalar { .. } | Node::Alias { .. } => None,
    }
}

#[cfg(test)]
#[expect(clippy::indexing_slicing, clippy::unwrap_used, reason = "test code")]
mod tests {
    use tower_lsp::lsp_types::NumberOrString;

    use super::super::code_actions;
    use super::super::test_helpers::{docs_for, line_range, make_diagnostic};
    use crate::test_utils::test_uri;

    #[test]
    fn should_not_offer_yaml11_octal_quote_for_out_of_bounds_range() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 100, 104, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());

        assert!(actions.iter().all(|a| a.title != "Quote as string"));
    }

    #[test]
    fn yaml11_octal_actions_attach_diagnostic() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 6, 10, "yaml11Octal");
        let actions = code_actions(
            &docs_for(text),
            text,
            line_range(0),
            std::slice::from_ref(&diag),
            &test_uri(),
        );

        for action in actions
            .iter()
            .filter(|a| a.title == "Quote as string" || a.title == "Convert to YAML 1.2 octal")
        {
            let attached = action.diagnostics.as_ref().unwrap();
            assert_eq!(attached.len(), 1);
            assert_eq!(
                attached[0].code,
                Some(NumberOrString::String("yaml11Octal".to_string()))
            );
        }
    }

    #[test]
    fn yaml11_octal_on_line_other_than_zero() {
        let text = "name: foo\nmode: 0755\n";
        let diag = make_diagnostic(1, 6, 10, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(1), &[diag], &test_uri());

        let action = actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].range.start.line, 1, "edit must target line 1");
        assert_eq!(
            edits[0].range.start.character, 6,
            "edit must start at scalar col"
        );
        assert_eq!(edits[0].new_text, "\"0755\"");
    }

    #[test]
    fn yaml11_octal_diagnostic_not_triggered_by_other_codes() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 0, 10, "flowSeq");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());

        assert!(actions.iter().all(|a| a.title != "Quote as string"));
        assert!(
            actions
                .iter()
                .all(|a| a.title != "Convert to YAML 1.2 octal")
        );
    }

    #[test]
    fn yaml11_octal_quote_action_new_text_is_scalar_only() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 6, 10, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "\"0755\"");
        assert_eq!(edits[0].range.start.character, 6);
        assert_eq!(edits[0].range.end.character, 10);
    }

    #[test]
    fn yaml11_octal_convert_action_new_text_is_scalar_only() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 6, 10, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Convert to YAML 1.2 octal")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "0o755");
        assert_eq!(edits[0].range.start.character, 6);
        assert_eq!(edits[0].range.end.character, 10);
    }

    #[test]
    fn yaml11_octal_quote_on_0777_produces_valid_double_quoted_yaml() {
        let text = "perms: 0777\n";
        let diag = make_diagnostic(0, 7, 11, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "\"0777\"");
        let start = edits[0].range.start.character as usize;
        let end = edits[0].range.end.character as usize;
        let line = "perms: 0777";
        let result = format!("{}{}{}\n", &line[..start], edits[0].new_text, &line[end..]);
        let parse_result = crate::parser::parse_yaml(&result);
        assert!(
            parse_result.diagnostics.is_empty(),
            "quoted octal must produce valid YAML; got: {:?}\nresult: {result:?}",
            parse_result.diagnostics
        );
    }

    #[test]
    fn yaml11_octal_convert_on_0755_produces_0o755() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 6, 10, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Convert to YAML 1.2 octal")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "0o755");
    }

    #[test]
    fn yaml11_octal_convert_on_0777_produces_0o777() {
        let text = "perms: 0777\n";
        let diag = make_diagnostic(0, 7, 11, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Convert to YAML 1.2 octal")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "0o777");
    }

    #[test]
    fn yaml11_octal_rejects_08_no_actions() {
        let text = "val: 08\n";
        let diag = make_diagnostic(0, 5, 7, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        assert!(actions.iter().all(|a| a.title != "Quote as string"));
        assert!(
            actions
                .iter()
                .all(|a| a.title != "Convert to YAML 1.2 octal")
        );
    }

    #[test]
    fn yaml11_octal_rejects_09_no_actions() {
        let text = "val: 09\n";
        let diag = make_diagnostic(0, 5, 7, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        assert!(actions.iter().all(|a| a.title != "Quote as string"));
        assert!(
            actions
                .iter()
                .all(|a| a.title != "Convert to YAML 1.2 octal")
        );
    }

    #[test]
    fn yaml11_octal_trailing_comment_preserved_quote_action() {
        let text = "mode: 0755  # keep this\n";
        let diag = make_diagnostic(0, 6, 10, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "\"0755\"");
        assert!(
            edits[0].range.end.character <= 10,
            "range must not reach into comment"
        );
        let start = edits[0].range.start.character as usize;
        let end = edits[0].range.end.character as usize;
        let line = "mode: 0755  # keep this";
        let result = format!("{}{}{}", &line[..start], edits[0].new_text, &line[end..]);
        assert!(
            result.contains("# keep this"),
            "trailing comment must survive in: {result:?}"
        );
    }

    #[test]
    fn yaml11_octal_trailing_comment_preserved_convert_action() {
        let text = "mode: 0755  # keep this\n";
        let diag = make_diagnostic(0, 6, 10, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Convert to YAML 1.2 octal")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert_eq!(edits[0].new_text, "0o755");
        assert!(
            edits[0].range.end.character <= 10,
            "range must not reach into comment"
        );
        let start = edits[0].range.start.character as usize;
        let end = edits[0].range.end.character as usize;
        let line = "mode: 0755  # keep this";
        let result = format!("{}{}{}", &line[..start], edits[0].new_text, &line[end..]);
        assert!(
            result.contains("# keep this"),
            "trailing comment must survive in: {result:?}"
        );
    }

    #[test]
    fn yaml11_octal_sequence_item_edit_starts_at_scalar_col() {
        let text = "modes:\n  - 0755\n";
        // `0755` starts at col 4 in line 1: "  - 0755"
        let diag = make_diagnostic(1, 4, 8, "yaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(1), &[diag], &test_uri());
        let action = actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let edits = &action.edit.as_ref().unwrap().changes.as_ref().unwrap()[&test_uri()];
        assert!(
            edits[0].range.start.character > 0,
            "sequence item edit must not start at col 0"
        );
        assert_eq!(edits[0].new_text, "\"0755\"");
    }

    #[test]
    fn yaml11_octal_multi_octal_per_line_schema_code_offers_no_action() {
        let text = "{a: 0755, b: 0644}\n";
        // schemaYaml11Octal uses line-only matching; two octals on the same line must suppress both
        let diag = make_diagnostic(0, 4, 8, "schemaYaml11Octal");
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        assert!(actions.iter().all(|a| a.title != "Quote as string"));
        assert!(
            actions
                .iter()
                .all(|a| a.title != "Convert to YAML 1.2 octal")
        );
    }

    #[test]
    fn yaml11_octal_multi_octal_per_line_direct_code_resolves_by_col() {
        let text = "{a: 0755, b: 0644}\n";
        let first_diag = make_diagnostic(0, 4, 8, "yaml11Octal");
        let second_diag = make_diagnostic(0, 13, 17, "yaml11Octal");

        let first_actions = code_actions(
            &docs_for(text),
            text,
            line_range(0),
            &[first_diag],
            &test_uri(),
        );
        let first_action = first_actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let first_edits = &first_action
            .edit
            .as_ref()
            .unwrap()
            .changes
            .as_ref()
            .unwrap()[&test_uri()];
        assert_eq!(first_edits[0].new_text, "\"0755\"");
        assert_eq!(first_edits[0].range.start.character, 4);

        let second_actions = code_actions(
            &docs_for(text),
            text,
            line_range(0),
            &[second_diag],
            &test_uri(),
        );
        let second_action = second_actions
            .iter()
            .find(|a| a.title == "Quote as string")
            .unwrap();
        let second_edits = &second_action
            .edit
            .as_ref()
            .unwrap()
            .changes
            .as_ref()
            .unwrap()[&test_uri()];
        assert_eq!(second_edits[0].new_text, "\"0644\"");
        assert_eq!(second_edits[0].range.start.character, 13);
    }

    // ════════════════════════════════════════════════════════════════════
    // Group F: schemaYaml11Octal code actions
    // ════════════════════════════════════════════════════════════════════

    // F4: both actions attach the triggering schemaYaml11Octal diagnostic
    #[test]
    fn schema_yaml11_octal_actions_attach_diagnostic() {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 6, 10, "schemaYaml11Octal");
        let actions = code_actions(
            &docs_for(text),
            text,
            line_range(0),
            std::slice::from_ref(&diag),
            &test_uri(),
        );
        for action in actions
            .iter()
            .filter(|a| a.title == "Quote as string" || a.title == "Convert to YAML 1.2 octal")
        {
            let diags = action.diagnostics.as_ref().unwrap();
            assert_eq!(diags.len(), 1);
            assert_eq!(
                super::super::diagnostic_code(&diags[0]),
                Some("schemaYaml11Octal"),
                "action '{}' should attach schemaYaml11Octal diagnostic",
                action.title
            );
        }
    }

    use rstest::rstest;

    #[rstest]
    #[case::yaml11_octal_code("yaml11Octal")]
    #[case::schema_yaml11_octal_code("schemaYaml11Octal")]
    fn yaml11_octal_both_diag_codes_produce_two_actions(#[case] code: &str) {
        let text = "mode: 0755\n";
        let diag = make_diagnostic(0, 6, 10, code);
        let actions = code_actions(&docs_for(text), text, line_range(0), &[diag], &test_uri());
        let count = actions
            .iter()
            .filter(|a| a.title == "Quote as string" || a.title == "Convert to YAML 1.2 octal")
            .count();
        assert_eq!(count, 2);
    }
}