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
use crate::semantic::resolver::{Resolver, build_export_maps, resolve_imports};
use crate::semantic::workspace::{Workspace, populator::WorkspacePopulator};
use crate::syntax::SyntaxFile;
use std::path::PathBuf;
impl Workspace<SyntaxFile> {
/// Populates the symbol table and reference index for all files
pub fn populate_all(&mut self) -> Result<(), String> {
let mut populator = WorkspacePopulator::new(
&self.files,
&mut self.symbol_table,
&mut self.reference_index,
);
let populated_paths = populator.populate_all()?;
for path in populated_paths {
self.mark_file_populated(&path);
}
// Phase 2: Resolve import paths to fully qualified paths
resolve_imports(&mut self.symbol_table);
// Phase 3: Build export maps with fixpoint iteration
build_export_maps(&mut self.symbol_table);
// Re-resolve reference targets after population
// This handles cross-file references that used simple names during population
self.resolve_reference_targets();
Ok(())
}
/// Populates only unpopulated files (for incremental updates)
pub fn populate_affected(&mut self) -> Result<usize, String> {
let mut populator = WorkspacePopulator::new(
&self.files,
&mut self.symbol_table,
&mut self.reference_index,
);
let populated_paths = populator.populate_affected()?;
let count = populated_paths.len();
for path in populated_paths {
self.mark_file_populated(&path);
}
// Phase 2: Resolve import paths to fully qualified paths
resolve_imports(&mut self.symbol_table);
// Phase 3: Build export maps with fixpoint iteration
build_export_maps(&mut self.symbol_table);
// Re-resolve reference targets after population
self.resolve_reference_targets();
Ok(count)
}
/// Populates a specific file
pub fn populate_file(&mut self, path: &PathBuf) -> Result<(), String> {
let mut populator = WorkspacePopulator::new(
&self.files,
&mut self.symbol_table,
&mut self.reference_index,
);
populator.populate_file(path)?;
self.mark_file_populated(path);
// Phase 2: Resolve import paths to fully qualified paths
resolve_imports(&mut self.symbol_table);
// Phase 3: Build export maps with fixpoint iteration
build_export_maps(&mut self.symbol_table);
// Re-resolve reference targets for the updated file
self.resolve_reference_targets();
Ok(())
}
/// Re-resolve reference targets from simple names to qualified names.
///
/// After population, some cross-file references may have been stored with simple names
/// because import resolution hadn't run yet. This method uses the Resolver to look up
/// the correct qualified names based on each file's scope.
fn resolve_reference_targets(&mut self) {
// Build a map of file path -> scope ID for resolution
let file_scopes: std::collections::HashMap<PathBuf, usize> = self
.symbol_table
.iter_symbols()
.filter_map(|sym| {
sym.source_file()
.and_then(|f| self.symbol_table.get_scope_for_file(f))
.map(|scope| (PathBuf::from(sym.source_file().unwrap()), scope))
})
.collect();
// Resolve simple (non-chain) references
self.reference_index
.resolve_targets(|simple_name, file, scope_id| {
// Use scope_id from reference if available, otherwise fall back to file scope
let scope = scope_id.or_else(|| file_scopes.get(file).copied())?;
// Use the Resolver to look up the symbol in scope
let resolver = Resolver::new(&self.symbol_table);
let symbol = resolver.resolve_in_scope(simple_name, scope)?;
Some(symbol.qualified_name().to_string())
});
// Resolve feature chain references (e.g., takePicture.focus)
self.reference_index
.resolve_chain_targets(|chain_parts, chain_index, scope_id| {
let resolver = Resolver::new(&self.symbol_table);
let parts: Vec<&str> = chain_parts.iter().map(|s| s.as_str()).collect();
let symbol = resolver.resolve_feature_chain(&parts, chain_index, scope_id)?;
Some(symbol.qualified_name().to_string())
});
}
}