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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
//! What vvv knows about a tree, and the questions commands ask of it.
//!
//! The store: every file, which language claims it, its text, and — once
//! asked — its facts, each kept as long as the file's [`Stamp`] stays the
//! same; per language, the [`Project`] its layout resolves paths against.
//! One walk serves everything a command needs. Kept for a session, the
//! graph re-reads only files that changed and parses a file once however
//! many questions are asked of it; built per call, it is the workspace read
//! once.
//!
//! The questions: what a search finds, what declares a name, what a file imports and who imports a module. Each
//! is answered here, through a [`Namespace`], so a command composes answers
//! instead of fetching languages, layouts and projects itself.
mod candidate;
mod fragment;
mod import_bindings;
mod module_navigation;
mod namespace;
mod navigation;
mod navigation_scope;
pub(crate) mod query_snapshot;
mod references;
mod scope;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use rayon::prelude::*;
use vvv_core::{
Address, FileSet, Language, LanguageId, LanguageRegistry, Name, Oracle, Package, Packages,
Project, Query, Role, SourceText,
};
pub use candidate::Candidate;
pub use fragment::{Declared, Edge, Fragment, Node, Structure};
pub use namespace::Namespace;
use references::{Declaration, Target};
pub use references::{DeclarationSite, Evidence};
pub use scope::Scope;
use crate::{
Dep, EngineError, Importer, Match, Occurrence, Reach, Reason, ReferencesQuery, Search, Skipped,
Stamp, Workspace,
};
pub struct Graph {
pub(crate) cancellation: Option<crate::ReadCancellation>,
workspace: Workspace,
languages: LanguageRegistry,
/// Every file of the last walk, absolute, in path order. Only a project
/// needs them relative and as a set, so that is built on demand.
walked: Vec<PathBuf>,
/// When that walk happened; `None` once something may have changed it.
walked_at: Option<Instant>,
/// Every claimed file, in path order.
entries: Vec<Entry>,
/// The workspace as a language's layout sees it, built from the last
/// walk the first time that language asks; a search never pays for it.
/// A project equal to the previous build keeps its allocation, so
/// fragments resolved against it stay valid.
projects: HashMap<LanguageId, Arc<Project>>,
module_sources:
HashMap<LanguageId, Arc<std::collections::BTreeMap<PathBuf, Arc<candidate::SourceFacts>>>>,
/// A second opinion for tokens syntax cannot place, when the host has
/// one; asked after the scope, never instead of it.
oracle: Option<Arc<dyn Oracle>>,
/// The last build of each language's project, to tell a refresh that
/// changed nothing about the project from one that did.
previous: HashMap<LanguageId, Arc<Project>>,
/// Manifest snapshots retained with each captured project for navigation validation.
project_sources: HashMap<LanguageId, Vec<crate::SourceFile>>,
}
struct Entry {
/// Absolute, as walked.
path: PathBuf,
/// `None` when the model will not be asked again, so nothing was stamped.
stamp: Option<Stamp>,
candidate: Candidate,
}
/// How long an [`Engine`](crate::Engine) keeps its graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Retention {
/// Read the workspace afresh for every question: right for a one-shot
/// command, whose single question has to read the tree anyway.
#[default]
PerCall,
/// Keep files and facts between questions and re-read only what changed:
/// right for a session that asks the same tree over and over. Within
/// `trust` of the last walk it does not even look — a burst of
/// keystrokes walks a large tree once, not per key — except after the
/// engine itself wrote, or was told the tree changed.
Session { trust: Duration },
}
impl Retention {
/// A session that checks the tree before every question.
pub const fn session() -> Self {
Self::Session {
trust: Duration::ZERO,
}
}
/// A session that trusts its last walk for `trust`.
pub const fn trusting(self, trust: Duration) -> Self {
match self {
Self::PerCall => Self::PerCall,
Self::Session { .. } => Self::Session { trust },
}
}
}
impl Graph {
pub fn new(workspace: Workspace, languages: LanguageRegistry) -> Self {
Self {
cancellation: None,
workspace,
languages,
walked: Vec::new(),
walked_at: None,
oracle: None,
entries: Vec::new(),
projects: HashMap::new(),
module_sources: HashMap::new(),
previous: HashMap::new(),
project_sources: HashMap::new(),
}
}
pub(crate) fn check_read(&self) -> Result<(), EngineError> {
self.cancellation
.as_ref()
.map_or(Ok(()), crate::ReadCancellation::check)
}
/// Walk the tree; load files that appeared or changed, forget files that
/// vanished, keep the rest. A graph kept for a session stamps what it
/// loads so the next refresh can tell; one built per call does not.
pub fn refresh(&mut self, retention: Retention) -> Result<(), EngineError> {
self.check_read()?;
if let Retention::Session { trust } = retention
&& self.walked_at.is_some_and(|at| at.elapsed() < trust)
{
return Ok(());
}
let walked = self.workspace.files()?;
self.check_read()?;
let claimed: Vec<(&Path, Arc<dyn Language>)> = walked
.iter()
.filter_map(|abs| Some((abs.as_path(), self.languages.for_path(abs)?)))
.collect();
let before = std::mem::take(&mut self.entries);
let vfs = self.workspace.vfs();
let workspace = &self.workspace;
let stamping = matches!(retention, Retention::Session { .. });
let now: Vec<Entry> = claimed
.into_par_iter()
.map(|(abs, language)| {
self.check_read()?;
let stamp = stamping.then(|| vfs.stamp(abs)).transpose()?;
let known = stamp.and_then(|stamp| {
before
.binary_search_by(|e| e.path.as_path().cmp(abs))
.ok()
.map(|i| &before[i])
.filter(|e| e.stamp == Some(stamp))
});
let candidate = match known {
Some(known) => known.candidate.clone(),
None => Candidate::new(workspace.load(abs)?, language),
};
self.check_read()?;
Ok(Entry {
path: abs.to_path_buf(),
stamp,
candidate,
})
})
.collect::<Result<_, EngineError>>()?;
self.entries = now;
self.module_sources.clear();
self.previous.extend(std::mem::take(&mut self.projects));
self.project_sources.clear();
self.walked = walked;
self.walked_at = Some(Instant::now());
Ok(())
}
/// The tree may have changed — the engine wrote to it, or was told so:
/// the next refresh walks whatever it was told to trust.
pub fn touched(&mut self) {
self.walked_at = None;
}
/// Consult `oracle` for the tokens the scope leaves unresolved.
pub fn with_oracle(mut self, oracle: Arc<dyn Oracle>) -> Self {
self.oracle = Some(oracle);
self
}
pub(crate) fn oracle(&self) -> Option<&Arc<dyn Oracle>> {
self.oracle.as_ref()
}
/// Every file `language` claims — any registered language when `None` —
/// in path order.
pub fn files(&self, language: Option<&LanguageId>) -> Vec<Candidate> {
self.entries
.iter()
.map(|e| &e.candidate)
.filter(|c| language.is_none_or(|id| &c.language() == id))
.cloned()
.collect()
}
/// The one claimed file at `path` (absolute or workspace-relative).
pub fn candidate(&self, path: &Path) -> Option<Candidate> {
let abs = self.workspace.absolute(path);
self.entries
.binary_search_by(|e| e.path.as_path().cmp(abs.as_path()))
.ok()
.map(|i| self.entries[i].candidate.clone())
}
/// The files of `language` that spell every one of `literals` as whole
/// tokens: the only ones a query built from those words can match.
pub fn containing(&self, language: Option<&LanguageId>, literals: &[&str]) -> Vec<Candidate> {
self.files(language)
.into_par_iter()
.filter(|c| c.contains_all(literals))
.collect()
}
/// The workspace as `language`'s layout sees it: the walk's files plus
/// the packages the layout's manifests declare. Built on first use after
/// a refresh; `None` when the language has no layout.
fn project_build(&mut self, language: &LanguageId) -> Option<Arc<Project>> {
if let Some(built) = self.projects.get(language) {
return Some(built.clone());
}
let language_impl = self.languages.get(language)?;
let layout = language_impl.layout()?;
let files: FileSet = self
.walked
.iter()
.map(|abs| self.workspace.absolute(abs))
.map(|abs| self.workspace.relative(&abs))
.collect();
let manifests: Vec<&Path> = files
.iter()
.filter(|p| {
p.file_name()
.is_some_and(|n| layout.manifests().iter().any(|m| n == *m))
})
.collect();
let sources: Vec<_> = manifests
.into_par_iter()
.filter_map(|manifest| {
self.check_read().ok()?;
self.workspace.load(manifest).ok()
})
.collect();
self.check_read().ok()?;
let packages: Vec<Package> = sources
.iter()
.filter_map(|file| layout.package(file.path(), file.text()))
.collect();
self.project_sources.insert(language.clone(), sources);
let packages = Packages::new(packages);
let project = Project { packages, files };
// The same project as last time keeps its allocation: every fragment
// resolved against it is still right.
let built = match self.previous.remove(language) {
Some(last) if *last == project => last,
_ => Arc::new(project),
};
self.projects.insert(language.clone(), built.clone());
Some(built)
}
// ------------------------------------------------------------ questions
/// Every language this graph understands.
pub fn language_ids(&self) -> Vec<LanguageId> {
self.languages.iter().map(|l| l.id()).collect()
}
/// The language claiming `path`, when one does.
pub fn language_of(&self, path: &Path) -> Option<Arc<dyn Language>> {
self.languages.for_path(path)
}
pub fn language(&self, id: &LanguageId) -> Option<Arc<dyn Language>> {
self.languages.get(id)
}
/// The language of a directory: the one claiming its first file.
pub fn language_of_directory(&self, dir: &Path) -> Option<Arc<dyn Language>> {
self.workspace
.vfs()
.walk(&self.workspace.absolute(dir))
.ok()?
.iter()
.find_map(|p| self.languages.for_path(p))
}
/// A language's module system on this tree, when it has a layout.
pub fn namespace(&mut self, language: &LanguageId) -> Option<Namespace> {
let language = self.languages.get(language)?;
language.layout()?;
let project = self.project_build(&language.id())?;
let sources = self
.module_sources
.entry(language.id())
.or_insert_with(|| {
Arc::new(
self.entries
.iter()
.filter(|e| e.candidate.language() == language.id())
.map(|e| (e.candidate.path().to_path_buf(), e.candidate.source.clone()))
.collect(),
)
})
.clone();
Some(Namespace::new(
language,
self.cancellation.clone(),
project,
sources,
))
}
/// The namespace of the language claiming `path`, or why there is none:
/// no language claims it, or the language has no layout.
pub fn namespace_of(&mut self, path: &Path) -> Result<Namespace, EngineError> {
let language = self
.languages
.for_path(path)
.ok_or_else(|| EngineError::NoLanguage(path.into()))?;
self.namespace(&language.id())
.ok_or_else(|| EngineError::NoLayout(language.id()))
}
/// The claimed file at `path`, or the error that says why there is none.
pub fn file(&self, path: &Path) -> Result<Candidate, EngineError> {
self.candidate(path)
.ok_or_else(|| EngineError::NoLanguage(path.into()))
}
/// Structural or symbolic search: only files spelling the query's
/// literal words are parsed. A language whose grammar cannot compile the
/// query is skipped and named, never fatal.
///
/// Declarations come first, with their module address when the file has
/// one, then everything else by path and position — the order human
/// output prints and numbers.
pub fn search(&mut self, query: &Query) -> Result<Search, EngineError> {
self.search_scoped(query, &crate::SearchScope::default())
}
pub(crate) fn search_scoped(
&mut self,
query: &Query,
scope: &crate::SearchScope,
) -> Result<Search, EngineError> {
let (candidates, skipped) = self.scoped_candidates(query, scope)?;
let per_file: Vec<Vec<Match>> = candidates
.par_iter()
.map(|c| {
self.check_read()?;
c.find(query)
})
.collect::<Result<_, _>>()?;
// Candidates come in path order and each file's matches in position
// order, so moving declarations to the front is all the ordering
// there is to do.
let mut matches: Vec<Match> = per_file.into_iter().flatten().collect();
self.address_declarations(&mut matches);
matches.sort_by_key(|m| m.role != Role::Declaration);
Ok(Search {
scope: scope.clone(),
query: query.clone(),
matches,
skipped,
})
}
/// Bound retained search results during collection, before a whole-tree vector exists.
pub(crate) fn search_bounded(
&mut self,
query: &Query,
scope: &crate::SearchScope,
maximum: usize,
) -> Result<Search, EngineError> {
let (candidates, skipped) = self.scoped_candidates(query, scope)?;
let mut result = Search {
scope: scope.clone(),
query: query.clone(),
matches: vec![],
skipped,
};
let mut bytes = crate::query_store::QueryStore::weight(&result);
for candidate in candidates {
self.check_read()?;
let mut matches = candidate.find(query)?;
self.address_declarations(&mut matches);
for found in matches {
bytes = bytes.saturating_add(crate::query_store::QueryStore::weight(&found));
if bytes > maximum {
return Err(EngineError::RetentionLimit);
}
result.matches.push(found);
}
}
result.matches.sort_by_key(|m| m.role != Role::Declaration);
Ok(result)
}
fn scoped_candidates(
&mut self,
query: &Query,
scope: &crate::SearchScope,
) -> Result<(Vec<Candidate>, Vec<Skipped>), EngineError> {
let (candidates, skipped) = self.search_candidates(query);
let mut kept = Vec::new();
for candidate in candidates {
self.check_read()?;
if !scope.includes_path(candidate.path()) {
continue;
}
if !scope.packages.is_empty() {
let project = self.project_build(&candidate.language());
self.check_read()?;
if !scope.includes_package(
project
.as_ref()
.and_then(|p| p.packages.containing(candidate.path())),
) {
continue;
}
}
kept.push(candidate);
}
Ok((kept, skipped))
}
fn search_candidates(&self, query: &Query) -> (Vec<Candidate>, Vec<Skipped>) {
let mut skipped = Vec::new();
let mut accepted: Vec<LanguageId> = Vec::new();
for language in self.languages.iter() {
if query.language().is_some_and(|id| id != &language.id()) {
continue;
}
match language.accepts(query) {
Ok(()) => accepted.push(language.id()),
Err(reason) => skipped.push(Skipped {
language: language.id(),
reason: reason.to_string(),
}),
}
}
let candidates = self
.containing(query.language(), &query.literals())
.into_iter()
.filter(|c| accepted.contains(&c.language()))
.collect();
(candidates, skipped)
}
/// Give each declaration the address `outline` would: its file's module
/// joined with its name, when the language has a layout, the file is
/// addressable and the kind is. One address lookup per file, relying on
/// a file's matches being contiguous.
fn address_declarations(&mut self, matches: &mut [Match]) {
let mut last: Option<(PathBuf, Option<Address>)> = None;
for m in matches.iter_mut() {
let Some(symbol) = &m.symbol else {
continue;
};
let Some(ns) = self.namespace(&m.language) else {
continue;
};
if last.as_ref().is_none_or(|(path, _)| path != &m.path) {
last = Some((m.path.to_path_buf(), ns.address(&m.path).ok()));
}
m.address = last
.as_ref()
.and_then(|(_, module)| module.as_ref())
.and_then(|module| ns.address_of(module, symbol));
}
}
/// Declarations called `query.name`, narrowed by kind, language and
/// `declared_in`; an error when there are none.
pub fn declarations(&mut self, query: &ReferencesQuery) -> Result<Vec<Match>, EngineError> {
let mut search = Query::named(query.name.as_str());
if let Some(symbol) = query.symbol {
search = search.with_symbol(symbol);
}
if let Some(language) = &query.language {
search = search.in_language(language.clone());
}
let mut declarations = self.search(&search)?.matches;
if let Some(file) = &query.declared_in {
let file = self.workspace.normalize(file);
declarations.retain(|d| d.path == file);
}
if declarations.is_empty() {
return Err(EngineError::NoSuchSymbol {
name: query.name.clone(),
kind: query.symbol,
});
}
Ok(declarations)
}
/// What a file's import statements name, each resolved to an address,
/// followed through re-exports to the declaration it reaches, and the
/// file declaring that when the layout can place it.
pub fn imports_of(&mut self, path: &Path) -> Result<Vec<Dep>, EngineError> {
let candidate = self.file(path)?;
let ns = self.namespace_of(path)?;
let source = candidate.file().source();
let fragment = candidate.fragment(&ns)?;
fragment
.imports()
.map(|edge| self.dep(&ns, path, source, edge))
.collect()
}
/// One import edge as an answer names it.
pub fn dep(
&self,
ns: &Namespace,
path: &Path,
source: &SourceText,
edge: &Edge,
) -> Result<Dep, EngineError> {
let address = edge.dependency_address(path);
let origin = match (&address, edge.address()) {
(Some(offered), Some(target)) if offered != target => Some(target.clone()),
(Some(address), _) => self
.origin_of(ns, address)?
.filter(|origin| origin != address),
_ => None,
};
let file = origin
.as_ref()
.or(address.as_ref())
.and_then(|address| ns.file_of(address));
Ok(Dep {
import: edge.import.clone(),
start: source.position(edge.import.span.start),
address,
origin,
file: file.map(Into::into),
})
}
/// Every file of `ns`'s language with its edges, in parallel, path order.
pub fn fragments(&self, ns: &Namespace) -> Result<Vec<Node>, EngineError> {
self.files(Some(&ns.id()))
.into_par_iter()
.map(|candidate| {
Ok(Node {
fragment: candidate.fragment(ns)?,
candidate,
})
})
.collect()
}
/// Every file of `ns`'s language other than `path` with a declared
/// import resolving to an address `leads` accepts, in path order.
pub fn importers(
&self,
ns: &Namespace,
path: &Path,
leads: impl Fn(&Address) -> bool + Sync,
) -> Result<Vec<Importer>, EngineError> {
Ok(self
.fragments(ns)?
.into_iter()
.filter(|node| node.path() != path)
.flat_map(|node| {
node.fragment
.imports()
.filter(|e| e.address().is_some_and(&leads))
.map(|e| Importer {
path: node.path().into(),
import: e.import.clone(),
start: node.candidate.file().source().position(e.import.span.start),
})
.collect::<Vec<_>>()
})
.collect())
}
/// Gather what a rename rests on. Occurrences come only from the
/// languages in which a matching declaration exists, so a Rust `foo`
/// never touches a TypeScript `foo`.
pub fn references(&mut self, query: &ReferencesQuery) -> Result<Evidence, EngineError> {
let graph = self;
let declarations = graph.declarations(query)?;
let languages: BTreeSet<LanguageId> =
declarations.iter().map(|m| m.language.clone()).collect();
let mut occurrences = Vec::new();
// Languages whose name has more than one declaration: an unresolved
// token there could belong to any of them.
let mut ambiguous = BTreeSet::new();
for language in languages {
let semantics = graph
.language(&language)
.map(|l| l.semantics())
.ok_or_else(|| EngineError::NoLayout(language.clone()))?;
let addressable = Declaration::addressable_in(&declarations, &language, semantics);
let mut target = Target::of(graph, query, &addressable)?;
if let Some(target) = &mut target
&& let Some(ns) = graph.namespace(&language)
{
target.aliases = graph.aliases_of(&ns, &target.address, &query.name)?;
}
if addressable.len() > 1 || target.as_ref().is_some_and(|t| !t.others.is_empty()) {
ambiguous.insert(language.clone());
}
// No token can spell a name the text does not contain.
let candidates = graph.containing(Some(&language), &[&query.name]);
let per_file: Vec<Vec<Occurrence>> = candidates
.par_iter()
.map(|c| match &target {
Some(target) => target.judge(c, &query.name),
// Nothing to judge against: every token is a candidate.
None => Ok(c
.references(&query.name)?
.into_iter()
.map(|m| Occurrence::judged(m, Reason::ByName))
.collect()),
})
.collect::<Result<_, _>>()?;
let mut judged: Vec<Occurrence> = per_file.into_iter().flatten().collect();
if let (Some(oracle), Some(target)) = (graph.oracle(), &target) {
graph.consult(oracle.as_ref(), target, &mut judged)?;
}
occurrences.extend(judged);
}
// Grouped as a preview lists them — `✓`, then `?`, then `✗`, and
// within those by reason — so the rows to add or drop with
// `--select` are one range.
occurrences.sort_by_key(|o| o.reason);
Ok(Evidence {
declarations,
occurrences,
ambiguous,
})
}
/// Every address that reaches `target` through re-exports, `target`
/// itself first: a `pub use` of the target (or of an alias of it) in a
/// file offers it on under that file's address — as the name it binds,
/// so `pub use a::X as Y` offers `Y`; a glob re-export of an alias's
/// module offers it under its own name. Followed to a fixed point, so
/// chains of re-exports resolve. Only files spelling one of the names
/// found so far (or a `*`) are read.
pub fn aliases_of(
&self,
ns: &Namespace,
target: &Address,
name: &str,
) -> Result<Vec<Address>, EngineError> {
let mut aliases = vec![target.clone()];
let mut names: BTreeSet<Name> = [Name::from(name)].into();
let mut read: HashSet<PathBuf> = HashSet::new();
let mut files: Vec<Arc<Fragment>> = Vec::new();
// A named re-export spells the name. A glob re-export spells the
// language's glob marker, and sits in the alias's own package or
// spells that package's name in its path (`pub use foo_core::*`).
let glob = ns.language().glob_marker();
let reach = self.reach_of(ns, target)?;
loop {
let packages: BTreeSet<vvv_core::PackageId> =
aliases.iter().map(|a| a.package().clone()).collect();
let globs_toward = |c: &Candidate| {
glob.is_some_and(|marker| c.text().contains(marker))
&& (packages.iter().any(|p| c.contains_all(&[p.as_str()]))
|| ns
.address(c.path())
.is_ok_and(|a| packages.contains(a.package())))
};
let fresh: Vec<(PathBuf, Arc<Fragment>)> = self
.files(Some(&ns.id()))
.into_par_iter()
.filter(|c| !read.contains(c.path()))
.filter(|c| names.iter().any(|n| c.contains_all(&[n.as_str()])) || globs_toward(c))
.map(|c| Ok((c.path().to_path_buf(), c.fragment(ns)?)))
.collect::<Result<_, EngineError>>()?;
for (path, fragment) in fresh {
read.insert(path);
files.push(fragment);
}
let mut grew = false;
for fragment in &files {
let Some(module) = &fragment.module else {
continue;
};
for edge in fragment.edges.iter().filter(|e| e.import.reexport) {
let Some(resolved) = edge.address() else {
continue;
};
let offered: Vec<Name> = if edge.import.glob {
// A glob brings in what the module can see: the
// declaration itself only where its reach admits.
aliases
.iter()
.filter(|a| a.parent().as_ref() == Some(resolved))
.filter(|a| *a != target || reach.admits(module))
.filter_map(|a| a.path().last().cloned())
.collect()
} else if aliases.contains(resolved) {
edge.import.binding().cloned().into_iter().collect()
} else {
Vec::new()
};
for name in offered {
let alias = module.join(name.as_str());
if !aliases.contains(&alias) {
names.insert(name);
aliases.push(alias);
grew = true;
}
}
}
}
if !grew {
return Ok(aliases);
}
}
}
/// Ask the oracle about every token the scope left unresolved. An
/// answer counts only as a declaration the graph knows: the target's
/// own name is `Oracle`, another addressable declaration's is
/// `OracleOther`, anything else leaves the token as it was.
fn consult(
&self,
oracle: &dyn Oracle,
target: &Target,
occurrences: &mut [Occurrence],
) -> Result<(), EngineError> {
for occurrence in occurrences
.iter_mut()
.filter(|o| o.reason == Reason::Unresolved)
{
let Some(referent) = oracle.refers(&occurrence.m.path, occurrence.m.span) else {
continue;
};
let Some(candidate) = self.candidate(&referent.path) else {
continue;
};
let fragment = candidate.fragment(&target.ns)?;
let Some(declared) = fragment
.declarations
.iter()
.find(|d| d.symbol.name_span == referent.name_span)
else {
continue;
};
let reason = if target.aliases.contains(&declared.address) {
Reason::Oracle
} else {
Reason::OracleOther
};
*occurrence = Occurrence::judged(occurrence.m.clone(), reason);
}
Ok(())
}
/// Who may name the declaration at `address`, as its file declares it;
/// everyone when the graph cannot find it (a re-export's own reach).
fn reach_of(&self, ns: &Namespace, address: &Address) -> Result<Reach, EngineError> {
let Some(file) = address.parent().and_then(|module| ns.file_of(&module)) else {
return Ok(Reach::Everyone);
};
let fragment = self.file(&file)?.fragment(ns)?;
Ok(fragment
.declarations
.iter()
.find(|d| d.address == *address)
.map_or(Reach::Everyone, |d| d.reach.clone()))
}
/// The declaration an address reaches through re-exports: the address
/// itself when its module declares the name; else what the module's
/// re-export of the name (a `pub use` binding it, or a glob of a module
/// that has it) leads to, followed to the end. `None` when the chain
/// leaves what the layout can place — another package, a type's item.
pub fn origin_of(
&self,
ns: &Namespace,
address: &Address,
) -> Result<Option<Address>, EngineError> {
self.origin_from(ns, address, &mut HashSet::new())
}
fn origin_from(
&self,
ns: &Namespace,
at: &Address,
seen: &mut HashSet<Address>,
) -> Result<Option<Address>, EngineError> {
if !seen.insert(at.clone()) {
return Ok(None);
}
let (Some(module), Some(name)) = (at.parent(), at.path().last()) else {
// A package root names itself.
return Ok(Some(at.clone()));
};
let Some(file) = ns.file_of(&module) else {
return Ok(None);
};
let fragment = self.file(&file)?.fragment(ns)?;
if fragment.module.as_ref() != Some(&module) {
return Ok(None);
}
if fragment.declarations.iter().any(|d| *name == d.symbol.name) {
return Ok(Some(at.clone()));
}
for edge in fragment.edges.iter().filter(|e| e.import.reexport) {
let Some(resolved) = edge.address() else {
continue;
};
let next = if edge.import.glob {
resolved.join(name.as_str())
} else if edge.import.binding() == Some(name) {
resolved.clone()
} else {
continue;
};
if let Some(origin) = self.origin_from(ns, &next, seen)? {
return Ok(Some(origin));
}
}
Ok(None)
}
/// Modules with an import or a qualified path resolving to `address` or
/// under it — every file of the namespace's language except `except`,
/// in path order. What must still see a declaration after it moves.
pub fn consumers(
&self,
ns: &Namespace,
address: &Address,
except: &[&Path],
) -> Result<Vec<Address>, EngineError> {
Ok(self
.fragments(ns)?
.into_iter()
.filter(|node| !except.contains(&node.path()))
.filter(|node| node.fragment.reaches(address))
.filter_map(|node| node.fragment.module.clone())
.collect())
}
}
impl std::fmt::Debug for Graph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Graph")
.field("root", &self.workspace.root())
.field("files", &self.walked.len())
.field("loaded", &self.entries.len())
.finish()
}
}
impl Graph {
pub(crate) fn relationship_files(
&mut self,
language: &crate::LanguageId,
scope: &crate::SearchScope,
) -> Result<Vec<Candidate>, EngineError> {
let mut files = Vec::new();
let group = self
.languages
.get(language)
.map(|plugin| plugin.reference_group());
for file in self.files(None) {
if self
.languages
.get(&file.language())
.map(|plugin| plugin.reference_group())
!= group
{
continue;
}
self.check_read()?;
if !scope.includes_path(file.path()) {
continue;
}
if !scope.packages.is_empty() {
let project = self.project_build(&file.language());
if !scope.includes_package(
project
.as_ref()
.and_then(|p| p.packages.containing(file.path())),
) {
continue;
}
}
files.push(file);
}
Ok(files)
}
}