php-lsp 0.26.0

A PHP Language Server Protocol implementation
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
use std::collections::HashMap;
use std::ops::ControlFlow;
use std::sync::Arc;

use php_ast::visitor::{Visitor, walk_expr};
use php_ast::{
    ClassMemberKind, EnumMemberKind, ExprKind, NamespaceBody, Stmt, StmtKind, TraitAdaptationKind,
};
use tower_lsp_server::ls_types::{
    CallHierarchyIncomingCall, CallHierarchyItem, CallHierarchyOutgoingCall, Position, Range,
    SymbolKind, Uri,
};

use crate::document::ast::{ParsedDoc, SourceView};

/// Finds the declaration matching `name` and returns a `CallHierarchyItem`,
/// narrowing candidate declaring files via mir's persistent per-file mention
/// cache (`mention_candidates`) instead of walking every document's AST:
/// O(matches) docs fetched and scanned instead of O(workspace). A candidate
/// is a *possible* declarer (mention, not proof) — `find_declaration_item`
/// below still does the real AST-level check.
/// `get_doc` resolves a candidate file to its parsed doc (typically
/// `DocumentStore::get_doc_salsa` — a memo hit for indexed files).
///
/// The workspace-wide trait-alias fallback only runs when no candidate
/// actually declares `name` — i.e. it's not a class/function/method/
/// property/constant under that literal spelling anywhere. When `name` *is*
/// declared as something but that something has no `CallHierarchyItem`
/// (nothing currently classifies as one other than functions/methods/
/// class-likes), there is nothing left to try: a name that is already a
/// literal declaration is never also a trait-alias spelling.
pub fn prepare_call_hierarchy_indexed(
    name: &str,
    cursor: (&Uri, Position),
    wi: &crate::db::workspace_index::WorkspaceIndexData,
    get_doc: &dyn Fn(&Uri) -> Option<Arc<ParsedDoc>>,
    mention_candidates: &dyn Fn(&str) -> Vec<Uri>,
) -> Option<CallHierarchyItem> {
    // Same-named declarations in one file: prefer the one under the cursor.
    if let Some(doc) = get_doc(cursor.0)
        && let Some(item) = declaration_item_at(&doc, cursor.0, cursor.1, name)
    {
        return Some(item);
    }
    for uri in &mention_candidates(name) {
        let Some(doc) = get_doc(uri) else { continue };
        if let Some(item) = find_declaration_item(name, &doc.program().stmts, doc.view(), uri) {
            return Some(item);
        }
    }
    // `name` might be a `use Trait { method as name; }` alias, which never
    // appears as a literal declaration anywhere.
    let original = resolve_trait_alias_indexed(name, wi)?;
    if original == name {
        return None;
    }
    prepare_call_hierarchy_indexed(&original, cursor, wi, get_doc, mention_candidates)
}

/// Resolves `name` against every class's recorded trait-method aliases in
/// the workspace index (`FileIndex::extract` already collects these from
/// `use Trait { method as alias; }`). Fallback only — the common path
/// resolves via a literal declaration, which never matches an alias spelling.
fn resolve_trait_alias_indexed(
    name: &str,
    wi: &crate::db::workspace_index::WorkspaceIndexData,
) -> Option<String> {
    let mut resolved = None;
    wi.for_each_class(|_, cls| {
        if resolved.is_some() {
            return;
        }
        for alias in &cls.trait_method_aliases {
            if alias.alias.as_ref() == name {
                resolved = Some(alias.original.to_string());
                break;
            }
        }
    });
    resolved
}

/// Calls made by the body of `item`, resolved by mir. Callee declarations are
/// located through mir and mapped back to hierarchy items by the name token
/// at the declaration.
pub fn outgoing_calls_via_mir(
    docs: &crate::document::document_store::DocumentStore,
    item: &CallHierarchyItem,
) -> Result<Vec<CallHierarchyOutgoingCall>, crate::document::document_store::ContentModified> {
    let Some(doc) = docs.get_doc_salsa(&item.uri) else {
        return Ok(Vec::new());
    };
    let source = doc.source();
    let offset = crate::text::position_to_byte_offset(source, item.selection_range.start) as u32;
    let callees = crate::navigation::mir_definition::mir_outgoing_callees(docs, &item.uri, offset)?;
    // mir attributes anonymous-class method bodies to the enclosing function.
    let anon_ranges = anonymous_class_ranges(&doc);

    let mut result: Vec<CallHierarchyOutgoingCall> = Vec::new();
    let mut index: HashMap<(Uri, Position), usize> = HashMap::new();
    for (callee, call_range) in callees {
        if anon_ranges
            .iter()
            .any(|r| range_contains(*r, call_range.start))
        {
            continue;
        }
        let Some(target_doc) = docs.get_doc_salsa(&callee.uri) else {
            continue;
        };
        let target_source = target_doc.source();
        let start = crate::text::position_to_byte_offset(target_source, callee.range.start);
        let end = crate::text::position_to_byte_offset(target_source, callee.range.end);
        let Some(name) = target_source.get(start..end) else {
            continue;
        };
        let Some(callee_item) =
            declaration_item_at(&target_doc, &callee.uri, callee.range.start, name).or_else(|| {
                find_declaration_item(
                    name,
                    &target_doc.program().stmts,
                    target_doc.view(),
                    &callee.uri,
                )
            })
        else {
            continue;
        };
        let key = (callee_item.uri.clone(), callee_item.selection_range.start);
        if let Some(&idx) = index.get(&key) {
            // mir can report a call site more than once (e.g. loop update clauses).
            if !result[idx].from_ranges.contains(&call_range) {
                result[idx].from_ranges.push(call_range);
            }
        } else {
            index.insert(key, result.len());
            result.push(CallHierarchyOutgoingCall {
                to: callee_item,
                from_ranges: vec![call_range],
            });
        }
    }
    Ok(result)
}

fn anonymous_class_ranges(doc: &ParsedDoc) -> Vec<Range> {
    struct Finder<'a> {
        doc: &'a ParsedDoc,
        out: Vec<Range>,
    }
    impl<'arena, 'src> Visitor<'arena, 'src> for Finder<'_> {
        fn visit_expr(&mut self, expr: &php_ast::Expr<'arena, 'src>) -> ControlFlow<()> {
            if matches!(expr.kind, ExprKind::AnonymousClass(_)) {
                self.out.push(self.doc.view().range_of(expr.span));
            }
            walk_expr(self, expr)
        }
    }
    let mut finder = Finder {
        doc,
        out: Vec::new(),
    };
    for stmt in doc.program().stmts.iter() {
        let _ = finder.visit_stmt(stmt);
    }
    finder.out
}

/// Find all callers of `item` and return them grouped by enclosing function.
///
/// Call sites come from mir's reference posting lists (`meth:`/`methname:`/
/// `fn:` keys via `indexed_references`), resolved against the item's own
/// declaring document; only the documents that actually contain call sites
/// are parsed to find the enclosing caller.
pub fn incoming_calls_indexed(
    item: &CallHierarchyItem,
    store: &crate::document::document_store::DocumentStore,
    cancel_rev: Option<u64>,
) -> Vec<CallHierarchyIncomingCall> {
    let Some(item_doc) = store.get_doc_salsa(&item.uri) else {
        return Vec::new();
    };
    let imports = crate::navigation::references::collect_file_imports(&item_doc);
    let resolve = |name: &str| -> String {
        crate::navigation::moniker::resolve_fqn(&item_doc, name, &imports)
            .trim_start_matches('\\')
            .to_string()
    };
    // `detail` carries the declaring class-like's short name for methods; an
    // unresolvable owner becomes the empty class, which mir answers from its
    // name-keyed fallback postings.
    let symbol = if item.kind == SymbolKind::METHOD {
        let owner = item.detail.as_deref().map(&resolve).unwrap_or_default();
        mir_analyzer::Name::method(owner.as_str(), &item.name)
    } else {
        mir_analyzer::Name::function(resolve(&item.name))
    };

    let files = store.reference_candidate_files(&symbol);
    let mut call_sites: Vec<tower_lsp_server::ls_types::Location> = store
        .indexed_references(&symbol, &files, false, cancel_rev)
        .unwrap_or_default()
        .into_iter()
        .filter_map(crate::navigation::references::session_tuple_to_location)
        .collect();
    crate::navigation::references::dedup_ref_locations(&mut call_sites);

    let mut result: Vec<CallHierarchyIncomingCall> = Vec::new();
    // Track (caller_name, caller_uri) → index in `result` for O(1) dedup.
    let mut index: HashMap<(String, Uri), usize> = HashMap::new();
    // Parse only the documents call sites landed in, each at most once.
    let mut doc_cache: HashMap<Uri, Option<Arc<ParsedDoc>>> = HashMap::new();

    for loc in call_sites {
        let doc = doc_cache
            .entry(loc.uri.clone())
            .or_insert_with(|| store.get_doc_salsa(&loc.uri));
        let caller = doc.as_ref().and_then(|doc| {
            enclosing_function(doc.view(), &doc.program().stmts, loc.range.start, &loc.uri)
        });

        let key = if let Some(ref ci) = caller {
            (ci.name.clone(), ci.uri.clone())
        } else {
            ("<file scope>".to_string(), loc.uri.clone())
        };

        if let Some(&idx) = index.get(&key) {
            result[idx].from_ranges.push(loc.range);
        } else {
            let from = caller.unwrap_or_else(|| CallHierarchyItem {
                name: "<file scope>".to_string(),
                kind: SymbolKind::FILE,
                tags: None,
                detail: None,
                uri: loc.uri.clone(),
                range: loc.range,
                selection_range: loc.range,
                data: None,
            });
            let idx = result.len();
            index.insert(key, idx);
            result.push(CallHierarchyIncomingCall {
                from,
                from_ranges: vec![loc.range],
            });
        }
    }

    result
}

// === Internal helpers ===

/// The function or method named `name` whose name token contains `pos`.
fn declaration_item_at(
    doc: &ParsedDoc,
    uri: &Uri,
    pos: Position,
    name: &str,
) -> Option<CallHierarchyItem> {
    enclosing_function(doc.view(), &doc.program().stmts, pos, uri)
        .filter(|item| item.name == name && range_contains(item.selection_range, pos))
}

fn find_declaration_item(
    name: &str,
    stmts: &[Stmt<'_, '_>],
    sv: SourceView<'_>,
    uri: &Uri,
) -> Option<CallHierarchyItem> {
    for stmt in stmts {
        match &stmt.kind {
            StmtKind::Function(f) if f.name == name => {
                let range = sv.range_of(stmt.span);
                let sel = sv.name_range_after_attrs(&f.name.to_string(), &f.attributes, stmt.span);
                return Some(CallHierarchyItem {
                    name: name.to_string(),
                    kind: SymbolKind::FUNCTION,
                    tags: None,
                    detail: None,
                    uri: uri.clone(),
                    range,
                    selection_range: sel,
                    data: None,
                });
            }
            // Class-name-itself match: `new X()` extracts the bare class name as
            // its "callee", and a class declaration named `X` is a valid
            // candidate here — without this arm, that lookup always missed
            // and fell through to the workspace-wide trait-alias scan for
            // every `new` expression in the codebase.
            StmtKind::Class(c) if c.name.is_some_and(|n| n == name) => {
                let range = sv.range_of(stmt.span);
                let sel = sv.name_range_after_attrs(name, &c.attributes, stmt.span);
                return Some(CallHierarchyItem {
                    name: name.to_string(),
                    kind: SymbolKind::CLASS,
                    tags: None,
                    detail: None,
                    uri: uri.clone(),
                    range,
                    selection_range: sel,
                    data: None,
                });
            }
            StmtKind::Class(c) => {
                for member in c.body.members.iter() {
                    if let ClassMemberKind::Method(m) = &member.kind
                        && m.name == name
                    {
                        let range = sv.range_of(member.span);
                        let sel = sv.name_range_after_attrs(
                            &m.name.to_string(),
                            &m.attributes,
                            member.span,
                        );
                        return Some(CallHierarchyItem {
                            name: name.to_string(),
                            kind: SymbolKind::METHOD,
                            tags: None,
                            detail: c.name.map(|n| n.to_string()),
                            uri: uri.clone(),
                            range,
                            selection_range: sel,
                            data: None,
                        });
                    }
                }
            }
            StmtKind::Interface(i) if i.name == name => {
                let range = sv.range_of(stmt.span);
                let sel = sv.name_range_after_attrs(name, &i.attributes, stmt.span);
                return Some(CallHierarchyItem {
                    name: name.to_string(),
                    kind: SymbolKind::INTERFACE,
                    tags: None,
                    detail: None,
                    uri: uri.clone(),
                    range,
                    selection_range: sel,
                    data: None,
                });
            }
            StmtKind::Interface(i) => {
                for member in i.body.members.iter() {
                    if let ClassMemberKind::Method(m) = &member.kind
                        && m.name == name
                    {
                        let range = sv.range_of(member.span);
                        let sel = sv.name_range_after_attrs(
                            &m.name.to_string(),
                            &m.attributes,
                            member.span,
                        );
                        return Some(CallHierarchyItem {
                            name: name.to_string(),
                            kind: SymbolKind::METHOD,
                            tags: None,
                            detail: Some(i.name.to_string()),
                            uri: uri.clone(),
                            range,
                            selection_range: sel,
                            data: None,
                        });
                    }
                }
            }
            StmtKind::Trait(t) if t.name == name => {
                let range = sv.range_of(stmt.span);
                let sel = sv.name_range_after_attrs(name, &t.attributes, stmt.span);
                return Some(CallHierarchyItem {
                    name: name.to_string(),
                    kind: SymbolKind::CLASS,
                    tags: None,
                    detail: None,
                    uri: uri.clone(),
                    range,
                    selection_range: sel,
                    data: None,
                });
            }
            StmtKind::Trait(t) => {
                for member in t.body.members.iter() {
                    if let ClassMemberKind::Method(m) = &member.kind
                        && m.name == name
                    {
                        let range = sv.range_of(member.span);
                        let sel = sv.name_range_after_attrs(
                            &m.name.to_string(),
                            &m.attributes,
                            member.span,
                        );
                        return Some(CallHierarchyItem {
                            name: name.to_string(),
                            kind: SymbolKind::METHOD,
                            tags: None,
                            detail: Some(t.name.to_string()),
                            uri: uri.clone(),
                            range,
                            selection_range: sel,
                            data: None,
                        });
                    }
                }
            }
            StmtKind::Enum(e) if e.name == name => {
                let range = sv.range_of(stmt.span);
                let sel = sv.name_range_after_attrs(name, &e.attributes, stmt.span);
                return Some(CallHierarchyItem {
                    name: name.to_string(),
                    kind: SymbolKind::ENUM,
                    tags: None,
                    detail: None,
                    uri: uri.clone(),
                    range,
                    selection_range: sel,
                    data: None,
                });
            }
            StmtKind::Enum(e) => {
                for member in e.body.members.iter() {
                    if let EnumMemberKind::Method(m) = &member.kind
                        && m.name == name
                    {
                        let range = sv.range_of(member.span);
                        let sel = sv.name_range_after_attrs(
                            &m.name.to_string(),
                            &m.attributes,
                            member.span,
                        );
                        return Some(CallHierarchyItem {
                            name: name.to_string(),
                            kind: SymbolKind::METHOD,
                            tags: None,
                            detail: Some(e.name.to_string()),
                            uri: uri.clone(),
                            range,
                            selection_range: sel,
                            data: None,
                        });
                    }
                }
            }
            StmtKind::Namespace(ns) => {
                if let NamespaceBody::Braced(inner) = &ns.body
                    && let Some(item) = find_declaration_item(name, &inner.stmts, sv, uri)
                {
                    return Some(item);
                }
            }
            _ => {}
        }
    }
    // `name` may be a `use Trait { method as name; }` alias rather than a
    // literal declaration anywhere — retry under the trait method's real name.
    if let Some(original) = resolve_trait_alias(name, stmts)
        && original != name
    {
        return find_declaration_item(&original, stmts, sv, uri);
    }
    None
}

/// If `name` is introduced by `use Trait { method as name; }` on some class
/// in `stmts`, returns the trait method's real declared name. Without this,
/// a call site written under the alias (the only spelling that exists in
/// source) can never resolve — no AST node is literally named the alias.
fn resolve_trait_alias(name: &str, stmts: &[Stmt<'_, '_>]) -> Option<String> {
    for stmt in stmts {
        match &stmt.kind {
            StmtKind::Class(c) => {
                for member in c.body.members.iter() {
                    if let ClassMemberKind::TraitUse(tu) = &member.kind {
                        for adaptation in tu.adaptations.iter() {
                            if let TraitAdaptationKind::Alias {
                                method,
                                new_name: Some(new_name),
                                ..
                            } = &adaptation.kind
                                && new_name.to_string_repr() == name
                            {
                                return Some(method.to_string_repr().into_owned());
                            }
                        }
                    }
                }
            }
            StmtKind::Namespace(ns) => {
                if let NamespaceBody::Braced(inner) = &ns.body
                    && let Some(found) = resolve_trait_alias(name, &inner.stmts)
                {
                    return Some(found);
                }
            }
            _ => {}
        }
    }
    None
}

fn enclosing_function(
    sv: SourceView<'_>,
    stmts: &[Stmt<'_, '_>],
    pos: Position,
    uri: &Uri,
) -> Option<CallHierarchyItem> {
    for stmt in stmts {
        if let Some(item) = enclosing_in_stmt(sv, stmt, pos, uri) {
            return Some(item);
        }
    }
    None
}

fn enclosing_in_stmt(
    sv: SourceView<'_>,
    stmt: &Stmt<'_, '_>,
    pos: Position,
    uri: &Uri,
) -> Option<CallHierarchyItem> {
    let range = sv.range_of(stmt.span);
    if !range_contains(range, pos) {
        return None;
    }
    match &stmt.kind {
        StmtKind::Function(f) => {
            let sel = sv.name_range_after_attrs(&f.name.to_string(), &f.attributes, stmt.span);
            Some(CallHierarchyItem {
                name: f.name.to_string(),
                kind: SymbolKind::FUNCTION,
                tags: None,
                detail: None,
                uri: uri.clone(),
                range,
                selection_range: sel,
                data: None,
            })
        }
        StmtKind::Class(c) => {
            for member in c.body.members.iter() {
                let m_range = sv.range_of(member.span);
                if range_contains(m_range, pos)
                    && let ClassMemberKind::Method(m) = &member.kind
                {
                    let sel =
                        sv.name_range_after_attrs(&m.name.to_string(), &m.attributes, member.span);
                    return Some(CallHierarchyItem {
                        name: m.name.to_string(),
                        kind: SymbolKind::METHOD,
                        tags: None,
                        detail: c.name.map(|n| n.to_string()),
                        uri: uri.clone(),
                        range: m_range,
                        selection_range: sel,
                        data: None,
                    });
                }
            }
            None
        }
        StmtKind::Interface(i) => {
            for member in i.body.members.iter() {
                let m_range = sv.range_of(member.span);
                if range_contains(m_range, pos)
                    && let ClassMemberKind::Method(m) = &member.kind
                {
                    let sel =
                        sv.name_range_after_attrs(&m.name.to_string(), &m.attributes, member.span);
                    return Some(CallHierarchyItem {
                        name: m.name.to_string(),
                        kind: SymbolKind::METHOD,
                        tags: None,
                        detail: Some(i.name.to_string()),
                        uri: uri.clone(),
                        range: m_range,
                        selection_range: sel,
                        data: None,
                    });
                }
            }
            None
        }
        StmtKind::Trait(t) => {
            for member in t.body.members.iter() {
                let m_range = sv.range_of(member.span);
                if range_contains(m_range, pos)
                    && let ClassMemberKind::Method(m) = &member.kind
                {
                    let sel =
                        sv.name_range_after_attrs(&m.name.to_string(), &m.attributes, member.span);
                    return Some(CallHierarchyItem {
                        name: m.name.to_string(),
                        kind: SymbolKind::METHOD,
                        tags: None,
                        detail: Some(t.name.to_string()),
                        uri: uri.clone(),
                        range: m_range,
                        selection_range: sel,
                        data: None,
                    });
                }
            }
            None
        }
        StmtKind::Enum(e) => {
            for member in e.body.members.iter() {
                let m_range = sv.range_of(member.span);
                if range_contains(m_range, pos)
                    && let EnumMemberKind::Method(m) = &member.kind
                {
                    let sel =
                        sv.name_range_after_attrs(&m.name.to_string(), &m.attributes, member.span);
                    return Some(CallHierarchyItem {
                        name: m.name.to_string(),
                        kind: SymbolKind::METHOD,
                        tags: None,
                        detail: Some(e.name.to_string()),
                        uri: uri.clone(),
                        range: m_range,
                        selection_range: sel,
                        data: None,
                    });
                }
            }
            None
        }
        StmtKind::Namespace(ns) => {
            if let NamespaceBody::Braced(inner) = &ns.body {
                return enclosing_function(sv, &inner.stmts, pos, uri);
            }
            None
        }
        _ => None,
    }
}

fn range_contains(range: Range, pos: Position) -> bool {
    if pos.line < range.start.line || pos.line > range.end.line {
        return false;
    }
    if pos.line == range.start.line && pos.character < range.start.character {
        return false;
    }
    if pos.line == range.end.line && pos.character >= range.end.character {
        return false;
    }
    true
}

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

    // ── range_contains boundary regression tests ─────────────────────────────
    //
    // These unit tests cover internal implementation details (helper function
    // boundary semantics). They are kept as unit tests rather than migrated to
    // E2E because they test infrastructure mechanics, not user-facing LSP
    // features. Protocol-wired E2E tests for call hierarchy features are in
    // tests/navigation/feature_hierarchy.rs and provide comprehensive coverage
    // of the public API.

    #[test]
    fn range_contains_excludes_exact_end_position() {
        // LSP ranges are half-open [start, end).  A position exactly at
        // range.end is OUTSIDE the range.  The old code used `>` instead of
        // `>=`, which incorrectly included the end position.
        let range = Range {
            start: Position {
                line: 1,
                character: 0,
            },
            end: Position {
                line: 3,
                character: 5,
            },
        };
        // One past the last character on the end line — clearly outside.
        assert!(
            !range_contains(
                range,
                Position {
                    line: 3,
                    character: 6
                }
            ),
            "position after end must be outside"
        );
        // Exactly at end — outside per LSP half-open semantics.
        assert!(
            !range_contains(
                range,
                Position {
                    line: 3,
                    character: 5
                }
            ),
            "position exactly at range.end must be outside (half-open range)"
        );
        // One before end — inside.
        assert!(
            range_contains(
                range,
                Position {
                    line: 3,
                    character: 4
                }
            ),
            "position just before end must be inside"
        );
        // Start of range — inside.
        assert!(
            range_contains(
                range,
                Position {
                    line: 1,
                    character: 0
                }
            ),
            "start position must be inside"
        );
    }
}