perl-lsp-document-highlight 0.12.2

SRP microcrate for Perl LSP document highlight (symbol occurrence highlighting)
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
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! Document Highlight Provider for Perl LSP
//!
//! Highlights all occurrences of a symbol when cursor is positioned on it.
//! Distinguishes between read and write access.

use perl_ast::{Node, NodeKind, SourceLocation};

/// Types of symbol highlights
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DocumentHighlightKind {
    /// Regular text occurrence (read access)
    Text = 1,
    /// Read access to a symbol
    Read = 2,
    /// Write access to a symbol
    Write = 3,
}

/// A highlighted range in the document
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocumentHighlight {
    /// Source location of the highlight
    pub location: SourceLocation,
    /// Type of highlight
    pub kind: DocumentHighlightKind,
}

/// Document Highlight Provider
pub struct DocumentHighlightProvider;

impl Default for DocumentHighlightProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl DocumentHighlightProvider {
    /// Create a new document highlight provider
    pub fn new() -> Self {
        Self
    }

    /// Find all highlights for the symbol at the given position in source code
    pub fn find_highlights(
        &self,
        ast: &Node,
        source: &str,
        byte_offset: usize,
    ) -> Vec<DocumentHighlight> {
        // Find the node at the cursor position
        let target_node = self.find_node_at_offset(ast, byte_offset);

        // Get the symbol name and kind
        let symbol_info = if let Some(ref node) = target_node {
            // Check if this variable is inside a subscript operation and normalize
            // the sigil accordingly (e.g., $array[0] -> @array, $hash{k} -> %hash)
            self.extract_symbol_info_with_context(node, source, ast, byte_offset)
        } else {
            // Fallback: check for synthetic positions (e.g., catch parameters)
            self.extract_symbol_at_offset(ast, source, byte_offset)
        };

        let symbol_info = match symbol_info {
            Some(info) => info,
            None => return Vec::new(),
        };

        // Find all occurrences of this symbol
        let mut highlights = Vec::new();
        self.collect_highlights(ast, source, &symbol_info, &mut highlights);

        // Deduplicate highlights by location, preferring Write over Read
        self.deduplicate_highlights(highlights)
    }

    /// Deduplicate highlights by location, preferring Write kind over Read
    fn deduplicate_highlights(&self, highlights: Vec<DocumentHighlight>) -> Vec<DocumentHighlight> {
        use std::collections::HashMap;

        // Group by location (start, end)
        let mut by_location: HashMap<(usize, usize), DocumentHighlight> = HashMap::new();

        for h in highlights {
            let key = (h.location.start, h.location.end);
            by_location
                .entry(key)
                .and_modify(|existing| {
                    // Prefer Write (3) over Read (2) over Text (1)
                    if (h.kind as u8) > (existing.kind as u8) {
                        *existing = h.clone();
                    }
                })
                .or_insert(h);
        }

        // Return sorted by position
        let mut result: Vec<_> = by_location.into_values().collect();
        result.sort_by_key(|h| h.location.start);
        result
    }

    /// Find the node at the given byte offset
    fn find_node_at_offset(&self, node: &Node, offset: usize) -> Option<Node> {
        // Check if offset is within this node
        if offset < node.location.start || offset >= node.location.end {
            return None;
        }

        // Check children first for more specific matches
        if let Some(children) = self.get_children(node) {
            for child in children {
                if let Some(found) = self.find_node_at_offset(child, offset) {
                    return Some(found);
                }
            }
        }

        // Check if this node is a relevant symbol
        if self.is_symbol_node(node) {
            return Some(node.clone());
        }

        None
    }

    /// Extract symbol info at an offset not covered by normal AST nodes
    /// (e.g., catch parameter variables stored as strings in Try nodes)
    fn extract_symbol_at_offset(
        &self,
        node: &Node,
        source: &str,
        offset: usize,
    ) -> Option<SymbolInfo> {
        if offset < node.location.start || offset >= node.location.end {
            return None;
        }

        // Check for Try catch parameters
        if let NodeKind::Try { catch_blocks, .. } = &node.kind {
            for (param, _) in catch_blocks {
                if let Some(var_str) = param {
                    // Find the catch parameter location in the source within this node
                    let node_source = source.get(node.location.start..node.location.end)?;
                    let relative_offset = offset - node.location.start;
                    // Search for the variable string near the offset
                    for (pos, _) in node_source.match_indices(var_str.as_str()) {
                        if pos <= relative_offset && relative_offset < pos + var_str.len() {
                            let first_char = var_str.chars().next()?;
                            if matches!(first_char, '$' | '@' | '%') {
                                return Some(SymbolInfo {
                                    name: var_str.get(1..)?.to_string(),
                                    sigil: Some(first_char.to_string()),
                                    is_method: false,
                                    is_function: false,
                                });
                            }
                        }
                    }
                }
            }
        }

        // Check for subroutine/method name at cursor position
        if let NodeKind::Subroutine { name: Some(sub_name), name_span: Some(span), .. } = &node.kind
        {
            if offset >= span.start && offset <= span.end {
                return Some(SymbolInfo {
                    name: sub_name.clone(),
                    sigil: None,
                    is_method: false,
                    is_function: true,
                });
            }
        }

        // Recurse into children
        if let Some(children) = self.get_children(node) {
            for child in children {
                if let Some(info) = self.extract_symbol_at_offset(child, source, offset) {
                    return Some(info);
                }
            }
        }

        None
    }

    /// Get children of a node
    fn get_children<'a>(&self, node: &'a Node) -> Option<Vec<&'a Node>> {
        match &node.kind {
            NodeKind::Program { statements } => Some(statements.iter().collect()),
            NodeKind::VariableDeclaration { variable, initializer, .. } => {
                let mut children = vec![variable.as_ref()];
                if let Some(init) = initializer {
                    children.push(init.as_ref());
                }
                Some(children)
            }
            NodeKind::VariableListDeclaration { variables, initializer, .. } => {
                let mut children: Vec<&Node> = variables.iter().collect();
                if let Some(init) = initializer {
                    children.push(init.as_ref());
                }
                Some(children)
            }
            NodeKind::Assignment { lhs, rhs, .. } => Some(vec![lhs.as_ref(), rhs.as_ref()]),
            NodeKind::Binary { left, right, .. } => Some(vec![left.as_ref(), right.as_ref()]),
            NodeKind::Unary { operand, .. } => Some(vec![operand.as_ref()]),
            NodeKind::MethodCall { object, args, .. } => {
                let mut children = vec![object.as_ref()];
                children.extend(args.iter().map(|a| a as &Node));
                Some(children)
            }
            NodeKind::FunctionCall { args, .. } => Some(args.iter().collect()),
            NodeKind::Block { statements } => Some(statements.iter().collect()),
            NodeKind::If { condition, then_branch, elsif_branches, else_branch } => {
                let mut children = vec![condition.as_ref(), then_branch.as_ref()];
                for (cond, branch) in elsif_branches {
                    children.push(cond.as_ref());
                    children.push(branch.as_ref());
                }
                if let Some(else_b) = else_branch {
                    children.push(else_b.as_ref());
                }
                Some(children)
            }
            NodeKind::For { init, condition, update, body, .. } => {
                let mut children = Vec::new();
                if let Some(i) = init {
                    children.push(i.as_ref());
                }
                if let Some(c) = condition {
                    children.push(c.as_ref());
                }
                if let Some(u) = update {
                    children.push(u.as_ref());
                }
                children.push(body.as_ref());
                Some(children)
            }
            NodeKind::Foreach { variable, list, body, continue_block } => {
                if let Some(cb) = continue_block {
                    Some(vec![variable.as_ref(), list.as_ref(), body.as_ref(), cb.as_ref()])
                } else {
                    Some(vec![variable.as_ref(), list.as_ref(), body.as_ref()])
                }
            }
            NodeKind::While { condition, body, .. } => {
                Some(vec![condition.as_ref(), body.as_ref()])
            }
            NodeKind::Subroutine { body, signature, .. } => {
                let mut children = Vec::new();
                if let Some(sig) = signature {
                    // Signature node may have zero-width span; expose parameters directly
                    if let NodeKind::Signature { parameters } = &sig.kind {
                        children.extend(parameters.iter());
                    } else {
                        children.push(sig.as_ref());
                    }
                }
                children.push(body.as_ref());
                Some(children)
            }
            NodeKind::Return { value } => value.as_ref().map(|v| vec![v.as_ref()]),
            NodeKind::ArrayLiteral { elements } => Some(elements.iter().collect()),
            NodeKind::HashLiteral { pairs } => {
                let mut children = Vec::new();
                for (k, v) in pairs {
                    children.push(k);
                    children.push(v);
                }
                Some(children)
            }
            NodeKind::Ternary { condition, then_expr, else_expr } => {
                Some(vec![condition.as_ref(), then_expr.as_ref(), else_expr.as_ref()])
            }
            NodeKind::VariableWithAttributes { variable, .. } => Some(vec![variable.as_ref()]),
            NodeKind::ExpressionStatement { expression } => Some(vec![expression.as_ref()]),
            // Statement modifiers (Issue #191)
            NodeKind::StatementModifier { statement, condition, .. } => {
                Some(vec![statement.as_ref(), condition.as_ref()])
            }
            // Regex operations - only expr is a child node, patterns are strings (Issue #191)
            NodeKind::Match { expr, .. }
            | NodeKind::Substitution { expr, .. }
            | NodeKind::Transliteration { expr, .. } => Some(vec![expr.as_ref()]),
            // Control flow (Issue #191)
            NodeKind::Given { expr, body } => Some(vec![expr.as_ref(), body.as_ref()]),
            NodeKind::When { condition, body } => Some(vec![condition.as_ref(), body.as_ref()]),
            NodeKind::Default { body } => Some(vec![body.as_ref()]),
            NodeKind::LabeledStatement { statement, .. } => Some(vec![statement.as_ref()]),
            // Code evaluation (Issue #191)
            NodeKind::Eval { block } | NodeKind::Do { block } => Some(vec![block.as_ref()]),
            // Error handling (Issue #191)
            NodeKind::Try { body, catch_blocks, finally_block } => {
                let mut children = vec![body.as_ref()];
                for (_, catch_body) in catch_blocks {
                    children.push(catch_body.as_ref());
                }
                if let Some(finally) = finally_block {
                    children.push(finally.as_ref());
                }
                Some(children)
            }
            // Method declarations (Issue #191)
            NodeKind::Method { body, signature, .. } => {
                let mut children = Vec::new();
                if let Some(sig) = signature {
                    // Signature node may have zero-width span; expose parameters directly
                    if let NodeKind::Signature { parameters } = &sig.kind {
                        children.extend(parameters.iter());
                    } else {
                        children.push(sig.as_ref());
                    }
                }
                children.push(body.as_ref());
                Some(children)
            }
            // Indirect calls (Issue #191)
            NodeKind::IndirectCall { object, args, .. } => {
                let mut children = vec![object.as_ref()];
                children.extend(args.iter());
                Some(children)
            }
            // Class declarations (Issue #191)
            NodeKind::Class { body, .. } => Some(vec![body.as_ref()]),
            // Signature and parameter types (Issue #191)
            NodeKind::Signature { parameters } => Some(parameters.iter().collect()),
            NodeKind::MandatoryParameter { variable } => Some(vec![variable.as_ref()]),
            NodeKind::OptionalParameter { variable, default_value } => {
                Some(vec![variable.as_ref(), default_value.as_ref()])
            }
            NodeKind::SlurpyParameter { variable } => Some(vec![variable.as_ref()]),
            NodeKind::NamedParameter { variable } => Some(vec![variable.as_ref()]),
            _ => None,
        }
    }

    /// Check if a node represents a symbol we can highlight
    fn is_symbol_node(&self, node: &Node) -> bool {
        matches!(
            node.kind,
            NodeKind::Variable { .. }
                | NodeKind::FunctionCall { .. }
                | NodeKind::MethodCall { .. }
                | NodeKind::Identifier { .. }
        )
    }

    /// Extract symbol information from a node
    fn extract_symbol_info(&self, node: &Node, source: &str) -> Option<SymbolInfo> {
        match &node.kind {
            NodeKind::Variable { sigil, name } => Some(SymbolInfo {
                name: name.clone(),
                sigil: Some(sigil.clone()),
                is_method: false,
                is_function: false,
            }),
            NodeKind::Identifier { name } => Some(SymbolInfo {
                name: name.clone(),
                sigil: None,
                is_method: false,
                is_function: false,
            }),
            NodeKind::FunctionCall { name, .. } => Some(SymbolInfo {
                name: name.clone(),
                sigil: None,
                is_method: false,
                is_function: true,
            }),
            NodeKind::MethodCall { method, .. } => Some(SymbolInfo {
                name: method.clone(),
                sigil: None,
                is_method: true,
                is_function: false,
            }),
            _ => {
                // Try to extract from source text
                let text = source.get(node.location.start..node.location.end)?;
                // Check for sigil prefix and extract safely
                let first = text.chars().next();
                match first {
                    Some(sigil @ ('$' | '@' | '%')) => Some(SymbolInfo {
                        name: text.get(1..).unwrap_or("").to_string(),
                        sigil: Some(sigil.to_string()),
                        is_method: false,
                        is_function: false,
                    }),
                    _ => None,
                }
            }
        }
    }

    /// Extract symbol info with AST context awareness.
    ///
    /// When the cursor is on a variable inside a subscript operation, this
    /// normalizes the sigil to the canonical container type:
    /// - `$array[0]` -> canonical sigil `@` (array access)
    /// - `$hash{key}` -> canonical sigil `%` (hash access)
    /// - `$#array` -> canonical sigil `@` (array last index)
    fn extract_symbol_info_with_context(
        &self,
        node: &Node,
        source: &str,
        ast: &Node,
        byte_offset: usize,
    ) -> Option<SymbolInfo> {
        let base_info = self.extract_symbol_info(node, source)?;

        // Only normalize when we have a $ sigil variable
        if base_info.sigil.as_deref() != Some("$") {
            return Some(base_info);
        }

        // Handle $#array -> normalize to @array
        if let Some(bare_name) = base_info.name.strip_prefix('#') {
            if !bare_name.is_empty() {
                return Some(SymbolInfo {
                    name: bare_name.to_string(),
                    sigil: Some("@".to_string()),
                    is_method: false,
                    is_function: false,
                });
            }
        }

        // Check if this $var is the left child of a Binary { op: "[]" | "{}" }
        if let Some(parent_op) = self.find_subscript_parent(ast, byte_offset) {
            match parent_op.as_str() {
                "[]" => {
                    return Some(SymbolInfo {
                        name: base_info.name,
                        sigil: Some("@".to_string()),
                        is_method: false,
                        is_function: false,
                    });
                }
                "{}" => {
                    return Some(SymbolInfo {
                        name: base_info.name,
                        sigil: Some("%".to_string()),
                        is_method: false,
                        is_function: false,
                    });
                }
                _ => {}
            }
        }

        Some(base_info)
    }

    /// Find the subscript operator of a Binary node that is the parent of the
    /// variable at the given offset, but only if the variable is the `left` child
    /// (the container being subscripted, not the index/key).
    fn find_subscript_parent(&self, node: &Node, offset: usize) -> Option<String> {
        if offset < node.location.start || offset >= node.location.end {
            return None;
        }

        // If this is a Binary subscript and the offset falls inside the left child
        if let NodeKind::Binary { op, left, .. } = &node.kind {
            if (op == "[]" || op == "{}")
                && offset >= left.location.start
                && offset < left.location.end
            {
                // Verify the left child is a Variable with $ sigil
                if let NodeKind::Variable { sigil, .. } = &left.kind {
                    if sigil == "$" {
                        return Some(op.clone());
                    }
                }
            }
        }

        // Recurse into children
        if let Some(children) = self.get_children(node) {
            for child in children {
                if let Some(op) = self.find_subscript_parent(child, offset) {
                    return Some(op);
                }
            }
        }

        None
    }

    /// Collect all highlights for a symbol
    fn collect_highlights(
        &self,
        node: &Node,
        source: &str,
        target: &SymbolInfo,
        highlights: &mut Vec<DocumentHighlight>,
    ) {
        self.collect_highlights_with_parent(node, source, target, highlights, None);
    }

    /// Collect all highlights for a symbol with parent context
    fn collect_highlights_with_parent(
        &self,
        node: &Node,
        source: &str,
        target: &SymbolInfo,
        highlights: &mut Vec<DocumentHighlight>,
        parent: Option<&Node>,
    ) {
        // Check if this node matches our symbol
        if self.node_matches_symbol(node, source, target) {
            let kind = self.determine_highlight_kind_with_parent(node, parent);
            // Use the full location including the sigil
            highlights.push(DocumentHighlight { location: node.location, kind });
        }

        // Cross-sigil matching for variables that refer to the same underlying
        // container but use a different sigil due to Perl's context rules:
        //   %hash  <-> $hash{key}   (hash element access)
        //   %hash  <-> @hash{@keys} (hash slice)
        //   @array <-> $array[idx]  (array element access)
        //   @array <-> $#array      (array last index)
        if let NodeKind::Variable { sigil, name } = &node.kind {
            if !self.node_matches_symbol(node, source, target) {
                if let Some(target_sigil) = &target.sigil {
                    let cross_match =
                        self.is_cross_sigil_match(sigil, name, target_sigil, &target.name, parent);
                    if cross_match {
                        let kind = self.determine_highlight_kind_with_parent(node, parent);
                        highlights.push(DocumentHighlight { location: node.location, kind });
                    }
                }
            }
        }

        // Emit highlight for subroutine definition name_span
        if let NodeKind::Subroutine { name: Some(sub_name), name_span: Some(span), .. } = &node.kind
        {
            if target.is_function && sub_name == &target.name {
                highlights.push(DocumentHighlight {
                    location: *span,
                    kind: DocumentHighlightKind::Write,
                });
            }
        }

        // Recursively check children with this node as parent
        if let Some(children) = self.get_children(node) {
            for child in children {
                self.collect_highlights_with_parent(child, source, target, highlights, Some(node));
            }
        }

        // Emit synthetic highlights for Try catch parameter variables
        if let NodeKind::Try { catch_blocks, body, .. } = &node.kind {
            if let Some(target_sigil) = &target.sigil {
                let expected = format!("{}{}", target_sigil, target.name);
                let mut search_from = body.location.end;
                for (param, catch_body) in catch_blocks {
                    if let Some(var_str) = param {
                        if var_str == &expected {
                            // Search between previous body/catch end and catch body start
                            let search_end = catch_body.location.start;
                            if search_from < search_end && search_end <= source.len() {
                                if let Some(search_area) = source.get(search_from..search_end) {
                                    if let Some(pos) = search_area.find(var_str.as_str()) {
                                        let var_start = search_from + pos;
                                        highlights.push(DocumentHighlight {
                                            location: SourceLocation {
                                                start: var_start,
                                                end: var_start + var_str.len(),
                                            },
                                            kind: DocumentHighlightKind::Write,
                                        });
                                    }
                                }
                            }
                        }
                    }
                    search_from = catch_body.location.end;
                }
            }
        }

        // Scan interpolated strings for variable references
        if let NodeKind::String { interpolated: true, .. } = &node.kind {
            if let Some(target_sigil) = &target.sigil {
                let expected = format!("{}{}", target_sigil, target.name);
                if let Some(node_text) = source.get(node.location.start..node.location.end) {
                    for (pos, _) in node_text.match_indices(expected.as_str()) {
                        // Avoid matching prefixes of longer variable names
                        let end_pos = pos + expected.len();
                        if end_pos < node_text.len() {
                            let next = node_text.as_bytes()[end_pos];
                            if next.is_ascii_alphanumeric() || next == b'_' {
                                continue;
                            }
                        }
                        let abs_start = node.location.start + pos;
                        // Skip if this is the whole node (already matched by normal traversal)
                        if abs_start == node.location.start
                            && node.location.end == abs_start + expected.len()
                        {
                            continue;
                        }
                        highlights.push(DocumentHighlight {
                            location: SourceLocation {
                                start: abs_start,
                                end: abs_start + expected.len(),
                            },
                            kind: DocumentHighlightKind::Read,
                        });
                    }
                }
            }
        }
    }

    /// Check whether a variable occurrence with `(sigil, name)` is a cross-sigil
    /// match for the target `(target_sigil, target_name)`.
    ///
    /// Cross-sigil relationships in Perl:
    /// - `$hash{key}` accesses `%hash` -> `$` + `{}` parent = `%`
    /// - `@hash{qw(a b)}` slices `%hash` -> `@` + `{}` parent = `%`
    /// - `$array[idx]` accesses `@array` -> `$` + `[]` parent = `@`
    /// - `$#array` is the last index of `@array` -> name `#foo` maps to `@foo`
    fn is_cross_sigil_match(
        &self,
        sigil: &str,
        name: &str,
        target_sigil: &str,
        target_name: &str,
        parent: Option<&Node>,
    ) -> bool {
        // Handle $#array <-> @array
        // $#array is Variable { sigil: "$", name: "#array" }
        if target_sigil == "@" && sigil == "$" {
            if let Some(bare) = name.strip_prefix('#') {
                if bare == target_name {
                    return true;
                }
            }
        }
        // Reverse: target is $#array (normalized to @array), node is @array
        // This case is handled by the normal sigil matching since we normalized
        // the target sigil in extract_symbol_info_with_context.

        // Same-name checks with subscript context
        if name != target_name {
            return false;
        }

        if let Some(parent_node) = parent {
            if let NodeKind::Binary { op, .. } = &parent_node.kind {
                // $hash{key} when target is %hash
                if target_sigil == "%" && sigil == "$" && op == "{}" {
                    return true;
                }
                // @hash{@keys} (hash slice) when target is %hash
                if target_sigil == "%" && sigil == "@" && op == "{}" {
                    return true;
                }
                // $array[idx] when target is @array
                if target_sigil == "@" && sigil == "$" && op == "[]" {
                    return true;
                }
                // @array[0,1] (array slice) when target is @array
                // This is already matched by normal sigil matching since both are @.
            }
        }

        false
    }

    /// Check if a node matches the target symbol
    fn node_matches_symbol(&self, node: &Node, source: &str, target: &SymbolInfo) -> bool {
        match &node.kind {
            NodeKind::Variable { sigil, name } => {
                if let Some(target_sigil) = &target.sigil {
                    sigil == target_sigil && name == &target.name
                } else {
                    false
                }
            }
            NodeKind::Identifier { name } => {
                !target.is_method && target.sigil.is_none() && name == &target.name
            }
            NodeKind::FunctionCall { name, .. } => target.is_function && name == &target.name,
            NodeKind::MethodCall { method, .. } => target.is_method && method == &target.name,
            _ => {
                // Check source text as fallback
                if let Some(target_sigil) = &target.sigil {
                    let expected = format!("{}{}", target_sigil, target.name);
                    source
                        .get(node.location.start..node.location.end)
                        .is_some_and(|text| text == expected)
                } else {
                    false
                }
            }
        }
    }

    /// Determine the kind of highlight based on context with parent information
    fn determine_highlight_kind_with_parent(
        &self,
        node: &Node,
        parent: Option<&Node>,
    ) -> DocumentHighlightKind {
        // Check if this variable is being written to (declaration or assignment)
        // Look for parent nodes that indicate write access
        match &node.kind {
            NodeKind::Variable { .. } => {
                // Check parent context to determine if this is a write or read
                if let Some(parent_node) = parent {
                    match &parent_node.kind {
                        // Variable declarations are writes
                        NodeKind::VariableDeclaration { variable, .. } => {
                            if std::ptr::eq(variable.as_ref(), node) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        // Variables in list declarations are writes
                        NodeKind::VariableListDeclaration { variables, .. } => {
                            if variables.iter().any(|v| std::ptr::eq(v, node)) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        // Left side of assignment is write (includes compound assignments)
                        NodeKind::Assignment { lhs, .. } => {
                            if std::ptr::eq(lhs.as_ref(), node) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        // Increment/decrement operations are writes
                        NodeKind::Unary { op, operand, .. } => {
                            if (op == "++" || op == "--") && std::ptr::eq(operand.as_ref(), node) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        // Foreach loop variable is a write (iterator binding)
                        NodeKind::Foreach { variable, .. } => {
                            if std::ptr::eq(variable.as_ref(), node) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        // Signature parameters are writes (value binding on call)
                        NodeKind::MandatoryParameter { variable }
                        | NodeKind::SlurpyParameter { variable }
                        | NodeKind::NamedParameter { variable } => {
                            if std::ptr::eq(variable.as_ref(), node) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        NodeKind::OptionalParameter { variable, .. } => {
                            if std::ptr::eq(variable.as_ref(), node) {
                                DocumentHighlightKind::Write
                            } else {
                                DocumentHighlightKind::Read
                            }
                        }
                        // Default to read for other contexts
                        _ => DocumentHighlightKind::Read,
                    }
                } else {
                    // If we don't have parent context, default to read
                    DocumentHighlightKind::Read
                }
            }
            _ => DocumentHighlightKind::Read,
        }
    }
}

// Internal SymbolInfo structure
struct SymbolInfo {
    name: String,
    sigil: Option<String>,
    is_method: bool,
    is_function: bool,
}