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
//! Bidirectional index for references with span information.
//!
//! Stores qualified names along with their reference spans and file paths.
//! Enables both:
//! - "Find References": given a target, find all sources that reference it
//! - "Find Specializations": given a source, find all targets it references
use crate::core::Span;
use crate::semantic::types::TokenType;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use tracing::trace;
/// Context for a reference that is part of a feature chain (e.g., `localClock.currentTime`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FeatureChainContext {
/// All parts of the chain (e.g., ["localClock", "currentTime"])
pub chain_parts: Vec<String>,
/// Index of this reference in the chain (0 = first part)
pub chain_index: usize,
}
/// A single reference from a source symbol to a target
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReferenceInfo {
/// Qualified name of the symbol that contains this reference
pub source_qname: String,
/// File containing the reference
pub file: PathBuf,
/// Span of the reference (where the target name appears)
pub span: Span,
/// Optional token type for semantic highlighting.
/// If None, defaults to TokenType::Type when generating semantic tokens.
pub token_type: Option<TokenType>,
/// Feature chain context if this is part of a chain (e.g., `a.b.c`)
pub chain_context: Option<FeatureChainContext>,
/// Scope ID where the reference was made (for proper resolution)
pub scope_id: Option<usize>,
}
/// Entry in the reverse index: all references to a target
#[derive(Debug, Clone, Default)]
struct ReferenceEntry {
/// All references to this target (deduplicated via HashSet)
references: HashSet<ReferenceInfo>,
}
/// Bidirectional index for references.
///
/// Stores references with their spans for accurate "Find References" results.
/// Also supports forward lookups (source → targets) for hover and documentation.
#[derive(Debug, Clone, Default)]
pub struct ReferenceIndex {
/// Reverse index: target_name → references to it
reverse: HashMap<String, ReferenceEntry>,
/// Forward index: source_qname → targets it references
forward: HashMap<String, HashSet<String>>,
/// Track which sources came from which file (for cleanup on file change)
source_to_file: HashMap<String, PathBuf>,
}
impl ReferenceIndex {
pub fn new() -> Self {
Self::default()
}
/// Add a reference from source to target with span information.
///
/// # Arguments
/// * `source_qname` - Qualified name of the symbol that has the reference
/// * `target_name` - Name of the target (may be simple or qualified)
/// * `source_file` - File containing the reference
/// * `span` - Location of the reference in source code
pub fn add_reference(
&mut self,
source_qname: &str,
target_name: &str,
source_file: Option<&PathBuf>,
span: Option<Span>,
) {
self.add_reference_with_type(source_qname, target_name, source_file, span, None);
}
/// Add a reference with an explicit token type for semantic highlighting.
///
/// Use this when the reference target's token type differs from the default (Type).
/// For example, redefinition and subsetting targets should use Property.
///
/// # Arguments
/// * `source_qname` - Qualified name of the symbol that has the reference
/// * `target_name` - Name of the target (may be simple or qualified)
/// * `source_file` - File containing the reference
/// * `span` - Location of the reference in source code
/// * `token_type` - Token type for semantic highlighting (None defaults to Type)
pub fn add_reference_with_type(
&mut self,
source_qname: &str,
target_name: &str,
source_file: Option<&PathBuf>,
span: Option<Span>,
token_type: Option<TokenType>,
) {
self.add_reference_full(
source_qname,
target_name,
source_file,
span,
token_type,
None,
None,
);
}
/// Add a reference with full context including feature chain information.
///
/// # Arguments
/// * `source_qname` - Qualified name of the symbol that has the reference
/// * `target_name` - Name of the target (may be simple or qualified)
/// * `source_file` - File containing the reference
/// * `span` - Location of the reference in source code
/// * `token_type` - Token type for semantic highlighting (None defaults to Type)
/// * `chain_context` - Feature chain context if this is part of a chain (e.g., `a.b.c`)
/// * `scope_id` - Scope ID where the reference was made (for proper resolution)
#[allow(clippy::too_many_arguments)]
pub fn add_reference_full(
&mut self,
source_qname: &str,
target_name: &str,
source_file: Option<&PathBuf>,
span: Option<Span>,
token_type: Option<TokenType>,
chain_context: Option<FeatureChainContext>,
scope_id: Option<usize>,
) {
trace!(
"[REF_INDEX] add_reference_full: source='{}' target='{}' span={:?} chain={:?} scope={:?}",
source_qname, target_name, span, chain_context, scope_id
);
// Only add if we have both file and span
if let (Some(file), Some(span)) = (source_file, span) {
let info = ReferenceInfo {
source_qname: source_qname.to_string(),
file: file.clone(),
span,
token_type,
chain_context,
scope_id,
};
// Add to reverse index (target → sources)
self.reverse
.entry(target_name.to_string())
.or_default()
.references
.insert(info);
// Add to forward index (source → targets)
self.forward
.entry(source_qname.to_string())
.or_default()
.insert(target_name.to_string());
// Track file for cleanup
self.source_to_file
.insert(source_qname.to_string(), file.clone());
}
}
/// Get all references to a target with their span information.
///
/// Returns references with file paths and spans for accurate location reporting.
pub fn get_references(&self, target: &str) -> Vec<&ReferenceInfo> {
self.reverse
.get(target)
.map(|entry| entry.references.iter().collect())
.unwrap_or_default()
}
/// Get all targets that a source references (forward lookup).
///
/// Returns the qualified names of all symbols that this source references.
/// Useful for showing specializations in hover.
pub fn get_targets(&self, source_qname: &str) -> Vec<&str> {
self.forward
.get(source_qname)
.map(|targets| targets.iter().map(|s| s.as_str()).collect())
.unwrap_or_default()
}
/// Get all sources that reference a target (qualified names only).
///
/// Returns the qualified names of all symbols that reference this target.
pub fn get_sources(&self, target: &str) -> Vec<&str> {
self.reverse
.get(target)
.map(|entry| {
entry
.references
.iter()
.map(|r| r.source_qname.as_str())
.collect()
})
.unwrap_or_default()
}
/// Check if a target has any references.
pub fn has_references(&self, target: &str) -> bool {
self.reverse
.get(target)
.map(|entry| !entry.references.is_empty())
.unwrap_or(false)
}
/// Get all targets that have references.
///
/// Useful for debugging and testing.
pub fn targets(&self) -> Vec<&str> {
self.reverse.keys().map(|s| s.as_str()).collect()
}
/// Remove all references from symbols in the given file.
///
/// Called when a file is modified or deleted to invalidate stale references.
pub fn remove_references_from_file(&mut self, file_path: &str) {
let path = PathBuf::from(file_path);
// Remove references that came from this file
for entry in self.reverse.values_mut() {
entry.references.retain(|r| r.file != path);
}
// Find all sources from this file and remove from tracking
let sources_to_remove: Vec<String> = self
.source_to_file
.iter()
.filter(|(_, f)| *f == &path)
.map(|(s, _)| s.clone())
.collect();
for source in &sources_to_remove {
self.source_to_file.remove(source);
self.forward.remove(source);
}
// Clean up empty entries
self.reverse.retain(|_, entry| !entry.references.is_empty());
}
/// Remove all references where the given qualified name is the source.
pub fn remove_source(&mut self, source_qname: &str) {
// Remove from source_to_file
self.source_to_file.remove(source_qname);
// Remove from forward index
self.forward.remove(source_qname);
// Remove references from this source
for entry in self.reverse.values_mut() {
entry.references.retain(|r| r.source_qname != source_qname);
}
// Clean up empty entries
self.reverse.retain(|_, entry| !entry.references.is_empty());
}
/// Clear all entries.
pub fn clear(&mut self) {
self.reverse.clear();
self.forward.clear();
self.source_to_file.clear();
}
/// Get the number of unique targets.
pub fn target_count(&self) -> usize {
self.reverse.len()
}
/// Get the total number of references.
pub fn reference_count(&self) -> usize {
self.reverse.values().map(|e| e.references.len()).sum()
}
/// Get all references that occur in a specific file.
///
/// Returns references with their spans for semantic token highlighting.
pub fn get_references_in_file(&self, file_path: &str) -> Vec<&ReferenceInfo> {
let path = PathBuf::from(file_path);
self.reverse
.values()
.flat_map(|entry| entry.references.iter())
.filter(|r| r.file == path)
.collect()
}
/// Find the reference target at a given position in a file.
///
/// Returns the target name (what is being referenced) if the position
/// falls within a reference span. This is used for hover and go-to-definition.
///
/// # Arguments
/// * `file_path` - The file to search in
/// * `position` - The cursor position (0-indexed line and column)
///
/// # Returns
/// The target name if found, or None if no reference is at that position.
pub fn get_reference_at_position(
&self,
file_path: &str,
position: crate::core::Position,
) -> Option<&str> {
let path = PathBuf::from(file_path);
for (target_name, entry) in &self.reverse {
for ref_info in &entry.references {
if ref_info.file == path && ref_info.span.contains(position) {
return Some(target_name.as_str());
}
}
}
None
}
/// Find the reference at a given position, returning both target name and full info.
///
/// This is useful when you need access to the source_qname for scope-aware resolution.
///
/// # Arguments
/// * `file_path` - The file to search in
/// * `position` - The cursor position (0-indexed line and column)
///
/// # Returns
/// A tuple of (target_name, reference_info) if found.
/// If multiple references match the position, prefers qualified names (containing `::`)
/// over simple names.
pub fn get_full_reference_at_position(
&self,
file_path: &str,
position: crate::core::Position,
) -> Option<(&str, &ReferenceInfo)> {
let path = PathBuf::from(file_path);
let mut best_match: Option<(&str, &ReferenceInfo)> = None;
for (target_name, entry) in &self.reverse {
for ref_info in &entry.references {
if ref_info.file == path && ref_info.span.contains(position) {
tracing::trace!(
"[REF_INDEX] Candidate at {:?}: target='{}' qualified={}",
ref_info.span,
target_name,
target_name.contains("::")
);
// Prefer qualified names (containing ::) over simple names
match &best_match {
None => {
best_match = Some((target_name.as_str(), ref_info));
}
Some((existing_name, _)) => {
// If current is qualified and existing is not, prefer current
let current_is_qualified = target_name.contains("::");
let existing_is_qualified = existing_name.contains("::");
if current_is_qualified && !existing_is_qualified {
tracing::trace!(
"[REF_INDEX] Preferring qualified '{}' over simple '{}'",
target_name,
existing_name
);
best_match = Some((target_name.as_str(), ref_info));
}
// If both are qualified, prefer the longer (more specific) one
else if current_is_qualified
&& existing_is_qualified
&& target_name.len() > existing_name.len()
{
tracing::trace!(
"[REF_INDEX] Preferring longer '{}' over '{}'",
target_name,
existing_name
);
best_match = Some((target_name.as_str(), ref_info));
}
}
}
}
}
}
tracing::trace!("[REF_INDEX] Final result: {:?}", best_match.map(|(n, _)| n));
best_match
}
/// Re-resolve simple reference targets to qualified names using the provided resolver.
///
/// Called after import resolution to update references that were stored with simple names
/// during population (before imports were resolved).
///
/// # Arguments
/// * `resolve_fn` - A function that takes (simple_name, source_file, scope_id) and returns Option<qualified_name>
pub fn resolve_targets<F>(&mut self, mut resolve_fn: F)
where
F: FnMut(&str, &PathBuf, Option<usize>) -> Option<String>,
{
// Collect references that need to be re-indexed under a new target name
let mut updates: Vec<(String, String, ReferenceInfo)> = Vec::new();
for (target_name, entry) in &self.reverse {
for ref_info in &entry.references {
// Skip feature chain parts - they need special handling via resolve_chain_targets
if ref_info.chain_context.is_some() {
tracing::trace!(
"[REF_INDEX] resolve_targets: skipping chain target='{}' file={:?}",
target_name,
ref_info.file
);
continue;
}
tracing::trace!(
"[REF_INDEX] resolve_targets: trying target='{}' file={:?} scope={:?}",
target_name,
ref_info.file,
ref_info.scope_id
);
if let Some(qualified_name) =
resolve_fn(target_name, &ref_info.file, ref_info.scope_id)
{
tracing::trace!(
"[REF_INDEX] resolve_targets: resolved '{}' -> '{}'",
target_name,
qualified_name
);
// Only update if the resolved name is different
if &qualified_name != target_name {
updates.push((target_name.clone(), qualified_name, ref_info.clone()));
}
} else {
tracing::trace!(
"[REF_INDEX] resolve_targets: could not resolve '{}'",
target_name
);
}
}
}
// Apply the updates
for (old_target, new_target, ref_info) in updates {
// Remove from old target
if let Some(entry) = self.reverse.get_mut(&old_target) {
entry.references.remove(&ref_info);
}
// Add to new target
self.reverse
.entry(new_target.clone())
.or_default()
.references
.insert(ref_info.clone());
// Update forward index
if let Some(targets) = self.forward.get_mut(&ref_info.source_qname) {
targets.remove(&old_target);
targets.insert(new_target);
}
}
// Clean up empty entries
self.reverse.retain(|_, entry| !entry.references.is_empty());
}
/// Re-resolve feature chain targets to qualified names.
///
/// This handles references that are part of a feature chain (e.g., `takePicture.focus`).
/// For each part, it uses the chain context to resolve through the type hierarchy.
///
/// # Arguments
/// * `resolve_chain_fn` - A function that takes (chain_parts, chain_index, scope_id)
/// and returns Option<qualified_name>
pub fn resolve_chain_targets<F>(&mut self, mut resolve_chain_fn: F)
where
F: FnMut(&[String], usize, usize) -> Option<String>,
{
let mut updates: Vec<(String, String, ReferenceInfo)> = Vec::new();
for (target_name, entry) in &self.reverse {
for ref_info in &entry.references {
// Only process references with chain context
if let Some(chain_ctx) = &ref_info.chain_context {
if let Some(scope_id) = ref_info.scope_id {
if let Some(qualified_name) = resolve_chain_fn(
&chain_ctx.chain_parts,
chain_ctx.chain_index,
scope_id,
) {
if &qualified_name != target_name {
updates.push((
target_name.clone(),
qualified_name,
ref_info.clone(),
));
}
}
}
}
}
}
// Apply the updates
for (old_target, new_target, ref_info) in updates {
if let Some(entry) = self.reverse.get_mut(&old_target) {
entry.references.remove(&ref_info);
}
self.reverse
.entry(new_target.clone())
.or_default()
.references
.insert(ref_info.clone());
if let Some(targets) = self.forward.get_mut(&ref_info.source_qname) {
targets.remove(&old_target);
targets.insert(new_target);
}
}
self.reverse.retain(|_, entry| !entry.references.is_empty());
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::Position;
fn test_span() -> Span {
Span::new(Position::new(0, 0), Position::new(0, 10))
}
#[test]
fn test_add_and_get_references() {
let mut index = ReferenceIndex::new();
let file = PathBuf::from("test.sysml");
index.add_reference("Car", "Vehicle", Some(&file), Some(test_span()));
index.add_reference("Truck", "Vehicle", Some(&file), Some(test_span()));
let refs = index.get_references("Vehicle");
assert_eq!(refs.len(), 2);
let sources: Vec<&str> = refs.iter().map(|r| r.source_qname.as_str()).collect();
assert!(sources.contains(&"Car"));
assert!(sources.contains(&"Truck"));
}
#[test]
fn test_get_sources() {
let mut index = ReferenceIndex::new();
let file = PathBuf::from("test.sysml");
index.add_reference("Car", "Vehicle", Some(&file), Some(test_span()));
index.add_reference("Truck", "Vehicle", Some(&file), Some(test_span()));
let sources = index.get_sources("Vehicle");
assert_eq!(sources.len(), 2);
assert!(sources.contains(&"Car"));
assert!(sources.contains(&"Truck"));
}
#[test]
fn test_get_sources_empty() {
let index = ReferenceIndex::new();
let sources = index.get_sources("NonExistent");
assert!(sources.is_empty());
}
#[test]
fn test_remove_references_from_file() {
let mut index = ReferenceIndex::new();
let file_a = PathBuf::from("a.sysml");
let file_b = PathBuf::from("b.sysml");
index.add_reference("Car", "Vehicle", Some(&file_a), Some(test_span()));
index.add_reference("Truck", "Vehicle", Some(&file_b), Some(test_span()));
index.remove_references_from_file(file_a.to_str().unwrap());
let sources = index.get_sources("Vehicle");
assert_eq!(sources.len(), 1);
assert!(sources.contains(&"Truck"));
}
#[test]
fn test_remove_source() {
let mut index = ReferenceIndex::new();
let file = PathBuf::from("test.sysml");
index.add_reference("Car", "Vehicle", Some(&file), Some(test_span()));
index.add_reference("Car", "Engine", Some(&file), Some(test_span()));
index.remove_source("Car");
assert!(!index.has_references("Vehicle"));
assert!(!index.has_references("Engine"));
}
#[test]
fn test_reference_count() {
let mut index = ReferenceIndex::new();
let file = PathBuf::from("test.sysml");
index.add_reference("Car", "Vehicle", Some(&file), Some(test_span()));
index.add_reference("Car", "Engine", Some(&file), Some(test_span()));
index.add_reference("Truck", "Vehicle", Some(&file), Some(test_span()));
assert_eq!(index.target_count(), 2); // Vehicle, Engine
assert_eq!(index.reference_count(), 3); // Car→Vehicle, Car→Engine, Truck→Vehicle
}
#[test]
fn test_get_reference_at_position() {
let mut index = ReferenceIndex::new();
let file = PathBuf::from("test.sysml");
// Add reference at line 5, columns 10-20
let span = Span::new(Position::new(5, 10), Position::new(5, 20));
index.add_reference("Car", "Vehicle", Some(&file), Some(span));
// Position inside the reference span
assert_eq!(
index.get_reference_at_position("test.sysml", Position::new(5, 15)),
Some("Vehicle")
);
// Position at start of span
assert_eq!(
index.get_reference_at_position("test.sysml", Position::new(5, 10)),
Some("Vehicle")
);
// Position at end of span
assert_eq!(
index.get_reference_at_position("test.sysml", Position::new(5, 20)),
Some("Vehicle")
);
// Position before span
assert_eq!(
index.get_reference_at_position("test.sysml", Position::new(5, 5)),
None
);
// Position after span
assert_eq!(
index.get_reference_at_position("test.sysml", Position::new(5, 25)),
None
);
// Different line
assert_eq!(
index.get_reference_at_position("test.sysml", Position::new(6, 15)),
None
);
// Different file
assert_eq!(
index.get_reference_at_position("other.sysml", Position::new(5, 15)),
None
);
}
}