perl-workspace 0.13.2

Workspace file discovery, indexing, and observability for Perl
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
//! Typed reference index for cross-file reference lookups.
//!
//! Maintains two indexes over [`ReferenceEdge`] entries:
//!
//! - `references_by_name` — keyed by the bare or qualified symbol key, for
//!   name-based lookups (e.g. find-references by symbol name).
//! - `references_by_entity` — keyed by [`EntityId`], for entity-based lookups
//!   (e.g. find all references to a specific declaration).
//!
//! Both indexes support incremental add/remove via [`ReferenceIndex::add_file`]
//! and [`ReferenceIndex::remove_file`], keyed by the file's source URI.

use perl_semantic_facts::{EdgeKind, EntityId, FileId, OccurrenceKind, ReferenceEdge};
use std::collections::HashMap;

use crate::workspace::workspace_index::FileFactShard;

/// Cross-file reference index backed by two `HashMap`s.
///
/// Populated from [`FileFactShard`] occurrences and edges during workspace
/// indexing. Supports incremental updates: call [`remove_file`](Self::remove_file)
/// to purge stale entries, then [`add_file`](Self::add_file) to insert fresh ones.
#[derive(Debug, Default)]
pub struct ReferenceIndex {
    /// Symbol-key → reference edges. The key is the bare or qualified name
    /// carried on each [`ReferenceEdge::symbol_key`].
    references_by_name: HashMap<String, Vec<ReferenceEdge>>,

    /// Entity → reference edges. One entry per target candidate in each
    /// [`ReferenceEdge::target_candidates`].
    references_by_entity: HashMap<EntityId, Vec<ReferenceEdge>>,

    /// Tracks which file URIs have been indexed so that [`remove_file`](Self::remove_file)
    /// can efficiently purge stale entries.
    indexed_files: HashMap<String, FileId>,
}

impl ReferenceIndex {
    /// Create an empty reference index.
    pub fn new() -> Self {
        Self::default()
    }

    /// Index all reference-like occurrences from a [`FileFactShard`].
    ///
    /// For each non-definition occurrence in the shard, a [`ReferenceEdge`] is
    /// synthesized and inserted into both lookup maps. Edge facts with kind
    /// [`EdgeKind::References`] are consulted to populate `target_candidates`.
    pub fn add_file(&mut self, shard: &FileFactShard) {
        // Record the file so remove_file can match by URI later.
        self.indexed_files.insert(shard.source_uri.clone(), shard.file_id);

        // Build a quick lookup: occurrence_id → list of target entity IDs from
        // Reference edges in the shard.
        let mut edge_targets: HashMap<u64, Vec<EntityId>> = HashMap::new();
        for edge in &shard.edges {
            if edge.kind == EdgeKind::References {
                if let Some(occ_id) = edge.via_occurrence_id {
                    edge_targets.entry(occ_id.0).or_default().push(edge.to_entity_id);
                }
            }
        }

        for occ in &shard.occurrences {
            // Skip definition occurrences — they are not references.
            if occ.kind == OccurrenceKind::Definition {
                continue;
            }

            // Build the target_candidates list from edges, falling back to the
            // occurrence's own entity_id when no edge exists.
            let target_candidates = match edge_targets.get(&occ.id.0) {
                Some(targets) => targets.clone(),
                None => occ.entity_id.into_iter().collect(),
            };

            // Derive the symbol_key from the entity canonical name when
            // available. For occurrences without a resolved entity we use the
            // anchor id as a placeholder key — callers that need name-based
            // lookup will not match these, but entity-based lookup still works.
            let symbol_key = self.derive_symbol_key(shard, occ);

            let ref_edge = ReferenceEdge::new(
                occ.id,
                occ.anchor_id,
                shard.file_id,
                symbol_key.clone(),
                target_candidates.clone(),
                occ.kind,
                occ.provenance,
                occ.confidence,
            );

            // Insert into name index.
            self.references_by_name.entry(symbol_key).or_default().push(ref_edge.clone());

            // Insert into entity index — one entry per target candidate.
            for entity_id in &target_candidates {
                self.references_by_entity.entry(*entity_id).or_default().push(ref_edge.clone());
            }
        }
    }

    /// Remove all reference entries that originated from the given file URI.
    ///
    /// This is the "remove" half of incremental re-indexing: call this before
    /// [`add_file`](Self::add_file) with the updated shard.
    pub fn remove_file(&mut self, source_uri: &str) {
        let file_id = match self.indexed_files.remove(source_uri) {
            Some(id) => id,
            None => return,
        };

        // Retain only entries from other files.
        for refs in self.references_by_name.values_mut() {
            refs.retain(|r| r.file_id != file_id);
        }
        // Remove empty buckets to keep the map tidy.
        self.references_by_name.retain(|_, v| !v.is_empty());

        for refs in self.references_by_entity.values_mut() {
            refs.retain(|r| r.file_id != file_id);
        }
        self.references_by_entity.retain(|_, v| !v.is_empty());
    }

    /// Look up all reference edges for a given symbol key (bare or qualified name).
    pub fn get_by_name(&self, symbol_key: &str) -> &[ReferenceEdge] {
        self.references_by_name.get(symbol_key).map(Vec::as_slice).unwrap_or_default()
    }

    /// Look up all reference edges targeting a given entity.
    pub fn get_by_entity(&self, entity_id: EntityId) -> &[ReferenceEdge] {
        self.references_by_entity.get(&entity_id).map(Vec::as_slice).unwrap_or_default()
    }

    /// Return the number of distinct symbol keys in the name index.
    pub fn name_count(&self) -> usize {
        self.references_by_name.len()
    }

    /// Return the number of distinct entities in the entity index.
    pub fn entity_count(&self) -> usize {
        self.references_by_entity.len()
    }

    // ── Private helpers ──

    /// Derive a symbol key for an occurrence.
    ///
    /// When the occurrence has a resolved entity_id, we look up the entity's
    /// canonical name from the shard. Otherwise we fall back to a synthetic
    /// key based on the anchor id.
    fn derive_symbol_key(
        &self,
        shard: &FileFactShard,
        occ: &perl_semantic_facts::OccurrenceFact,
    ) -> String {
        if let Some(entity_id) = occ.entity_id {
            // Try to find the entity in the same shard for its canonical name.
            if let Some(entity) = shard.entities.iter().find(|e| e.id == entity_id) {
                return entity.canonical_name.clone();
            }
        }
        // Fallback: use anchor id as a synthetic key.
        format!("__unresolved_anchor_{}", occ.anchor_id.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use perl_semantic_facts::{
        AnchorFact, AnchorId, Confidence, EdgeFact, EdgeId, EntityFact, EntityKind, OccurrenceFact,
        OccurrenceId, Provenance, ScopeId,
    };

    /// Build a minimal `FileFactShard` with one entity, one reference occurrence,
    /// and one `References` edge linking them.
    fn sample_shard() -> FileFactShard {
        let file_id = FileId(1);
        let entity_id = EntityId(100);
        let anchor_def = AnchorId(10);
        let anchor_ref = AnchorId(20);
        let occ_id = OccurrenceId(400);

        FileFactShard {
            source_uri: "file:///lib/Foo.pm".to_string(),
            file_id,
            content_hash: 999,
            anchors_hash: None,
            entities_hash: None,
            occurrences_hash: None,
            edges_hash: None,
            anchors: vec![
                AnchorFact {
                    id: anchor_def,
                    file_id,
                    span_start_byte: 0,
                    span_end_byte: 10,
                    scope_id: None,
                    provenance: Provenance::ExactAst,
                    confidence: Confidence::High,
                },
                AnchorFact {
                    id: anchor_ref,
                    file_id,
                    span_start_byte: 50,
                    span_end_byte: 55,
                    scope_id: None,
                    provenance: Provenance::ExactAst,
                    confidence: Confidence::High,
                },
            ],
            entities: vec![EntityFact {
                id: entity_id,
                kind: EntityKind::Subroutine,
                canonical_name: "Foo::bar".to_string(),
                anchor_id: Some(anchor_def),
                scope_id: Some(ScopeId(1)),
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            occurrences: vec![OccurrenceFact {
                id: occ_id,
                kind: OccurrenceKind::Call,
                entity_id: Some(entity_id),
                anchor_id: anchor_ref,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            edges: vec![EdgeFact {
                id: EdgeId(500),
                kind: EdgeKind::References,
                from_entity_id: EntityId(0), // caller entity (not relevant here)
                to_entity_id: entity_id,
                via_occurrence_id: Some(occ_id),
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
        }
    }

    #[test]
    fn add_file_populates_name_index() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        let refs = index.get_by_name("Foo::bar");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].kind, OccurrenceKind::Call);
        assert_eq!(refs[0].symbol_key, "Foo::bar");
        Ok(())
    }

    #[test]
    fn add_file_populates_entity_index() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        let refs = index.get_by_entity(EntityId(100));
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].occurrence_id, OccurrenceId(400));
        Ok(())
    }

    #[test]
    fn remove_file_clears_entries() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        assert_eq!(index.name_count(), 1);
        assert_eq!(index.entity_count(), 1);

        index.remove_file("file:///lib/Foo.pm");

        assert_eq!(index.name_count(), 0);
        assert_eq!(index.entity_count(), 0);
        assert!(index.get_by_name("Foo::bar").is_empty());
        assert!(index.get_by_entity(EntityId(100)).is_empty());
        Ok(())
    }

    #[test]
    fn remove_file_is_idempotent() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        index.remove_file("file:///lib/Foo.pm");
        // Second remove should be a no-op.
        index.remove_file("file:///lib/Foo.pm");

        assert_eq!(index.name_count(), 0);
        assert_eq!(index.entity_count(), 0);
        Ok(())
    }

    #[test]
    fn remove_unknown_file_is_noop() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        index.remove_file("file:///nonexistent.pm");

        // Original entries should still be present.
        assert_eq!(index.name_count(), 1);
        assert_eq!(index.entity_count(), 1);
        Ok(())
    }

    #[test]
    fn definition_occurrences_are_excluded() -> Result<(), Box<dyn std::error::Error>> {
        let file_id = FileId(2);
        let entity_id = EntityId(200);
        let anchor_id = AnchorId(30);

        let shard = FileFactShard {
            source_uri: "file:///lib/Defs.pm".to_string(),
            file_id,
            content_hash: 111,
            anchors_hash: None,
            entities_hash: None,
            occurrences_hash: None,
            edges_hash: None,
            anchors: vec![AnchorFact {
                id: anchor_id,
                file_id,
                span_start_byte: 0,
                span_end_byte: 5,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            entities: vec![EntityFact {
                id: entity_id,
                kind: EntityKind::Subroutine,
                canonical_name: "Defs::init".to_string(),
                anchor_id: Some(anchor_id),
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            occurrences: vec![OccurrenceFact {
                id: OccurrenceId(600),
                kind: OccurrenceKind::Definition,
                entity_id: Some(entity_id),
                anchor_id,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            edges: vec![],
        };

        let mut index = ReferenceIndex::new();
        index.add_file(&shard);

        // Definition occurrences should not appear in the reference index.
        assert_eq!(index.name_count(), 0);
        assert_eq!(index.entity_count(), 0);
        Ok(())
    }

    #[test]
    fn multiple_files_coexist() -> Result<(), Box<dyn std::error::Error>> {
        let shard_a = sample_shard();

        let file_id_b = FileId(2);
        let entity_id = EntityId(100); // same target entity
        let occ_id_b = OccurrenceId(700);
        let anchor_b = AnchorId(40);

        let shard_b = FileFactShard {
            source_uri: "file:///lib/Bar.pm".to_string(),
            file_id: file_id_b,
            content_hash: 888,
            anchors_hash: None,
            entities_hash: None,
            occurrences_hash: None,
            edges_hash: None,
            anchors: vec![AnchorFact {
                id: anchor_b,
                file_id: file_id_b,
                span_start_byte: 10,
                span_end_byte: 18,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            entities: vec![EntityFact {
                id: entity_id,
                kind: EntityKind::Subroutine,
                canonical_name: "Foo::bar".to_string(),
                anchor_id: None,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            occurrences: vec![OccurrenceFact {
                id: occ_id_b,
                kind: OccurrenceKind::Call,
                entity_id: Some(entity_id),
                anchor_id: anchor_b,
                scope_id: None,
                provenance: Provenance::NameHeuristic,
                confidence: Confidence::Medium,
            }],
            edges: vec![],
        };

        let mut index = ReferenceIndex::new();
        index.add_file(&shard_a);
        index.add_file(&shard_b);

        // Both files contribute references to the same name.
        assert_eq!(index.get_by_name("Foo::bar").len(), 2);
        // Both files contribute references to the same entity.
        assert_eq!(index.get_by_entity(entity_id).len(), 2);

        // Remove one file — only its entries should disappear.
        index.remove_file("file:///lib/Foo.pm");
        assert_eq!(index.get_by_name("Foo::bar").len(), 1);
        assert_eq!(index.get_by_entity(entity_id).len(), 1);
        assert_eq!(index.get_by_name("Foo::bar")[0].file_id, file_id_b);

        Ok(())
    }

    #[test]
    fn incremental_reindex_replaces_entries() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        assert_eq!(index.get_by_name("Foo::bar").len(), 1);

        // Simulate re-indexing: remove old, add updated shard.
        index.remove_file("file:///lib/Foo.pm");

        // Updated shard with a different occurrence.
        let file_id = FileId(1);
        let entity_id = EntityId(100);
        let updated_shard = FileFactShard {
            source_uri: "file:///lib/Foo.pm".to_string(),
            file_id,
            content_hash: 1000,
            anchors_hash: None,
            entities_hash: None,
            occurrences_hash: None,
            edges_hash: None,
            anchors: vec![AnchorFact {
                id: AnchorId(50),
                file_id,
                span_start_byte: 60,
                span_end_byte: 68,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            entities: vec![EntityFact {
                id: entity_id,
                kind: EntityKind::Subroutine,
                canonical_name: "Foo::bar".to_string(),
                anchor_id: None,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            occurrences: vec![OccurrenceFact {
                id: OccurrenceId(800),
                kind: OccurrenceKind::Read,
                entity_id: Some(entity_id),
                anchor_id: AnchorId(50),
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            edges: vec![],
        };

        index.add_file(&updated_shard);

        let refs = index.get_by_name("Foo::bar");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].occurrence_id, OccurrenceId(800));
        assert_eq!(refs[0].kind, OccurrenceKind::Read);
        Ok(())
    }

    #[test]
    fn unresolved_occurrence_uses_fallback_key() -> Result<(), Box<dyn std::error::Error>> {
        let file_id = FileId(3);
        let anchor_id = AnchorId(60);

        let shard = FileFactShard {
            source_uri: "file:///lib/Unresolved.pm".to_string(),
            file_id,
            content_hash: 222,
            anchors_hash: None,
            entities_hash: None,
            occurrences_hash: None,
            edges_hash: None,
            anchors: vec![AnchorFact {
                id: anchor_id,
                file_id,
                span_start_byte: 0,
                span_end_byte: 8,
                scope_id: None,
                provenance: Provenance::NameHeuristic,
                confidence: Confidence::Low,
            }],
            entities: vec![],
            occurrences: vec![OccurrenceFact {
                id: OccurrenceId(900),
                kind: OccurrenceKind::Call,
                entity_id: None,
                anchor_id,
                scope_id: None,
                provenance: Provenance::NameHeuristic,
                confidence: Confidence::Low,
            }],
            edges: vec![],
        };

        let mut index = ReferenceIndex::new();
        index.add_file(&shard);

        // The fallback key should be based on the anchor id.
        let fallback_key = "__unresolved_anchor_60";
        let refs = index.get_by_name(fallback_key);
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].confidence, Confidence::Low);

        // No entity-based entries since there are no target candidates.
        assert_eq!(index.entity_count(), 0);
        Ok(())
    }

    #[test]
    fn edge_targets_populate_candidates() -> Result<(), Box<dyn std::error::Error>> {
        let mut index = ReferenceIndex::new();
        index.add_file(&sample_shard());

        let refs = index.get_by_entity(EntityId(100));
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].target_candidates, vec![EntityId(100)]);
        Ok(())
    }

    #[test]
    fn multiple_edge_targets_produce_multiple_entity_entries()
    -> Result<(), Box<dyn std::error::Error>> {
        let file_id = FileId(4);
        let occ_id = OccurrenceId(1000);
        let anchor_id = AnchorId(70);
        let entity_a = EntityId(300);
        let entity_b = EntityId(301);

        let shard = FileFactShard {
            source_uri: "file:///lib/Ambiguous.pm".to_string(),
            file_id,
            content_hash: 333,
            anchors_hash: None,
            entities_hash: None,
            occurrences_hash: None,
            edges_hash: None,
            anchors: vec![AnchorFact {
                id: anchor_id,
                file_id,
                span_start_byte: 0,
                span_end_byte: 5,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::High,
            }],
            entities: vec![
                EntityFact {
                    id: entity_a,
                    kind: EntityKind::Subroutine,
                    canonical_name: "ambig_func".to_string(),
                    anchor_id: None,
                    scope_id: None,
                    provenance: Provenance::ExactAst,
                    confidence: Confidence::High,
                },
                EntityFact {
                    id: entity_b,
                    kind: EntityKind::Subroutine,
                    canonical_name: "ambig_func".to_string(),
                    anchor_id: None,
                    scope_id: None,
                    provenance: Provenance::NameHeuristic,
                    confidence: Confidence::Low,
                },
            ],
            occurrences: vec![OccurrenceFact {
                id: occ_id,
                kind: OccurrenceKind::Call,
                entity_id: Some(entity_a),
                anchor_id,
                scope_id: None,
                provenance: Provenance::ExactAst,
                confidence: Confidence::Medium,
            }],
            edges: vec![
                EdgeFact {
                    id: EdgeId(1001),
                    kind: EdgeKind::References,
                    from_entity_id: EntityId(0),
                    to_entity_id: entity_a,
                    via_occurrence_id: Some(occ_id),
                    provenance: Provenance::ExactAst,
                    confidence: Confidence::High,
                },
                EdgeFact {
                    id: EdgeId(1002),
                    kind: EdgeKind::References,
                    from_entity_id: EntityId(0),
                    to_entity_id: entity_b,
                    via_occurrence_id: Some(occ_id),
                    provenance: Provenance::NameHeuristic,
                    confidence: Confidence::Low,
                },
            ],
        };

        let mut index = ReferenceIndex::new();
        index.add_file(&shard);

        // Both entities should have entries in the entity index.
        let refs_a = index.get_by_entity(entity_a);
        assert_eq!(refs_a.len(), 1);
        assert_eq!(refs_a[0].target_candidates.len(), 2);

        let refs_b = index.get_by_entity(entity_b);
        assert_eq!(refs_b.len(), 1);
        assert_eq!(refs_b[0].target_candidates.len(), 2);

        // Name index should have one entry (same symbol key).
        let refs_name = index.get_by_name("ambig_func");
        assert_eq!(refs_name.len(), 1);
        assert_eq!(refs_name[0].target_candidates.len(), 2);

        Ok(())
    }
}