mir_analyzer/source_provider.rs
1//! Abstraction over how the analyzer obtains file source text it doesn't yet
2//! have in its in-memory salsa inputs.
3//!
4//! Today the analyzer occasionally needs to fault in a file (lazy-load of a
5//! referenced class). The default implementation [`FsSourceProvider`] reads
6//! disk; LSPs swap in a VFS-backed provider so unsaved editor buffers
7//! authoritatively override the on-disk content.
8//!
9//! Boundary: the analyzer never invents file paths. Paths come from one of
10//! - a class resolver (PSR-4, classmap) it was configured with, or
11//! - `AnalysisSession::set_file_text` registrations from the consumer.
12//!
13//! Consumers can therefore reason about exactly which paths the analyzer
14//! might ask for, and serve them from whatever source they prefer.
15
16use std::sync::Arc;
17
18/// Read a file's source text on demand. Returns `None` if the path is
19/// unreadable or doesn't exist — the analyzer treats that as "this class is
20/// unresolvable" and (negative-)caches the failure.
21pub trait SourceProvider: Send + Sync {
22 fn read(&self, path: &str) -> Option<Arc<str>>;
23}
24
25/// Reads source text from the local filesystem. The default provider for
26/// CLI batch contexts.
27pub struct FsSourceProvider;
28
29impl SourceProvider for FsSourceProvider {
30 fn read(&self, path: &str) -> Option<Arc<str>> {
31 std::fs::read_to_string(path).ok().map(Arc::from)
32 }
33}