perl-dap 0.15.0

Debug Adapter Protocol server for Perl
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Inline value extraction for DAP inlineValues requests.
//!
//! This module provides both a lightweight regex-based implementation and
//! a runtime-enriched version that queries the Perl debugger for actual
//! variable values.

use std::collections::{HashMap, HashSet};

mod code_mask;
mod regex_support;

use code_mask::code_byte_mask;
use regex_support::{BRACED_PERL_VAR_RE, PERL_VAR_RE, SCALAR_VAR_RE, is_special_variable_name};

use crate::protocol::InlineValueText;

/// Convert 1-based line bounds into inclusive 0-based indexes.
///
/// Non-positive line inputs are clamped to line 1. End bounds past the
/// available line count are clamped to the final line.
fn normalize_line_bounds(
    start_line: i64,
    end_line: i64,
    line_count: usize,
) -> Option<(usize, usize)> {
    if line_count == 0 {
        return None;
    }

    let start_1_based = start_line.max(1) as usize;
    if start_1_based > line_count {
        return None;
    }
    let end_1_based = (end_line.max(1) as usize).min(line_count);

    let start_idx = start_1_based.saturating_sub(1);
    let end_idx = end_1_based.saturating_sub(1);

    (start_idx <= end_idx).then_some((start_idx, end_idx))
}

fn collect_line_variables(line: &str, include_non_scalars: bool) -> Vec<(usize, usize, String)> {
    let mut matches = Vec::new();

    let base_re = if include_non_scalars { PERL_VAR_RE.as_ref() } else { SCALAR_VAR_RE.as_ref() };
    if let Some(re) = base_re {
        for cap in re.captures_iter(line) {
            if let Some(m) = cap.iter().flatten().next() {
                matches.push((m.start(), m.end(), m.as_str().to_string()));
            }
        }
    }

    if let Some(re) = BRACED_PERL_VAR_RE.as_ref() {
        for cap in re.captures_iter(line) {
            let (Some(full_match), Some(sigil_match), Some(name_match)) =
                (cap.iter().flatten().next(), cap.get(1), cap.get(2))
            else {
                continue;
            };
            if !include_non_scalars && sigil_match.as_str() != "$" {
                continue;
            }
            matches.push((
                full_match.start(),
                full_match.end(),
                format!("{}{}", sigil_match.as_str(), name_match.as_str()),
            ));
        }
    }

    matches.sort_by(|a, b| (a.0, a.1, &a.2).cmp(&(b.0, b.1, &b.2)));
    matches.dedup_by(|a, b| a.0 == b.0 && a.1 == b.1 && a.2 == b.2);
    matches
}

/// Extract unique variable names from source code within a line range.
///
/// Lines are 1-based. Returns deduplicated variable names with their sigils.
pub fn extract_variable_names(source: &str, start_line: i64, end_line: i64) -> Vec<String> {
    if PERL_VAR_RE.is_none() && BRACED_PERL_VAR_RE.is_none() {
        return Vec::new();
    }
    let lines: Vec<&str> = source.lines().collect();
    if lines.is_empty() {
        return Vec::new();
    }

    let Some((start_idx, end_idx)) = normalize_line_bounds(start_line, end_line, lines.len())
    else {
        return Vec::new();
    };

    let mut seen = HashSet::new();
    let mut names = Vec::new();

    for line in lines.iter().skip(start_idx).take(end_idx - start_idx + 1) {
        let code_mask = code_byte_mask(line);
        for (start, end, name) in collect_line_variables(line, true) {
            if !code_mask[start..end].iter().all(|is_code| *is_code) {
                continue;
            }
            if !is_special_variable_name(&name) && seen.insert(name.clone()) {
                names.push(name);
            }
        }
    }

    names
}

/// Format a variable's inline value with Perl-idiomatic formatting.
///
/// - Scalars: `$x = "value"`
/// - Arrays: `@arr = (3 elements)`
/// - Hashes: `%hash = (5 keys)`
/// - Blessed refs: `$obj = Foo=HASH(...)`
pub fn format_inline_value(name: &str, raw_value: &str) -> String {
    let sigil = name.chars().next().unwrap_or('$');
    match sigil {
        '@' => {
            let count = parse_array_element_count(raw_value);
            format!("{name} = ({count} elements)")
        }
        '%' => {
            let count = parse_hash_key_count(raw_value);
            format!("{name} = ({count} keys)")
        }
        _ => {
            let trimmed = raw_value.trim();
            if trimmed.len() > 60 {
                let preview: String = trimmed.chars().take(57).collect();
                format!("{name} = {}...", preview)
            } else {
                format!("{name} = {trimmed}")
            }
        }
    }
}

/// Parse an array element count from a debugger response.
fn parse_array_element_count(raw: &str) -> &str {
    let trimmed = raw.trim();
    if trimmed.chars().all(|c| c.is_ascii_digit()) {
        return trimmed;
    }
    "?"
}

/// Parse a hash key count from a debugger response.
fn parse_hash_key_count(raw: &str) -> &str {
    let trimmed = raw.trim();
    if trimmed.chars().all(|c| c.is_ascii_digit()) {
        return trimmed;
    }
    "?"
}

/// Collect inline values with runtime variable resolution.
///
/// When `runtime_values` is provided, variables are displayed with their
/// actual values from the debugger. Otherwise, a `= ?` placeholder is used.
///
/// Lines and columns are 1-based to match the DAP defaults.
pub fn collect_inline_values_with_runtime(
    source: &str,
    start_line: i64,
    end_line: i64,
    runtime_values: Option<&HashMap<String, String>>,
) -> Vec<InlineValueText> {
    if PERL_VAR_RE.is_none() && BRACED_PERL_VAR_RE.is_none() {
        return Vec::new();
    }
    let lines: Vec<&str> = source.lines().collect();
    if lines.is_empty() {
        return Vec::new();
    }

    let Some((start_idx, end_idx)) = normalize_line_bounds(start_line, end_line, lines.len())
    else {
        return Vec::new();
    };

    let mut inline_values = Vec::new();
    let mut seen_on_line: HashSet<(usize, String)> = HashSet::new();

    for (idx, line) in lines.iter().enumerate().skip(start_idx).take(end_idx - start_idx + 1) {
        let code_mask = code_byte_mask(line);
        for (start, end, var_name) in collect_line_variables(line, true) {
            if !code_mask[start..end].iter().all(|is_code| *is_code) {
                continue;
            }
            if is_special_variable_name(&var_name) {
                continue;
            }
            if !seen_on_line.insert((idx, var_name.clone())) {
                continue;
            }
            let column = (start + 1) as i64;
            let text = match runtime_values.and_then(|rv| rv.get(&var_name)) {
                Some(rv) => format_inline_value(&var_name, rv),
                None => format!("{} = ?", var_name),
            };
            inline_values.push(InlineValueText { line: (idx + 1) as i64, column, text });
        }
    }

    inline_values
}

/// Legacy: Collect inline values for scalar variables within a line range.
///
/// Lines and columns are 1-based to match the DAP defaults.
/// Kept for backward compatibility with `DapDispatcher`.
pub fn collect_inline_values(source: &str, start_line: i64, end_line: i64) -> Vec<InlineValueText> {
    let lines: Vec<&str> = source.lines().collect();
    if lines.is_empty() {
        return Vec::new();
    }

    let Some((start_idx, end_idx)) = normalize_line_bounds(start_line, end_line, lines.len())
    else {
        return Vec::new();
    };

    if SCALAR_VAR_RE.is_none() && BRACED_PERL_VAR_RE.is_none() {
        return Vec::new();
    }
    let mut inline_values = Vec::new();
    let mut seen_on_line: HashSet<(usize, String)> = HashSet::new();

    for (idx, line) in lines.iter().enumerate().skip(start_idx).take(end_idx - start_idx + 1) {
        let code_mask = code_byte_mask(line);
        for (start, end, var_name) in collect_line_variables(line, false) {
            if !code_mask[start..end].iter().all(|is_code| *is_code) {
                continue;
            }
            if is_special_variable_name(&var_name) {
                continue;
            }
            if !seen_on_line.insert((idx, var_name.clone())) {
                continue;
            }
            let column = (start + 1) as i64;
            inline_values.push(InlineValueText {
                line: (idx + 1) as i64,
                column,
                text: format!("{} = ?", var_name),
            });
        }
    }

    inline_values
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_inline_value_regexes_compile() {
        assert!(PERL_VAR_RE.is_some());
        assert!(BRACED_PERL_VAR_RE.is_some());
        assert!(SCALAR_VAR_RE.is_some());
    }

    #[test]
    fn test_collect_line_variables_keeps_code_scalars_visible() {
        let line = "my $x = 1;";
        let matches = collect_line_variables(line, false);
        assert_eq!(matches, vec![(3, 5, "$x".to_string())]);

        let code_mask = code_byte_mask(line);
        assert!(code_mask[3..5].iter().all(|is_code| *is_code));
    }

    #[test]
    fn test_collect_inline_values_legacy() {
        let source = "my $x = 1;\nmy $y = $x + 2;";
        let values = collect_inline_values(source, 1, 2);
        assert!(values.iter().any(|v| v.text.contains("$x")));
        assert!(values.iter().any(|v| v.text.contains("$y")));
    }

    #[test]
    fn test_scalar_inline_value() {
        let source = "my $name = \"Hello\";";
        let mut rv = HashMap::new();
        rv.insert("$name".to_string(), "'Hello'".to_string());
        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$name = 'Hello'");
    }

    #[test]
    fn test_array_inline_value() {
        let source = "my @items = (1, 2, 3);";
        let mut rv = HashMap::new();
        rv.insert("@items".to_string(), "3".to_string());
        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values[0].text, "@items = (3 elements)");
    }

    #[test]
    fn test_hash_inline_value() {
        let source = "my %config = (a => 1);";
        let mut rv = HashMap::new();
        rv.insert("%config".to_string(), "5".to_string());
        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values[0].text, "%config = (5 keys)");
    }

    #[test]
    fn test_blessed_ref_inline_value() {
        let source = "my $obj = Foo->new();";
        let mut rv = HashMap::new();
        rv.insert("$obj".to_string(), "Foo=HASH(0xdeadbeef)".to_string());
        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values[0].text, "$obj = Foo=HASH(0xdeadbeef)");
    }

    #[test]
    fn test_empty_collections() {
        let mut rv = HashMap::new();
        rv.insert("@empty".to_string(), "0".to_string());
        rv.insert("%none".to_string(), "0".to_string());
        let source = "my @empty; my %none;";
        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert!(values.iter().any(|v| v.text == "@empty = (0 elements)"));
        assert!(values.iter().any(|v| v.text == "%none = (0 keys)"));
    }

    #[test]
    fn test_deduplication_per_line() {
        let source = "$x = $x + $x;";
        let values = collect_inline_values_with_runtime(source, 1, 2, None);
        assert_eq!(values.len(), 1);
    }

    #[test]
    fn test_special_vars_excluded() {
        let source = "print $_; warn $!; my $val = 1;";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert_eq!(values.len(), 1);
        assert!(values[0].text.contains("$val"));
    }

    #[test]
    fn test_extract_variable_names() {
        let source = "my $x = 1;\nmy @arr = (1,2,3);\nmy %h = (a => 1);";
        let names = extract_variable_names(source, 1, 3);
        assert!(names.contains(&"$x".to_string()));
        assert!(names.contains(&"@arr".to_string()));
        assert!(names.contains(&"%h".to_string()));
    }

    #[test]
    fn test_extract_variable_names_with_namespace_qualifiers() {
        let source = "our $Foo::bar = 1;\nour @My::Pkg::items = (1);\nour %App::Config::opts = ();";
        let names = extract_variable_names(source, 1, 3);
        assert!(names.contains(&"$Foo::bar".to_string()));
        assert!(names.contains(&"@My::Pkg::items".to_string()));
        assert!(names.contains(&"%App::Config::opts".to_string()));
    }

    #[test]
    fn test_extract_variable_names_with_legacy_namespace_qualifiers() {
        let source = "our $Foo'bar = 1;\nour @My'Pkg'items = (1);\nour %App'Config'opts = ();";
        let names = extract_variable_names(source, 1, 3);
        assert!(names.contains(&"$Foo'bar".to_string()));
        assert!(names.contains(&"@My'Pkg'items".to_string()));
        assert!(names.contains(&"%App'Config'opts".to_string()));
    }

    #[test]
    fn test_no_runtime_fallback() {
        let source = "my $x = 1;";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$x = ?");
    }

    #[test]
    fn test_scalar_truncation_uses_char_boundaries() {
        let source = "my $name = 1;";
        let long_value = "é".repeat(80);
        let mut rv = HashMap::new();
        rv.insert("$name".to_string(), long_value);

        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values.len(), 1);
        assert!(values[0].text.starts_with("$name = "));
        assert!(values[0].text.ends_with("..."));
    }

    #[test]
    fn test_non_positive_line_bounds_are_clamped() {
        let source = "my $x = 1;\nmy $y = 2;";
        let names = extract_variable_names(source, 0, 1);
        assert_eq!(names, vec!["$x".to_string()]);

        let values = collect_inline_values_with_runtime(source, 0, 0, None);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$x = ?");
    }

    #[test]
    fn test_inverted_line_bounds_return_empty() {
        let source = "my $x = 1;\nmy $y = 2;";
        assert!(extract_variable_names(source, 2, 1).is_empty());
        assert!(collect_inline_values_with_runtime(source, 2, 1, None).is_empty());
        assert!(collect_inline_values(source, 2, 1).is_empty());
    }

    #[test]
    fn test_out_of_range_line_bounds_return_empty() {
        let source = "my $x = 1;\nmy $y = 2;";
        assert!(extract_variable_names(source, 3, 3).is_empty());
        assert!(collect_inline_values_with_runtime(source, 3, 3, None).is_empty());
        assert!(collect_inline_values(source, 3, 3).is_empty());
    }

    #[test]
    fn test_end_line_past_file_is_clamped() {
        let source = "my $x = 1;\nmy $y = 2;";

        let names = extract_variable_names(source, 2, 999);
        assert_eq!(names, vec!["$y".to_string()]);

        let values = collect_inline_values_with_runtime(source, 2, 999, None);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$y = ?");

        let legacy_values = collect_inline_values(source, 2, 999);
        assert_eq!(legacy_values.len(), 1);
        assert_eq!(legacy_values[0].text, "$y = ?");
    }

    #[test]
    fn test_runtime_inline_value_for_namespaced_scalar() {
        let source = "our $Foo::bar = 1;";
        let mut rv = HashMap::new();
        rv.insert("$Foo::bar".to_string(), "42".to_string());

        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$Foo::bar = 42");
    }

    #[test]
    fn test_runtime_inline_value_for_legacy_namespaced_scalar() {
        let source = "our $Foo'bar = 1;";
        let mut rv = HashMap::new();
        rv.insert("$Foo'bar".to_string(), "42".to_string());

        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$Foo'bar = 42");
    }

    #[test]
    fn test_runtime_inline_value_for_main_namespaced_scalar() {
        let source = "our $::bar = 1;";
        let mut rv = HashMap::new();
        rv.insert("$::bar".to_string(), "42".to_string());

        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$::bar = 42");
    }

    #[test]
    fn test_variable_like_tokens_in_strings_and_comments_are_ignored() {
        let source = "my $real = 1; my $msg = \"$fake\"; # $commented";
        let names = extract_variable_names(source, 1, 1);
        assert!(names.contains(&"$real".to_string()));
        assert!(names.contains(&"$msg".to_string()));
        assert!(!names.contains(&"$fake".to_string()));
        assert!(!names.contains(&"$commented".to_string()));
    }

    #[test]
    fn test_inline_values_ignore_strings_and_comments() {
        let source = "my $real = 1; print \"$ignored\"; # and $commented";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$real = ?");
    }

    #[test]
    fn test_array_length_marker_does_not_start_comment() {
        let source = "my $len = $#arr; my $next = 1;";

        let names = extract_variable_names(source, 1, 1);
        assert!(names.contains(&"$len".to_string()));
        assert!(names.contains(&"$next".to_string()));

        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert!(values.iter().any(|v| v.text == "$len = ?"));
        assert!(values.iter().any(|v| v.text == "$next = ?"));
    }

    #[test]
    fn test_braced_array_length_marker_does_not_start_comment() {
        let source = "my $len = ${#arr}; my $next = 1;";

        let names = extract_variable_names(source, 1, 1);
        assert!(names.contains(&"$len".to_string()));
        assert!(names.contains(&"$next".to_string()));
    }

    #[test]
    fn test_variable_like_tokens_in_quote_like_operators_are_ignored() {
        let source = "my $real = 1; my $str = qq{$fake}; my $lit = q[$ghost];";
        let names = extract_variable_names(source, 1, 1);
        assert!(names.contains(&"$real".to_string()));
        assert!(names.contains(&"$str".to_string()));
        assert!(names.contains(&"$lit".to_string()));
        assert!(!names.contains(&"$fake".to_string()));
        assert!(!names.contains(&"$ghost".to_string()));
    }

    #[test]
    fn test_inline_values_ignore_quote_like_operators() {
        let source = "my $real = 1; my $str = qq($fake); my $lit = q/$ghost/;";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert_eq!(values.len(), 3);
        assert!(values.iter().any(|v| v.text == "$real = ?"));
        assert!(values.iter().any(|v| v.text == "$str = ?"));
        assert!(values.iter().any(|v| v.text == "$lit = ?"));
        assert!(!values.iter().any(|v| v.text.contains("$fake")));
        assert!(!values.iter().any(|v| v.text.contains("$ghost")));
    }

    #[test]
    fn test_unclosed_quote_like_operator_masks_to_end_of_line() {
        let source = "my $real = 1; my $str = qq($fake";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert_eq!(values.len(), 2);
        assert!(values.iter().any(|v| v.text == "$real = ?"));
        assert!(values.iter().any(|v| v.text == "$str = ?"));
        assert!(!values.iter().any(|v| v.text.contains("$fake")));
    }

    #[test]
    fn test_regex_and_translation_operators_are_ignored() {
        let source =
            "my $real = 1; $text =~ m/$ghost/; $text =~ s/$from/$to/; $text =~ tr/$src/$dst/;";
        let names = extract_variable_names(source, 1, 1);
        assert!(names.contains(&"$real".to_string()));
        assert!(!names.contains(&"$ghost".to_string()));
        assert!(!names.contains(&"$from".to_string()));
        assert!(!names.contains(&"$to".to_string()));
        assert!(!names.contains(&"$src".to_string()));
        assert!(!names.contains(&"$dst".to_string()));
    }

    #[test]
    fn test_quote_like_parser_requires_identifier_boundary() {
        let source = "my $keep = 1; my $weird = qqx$also_keep;";
        let names = extract_variable_names(source, 1, 1);
        assert!(names.contains(&"$keep".to_string()));
        assert!(names.contains(&"$weird".to_string()));
        assert!(names.contains(&"$also_keep".to_string()));
    }

    #[test]
    fn test_extract_variable_names_deduplicates_repeated_mentions() {
        let source = "my $foo = ${foo};\n";
        let names = extract_variable_names(source, 1, 1);

        assert_eq!(names, vec!["$foo".to_string()]);
    }

    #[test]
    fn test_extract_variable_names_supports_braced_variables() {
        let source = "my ${scalar_name} = 1;\nmy @{Pkg::items};\nmy %{App::Config::opts};";
        let names = extract_variable_names(source, 1, 3);
        assert!(names.contains(&"$scalar_name".to_string()));
        assert!(names.contains(&"@Pkg::items".to_string()));
        assert!(names.contains(&"%App::Config::opts".to_string()));
    }

    #[test]
    fn test_inline_values_support_braced_scalars_with_runtime_values() {
        let source = "my ${scalar_name} = 1;";
        let mut rv = HashMap::new();
        rv.insert("$scalar_name".to_string(), "7".to_string());
        let values = collect_inline_values_with_runtime(source, 1, 1, Some(&rv));
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$scalar_name = 7");
    }

    #[test]
    fn test_legacy_inline_values_support_braced_scalars() {
        let source = "my ${scalar_name} = 1;";
        let values = collect_inline_values(source, 1, 1);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$scalar_name = ?");
    }

    #[test]
    fn test_legacy_inline_values_deduplicate_per_line() {
        let source = "$x = $x + $x;";
        let values = collect_inline_values(source, 1, 1);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$x = ?");
    }

    #[test]
    fn test_legacy_inline_values_exclude_special_variables() {
        let source = "print $_; warn $!; my $ok = 1;";
        let values = collect_inline_values(source, 1, 1);
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].text, "$ok = ?");
    }

    #[test]
    fn test_inline_values_ignore_regex_match_operator_body() {
        let source = r"my $target = 1; if ($line =~ m/(\$regex_capture)/) { my $ok = 1; }";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert!(values.iter().any(|v| v.text == "$target = ?"));
        assert!(values.iter().any(|v| v.text == "$line = ?"));
        assert!(values.iter().any(|v| v.text == "$ok = ?"));
        assert!(!values.iter().any(|v| v.text.contains("$regex_capture")));
    }

    #[test]
    fn test_inline_values_ignore_substitution_and_transliteration_bodies() {
        let source = "my $line = 1; $line =~ s/$find/$replace/g; $line =~ tr/$from/$to/;";
        let values = collect_inline_values_with_runtime(source, 1, 1, None);
        assert!(values.iter().any(|v| v.text == "$line = ?"));
        assert!(!values.iter().any(|v| v.text.contains("$find")));
        assert!(!values.iter().any(|v| v.text.contains("$replace")));
        assert!(!values.iter().any(|v| v.text.contains("$from")));
        assert!(!values.iter().any(|v| v.text.contains("$to")));
    }
}