use std::collections::{HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use crate::cache::SummaryKey;
use crate::file::coordinates::{FileOffset, ReadingOffset};
use crate::file::paths::{DiskFiles, FileProvider, OverlayFiles, normalize_path};
use crate::file::vfs::{Vfs, VfsFile};
use crate::file::view::{FileView, ParsedView, TokensOf};
use crate::include::config::{CompileCommands, CompilerConfig, project_config_from_flags};
use crate::include::toolchain::{self, Environment, Toolchain};
use crate::index::project::{
MemberCompletions, MemberList, NameCompletions, ProjectDefinition, ProjectDefinitions,
ProjectIndex, ProjectMacro, substituted_members,
};
use crate::index::references::{MacroReferences, ReferenceBudget, macro_references};
use crate::index::store::{StoreStats, SummaryStore};
use crate::index::watch::{ChangeBatch, FileEvent, Response, WatchFilter};
use crate::index::worklist::StepOutcome;
use crate::index::worklist::{Priority, Step, outcome_of};
use crate::index::{
FileIndexer, definition_across_files, definitions_across_files, macro_across_files,
member_completions_at, members_of,
};
use crate::inlay::ParameterHint;
use crate::macros::MacroTable;
use crate::project::{ConfigReport, ProjectDiscovery};
use crate::sema::modules::{ImportOutcome, ModuleScanner};
use crate::summary::{FileSummary, OutlineSymbol, UnitReading};
use crate::symbol::{Known, UnknownReason};
use crate::{PathInterner, cache};
#[allow(dead_code)]
const SHARING_FILE: &str = "sharing.bin";
const COOK_SLICE: usize = 4;
pub struct DiagTrace(Option<(&'static str, std::time::Instant)>);
impl DiagTrace {
pub fn new(what: &'static str) -> DiagTrace {
DiagTrace(crate::probes::diagnostics().then(|| (what, std::time::Instant::now())))
}
}
impl Drop for DiagTrace {
fn drop(&mut self) {
if let Some((what, started)) = self.0 {
eprintln!(
" [diagnostics] {what:<28} {:>7} ms",
started.elapsed().as_millis()
);
}
}
}
const RE_READ_SLICE: usize = 16;
const SUMMARY_BUDGET_BYTES: usize = 1 << 30;
const MAX_COMPLETION_ITEMS: usize = 20_000;
const WALKS_WORTH_SHARING: usize = 4;
const MACRO_SLICE: usize = 1;
const UNTIL_A_UNIT_MAY_BE_READ: std::time::Duration = std::time::Duration::from_secs(1);
const PROGRAM_READ_ATTEMPTS: u8 = 3;
const THE_LARGEST_PROGRAM_READ_AT_ONCE: usize = 8 * 1024 * 1024;
const COOK_TWO_LEVELS_FURTHER: usize = 24;
const COOK_ONE_LEVEL_FURTHER: usize = 64;
const MAX_PROJECT_FILES: usize = 10_000;
const SOURCE_EXTENSIONS: &[&str] = &[
"c", "cc", "cpp", "cxx", "c++", "h", "hh", "hpp", "hxx", "h++", "ipp", "inl", "tpp", "tcc",
"cppm", "ixx",
];
pub type SessionFiles<F = DiskFiles> = OverlayFiles<OpenDocuments, F>;
#[derive(Debug, Clone, Default)]
pub struct OpenDocuments {
buffers: Arc<RwLock<HashMap<String, String>>>,
revision: Arc<std::sync::atomic::AtomicU64>,
}
impl OpenDocuments {
pub fn new() -> Self {
OpenDocuments::default()
}
pub fn open(&self, path: impl AsRef<Path>, text: impl Into<String>) {
let mut buffers = self
.buffers
.write()
.expect("the buffer map is not poisoned");
let key = normalize_path(path.as_ref(), cfg!(windows));
let text = text.into();
if buffers.get(&key) != Some(&text) {
buffers.insert(key, text);
self.revision
.fetch_add(1, std::sync::atomic::Ordering::Release);
}
}
pub fn close(&self, path: impl AsRef<Path>) -> bool {
let mut buffers = self
.buffers
.write()
.expect("the buffer map is not poisoned");
let removed = buffers
.remove(&normalize_path(path.as_ref(), cfg!(windows)))
.is_some();
if removed {
self.revision
.fetch_add(1, std::sync::atomic::Ordering::Release);
}
removed
}
fn revision(&self) -> u64 {
self.revision.load(std::sync::atomic::Ordering::Acquire)
}
pub fn text(&self, path: impl AsRef<Path>) -> Option<String> {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.get(&normalize_path(path.as_ref(), cfg!(windows)))
.cloned()
}
pub fn is_open(&self, path: impl AsRef<Path>) -> bool {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.contains_key(&normalize_path(path.as_ref(), cfg!(windows)))
}
pub fn paths(&self) -> Vec<PathBuf> {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.keys()
.map(PathBuf::from)
.collect()
}
pub fn len(&self) -> usize {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl FileProvider for OpenDocuments {
fn read(&self, path: &Path) -> Option<String> {
self.text(path)
}
fn exists(&self, path: &Path) -> bool {
self.is_open(path)
}
fn is_open_buffer(&self, path: &Path) -> bool {
self.is_open(path)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct UnitStats {
pub from_memory: usize,
pub from_disk: usize,
pub walked: usize,
}
#[derive(Debug, Clone, Copy)]
enum Which {
FromMemory,
FromDisk,
Walked,
}
#[derive(Debug)]
struct PerFileConfig {
config: std::sync::Arc<CompilerConfig>,
language: cpp_parser::features::LanguageConfig,
cuda: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ViewKey {
path: PathBuf,
rendering: Option<RenderingId>,
text: u64,
id: crate::file::paths::FileId,
cuda: bool,
language: Option<cpp_parser::features::LanguageConfig>,
open: bool,
environment: Option<EnvironmentId>,
}
fn buffer_view_key(
file: &VfsFile,
cuda: bool,
environment: Option<Arc<cpp_parser::MacroEnvironment>>,
) -> ViewKey {
ViewKey {
path: file.path.clone(),
rendering: None,
text: crate::cache::content_hash(&file.text),
id: file.id,
cuda,
language: None,
open: file.open,
environment: environment.map(EnvironmentId),
}
}
#[derive(Debug, Clone)]
struct RenderingId(std::sync::Arc<crate::preprocess::cooked::RenderedCooked>);
impl PartialEq for RenderingId {
fn eq(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for RenderingId {}
impl std::hash::Hash for RenderingId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::sync::Arc::as_ptr(&self.0).hash(state);
}
}
#[derive(Debug, Clone)]
struct EnvironmentId(std::sync::Arc<cpp_parser::MacroEnvironment>);
impl PartialEq for EnvironmentId {
fn eq(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for EnvironmentId {}
impl std::hash::Hash for EnvironmentId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::sync::Arc::as_ptr(&self.0).hash(state);
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CacheSizes {
pub renderings: usize,
pub rendering_bytes: usize,
pub units: usize,
pub summaries: usize,
pub facts: usize,
pub macros: usize,
pub cooked_files: usize,
pub cooked_declarations: usize,
pub views: usize,
pub view_bytes: usize,
}
fn a_view_weighs(view: &FileView) -> usize {
view.source.len().saturating_mul(8) + view.tree.get_tokens().len().saturating_mul(16)
}
#[derive(Clone, Debug)]
pub struct SessionRevision {
session: Arc<()>,
changes: u64,
documents: u64,
}
pub struct Session<F: FileProvider = DiskFiles> {
root: PathBuf,
identity: Arc<()>,
changes: u64,
config: CompilerConfig,
toolchain: Option<Toolchain>,
discovery: ProjectDiscovery,
files: SessionFiles<F>,
documents: OpenDocuments,
vfs: Vfs<SessionFiles<F>>,
store: SummaryStore<SessionFiles<F>>,
filter: WatchFilter,
share_contributions: bool,
project: Vec<PathBuf>,
project_paths: HashMap<String, PathBuf>,
queue: Work,
parsed_since_the_last_pass: Vec<PathBuf>,
refined_on_request: std::sync::Mutex<std::collections::HashSet<PathBuf>>,
refined_readings: std::sync::Mutex<std::collections::HashSet<String>>,
cooking: Cooking,
definitions: std::sync::Mutex<crate::MacroDefinitions>,
units_read: std::collections::HashSet<String>,
programs_wanted: std::collections::HashSet<String>,
units_attempted: std::collections::HashMap<String, u8>,
checks_memo: std::sync::Mutex<Option<(u64, Vec<crate::sema::check::Finding>)>>,
pull_hits: std::sync::atomic::AtomicU64,
pull_runs: std::sync::atomic::AtomicU64,
last_edit: std::time::Instant,
unit_index: std::sync::Mutex<Option<(u64, crate::IndexedUnit)>>,
rendered_unit: std::sync::Mutex<Option<(u64, crate::RenderedUnit)>>,
unit_roots: std::sync::Mutex<std::collections::HashMap<(PathBuf, u64), u64>>,
directive_signatures:
std::collections::HashMap<String, crate::preprocess::directive::DirectiveSignature>,
prefetched: HashMap<String, crate::index::store::Prepared>,
units: std::sync::Mutex<UnitTable>,
walked_by_content: std::sync::Mutex<
cache::cache_lru::Cache<(u64, u64), std::sync::Arc<crate::TranslationUnit>>,
>,
headers: crate::SharedHeaders,
macro_environments: std::sync::Mutex<
cache::cache_lru::Cache<(PathBuf, u64), std::sync::Arc<cpp_parser::MacroEnvironment>>,
>,
macro_work: std::sync::Mutex<Cooking>,
parsed_definitions: std::sync::Mutex<crate::ParsedDefinitions>,
config_for_file: std::sync::Mutex<HashMap<PathBuf, std::sync::Arc<PerFileConfig>>>,
surfaces: std::sync::Mutex<crate::effect::Surfaces>,
effects: std::sync::Mutex<HashMap<u64, crate::effect::Effect>>,
renderings: std::sync::Mutex<
cache::cache_lru::Cache<
(PathBuf, u64),
std::sync::Arc<crate::preprocess::cooked::RenderedCooked>,
>,
>,
views: std::sync::Mutex<
cache::cache_lru::Cache<ViewKey, std::sync::Arc<crate::file::view::ParsedView>>,
>,
rendering_summaries: std::sync::Mutex<
cache::cache_lru::Cache<(PathBuf, u64), std::sync::Arc<crate::FileSummary>>,
>,
cooking_refused: std::sync::Mutex<std::collections::HashSet<String>>,
unit_from_memory: std::sync::atomic::AtomicUsize,
unit_from_disk: std::sync::atomic::AtomicUsize,
unit_walked: std::sync::atomic::AtomicUsize,
walks_by_root: std::sync::Mutex<std::collections::HashMap<String, u64>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleImport {
pub name: String,
pub range: cpp_parser::SourceRange,
pub resolved: Option<PathBuf>,
}
fn the_directive_defining(name: &str, text: &str, offset: usize) -> Option<String> {
let mut settled: Option<String> = None;
let mut at = 0usize;
for line in text.split_inclusive('\n') {
let start = at;
at += line.len();
if start > offset {
break;
}
let trimmed = line.trim_start();
let rest = if let Some(rest) = trimmed.strip_prefix("#define") {
rest.trim_start()
} else if let Some(rest) = trimmed.strip_prefix("#undef") {
rest.trim_start()
} else {
continue;
};
let end = rest
.find(|character: char| !(character.is_alphanumeric() || character == '_'))
.unwrap_or(rest.len());
if &rest[..end] != name {
continue;
}
if start > offset {
continue;
}
let mut directive = trimmed.trim_end().to_string();
while directive.ends_with('\\') {
let next_start = at;
let Some(next) = text[next_start..].split_inclusive('\n').next() else {
break;
};
at += next.len();
directive.pop();
directive.push_str(next.trim_end());
if next.trim_end().ends_with('\\') {
continue;
}
break;
}
settled = Some(directive.trim_end().to_string());
}
settled
}
fn the_standards_own_macro(
name: &str,
language: &cpp_parser::features::LanguageConfig,
) -> Option<String> {
use cpp_parser::features::{Family, Standard};
let family = language.family();
let c_standard = language
.standards()
.filter(|standard| standard.family() == Family::C)
.max();
let cpp_standard = language
.standards()
.filter(|standard| standard.family() == Family::Cpp)
.max();
match name {
"__STDC__" if family != Some(Family::Cpp) => Some("1".to_string()),
"__STDC_HOSTED__" if family != Some(Family::Cpp) => Some("1".to_string()),
"__STDC_VERSION__" if family != Some(Family::Cpp) => match c_standard? {
Standard::C89 => None,
Standard::C99 => Some("199901L".to_string()),
Standard::C11 => Some("201112L".to_string()),
Standard::C17 => Some("201710L".to_string()),
Standard::C23 => Some("202311L".to_string()),
_ => None,
},
"__cplusplus" if family != Some(Family::C) => match cpp_standard? {
Standard::Cpp11 => Some("201103L".to_string()),
Standard::Cpp14 => Some("201402L".to_string()),
Standard::Cpp17 => Some("201703L".to_string()),
Standard::Cpp20 => Some("202002L".to_string()),
Standard::Cpp23 => Some("202302L".to_string()),
_ => None,
},
_ => None,
}
}
fn a_macro_whose_value_depends_on_the_use(name: &str) -> bool {
matches!(
name,
"__LINE__" | "__FILE__" | "__DATE__" | "__TIME__" | "__COUNTER__"
)
}
impl Session<DiskFiles> {
pub fn open(
root: impl Into<PathBuf>,
files: SessionFiles<DiskFiles>,
filter: WatchFilter,
) -> Session<DiskFiles> {
Session::open_with_config_file(root.into(), files, filter, None)
}
pub fn open_with_config_file(
root: PathBuf,
files: SessionFiles<DiskFiles>,
filter: WatchFilter,
config_file: Option<&Path>,
) -> Session<DiskFiles> {
let discovery = crate::project::discover(&files, &root, config_file);
let project_config = discovery.config.clone();
if let Some(locale) = project_config.config.locale.as_deref() {
cpp_parser::set_locale(locale);
}
let filter = apply_project_config(filter, &project_config);
let for_file = discovery
.database
.as_ref()
.and_then(|database| database.commands.commands.first())
.map(|command| command.file.clone())
.unwrap_or_else(|| root.clone());
let base = project_config_from_flags(
&discovery.flags(),
discovery.working_directory(),
&project_config.config.compile,
);
let base = CompilerConfig {
root: Some(root.to_path_buf()),
fuzzy_includes: project_config
.config
.compile
.fuzzy_includes
.unwrap_or(discovery.database.is_none()),
targets: crate::project::targets_for(&project_config.config.compile),
libraries: project_config.config.libraries.clone(),
database: discovery
.database
.as_ref()
.map(|database| std::sync::Arc::new(database.commands.clone())),
remove_args: project_config.config.compile.remove_args.clone(),
extra_args: project_config.config.compile.extra_args.clone(),
..base
};
let base = match (&project_config.config.compile.standard, &base.standard) {
(Some(stated), _) => base.with_standard(stated.clone()),
(None, _) => base,
};
let base = match (base.standard.clone(), discovery.standard()) {
(None, Some(standard)) => base.with_standard(standard),
_ => base,
};
let environment = Environment::current();
let layout = crate::include::msvc::WindowsLayout::current();
let named = discovery.named_compilers();
let toolchain_cache = project_config
.config
.index
.cache_dir
.clone()
.map(std::path::PathBuf::from)
.unwrap_or_else(|| root.join(".cppls"))
.join("toolchain");
let commands = toolchain::CachedCommands::new(toolchain_cache);
let toolchain = toolchain::discover_with(
&files,
&commands,
&named,
toolchain::BuildStatement {
commands: discovery
.database
.as_ref()
.map(|database| &database.commands),
standard: base.standard.as_deref(),
},
&for_file,
&environment,
&layout,
);
let config = match &toolchain {
Some(found) => found.config(&base),
None => base,
};
let config = match &project_config.config.compile.cuda {
Some(cuda) => cuda.applied_to(config, &root),
None => config,
};
let config = match &project_config.config.unreal {
Some(unreal) => unreal.applied_to(config, &root),
None => config,
};
let config = match toolchain.as_ref().and_then(|found| found.dialect) {
Some(dialect) => config.with_dialect(dialect),
None => config,
};
let session = Session::assemble(root, files, filter, config, toolchain, discovery);
session
}
}
pub struct RenderedCook {
pub text_hash: u64,
pub indexed: crate::IndexedRendering,
pub reading: CookedReading,
revision: SessionRevision,
}
pub struct CookingMaterials {
pub path: PathBuf,
pub key: SummaryKey,
pub unit: Option<std::sync::Arc<crate::TranslationUnit>>,
pub configuration: Option<crate::preprocess::cooked::Configuration>,
pub text: String,
pub text_hash: u64,
pub seed: MacroTable,
pub already_declared: std::collections::HashSet<String>,
revision: SessionRevision,
}
pub struct DetachedCook<F: FileProvider> {
files: SessionFiles<F>,
config: CompilerConfig,
materials: CookingMaterials,
}
impl<F: FileProvider + Clone> DetachedCook<F> {
pub fn render(self) -> Option<RenderedCook> {
Session::<F>::render_cook(&self.files, &self.config, &self.materials)
}
}
#[derive(Default)]
pub struct PreparedWave {
pub prepared: Vec<(PathBuf, crate::index::store::Prepared)>,
pub units: Vec<std::sync::Arc<crate::TranslationUnit>>,
pub refinements: Vec<crate::FileSummary>,
revision: Option<SessionRevision>,
}
pub struct FileReadMaterials<F: FileProvider> {
path: PathBuf,
revision: SessionRevision,
input: crate::index::store::DetachedRead<F>,
view: Option<BufferViewMaterials>,
}
pub struct PreparedFile {
path: PathBuf,
revision: SessionRevision,
prepared: crate::index::store::Prepared,
view: Option<PreparedBufferView>,
}
#[derive(Clone)]
struct BufferViewMaterials {
file: VfsFile,
cuda: bool,
environment: Option<Arc<cpp_parser::MacroEnvironment>>,
}
struct PreparedBufferView {
key: ViewKey,
parsed: ParsedView,
weight: usize,
}
impl BufferViewMaterials {
fn parse(self) -> PreparedBufferView {
let key = buffer_view_key(&self.file, self.cuda, self.environment.clone());
let macros = self.environment.unwrap_or_default();
let view = FileView::parse_with_cuda(&self.file, macros.as_ref(), self.cuda);
let weight = a_view_weighs(&view);
PreparedBufferView {
key,
parsed: view.into_parsed(),
weight,
}
}
}
impl<F: FileProvider> FileReadMaterials<F> {
pub fn read(self) -> PreparedFile {
let (prepared, view) = match self.view {
Some(view)
if view.file.text.len() >= 16 * 1024
&& std::thread::available_parallelism().is_ok_and(|count| count.get() > 1) =>
{
std::thread::scope(|scope| {
let worker_view = view.clone();
let worker = std::thread::Builder::new()
.name("cpp-ls-buffer-view".to_string())
.stack_size(16 * 1024 * 1024)
.spawn_scoped(scope, move || worker_view.parse());
match worker {
Ok(worker) => {
let prepared = self.input.read();
let view = worker
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic));
(prepared, Some(view))
}
Err(error) => {
log::warn!("could not start a buffer-view worker: {error}");
(self.input.read(), Some(view.parse()))
}
}
})
}
Some(view) => (self.input.read(), Some(view.parse())),
None => (self.input.read(), None),
};
PreparedFile {
path: self.path,
revision: self.revision,
prepared,
view,
}
}
}
#[derive(Default)]
pub struct Drain {
files: Vec<PathBuf>,
units: Vec<std::sync::Arc<crate::TranslationUnit>>,
environments: Vec<PathBuf>,
revision: Option<SessionRevision>,
}
impl Drain {
pub fn is_empty(&self) -> bool {
self.files.is_empty() && self.environments.is_empty()
}
}
#[derive(Default)]
pub struct PreparedDrain {
re_read: Option<crate::index::store::ReRead>,
rendered: Vec<(
(PathBuf, u64),
std::sync::Arc<crate::preprocess::cooked::RenderedCooked>,
)>,
revision: Option<SessionRevision>,
retry_files: Vec<PathBuf>,
retry_environments: Vec<PathBuf>,
}
pub struct PreparedUnitReading {
root: PathBuf,
revision: SessionRevision,
program: u64,
indexed: crate::IndexedUnit,
walked: Vec<PathBuf>,
}
pub struct UnitReadMaterials<F: FileProvider> {
root: PathBuf,
revision: SessionRevision,
key: SummaryKey,
units_key: u64,
unit: Option<std::sync::Arc<crate::TranslationUnit>>,
sources: HashMap<PathBuf, String>,
macros: crate::Marked,
config: CompilerConfig,
files: F,
database: Option<std::sync::Arc<cache::cache_db::SummaryCache>>,
program: Option<u64>,
rendered: Option<crate::RenderedUnit>,
known: Option<crate::IndexedUnit>,
}
impl<F: FileProvider> UnitReadMaterials<F> {
pub fn root(&self) -> &Path {
&self.root
}
pub fn units_key(&self) -> u64 {
self.units_key
}
pub fn read_the_program(self) -> Option<PreparedUnitReading> {
if let Some(indexed) = self.known {
return Some(PreparedUnitReading {
root: self.root,
revision: self.revision,
program: self.program?,
indexed,
walked: self
.rendered
.as_ref()
.map(|r| r.walked.clone())
.unwrap_or_default(),
});
}
let stream = match self.rendered {
Some(rendered) => rendered,
None => {
let unit = self.unit.as_ref()?;
let definitions = unit.definitions();
let seed = MacroTable::from_marked(&self.macros);
let search = crate::preprocess::cooked::Search::new(&self.files, &self.config);
let cooking = std::time::Instant::now();
#[cfg(feature = "perf-probe")]
crate::perf::mark("before the cook");
let mut stream = unit.cook_the_unit(
&self.sources,
&definitions,
Some(&seed),
true,
Some(&search),
);
#[cfg(feature = "perf-probe")]
crate::perf::mark("cook");
if crate::probes::units() {
eprintln!(
"[unit] cook {:>6.1} ms ({} file(s), {} bytes)",
cooking.elapsed().as_secs_f64() * 1000.0,
stream.files.len(),
stream.text.len()
);
}
stream.walked = unit
.files()
.map(|path| path.to_path_buf())
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
.collect();
if let Some(database) = &self.database {
let _ = database.put_unit(self.units_key, &stream);
}
stream
}
};
let program = crate::cache::content_hash(&stream.text);
if let Some(database) = &self.database
&& let Ok(Some(known)) = database.indexed_unit(program)
{
return Some(PreparedUnitReading {
root: self.root,
revision: self.revision,
program,
indexed: known,
walked: stream.walked.clone(),
});
}
let indexer = FileIndexer::new(&self.files, &self.config);
let indexed = indexer.index_unit_rendering(&self.root, &stream, self.key);
if let Some(database) = &self.database {
let _ = database.put_indexed_unit(program, &indexed);
}
Some(PreparedUnitReading {
root: self.root,
revision: self.revision,
program,
indexed,
walked: stream.walked.clone(),
})
}
}
struct SessionReads<'a, F: FileProvider> {
files: &'a F,
index: &'a crate::ProjectIndex,
}
impl<F: FileProvider> crate::effect::Reads for SessionReads<'_, F> {
fn read(&self, path: &std::path::Path) -> Option<String> {
self.files.read(path)
}
fn resolve(&self, from: &std::path::Path, spelling: &str) -> Option<PathBuf> {
let wanted = spelling.trim_matches(|c| c == '"' || c == '<' || c == '>');
let summary = self.index.summary(from)?;
summary
.includes
.iter()
.find(|include| include.spelling.trim() == wanted)
.and_then(|include| include.resolved.clone())
}
}
impl<F: FileProvider + Clone> Session<F> {
pub fn with_config(
root: impl Into<PathBuf>,
files: SessionFiles<F>,
filter: WatchFilter,
config: CompilerConfig,
) -> Session<F> {
let root = root.into();
let config_report = crate::project::load_config(&files, &root, None);
let filter = apply_project_config(filter, &config_report);
let discovery = ProjectDiscovery {
root: root.clone(),
config: config_report,
database: None,
problems: Vec::new(),
};
Session::assemble(root, files, filter, config, None, discovery)
}
fn assemble(
root: PathBuf,
files: SessionFiles<F>,
filter: WatchFilter,
config: CompilerConfig,
toolchain: Option<Toolchain>,
discovery: ProjectDiscovery,
) -> Session<F> {
let project_config = discovery.config.clone();
let database = discovery
.database
.as_ref()
.map(|database| &database.commands);
let documents = files.overlay.clone();
let mut extra_extensions = project_config.config.workspace.source_extensions.clone();
if config.cuda() {
extra_extensions.extend(["cu".to_string(), "cuh".to_string()]);
}
let from_libraries = library_files_to_read(&project_config.config.libraries, &root);
let mut project = project_files(
&files,
&filter,
&root,
database,
&extra_extensions,
project_config
.config
.index
.max_files
.unwrap_or(MAX_PROJECT_FILES),
);
for file in from_libraries {
if !project.iter().any(|held| held == &file) {
project.push(file);
}
}
let configured = the_compilation_is_known(toolchain.as_ref());
let mut store = SummaryStore::with_provider(root.clone(), config.clone(), files.clone())
.with_macros(crate::index::environment::compilation_environment(
&config,
toolchain.as_ref(),
configured,
));
if let Some(cache_dir) = &project_config.config.index.cache_dir {
store = store.with_cache_directory(cache_dir);
}
let headers = crate::completion::header_index(&config, &root, project.clone());
let mut session = Session {
root: root.clone(),
identity: Arc::new(()),
changes: 0,
config,
toolchain,
discovery,
vfs: Vfs::new(files.clone()),
files,
documents,
store,
filter,
share_contributions: crate::probes::share(),
project_paths: project
.iter()
.map(|path| (queue_key(path), path.clone()))
.collect(),
project: project.clone(),
queue: Work::default(),
parsed_since_the_last_pass: Vec::new(),
refined_on_request: std::sync::Mutex::new(std::collections::HashSet::new()),
refined_readings: std::sync::Mutex::new(std::collections::HashSet::new()),
cooking: Cooking::default(),
definitions: std::sync::Mutex::new(crate::MacroDefinitions::default()),
units: std::sync::Mutex::new(UnitTable::default()),
walked_by_content: std::sync::Mutex::new(cache::cache_lru::Cache::new(
MAX_WALKS_BY_CONTENT_ENTRIES,
MAX_WALKS_BY_CONTENT_BYTES,
)),
units_read: std::collections::HashSet::new(),
programs_wanted: std::collections::HashSet::new(),
units_attempted: std::collections::HashMap::new(),
checks_memo: std::sync::Mutex::new(None),
pull_hits: std::sync::atomic::AtomicU64::new(0),
pull_runs: std::sync::atomic::AtomicU64::new(0),
unit_index: std::sync::Mutex::new(None),
rendered_unit: std::sync::Mutex::new(None),
unit_roots: std::sync::Mutex::new(HashMap::new()),
last_edit: std::time::Instant::now(),
directive_signatures: std::collections::HashMap::new(),
prefetched: HashMap::new(),
headers,
macro_work: std::sync::Mutex::new(Cooking::default()),
parsed_definitions: std::sync::Mutex::new(crate::ParsedDefinitions::new()),
config_for_file: std::sync::Mutex::new(HashMap::new()),
macro_environments: std::sync::Mutex::new(cache::cache_lru::Cache::macro_environments()),
surfaces: std::sync::Mutex::new(crate::effect::Surfaces::new()),
effects: std::sync::Mutex::new(HashMap::new()),
renderings: std::sync::Mutex::new(cache::cache_lru::Cache::renderings()),
views: std::sync::Mutex::new(cache::cache_lru::Cache::views()),
rendering_summaries: std::sync::Mutex::new(cache::cache_lru::Cache::new(
RENDERING_SUMMARY_ENTRIES,
RENDERING_SUMMARY_BYTES,
)),
cooking_refused: std::sync::Mutex::new(std::collections::HashSet::new()),
unit_from_memory: std::sync::atomic::AtomicUsize::new(0),
unit_from_disk: std::sync::atomic::AtomicUsize::new(0),
unit_walked: std::sync::atomic::AtomicUsize::new(0),
walks_by_root: std::sync::Mutex::new(std::collections::HashMap::new()),
};
for path in session.project.clone() {
session.queue.add(path, Priority::Rest, 0);
}
session
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn revision(&self) -> SessionRevision {
SessionRevision {
session: self.identity.clone(),
changes: self.changes,
documents: self.documents.revision(),
}
}
pub fn is_current(&self, revision: &SessionRevision) -> bool {
self.owns_revision(revision)
&& self.changes == revision.changes
&& self.documents.revision() == revision.documents
}
pub fn owns_revision(&self, revision: &SessionRevision) -> bool {
Arc::ptr_eq(&self.identity, &revision.session)
}
fn changed_input(&mut self) {
self.changes = self
.changes
.checked_add(1)
.expect("session revision overflow");
}
fn forget_refinement(&self, path: &Path) {
self.refined_readings
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.remove(&queue_key(path));
}
pub fn config_for(&self, file: &std::path::Path) -> CompilerConfig {
self.config().for_file(file)
}
fn config_for_file(&self, file: &std::path::Path) -> std::sync::Arc<PerFileConfig> {
if let Ok(known) = self.config_for_file.lock()
&& let Some(held) = known.get(file)
{
return std::sync::Arc::clone(held);
}
let config = std::sync::Arc::new(self.config().for_file(file));
let worked_out = std::sync::Arc::new(PerFileConfig {
language: config.language_for(file),
cuda: config.cuda(),
config,
});
if let Ok(mut known) = self.config_for_file.lock() {
if known.len() >= CONFIG_FOR_FILE_ENTRIES {
known.clear();
}
known.insert(file.to_path_buf(), std::sync::Arc::clone(&worked_out));
}
worked_out
}
pub fn config(&self) -> &CompilerConfig {
&self.config
}
pub fn project_config(&self) -> &ConfigReport {
&self.discovery.config
}
pub fn discovery(&self) -> &ProjectDiscovery {
&self.discovery
}
pub fn toolchain(&self) -> Option<&Toolchain> {
self.toolchain.as_ref()
}
pub fn compile_database(&self) -> Option<&CompileCommands> {
self.discovery
.database
.as_ref()
.map(|database| &database.commands)
}
pub fn documents(&self) -> &OpenDocuments {
&self.documents
}
pub fn index(&self) -> &ProjectIndex {
self.store.index()
}
pub fn directive_summary(&self, path: &Path) -> Option<crate::FileSummary> {
self.store
.directive_summary(path)
.map(|summary| (*summary).clone())
}
pub fn parsed_summary(&self, path: &Path) -> Option<crate::FileSummary> {
self.store.parsed_summary(path)
}
pub fn summary_database_sizes(&self) -> Option<cache::cache_db::DatabaseSizes> {
self.store.summary_database_sizes()
}
pub fn collect_the_cache(&mut self) -> Option<cache::cache_db::Collected> {
self.store.collect_the_cache()
}
pub fn flush_summaries(&self) {
self.store.flush_summaries();
}
pub fn cache_sizes(&self) -> CacheSizes {
let (renderings, rendering_bytes) = self
.renderings
.lock()
.map(|known| {
let bytes = known
.values()
.map(|rendering| rendering.text.len())
.sum::<usize>();
(known.len(), bytes)
})
.unwrap_or((0, 0));
let summaries = self.store.index().summaries().count();
let facts = self
.store
.index()
.summaries()
.map(|summary| summary.declarations.len())
.sum();
let macros = self
.store
.index()
.summaries()
.map(|summary| summary.macros.len())
.sum();
let (cooked_files, cooked_declarations) = self.store.index().cooked_size();
let (views, view_bytes) = self
.views
.lock()
.map(|known| (known.len(), known.weight()))
.unwrap_or((0, 0));
CacheSizes {
renderings,
rendering_bytes,
units: self.held_units().held.len(),
summaries,
facts,
macros,
cooked_files,
cooked_declarations,
views,
view_bytes,
}
}
fn a_view_already_parsed(&self, key: &ViewKey) -> Option<FileView> {
let mut known = self.views.lock().ok()?;
let parsed = known.get(key)?;
Some(FileView::from_parsed((**parsed).clone()))
}
fn remember_a_view(&self, key: ViewKey, view: &FileView) {
let weight = a_view_weighs(view);
if let Ok(mut known) = self.views.lock() {
let parsed = view.clone().into_parsed();
known.insert(key, std::sync::Arc::new(parsed), weight);
}
}
pub fn set_sharing_contributions(&mut self, on: bool) {
self.share_contributions = on;
}
pub fn walk_contributions_held(&self) -> usize {
self.store.contributions().held()
}
pub fn enforce_the_summary_budget(&mut self, budget: usize) -> usize {
let (_, bytes, _, _) = self.store.index().accounting();
if bytes <= budget {
return 0;
}
let mut candidates: Vec<(PathBuf, usize)> = self
.store
.index()
.summaries()
.map(|summary| {
(
summary.path.clone(),
crate::FileSummary::heap_bytes(summary),
)
})
.collect();
candidates.sort_unstable_by_key(|candidate| std::cmp::Reverse(candidate.1));
let open: std::collections::HashSet<String> = self
.documents
.paths()
.iter()
.map(|path| queue_key(path))
.collect();
let refined: std::collections::HashSet<String> = self
.refined_readings
.lock()
.map(|refined| refined.iter().cloned().collect())
.unwrap_or_default();
let mut over = bytes.saturating_sub(budget);
let mut forgotten = 0usize;
for (path, size) in candidates {
if over == 0 {
break;
}
if open.contains(&queue_key(&path))
|| refined.contains(&queue_key(&path))
|| self.queue.is_queued(&path)
|| self
.parsed_since_the_last_pass
.iter()
.any(|queued| queued == &path)
{
continue;
}
if self.store.index_mut().forget(&path) {
over = over.saturating_sub(size);
forgotten += 1;
}
}
forgotten
}
pub fn memory_report(&self) -> crate::MemoryReport {
let (vfs_files, vfs_text_bytes, vfs_line_index_bytes) = self.vfs.accounting();
let (summaries, summary_bytes, cooked_files, cooked_bytes) =
self.store.index().accounting();
let (definitions, definition_bytes) = self
.definitions
.lock()
.map(|definitions| definitions.accounting())
.unwrap_or((0, 0));
let (units, unit_bytes) = self.held_units().accounting();
let (renderings, rendering_bytes) = self
.renderings
.lock()
.map(|known| {
let bytes = known
.values()
.map(|rendering| rendering.text.len())
.sum::<usize>();
(known.len(), bytes)
})
.unwrap_or((0, 0));
let (dead_macro_files, dead_macro_offsets) = self.dead_macro_decisions();
let (views, view_bytes) = self
.views
.lock()
.map(|known| (known.len(), known.weight()))
.unwrap_or((0, 0));
let (walk_cache_entries, walk_cache_bytes) = self.store.contributions().accounting();
let (walks_by_content_entries, walks_by_content_bytes) = self
.walked_by_content
.lock()
.map(|held| (held.len(), held.weight()))
.unwrap_or((0, 0));
let shared_timeline_bytes = {
let mut seen: std::collections::HashSet<usize> = std::collections::HashSet::new();
for unit in self.held_units().timelines() {
seen.insert(std::sync::Arc::as_ptr(unit) as *const () as usize);
}
let mut shared = 0usize;
if let Ok(held) = self.walked_by_content.lock() {
for unit in held.values() {
if !seen.insert(std::sync::Arc::as_ptr(unit) as *const () as usize) {
shared += unit.heap_bytes();
}
}
}
shared
};
let tables = self.store.index().table_accounting();
crate::MemoryReport {
vfs_files,
vfs_text_bytes,
vfs_line_index_bytes,
summaries,
summary_bytes,
cooked_files,
cooked_bytes,
definitions,
definition_bytes,
units,
unit_bytes,
renderings,
rendering_bytes,
views,
view_bytes,
dead_macro_decisions: dead_macro_files,
dead_macro_bytes: dead_macro_offsets * std::mem::size_of::<usize>(),
directive_signatures: self.directive_signatures.len(),
include_searches: self.store.include_search_count().unwrap_or(0),
units_read: self.units_read.len(),
walk_cache_entries,
walk_cache_bytes,
walks_by_content_entries,
walks_by_content_bytes,
shared_timeline_bytes,
index_names_bytes: tables.names.bytes,
index_names_entries: tables.names.name_entries,
index_scope_entries: tables.names.scope_entries,
index_name_postings: tables.names.name_postings,
index_scope_postings: tables.names.scope_postings,
index_edges: tables.edges,
index_edge_bytes: tables.edge_bytes,
index_member_lists: tables.member_list_entries,
index_member_list_bytes: tables.member_list_bytes,
index_memo_entries: tables.guard_entries
+ tables.visibility_entries
+ tables.condition_entries,
index_memo_bytes: tables.guard_bytes + tables.visibility_bytes + tables.condition_bytes,
}
}
pub fn macro_at(&mut self, file: &Path, name: &str, offset: usize) -> Option<crate::MacroAt> {
let mut candidates: Vec<PathBuf> = Vec::new();
if self.project.iter().any(|root| root == file) {
candidates.push(file.to_path_buf());
}
candidates.extend(self.project.iter().filter(|root| *root != file).cloned());
for root in candidates {
let Some(unit) = self.translation_unit_of(&root) else {
continue;
};
if let Some(answer) = unit.macro_at(file, name, offset) {
return Some(answer);
}
}
None
}
pub fn stats(&self) -> StoreStats {
self.store.stats()
}
pub fn pull_counts(&self) -> (u64, u64) {
(
self.pull_hits.load(std::sync::atomic::Ordering::Relaxed),
self.pull_runs.load(std::sync::atomic::Ordering::Relaxed),
)
}
pub fn dead_macro_decisions(&self) -> (usize, usize) {
self.store.dead_macro_decisions()
}
pub fn cache_directory(&self) -> &Path {
self.store.cache_directory()
}
pub fn project_files(&self) -> &[PathBuf] {
&self.project
}
pub fn contains_project_file(&self, path: &Path) -> bool {
self.project_paths.contains_key(&queue_key(path))
}
pub fn add_project_files(&mut self, paths: impl IntoIterator<Item = PathBuf>) -> usize {
let mut added = 0;
for path in paths {
if let std::collections::hash_map::Entry::Vacant(entry) =
self.project_paths.entry(queue_key(&path))
{
entry.insert(path.clone());
self.project.push(path.clone());
added += 1;
}
self.queue.add(path, Priority::Rest, 0);
}
if added > 0
&& let Ok(mut headers) = self.headers.write()
{
*headers = headers.clone().with_project(self.project.clone());
}
added
}
pub fn did_open(&mut self, path: impl AsRef<Path>, text: &str) {
self.buffer_changed(path.as_ref(), text);
}
pub fn did_change(&mut self, path: impl AsRef<Path>, text: &str) {
self.buffer_changed(path.as_ref(), text);
}
pub fn did_save(&mut self, path: impl AsRef<Path>, text: Option<&str>) {
if let Some(text) = text {
self.buffer_changed(path.as_ref(), text);
}
}
pub fn did_close(&mut self, path: impl AsRef<Path>) {
self.changed_input();
let path = path.as_ref();
self.forget_refinement(path);
self.documents.close(path);
self.vfs.close(path);
self.store.forget(path);
self.prefetched.clear();
self.directive_signatures.remove(&queue_key(path));
self.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clear();
self.units_read.clear();
self.queue.again(path.to_path_buf(), Priority::Rest, 0);
}
pub fn changed(&mut self, events: impl IntoIterator<Item = FileEvent>) -> Response {
let mut batch = ChangeBatch::new(self.filter.clone());
batch.extend(events);
self.respond(&batch)
}
pub fn watch_filter(&self) -> WatchFilter {
self.filter.clone()
}
pub fn respond(&mut self, batch: &ChangeBatch) -> Response {
if !batch.is_empty() {
self.changed_input();
}
self.prefetched.clear();
for (path, _) in batch.changed() {
let text = self.files.read(path);
let moved = self.directives_moved(path, text.as_deref());
if moved.environment {
self.invalidate_dependents(path);
}
self.held_definitions().forget(path);
if moved.layout {
self.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clear();
self.units_read.clear();
}
}
let response = self.store.respond(batch);
for path in &response.forgotten {
self.forget_refinement(path);
if !self.documents.is_open(path) {
self.vfs.close(path);
}
}
if response.everything {
let project = self.project.clone();
for path in project {
self.queue.requeue(path, Priority::Rest, 0);
}
for path in self.documents.paths() {
self.queue.again(path, Priority::Open, 0);
}
} else {
for path in &response.reindex {
self.forget_refinement(path);
self.store.forget(path);
if !self.documents.is_open(path) {
self.vfs.close(path);
}
let priority = if self.documents.is_open(path) {
Priority::Open
} else {
Priority::Rest
};
self.queue.again(path.clone(), priority, 0);
}
}
response
}
fn buffer_changed(&mut self, path: &Path, text: &str) {
self.changed_input();
self.last_edit = std::time::Instant::now();
if !self.directive_signatures.contains_key(&queue_key(path))
&& let Some(held) = self.vfs.held(path)
{
let before = crate::preprocess::directive::directive_signature(&held.text);
self.directive_signatures.insert(queue_key(path), before);
}
let moved = self.directives_moved(path, Some(text));
self.prefetched.clear();
self.documents.open(path, text);
self.vfs.insert(path, text, true);
if moved.environment {
self.invalidate_dependents(path);
}
self.forget_refinement(path);
self.store.forget(path);
self.held_definitions().forget(path);
if moved.layout {
self.held_units().remove(&queue_key(path));
self.units_read.remove(&queue_key(path));
}
if let Ok(mut refused) = self.cooking_refused.lock() {
refused.remove(&queue_key(path));
}
self.cooking.want(path);
self.queue.again(path.to_path_buf(), Priority::Open, 0);
}
fn directives_moved(&mut self, path: &Path, text: Option<&str>) -> DirectivesMoved {
let key = queue_key(path);
let Some(text) = text else {
self.directive_signatures.remove(&key);
return DirectivesMoved {
environment: true,
layout: true,
};
};
let now = crate::preprocess::directive::directive_signature(text);
match self.directive_signatures.insert(key, now) {
Some(before) => DirectivesMoved {
environment: before.environment != now.environment,
layout: before.layout != now.layout,
},
None => DirectivesMoved {
environment: true,
layout: true,
},
}
}
fn invalidate_dependents(&mut self, path: &Path) {
let dependents = self.store.index().dependents_of(path);
for dependent in dependents {
self.held_units().remove(&queue_key(&dependent));
self.forget_refinement(&dependent);
let was_indexed = self.store.forget(&dependent);
if was_indexed {
self.queue.again(dependent.clone(), Priority::Rest, 0);
}
if let Ok(mut known) = self.renderings.lock() {
known.retain(|(held, _)| held != &dependent);
}
}
}
fn want_the_closure_cooked(&mut self, root: &Path) {
let already_read =
|session: &Self, path: &Path| session.store.index().cooked_declarations(path).is_some();
let can_be_cooked =
|session: &Self, path: &Path| session.store.index().summary(path).is_some();
let direct = self.direct_includes_of(root);
let refused = |session: &Self, path: &Path| {
session
.cooking_refused
.lock()
.map(|refused| refused.contains(&queue_key(path)))
.unwrap_or(false)
};
for included in &direct {
if !already_read(self, included)
&& can_be_cooked(self, included)
&& !refused(self, included)
{
if crate::probes::cooking() {
eprintln!(
"want_the_closure_cooked({}): queued {} (not read, cookable, not refused)",
root.display(),
included.display()
);
}
self.cooking.want(included);
}
}
let mut budget = COOK_ONE_LEVEL_FURTHER;
let mut nested_files: Vec<PathBuf> = Vec::new();
for included in &direct {
for nested in self.direct_includes_of(included) {
if budget == 0 {
break;
}
if already_read(self, &nested) || refused(self, &nested) {
continue;
}
if !nested_files.contains(&nested) {
nested_files.push(nested.clone());
}
self.cooking.want(&nested);
budget -= 1;
}
}
let mut budget = COOK_TWO_LEVELS_FURTHER;
for nested in &nested_files {
for deeper in self.direct_includes_of(nested) {
if budget == 0 {
break;
}
if already_read(self, &deeper) || refused(self, &deeper) {
continue;
}
self.cooking.want(&deeper);
budget -= 1;
}
}
if !already_read(self, root) {
self.cooking.want(root);
}
}
fn direct_includes_of(&self, path: &Path) -> Vec<PathBuf> {
self.store
.index()
.summary(path)
.map(|summary| {
summary
.includes
.iter()
.filter_map(|include| include.resolved.clone())
.collect()
})
.unwrap_or_default()
}
pub fn diagnose_everything(
&mut self,
up_to: usize,
) -> Vec<(std::path::PathBuf, FileDiagnostics)> {
let paths: Vec<std::path::PathBuf> = self
.store
.index()
.summaries()
.map(|summary| summary.path.clone())
.take(up_to.max(1))
.collect();
self.want_everything_cooked();
let mut waves = 0usize;
while self.pending() > 0 {
if self.advance(64).is_empty() {
break;
}
waves += 1;
if waves > 100_000 {
break;
}
}
let mut cooked = 0usize;
loop {
let claimed = self.claim_cooking(64);
if claimed.is_empty() {
break;
}
let materials = self.materials_for(claimed);
let rendered = self.render_them(materials);
cooked += self.commit_them(rendered).len();
if cooked > up_to.max(1) * 2 + 1024 {
break;
}
}
paths
.into_iter()
.filter_map(|path| self.diagnostics(&path).map(|found| (path, found)))
.collect()
}
pub fn want_cooked_reading(&mut self, path: &Path) {
let refused = self
.cooking_refused
.lock()
.map(|refused| refused.contains(&queue_key(path)))
.unwrap_or(false);
if refused {
return;
}
if self.store.index().summary(path).is_none() {
self.queue.add(path.to_path_buf(), Priority::Open, 0);
}
if self.store.index().cooked_declarations(path).is_some() {
let rendered = self
.vfs
.held(path)
.is_some_and(|file| self.known_rendering_of(&file.path, &file.text).is_some());
if rendered {
return;
}
}
self.cooking.want(path);
}
pub fn is_ready_for_a_request_about(&self, path: &Path) -> bool {
self.vfs.held(path).is_some()
&& (self.store.index().cooked_declarations(path).is_some() || self.cooking.wants(path))
}
pub fn want_everything_cooked(&mut self) {
let wanted: Vec<PathBuf> = self
.store
.index()
.summaries()
.filter(|summary| {
self.store
.index()
.cooked_declarations(&summary.path)
.is_none()
})
.map(|summary| summary.path.clone())
.collect();
for path in wanted {
self.cooking.want(&path);
}
}
pub fn advance(&mut self, steps: usize) -> Vec<Step> {
let mut done = Vec::new();
let started = std::time::Instant::now();
for taken in 0..steps {
let Some((path, priority, depth)) = self.queue.pop() else {
break;
};
let key = queue_key(&path);
let left = steps - taken;
let wave_would_parse_it_as_text = left > 1
&& !self.will_read_it_as_the_program(&path)
&& !self.prefetched.contains_key(&key);
if wave_would_parse_it_as_text {
let mut wave = vec![path.clone()];
wave.extend(
self.queue
.peek(left - 1)
.into_iter()
.filter(|queued| !self.prefetched.contains_key(&queue_key(queued))),
);
let room = (steps * 4).saturating_sub(self.prefetched.len());
let prepared_from = std::time::Instant::now();
let made = {
let queue = &self.queue;
let prefetched = &self.prefetched;
self.store.prepare_closure(
&wave,
|target| {
!queue.is_worked(target) && !prefetched.contains_key(&queue_key(target))
},
room.min(steps * 2).max(wave.len()),
)
};
if crate::probes::units() {
eprintln!(
"ADVANCE wave of {} file(s) prepared {} in {:?}",
wave.len(),
made.len(),
prepared_from.elapsed(),
);
}
for (path, made) in made {
self.prefetched.insert(queue_key(&path), made);
}
}
let ready = self.prefetched.remove(&key);
let step_started = std::time::Instant::now();
done.push(self.index_one(path, priority, depth, ready));
if crate::probes::units() {
eprintln!(
"[advance] one step took {} ms ({} of {} step(s))",
step_started.elapsed().as_millis(),
taken + 1,
steps
);
}
}
if self.queue.pending() == 0 {
self.prefetched.clear();
}
let units = Vec::new();
if crate::probes::units() {
eprintln!(
"ADVANCE steps={} in {:?} done={}",
steps,
started.elapsed(),
done.len()
);
}
let drain = {
let finishing = std::time::Instant::now();
let drain = self.finish_a_slice(&done, units);
if crate::probes::units() {
eprintln!(
"ADVANCE finish {} file(s) in {:?} (batch total {:?})",
done.len(),
finishing.elapsed(),
started.elapsed(),
);
}
drain
};
let drain_started = std::time::Instant::now();
let prepared = self.prepare_the_drain(drain);
let prepared_in = drain_started.elapsed();
let committed = self.commit_the_drain(prepared);
if crate::probes::units() {
eprintln!(
"ADVANCE drain prepared {:?}, committed {committed} in {:?}",
prepared_in,
drain_started.elapsed() - prepared_in,
);
}
if self.is_idle() && self.pending_cooking() == 0 && self.refinements_wanted() == 0 {
let reading = std::time::Instant::now();
self.read_a_program_now();
if crate::probes::units() {
eprintln!("ADVANCE read_a_program_now in {:?}", reading.elapsed());
}
}
done
}
pub fn prepare_a_wave(&self, steps: usize) -> PreparedWave {
self.prepare_wave(steps, false)
}
pub fn prepare_background_wave(&self, steps: usize) -> PreparedWave {
self.prepare_wave(steps, true)
}
fn prepare_wave(&self, steps: usize, defer_open_files: bool) -> PreparedWave {
let revision = Some(self.revision());
let candidates: Vec<PathBuf> = self
.queue
.peek(steps)
.into_iter()
.filter(|path| {
let key = queue_key(path);
!self.prefetched.contains_key(&key)
&& !self.queue.is_worked(path)
&& (!defer_open_files || !self.documents.is_open(path))
})
.collect();
let mut prepared = Vec::new();
let mut wave = Vec::new();
for path in candidates {
if self.documents.is_open(&path) {
let spelled = self
.store
.index()
.summary(&path)
.map(|summary| summary.path.as_path())
.unwrap_or(&path);
if let Some(unit) = self.the_timeline_to_read_through(spelled) {
if prepared.is_empty() {
let reading = self.store.prepare_with_the_timeline(&path, &unit);
prepared.push((path, reading));
}
continue;
}
}
wave.push(path);
}
let slice: Vec<PathBuf> = self
.parsed_since_the_last_pass
.iter()
.take(RE_READ_SLICE)
.cloned()
.collect();
let mut refinements: Vec<crate::FileSummary> = Vec::new();
for path in &slice {
if let Some(summary) = self.store.the_stored_refinement(path) {
refinements.push(summary);
}
}
let units = Vec::new();
if wave.is_empty() {
return PreparedWave {
prepared,
units,
refinements,
revision,
};
}
let room = (steps * 4)
.saturating_sub(self.prefetched.len())
.max(wave.len());
let queue = &self.queue;
let prefetched = &self.prefetched;
prepared.extend(self.store.prepare_closure(
&wave,
|target| {
!queue.is_worked(target)
&& !prefetched.contains_key(&queue_key(target))
&& (!self.documents.is_open(target)
|| wave.iter().any(|root| queue_key(root) == queue_key(target)))
},
room,
));
PreparedWave {
prepared,
units,
refinements,
revision,
}
}
pub fn commit_a_wave(&mut self, wave: PreparedWave, steps: usize) -> (Vec<Step>, Drain) {
if !wave
.revision
.as_ref()
.is_some_and(|revision| self.is_current(revision))
{
log::debug!("discarding an index wave prepared for an older session revision");
return (Vec::new(), Drain::default());
}
let PreparedWave {
prepared,
units,
refinements,
revision: _,
} = wave;
for (path, made) in prepared {
self.prefetched.insert(queue_key(&path), made);
}
for summary in refinements {
self.parsed_since_the_last_pass
.retain(|queued| queued != &summary.path);
self.store.apply_a_stored_refinement(summary);
}
const COMMIT_MS: u128 = 20;
let mut done = Vec::new();
let committing = std::time::Instant::now();
for _ in 0..steps {
if !done.is_empty() && committing.elapsed().as_millis() >= COMMIT_MS {
break;
}
if self.queue.peek(1).first().is_some_and(|path| {
self.documents.is_open(path) && !self.prefetched.contains_key(&queue_key(path))
}) {
break;
}
let Some((path, priority, depth)) = self.queue.pop() else {
break;
};
let ready = self.prefetched.remove(&queue_key(&path));
done.push(self.index_one(path, priority, depth, ready));
}
if self.queue.pending() == 0 {
self.prefetched.clear();
}
let drain = self.finish_a_slice(&done, units);
(done, drain)
}
pub fn next_open_file_to_read(&self) -> Option<PathBuf> {
self.queue
.peek(1)
.into_iter()
.find(|path| self.documents.is_open(path))
}
pub fn next_file_to_catch_up(&self, path: &Path) -> Option<PathBuf> {
if self.queue.is_queued(path) {
return Some(path.to_path_buf());
}
self.unread_includes_of(path)
.into_iter()
.find(|include| self.queue.is_queued(include))
}
pub fn ensure_file_to_catch_up(&mut self, path: &Path) -> Option<PathBuf> {
self.want_refinement(path);
if let Some(next) = self.next_file_to_catch_up(path) {
return Some(next);
}
let include = self.unread_includes_of(path).into_iter().next()?;
self.queue.again(include.clone(), Priority::Open, 1);
Some(include)
}
pub fn gather_a_file_read(&self, path: &Path) -> Option<FileReadMaterials<SessionFiles<F>>> {
if !self.queue.is_queued(path) {
return None;
}
let revision = self.revision();
let spelled = self
.vfs
.held(path)
.filter(|file| file.open)
.map(|file| file.path.as_path())
.or_else(|| {
self.project_paths
.get(&queue_key(path))
.map(PathBuf::as_path)
})
.or_else(|| self.vfs.held(path).map(|file| file.path.as_path()))
.or_else(|| {
self.store
.index()
.summary(path)
.map(|summary| summary.path.as_path())
})
.unwrap_or(path);
let timeline = if self.documents.is_open(path) {
self.the_timeline_to_read_through(spelled)
} else {
None
};
let input = self.store.detached_read(spelled, timeline)?;
let view =
self.vfs
.held(spelled)
.filter(|file| file.open)
.map(|file| BufferViewMaterials {
file: file.clone(),
cuda: self.config.cuda(),
environment: self.known_macros_of(&file.path, &file.text),
});
Some(FileReadMaterials {
path: spelled.to_path_buf(),
revision,
input,
view,
})
}
pub fn commit_a_file_read(&mut self, file: PreparedFile) -> bool {
if !self.is_current(&file.revision) || !self.queue.forget(&file.path) {
log::debug!(
"discarding an obsolete file read for {}",
file.path.display()
);
return false;
}
let path = file.path;
self.prefetched.remove(&queue_key(&path));
let rendering = file.prepared.rendering();
let step = self.index_one(path.clone(), Priority::Open, 0, Some(file.prepared));
if step.with_the_environment {
self.parsed_since_the_last_pass
.retain(|queued| queued != &path);
} else if matches!(
step.outcome,
StepOutcome::Built | StepOutcome::Unstored | StepOutcome::Reused
) {
self.parsed_since_the_last_pass.push(path.clone());
}
if let Some((hash, rendered)) = rendering {
let weight = rendered.weight();
self.renderings
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.insert((path.clone(), hash), rendered, weight);
self.cooking.forget(&path);
}
if let Some(view) = file.view
&& let Some(current) = self.vfs.held(&path)
&& view.key
== buffer_view_key(
current,
self.config.cuda(),
self.known_macros_of(¤t.path, ¤t.text),
)
{
self.views
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.insert(view.key, Arc::new(view.parsed), view.weight);
}
if !step.with_the_environment {
self.want_refinement(&path);
}
true
}
fn finish_a_slice(
&mut self,
done: &[Step],
units: Vec<std::sync::Arc<crate::TranslationUnit>>,
) -> Drain {
if done.iter().any(|step| step.with_the_environment) {
let repaired: std::collections::HashSet<PathBuf> = done
.iter()
.filter(|step| step.with_the_environment)
.map(|step| step.path.clone())
.collect();
self.parsed_since_the_last_pass
.retain(|queued| !repaired.contains(queued));
}
self.parsed_since_the_last_pass.extend(
done.iter()
.filter(|step| {
matches!(
step.outcome,
StepOutcome::Built | StepOutcome::Unstored | StepOutcome::Reused
)
})
.filter(|step| !step.with_the_environment)
.map(|step| step.path.clone()),
);
let files = if self.is_idle() {
self.take_the_files_wanted_for_refinement(RE_READ_SLICE)
} else {
Vec::new()
};
let served = self.store.take_the_stored_refinements(files);
let files = served.still_to_do;
let _ = &units;
let slice_units = if files.len() < WALKS_WORTH_SHARING {
Vec::new()
} else {
self.units_for_the_pass(&files)
};
let mut drain = Drain {
files,
units: {
let mut all = units;
all.extend(slice_units);
all
},
environments: Vec::new(),
revision: Some(self.revision()),
};
if self.is_idle() {
for open in self.documents.paths() {
self.want_the_closure_cooked(&open);
}
while let Some(path) = self
.macro_work
.lock()
.ok()
.and_then(|mut work| work.take(1).into_iter().next())
{
self.cooking.want(&path);
}
for _ in 0..MACRO_SLICE {
let Some(path) = self
.macro_work
.lock()
.ok()
.and_then(|mut work| work.take(1).into_iter().next())
else {
break;
};
drain.environments.push(path);
}
}
drain
}
pub fn read_a_program_now(&mut self) -> Option<UnitReading> {
let root = self.the_program_to_read()?;
let materials = self.gather_a_unit_read(&root);
let prepared = materials.and_then(UnitReadMaterials::read_the_program);
self.commit_a_program(&root, prepared)
}
pub fn commit_a_program(
&mut self,
root: &Path,
prepared: Option<PreparedUnitReading>,
) -> Option<UnitReading> {
let Some(prepared) = prepared else {
let key = queue_key(root);
let attempts = self.units_attempted.entry(key).or_default();
*attempts += 1;
if *attempts >= PROGRAM_READ_ATTEMPTS {
self.units_read.insert(queue_key(root));
}
return None;
};
if queue_key(root) != queue_key(&prepared.root) || !self.is_current(&prepared.revision) {
log::debug!("discarding a program prepared for an older session revision");
return None;
}
let committed = self.commit_a_unit_reading(prepared)?;
self.units_read.insert(queue_key(root));
#[cfg(feature = "perf-probe")]
crate::perf::mark("commit");
Some(committed)
}
pub fn prepare_the_drain(&self, drain: Drain) -> PreparedDrain {
let Drain {
files,
units,
environments,
revision,
} = drain;
let retry_files = files.clone();
let retry_environments = environments.clone();
if !revision
.as_ref()
.is_some_and(|revision| self.is_current(revision))
{
return PreparedDrain {
revision,
retry_files,
retry_environments,
..PreparedDrain::default()
};
}
let tracing = crate::probes::cooking();
let started = std::time::Instant::now();
let re_read =
(!files.is_empty()).then(|| self.store.prepare_the_re_read(&files, &units, false, true));
if tracing && !files.is_empty() {
eprintln!(
"drain: re-read {} file(s) in {} ms",
files.len(),
started.elapsed().as_millis()
);
}
let mut rendered = Vec::new();
for path in environments {
let step = std::time::Instant::now();
let Some(reading) = self.rendering_of(&path) else {
continue;
};
let Some(text) = self.text(&path) else {
continue;
};
let _ = self.the_macros_of(&path, &text);
if tracing {
eprintln!(
"drain: environment for {} in {} ms",
path.display(),
step.elapsed().as_millis()
);
}
rendered.push((
(path, crate::cache::content_hash(&text)),
std::sync::Arc::new(reading),
));
}
PreparedDrain {
re_read,
rendered,
revision,
retry_files,
retry_environments,
}
}
pub fn commit_the_drain(&mut self, prepared: PreparedDrain) -> usize {
if !prepared
.revision
.as_ref()
.is_some_and(|revision| self.is_current(revision))
{
if prepared
.revision
.as_ref()
.is_some_and(|revision| Arc::ptr_eq(&self.identity, &revision.session))
{
let mut wanted = self
.refined_on_request
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
for path in prepared.retry_files {
if self.files.exists(&path) {
wanted.insert(path);
}
}
drop(wanted);
let mut work = self
.macro_work
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
for path in prepared.retry_environments {
if self.files.exists(&path) {
work.want(&path);
}
}
}
log::debug!("discarding a drain prepared for an older session revision");
return 0;
}
for (key, rendered) in prepared.rendered {
if let Ok(mut known) = self.renderings.lock() {
let weight = rendered.weight();
known.insert(key, rendered, weight);
}
}
let Some(plan) = prepared.re_read else {
return 0;
};
let waiting: Vec<PathBuf> = plan.waiting_for_the_index().to_vec();
let refined: Vec<PathBuf> = plan
.rebuilt
.iter()
.map(|(summary, _)| summary.path.clone())
.collect();
let re_read = self.store.commit_the_re_read(plan);
if let Ok(mut known) = self.refined_readings.lock() {
known.extend(refined.iter().map(|path| queue_key(path)));
}
if !waiting.is_empty() && self.pending_work() > 0 {
if crate::probes::cooking() {
eprintln!(
"re-read: {} file(s) go back on the queue — their closures are still filling",
waiting.len()
);
}
for path in waiting {
self.queue.again(path, Priority::Rest, 0);
}
}
re_read
}
fn units_for_the_pass(&self, files: &[PathBuf]) -> Vec<std::sync::Arc<crate::TranslationUnit>> {
let mut candidates: Vec<PathBuf> = Vec::new();
let mut seen: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
for path in files.iter() {
let path = self
.store
.index()
.summary(path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.clone());
if seen.insert(path.clone()) {
candidates.push(path);
}
}
let mut units: Vec<std::sync::Arc<crate::TranslationUnit>> = Vec::new();
let held: Vec<std::sync::Arc<crate::TranslationUnit>> = {
let table = self.held_units();
table
.held
.values()
.filter(|held| !held.provisional)
.map(|held| std::sync::Arc::clone(&held.unit))
.collect()
};
let mut still_to_walk: Vec<PathBuf> = Vec::new();
for candidate in candidates {
if let Some(unit) = held
.iter()
.find(|unit| unit.environment_of(&candidate).is_some())
{
if !units
.iter()
.any(|known| std::sync::Arc::ptr_eq(known, unit))
{
units.push(std::sync::Arc::clone(unit));
}
continue;
}
still_to_walk.push(candidate);
}
for (candidate, unit) in self.walk_the_timelines(still_to_walk) {
if units
.iter()
.any(|unit| unit.environment_of(&candidate).is_some())
{
continue;
}
if let Some(unit) = unit {
units.push(unit);
}
}
units
}
#[allow(dead_code)]
fn walk_the_timelines(
&self,
roots: Vec<PathBuf>,
) -> Vec<(PathBuf, Option<std::sync::Arc<crate::TranslationUnit>>)> {
roots
.into_iter()
.map(|root| {
let unit = self.translation_unit_of(&root);
(root, unit)
})
.collect()
}
fn index_one(
&mut self,
path: PathBuf,
priority: Priority,
depth: usize,
ready: Option<crate::index::store::Prepared>,
) -> Step {
{
let loading = std::time::Instant::now();
self.vfs.load(&path);
if crate::probes::units() {
eprintln!(
"[step] load {} took {} us",
path.display(),
loading.elapsed().as_micros()
);
}
}
let before = self.store.stats();
let spelled = self
.store
.index()
.summary(&path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.clone());
let prepared_with_environment = ready
.as_ref()
.is_some_and(crate::index::store::Prepared::with_environment);
let unit = if ready.is_none() {
self.the_timeline_to_read_through(&spelled)
} else {
None
};
let timed = std::time::Instant::now();
let timeline_ask = std::time::Instant::now();
if crate::probes::units() {
eprintln!(
"[step] timeline question for {} took {} ms (timeline {})",
path.display(),
timeline_ask.elapsed().as_millis(),
unit.is_some()
);
}
let has_a_timeline = unit
.as_ref()
.is_some_and(|unit| unit.environment_of(&spelled).is_some());
let read_here = ready.is_none();
let summary = match ready {
Some(ready) => self.store.commit(&path, ready),
None => match &unit {
Some(unit) => {
let prepared = self.store.prepare_with_the_timeline(&path, unit);
self.store.commit(&path, prepared)
}
None => self.store.get(&path),
},
};
if crate::probes::units() {
eprintln!(
"[step] {} read in {} ms after a {} ms timeline question (timeline {})",
path.display(),
timed.elapsed().as_millis(),
timeline_ask.elapsed().as_millis(),
unit.is_some()
);
}
let tail = std::time::Instant::now();
let includes: Vec<PathBuf> = summary
.map(|summary| {
summary
.includes
.iter()
.filter_map(|include| include.resolved.clone())
.collect()
})
.unwrap_or_default();
let after = self.store.stats();
let outcome = outcome_of(before, after);
if crate::probes::units() {
eprintln!(
"[step] {} includes+stats took {} us ({} include(s))",
path.display(),
tail.elapsed().as_micros(),
includes.len()
);
}
let queueing = std::time::Instant::now();
let include_count = includes.len();
for include in includes {
self.queue.add(include, priority, depth + 1);
}
if crate::probes::units() {
eprintln!(
"[step] {} queued {include_count} include(s) in {} us",
path.display(),
queueing.elapsed().as_micros()
);
}
Step {
path,
priority,
depth,
outcome,
with_the_environment: prepared_with_environment
|| (read_here
&& has_a_timeline
&& matches!(outcome, StepOutcome::Built | StepOutcome::Unstored)),
}
}
pub fn catch_up(&mut self, path: &Path) {
self.want_refinement(path);
if self.queue.forget(path) {
self.index_one(path.to_path_buf(), Priority::Open, 0, None);
}
const INCLUDES_A_REQUEST_READS: usize = 8;
let mut includes = self.unread_includes_of(path);
if includes.len() > INCLUDES_A_REQUEST_READS {
for include in includes.split_off(INCLUDES_A_REQUEST_READS) {
self.queue.add(include, Priority::Open, 1);
}
}
for include in includes {
self.queue.forget(&include);
self.index_one(include, Priority::Open, 1, None);
}
}
fn unread_includes_of(&self, path: &Path) -> Vec<PathBuf> {
let Some(summary) = self.store.index().summary(path) else {
return Vec::new();
};
summary
.includes
.iter()
.filter_map(|include| include.resolved.as_ref())
.filter(|resolved| self.store.index().summary(resolved).is_none())
.cloned()
.collect()
}
pub fn needs_catching_up(&self, path: &Path) -> bool {
self.queue.is_queued(path) || !self.unread_includes_of(path).is_empty()
}
pub fn is_closure_ready(&self, path: &Path) -> bool {
if !self.store.index().summary(path).is_some() {
return false;
}
self.store
.index()
.visible_files(path)
.iter()
.all(|(file, visibility)| {
*visibility != crate::IncludeVisibility::Unconditional
|| self.store.index().summary(Path::new(file)).is_some()
})
}
pub fn is_locally_ready(&self, path: &Path) -> bool {
self.is_indexed(path) && !self.needs_catching_up(path)
}
pub fn begin_the_cache_generation(&mut self) {
self.store.begin_generation();
}
pub fn index_everything(&mut self) -> usize {
let mut total = 0;
let tracing = crate::probes::cooking();
loop {
let round_started = std::time::Instant::now();
let steps = self.advance(64);
let advanced = round_started.elapsed();
let _ = &steps;
let refined = round_started.elapsed() - advanced;
let read = steps.len();
total += read;
let cook_started = std::time::Instant::now();
let cooked = {
let claimed = self.claim_cooking(COOK_SLICE);
if tracing && !claimed.is_empty() {
eprintln!("cooking: claimed {} file(s) {:?}", claimed.len(), claimed);
}
let materials = self.materials_for(claimed);
let wanted = materials.len();
let rendered = self.render_them(materials);
let built = rendered.len();
let committed = self.commit_them(rendered).len();
if tracing && (built != wanted || committed != built) {
eprintln!(
"cooking: rendered {built} of {wanted} claimed, committed {committed} \
(cooking backlog {})",
self.pending_cooking()
);
}
committed
};
let cook = cook_started.elapsed();
if crate::probes::units() {
eprintln!(
"DRAIN round: advance {advanced:?} ({read} file(s)), refine {refined:?}, cook {cook:?} ({cooked} committed), round total {:?}",
round_started.elapsed(),
);
}
if read == 0 && cooked == 0 {
if tracing {
let cached: Vec<String> = self
.renderings
.lock()
.map(|known| {
known
.keys()
.map(|(path, hash)| format!("{}#{hash}", path.display()))
.collect()
})
.unwrap_or_default();
eprintln!(
"cooking: stopping with read 0, cooked 0, backlog {} | cache {cached:?}",
self.pending_cooking()
);
}
if let Some(collected) = self.collect_the_cache() {
log::debug!(
"cache: collected {} summary(ies), {} rendering(s), {} projection(s)",
collected.summaries,
collected.renderings,
collected.projections
);
}
let forgotten = self.enforce_the_summary_budget(SUMMARY_BUDGET_BYTES);
if forgotten > 0 {
log::debug!("index: forgot {forgotten} summary(ies) over the byte budget");
}
return total;
}
}
}
pub fn pending(&self) -> usize {
self.queue.pending()
}
pub fn pending_work(&self) -> usize {
self.queue.pending() + self.cooking.len() + self.refinements_wanted()
}
pub fn queue_sizes(&self) -> (usize, usize, usize) {
(
self.queue.pending(),
self.queue.open.len(),
self.queue.rest.len(),
)
}
pub fn is_idle(&self) -> bool {
self.queue.pending() == 0
}
pub fn pending_cooking(&self) -> usize {
self.cooking.len()
}
pub fn re_read_backlog(&self) -> usize {
self.parsed_since_the_last_pass.len()
}
pub fn refinements_wanted(&self) -> usize {
self.refined_on_request
.lock()
.map(|wanted| wanted.len())
.unwrap_or(0)
}
pub fn want_refinement(&self, path: &Path) {
if self.is_refined(path) {
return;
}
if let Ok(mut wanted) = self.refined_on_request.lock() {
wanted.insert(path.to_path_buf());
}
}
fn take_the_files_wanted_for_refinement(&mut self, at_most: usize) -> Vec<PathBuf> {
let mut taken = Vec::new();
let Ok(mut wanted) = self.refined_on_request.lock() else {
return taken;
};
let mut not_yet: Vec<PathBuf> = Vec::new();
while taken.len() < at_most {
let Some(path) = wanted.iter().next().cloned() else {
break;
};
wanted.remove(&path);
if self.store.index().summary(&path).is_none() {
not_yet.push(path);
continue;
}
if let Some(at) = self
.parsed_since_the_last_pass
.iter()
.position(|queued| queued == &path)
{
self.parsed_since_the_last_pass.swap_remove(at);
}
taken.push(path);
}
for path in not_yet {
wanted.insert(path);
}
taken
}
pub fn is_owed_a_re_read(&self, path: &Path) -> bool {
let key = crate::file::paths::normalize_path(path, cfg!(windows));
self.parsed_since_the_last_pass
.iter()
.any(|queued| crate::file::paths::normalize_path(queued, cfg!(windows)) == key)
}
pub fn its_closure_is_owed(&self, path: &Path) -> bool {
if self.parsed_since_the_last_pass.is_empty() {
return false;
}
if self.is_owed_a_re_read(path) {
return true;
}
self.store
.index()
.visible_files(path)
.iter()
.any(|(file, _)| self.is_owed_a_re_read(Path::new(file)))
}
pub fn abandon_the_cooking_queue(&mut self) -> usize {
self.cooking.abandon()
}
pub fn save_the_sharing(&self) -> std::io::Result<usize> {
if self.store.database().is_none() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"this project has no summary database to write the sharing cache into",
));
}
Ok(self.store.save_the_contributions())
}
pub fn load_the_sharing(&self) -> usize {
self.store.load_the_contributions()
}
pub fn is_indexed(&self, path: impl AsRef<Path>) -> bool {
self.store.index().summary(path.as_ref()).is_some()
}
pub fn tokens_of(&self, path: impl AsRef<Path>) -> Option<TokensOf> {
let file = self.vfs.held(path)?;
Some(TokensOf {
path: file.path.clone(),
source: file.text.clone(),
line_index: file.line_index.clone(),
open: file.open,
tokens: cpp_parser::lex(&file.text, &cpp_parser::LexerConfig::default()).0,
})
}
pub fn has_rendering(&self, path: impl AsRef<Path>) -> bool {
let requested = path.as_ref();
let key = self
.store
.index()
.summary(requested)
.map(|summary| summary.path.as_path())
.unwrap_or(requested);
self.vfs
.held(key)
.is_some_and(|file| self.known_rendering_of(&file.path, &file.text).is_some())
}
pub fn view(&self, path: impl AsRef<Path>) -> Option<FileView> {
let requested = path.as_ref();
let key = self
.store
.index()
.summary(requested)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| requested.to_path_buf());
if crate::probes::view() {
eprintln!(
"view: asked {:?} key {:?} summary {} held {}",
requested,
key,
self.store.index().summary(requested).is_some(),
self.vfs.held(&key).is_some()
);
}
let file = self.vfs.held(&key)?;
if crate::probes::view() {
eprintln!(
"view: file.path {:?} hash {:?}",
file.path,
crate::cache::content_hash(&file.text)
);
}
match self.known_rendering_of(&file.path, &file.text) {
Some(rendered) => {
let config = self.config_for_file(&file.path);
let key = ViewKey {
path: file.path.clone(),
rendering: Some(RenderingId(rendered.clone())),
text: 0,
id: file.id,
cuda: config.cuda,
language: Some(config.language),
open: file.open,
environment: None,
};
if let Some(held) = self.a_view_already_parsed(&key) {
if crate::probes::view() {
eprintln!("view: {} came from the cache", file.path.display());
}
return Some(held);
}
let view = FileView::parse_rendering_in(file, &rendered, &config.config);
self.remember_a_view(key, &view);
Some(view)
}
None => {
if crate::probes::cooking() {
eprintln!(
"view: no rendering for {} | hash {} | {} entr(ies) cached | open {}",
file.path.display(),
crate::cache::content_hash(&file.text),
self.renderings.lock().map(|known| known.len()).unwrap_or(0),
file.open
);
}
if let Ok(mut work) = self.macro_work.lock() {
work.want(&file.path);
}
None
}
}
}
pub fn view_of_the_file(&self, path: impl AsRef<Path>) -> Option<FileView> {
let requested = path.as_ref();
let file = self.vfs.held(requested)?;
if self.known_rendering_of(&file.path, &file.text).is_none()
&& let Ok(mut work) = self.macro_work.lock()
{
work.want(&file.path);
}
let known = self.known_macros_of(&file.path, &file.text);
let key = buffer_view_key(file, self.config.cuda(), known.clone());
if let Some(held) = self.a_view_already_parsed(&key) {
return Some(held);
}
let macros = known.unwrap_or_default();
let view = FileView::parse_with_cuda(file, macros.as_ref(), self.config.cuda());
self.remember_a_view(key, &view);
Some(view)
}
pub fn view_of_the_rendering(&mut self, path: impl AsRef<Path>) -> Option<FileView> {
let file = self.vfs.held(path)?.clone();
let rendered = self.rendering_of(&file.path)?;
self.known_rendering_of(&file.path, &file.text);
Some(FileView::parse_rendering_in(
&file,
&rendered,
&self.config_for(&file.path),
))
}
fn known_rendering_of(
&self,
path: &Path,
text: &str,
) -> Option<std::sync::Arc<crate::preprocess::cooked::RenderedCooked>> {
let key = (path.to_path_buf(), crate::cache::content_hash(text));
self.renderings.lock().ok()?.get(&key).cloned()
}
fn known_macros_of(
&self,
path: &Path,
text: &str,
) -> Option<std::sync::Arc<cpp_parser::MacroEnvironment>> {
let key = (path.to_path_buf(), crate::cache::content_hash(text));
self.macro_environments.lock().ok()?.get(&key).cloned()
}
fn the_macros_of(
&self,
path: &Path,
text: &str,
) -> Option<std::sync::Arc<cpp_parser::MacroEnvironment>> {
if let Some(known) = self.known_macros_of(path, text) {
return Some(known);
}
let summary = self.store.index().summary(path)?;
let (closure, by_key) = self.closure_with_text(path);
let mut definitions = crate::summary::MacroDefinitions::default();
let evidence = crate::summary::macros_from_the_closure_with_bodies(
summary,
|wanted| {
let key = normalize_path(wanted, cfg!(windows));
let (defined_in, text) = closure.get(*by_key.get(&key)?)?;
Some((self.store.index().summary(defined_in)?, text.as_str()))
},
self.store.index().macros(),
&mut definitions,
);
let made = std::sync::Arc::new(
cpp_parser::MacroEnvironment::from_included_macros(evidence.macros)
.with_bodies_in_force(evidence.conditional_bodies),
);
let weight = cpp_parser::MacroFacts::len(made.as_ref());
if let Ok(mut known) = self.macro_environments.lock() {
known.insert(
(path.to_path_buf(), crate::cache::content_hash(text)),
made.clone(),
weight,
);
}
Some(made)
}
pub fn notes_about_the_modules(&self, view: &crate::FileView) -> Vec<ModuleNote> {
let info = crate::ModuleInfo::from_tree(&view.root);
let own_module = info.module_name.clone();
let mut notes = Vec::new();
for declaration in &info.imports {
let start = view
.file_offset_of(ReadingOffset::new(declaration.range.start_offset))
.map_or(declaration.range.start_offset, |at| at.get());
let end = view
.file_offset_of(ReadingOffset::new(declaration.range.end_offset()))
.map_or(declaration.range.end_offset(), |at| at.get());
let range = (start, end);
if let Some(partition) = declaration.target.partition_name() {
let Some(module) = own_module.as_deref() else {
continue;
};
if self.the_partition_is_read(module, partition) {
continue;
}
notes.push(ModuleNote {
message: match &self.toolchain {
Some(toolchain) => toolchain.partition_note(module, partition),
None => format!(
"no file this project contains declares partition `:{partition}` of module `{module}` \
— a partition is not a module, and until its file is read the names it exports are \
unknown rather than absent"
),
},
start: range.0,
end: range.1,
});
continue;
}
let Some(module) = declaration.target.module_name() else {
continue;
};
if self.store.index().interface_unit_of(module).is_some() {
continue;
}
let message = match &self.toolchain {
Some(toolchain) => toolchain.module_note(module),
None => format!(
"no file this project contains declares module `{module}` — a module outside the project is \
one only the compiler can see, and until then the names it exports are unknown rather than \
absent"
),
};
notes.push(ModuleNote {
message,
start: range.0,
end: range.1,
});
}
notes
}
fn the_partition_is_read(&self, module: &str, partition: &str) -> bool {
self.store.index().summaries().any(|summary| {
summary.modules.module.as_deref() == Some(module)
&& summary.modules.partition.as_deref() == Some(partition)
&& summary.modules.is_interface
})
}
pub fn diagnostics(&self, path: impl AsRef<Path>) -> Option<FileDiagnostics> {
let path = path.as_ref();
let _whole = DiagTrace::new("diagnostics");
let key = self
.store
.index()
.summary(path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.to_path_buf());
let mut notes = Vec::new();
if let Some(cooked) = self.store.index().cooked_reading(&key) {
let view = {
let _t = DiagTrace::new("the file's tree (view)");
self.view(path)
};
if let Some(view) = &view {
let _t = DiagTrace::new("module notes");
notes = if self.pending_work() == 0 {
self.notes_about_the_modules(view)
} else {
Vec::new()
};
}
let trusted = self.is_closure_ready(&key);
let checks = match (&view, trusted) {
(Some(view), true) => self.checks_about(&key, view),
_ => Vec::new(),
};
return Some(FileDiagnostics {
reading: DiagnosticReading::Cooked,
unplaced: cooked.unplaced,
errors: match view.as_ref().and_then(Self::a_fragment_and_its_message) {
Some((start, end, message)) => vec![FileDiagnostic {
start,
end,
message,
code: crate::sema::check::DiagnosticCode::SyntaxError,
}],
None => cooked
.diagnostics
.iter()
.map(|error| FileDiagnostic {
start: error.range.start_offset,
end: error.range.end_offset(),
message: error.message.clone(),
code: crate::sema::check::DiagnosticCode::SyntaxError,
})
.collect(),
},
notes,
checks,
checks_withheld: !trusted,
});
}
None
}
fn a_fragment_and_its_message(view: &crate::FileView) -> Option<(usize, usize, String)> {
let written = view.written_text().unwrap_or(&view.source);
let (start, end, message) = Self::error_directive_line(written)?;
let first = view
.root
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.map(|token| cpp_parser::CppTokenKind::from(token.kind()))
.find(|kind| {
!matches!(
kind,
cpp_parser::CppTokenKind::Whitespace | cpp_parser::CppTokenKind::Newline
)
})?;
if !matches!(
first,
cpp_parser::CppTokenKind::LeftBrace | cpp_parser::CppTokenKind::RightBrace
) {
return None;
}
Some((start, end, message))
}
fn error_directive_line(text: &str) -> Option<(usize, usize, String)> {
let mut at = 0;
for line in text.split_inclusive('\n') {
let trimmed = line.trim_start();
let indent = line.len() - trimmed.len();
if let Some(rest) = trimmed.strip_prefix('#') {
let rest = rest.trim_start();
if let Some(message) = rest.strip_prefix("error") {
let message = message.trim().trim_matches('"').trim();
return Some((at + indent, at + line.trim_end().len(), message.to_string()));
}
}
at += line.len();
}
None
}
fn checks_about(&self, key: &Path, view: &crate::FileView) -> Vec<crate::sema::check::Finding> {
let index = self.store.index();
let Some(summary) = index.summary(key) else {
return Vec::new();
};
let state = self.the_state_of_a_pull(key, summary);
if let Ok(memo) = self.checks_memo.lock()
&& let Some((remembered, findings)) = memo.as_ref()
&& *remembered == state
{
self.pull_hits
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return findings.clone();
}
self.pull_runs
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let model = self.semantic_model(view);
let config = crate::sema::check::CheckConfig::from_names(
&self.project_config().config.diagnostics.enable,
&self.project_config().config.diagnostics.disable,
);
for name in config.unknown() {
log::warn!("`[diagnostics]` names a rule this build does not have: `{name}`");
}
let inside = {
let mut inside: Vec<std::path::PathBuf> = vec![self.root.clone()];
inside.extend(
self.project
.iter()
.filter_map(|file| file.parent().map(std::path::Path::to_path_buf)),
);
inside.extend(
self.project_config()
.config
.libraries
.iter()
.map(|library| library.root(&self.root)),
);
inside.sort();
inside.dedup();
inside
};
let findings = crate::sema::check::run(crate::sema::check::CheckContext::new(
key,
summary,
model,
&view.source,
view.written_text().unwrap_or(&view.source),
view.tree.get_errors(),
view,
config,
self.config_for(key).language_for(key),
self.project_config()
.config
.unreal
.as_ref()
.is_some_and(|unreal| unreal.is_on()),
&inside,
));
if let Ok(mut memo) = self.checks_memo.lock() {
*memo = Some((state, findings.clone()));
}
findings
}
fn the_state_of_a_pull(&self, key: &Path, summary: &crate::FileSummary) -> u64 {
let mut bytes: Vec<u8> = Vec::new();
bytes.extend_from_slice(format!("{:?}", summary.key).as_bytes());
match self.store.the_closure_fingerprint(key) {
Some(closure) => bytes.extend_from_slice(&closure.to_le_bytes()),
None => bytes.extend_from_slice(&self.store.revision().to_le_bytes()),
}
bytes.extend_from_slice(&(self.store.index().len() as u64).to_le_bytes());
let config = self.project_config();
for rule in config
.config
.diagnostics
.enable
.iter()
.chain(config.config.diagnostics.disable.iter())
{
bytes.extend_from_slice(rule.as_bytes());
bytes.push(0x1f);
}
bytes.extend_from_slice(format!("{:?}", self.config_for(key).language_for(key)).as_bytes());
bytes.push(u8::from(
config
.config
.unreal
.as_ref()
.is_some_and(|unreal| unreal.is_on()),
));
crate::cache::fnv1a64(&bytes)
}
pub fn warm_the_pulls(&self) -> usize {
let mut warmed = 0;
for path in self.documents.paths().into_iter().take(2) {
if self.diagnostics(&path).is_some() {
warmed += 1;
}
}
warmed
}
pub fn semantic_model<'s>(
&'s self,
view: &'s FileView,
) -> crate::sema::model::SemanticModel<'s> {
let session: &'s Session<F> = self;
let index: &'s crate::ProjectIndex = session.store.index();
let seen: std::cell::RefCell<std::collections::HashMap<PathBuf, Option<FileView>>> =
std::cell::RefCell::new(std::collections::HashMap::new());
let resolver = Box::new(move |path: &Path| -> Option<FileView> {
if let Some(known) = seen.borrow().get(path) {
return known.clone();
}
let started = std::time::Instant::now();
let made = session.view_of_the_file(path);
if crate::probes::timing() {
eprintln!(
"[resolver] {:>5} ms {} ({})",
started.elapsed().as_millis(),
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| path.display().to_string()),
if made.is_some() { "read" } else { "none" }
);
}
seen.borrow_mut().insert(path.to_path_buf(), made.clone());
made
});
crate::sema::model::SemanticModel::new(
index,
&view.root,
&view.scopes,
&view.source,
&view.path,
resolver,
)
}
pub fn text(&self, path: impl AsRef<Path>) -> Option<String> {
Some(self.vfs.held(path)?.text.to_string())
}
pub fn load(&mut self, path: impl AsRef<Path>) -> Option<crate::FileId> {
self.vfs.load(path)
}
pub fn files(&self) -> &Vfs<SessionFiles<F>> {
&self.vfs
}
fn translation_unit_of(&self, path: &Path) -> Option<std::sync::Arc<crate::TranslationUnit>> {
self.unit_and_state_of(path).map(|(unit, _)| unit)
}
fn unit_and_state_of(
&self,
path: &Path,
) -> Option<(std::sync::Arc<crate::TranslationUnit>, bool)> {
let key = queue_key(path);
let held = self.held_units().get(&key);
if let Some((provisional, unit)) = held {
if !provisional || self.queue.pending() > 0 {
self.note_a_unit(Which::FromMemory);
return Some((unit, provisional));
}
self.held_units().remove(&key);
}
let context = self.store.context_hash(path);
let cached: Option<crate::TranslationUnit> = None;
let (unit, provisional) = match cached {
Some(unit) => {
self.note_a_unit(Which::FromDisk);
(std::sync::Arc::new(unit), false)
}
None => {
let owned_root;
let index = self.store.index();
let root = match index.summary(path) {
Some(summary) => summary,
None if self.documents.is_open(path) => {
owned_root = self.store.directive_summary(path)?;
self.the_closure_of_the_directives(path, &owned_root)?;
owned_root.as_ref()
}
None => return None,
};
let read_started = std::time::Instant::now();
let (closure, closure_by_key) = { self.closure_with_text_from(path, Some(root)) };
let read_took = read_started.elapsed();
let closure_bytes: usize = closure.iter().map(|(_, text)| text.len()).sum();
let walk_started = std::time::Instant::now();
#[cfg(feature = "perf-probe")]
crate::perf::mark("closure");
let mut definitions = self.held_definitions();
#[cfg(feature = "perf-probe")]
crate::perf::mark("definitions");
let identity = self.closure_identity(&closure, context);
let shared = self
.walked_by_content
.lock()
.ok()
.and_then(|mut held| held.get(&identity).cloned());
let (unit, provisional) = if let Some(shared) = shared {
#[cfg(feature = "perf-probe")]
crate::perf::mark("walk");
self.note_a_unit(Which::FromMemory);
(shared, false)
} else {
let provisional = self.closure_is_still_growing(&closure, &closure_by_key);
let walked = std::sync::Arc::new({
let root_key = queue_key(path);
crate::TranslationUnit::walk(
root,
|wanted| {
let (held, text) =
closure.get(*closure_by_key.get(&queue_key(wanted))?)?;
let summary = if queue_key(held) == root_key {
root
} else {
index.summary(held)?
};
Some((summary, text.as_str()))
},
index.macros(),
&mut definitions,
!provisional && self.share_contributions,
self.store.contributions(),
)
});
let walk_took = walk_started.elapsed();
let mut put_took = std::time::Duration::ZERO;
if !provisional {
let put_started = std::time::Instant::now();
put_took = put_started.elapsed();
let weight = walked.heap_bytes();
if let Ok(mut held) = self.walked_by_content.lock() {
held.insert(identity, std::sync::Arc::clone(&walked), weight);
}
}
self.note_a_unit(Which::Walked);
if let Ok(mut walks) = self.walks_by_root.lock() {
*walks.entry(queue_key(path)).or_insert(0) += 1;
}
if crate::probes::timing() {
eprintln!(
"[unit] read {:>4} ms, walk {:>4} ms, put {:>4} ms, {} file(s) / {} bytes {}",
read_took.as_millis(),
walk_took.as_millis(),
put_took.as_millis(),
closure.len(),
closure_bytes,
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| path.display().to_string())
);
}
(walked, provisional)
};
(unit, provisional)
}
};
self.held_units()
.insert(key, std::sync::Arc::clone(&unit), provisional);
Some((unit, provisional))
}
fn holds_an_environment_for(&self, path: &Path) -> bool {
let spelled = self
.store
.index()
.summary(path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.to_path_buf());
let held = self.held_units().get(&queue_key(&spelled));
held.is_some_and(|(provisional, _)| !provisional)
}
fn will_read_it_as_the_program(&self, path: &Path) -> bool {
if !self.documents.is_open(path) {
return false;
}
self.holds_an_environment_for(path) || self.a_timeline_can_be_walked_for(path)
}
fn a_timeline_can_be_walked_for(&self, path: &Path) -> bool {
if !self.documents.is_open(path) {
return false;
}
match self.store.index().summary(path) {
Some(_) => self.is_closure_ready(path),
None => self
.store
.directive_summary(path)
.is_some_and(|directives| {
self.the_closure_of_the_directives(path, &directives)
.is_some()
}),
}
}
fn the_timeline_to_read_through(
&self,
path: &Path,
) -> Option<std::sync::Arc<crate::TranslationUnit>> {
if let Some((false, unit)) = self.held_units().get(&queue_key(path))
&& unit.environment_of(path).is_some()
{
return Some(unit);
}
if !self.a_timeline_can_be_walked_for(path) {
return None;
}
let (unit, provisional) = self.unit_and_state_of(path)?;
(!provisional && unit.environment_of(path).is_some()).then_some(unit)
}
fn closure_is_still_growing(
&self,
closure: &[(PathBuf, String)],
in_closure: &HashMap<String, usize>,
) -> bool {
let index = self.store.index();
closure.iter().any(|(path, _)| {
index.summary(path).is_some_and(|summary| {
summary.includes.iter().any(|include| {
include.resolved.as_ref().is_some_and(|target| {
!in_closure.contains_key(&queue_key(target)) && self.queue.is_queued(target)
})
})
})
})
}
fn note_a_unit(&self, which: Which) {
use std::sync::atomic::Ordering::Relaxed;
let counter = match which {
Which::FromMemory => &self.unit_from_memory,
Which::FromDisk => &self.unit_from_disk,
Which::Walked => &self.unit_walked,
};
counter.fetch_add(1, Relaxed);
}
pub fn unit_stats(&self) -> UnitStats {
use std::sync::atomic::Ordering::Relaxed;
UnitStats {
from_memory: self.unit_from_memory.load(Relaxed),
from_disk: self.unit_from_disk.load(Relaxed),
walked: self.unit_walked.load(Relaxed),
}
}
pub fn walks_by_root(&self) -> Vec<(PathBuf, u64)> {
let Ok(walks) = self.walks_by_root.lock() else {
return Vec::new();
};
let mut walked: Vec<(PathBuf, u64)> = walks
.iter()
.map(|(path, count)| (PathBuf::from(path), *count))
.collect();
walked.sort_by(|left, right| right.1.cmp(&left.1).then_with(|| left.0.cmp(&right.0)));
walked
}
fn closure_identity(&self, closure: &[(PathBuf, String)], context: u64) -> (u64, u64) {
let mut contents: Vec<u64> = closure
.iter()
.map(|(_, text)| crate::cache::content_hash(text))
.collect();
contents.sort_unstable();
let mut identity: u64 = 0xcbf2_9ce4_8422_2325;
for hash in &contents {
identity ^= *hash;
identity = identity.wrapping_mul(0x0000_0100_0000_01b3);
}
identity ^= (contents.len() as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15);
(context, identity)
}
fn held_units(&self) -> std::sync::MutexGuard<'_, UnitTable> {
self.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn held_definitions(&self) -> std::sync::MutexGuard<'_, crate::MacroDefinitions> {
self.definitions
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
pub fn effect_walk(
&self,
root: &Path,
) -> Option<(HashMap<PathBuf, u64>, crate::effect::Walked)> {
self.store.index().summary(root)?;
let reads = SessionReads {
files: &self.files,
index: self.store.index(),
};
let mut cache = self
.effects
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if cache.len() >= cache::cache_lru::EFFECT_ENTRIES {
cache.clear();
}
let seed = crate::effect::Macros::new();
let surfaces = self
.surfaces
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let (signatures, walked) = crate::effect::Chain::new(&reads, &mut cache)
.with_surfaces(&surfaces)
.at_signatures(root, &seed);
Some((signatures, walked))
}
pub fn render_the_unit(&self, root: &Path) -> Option<crate::RenderedUnit> {
let unit = self.translation_unit_of(root)?;
let (sources, key) = self.the_units_inputs(&unit);
if let Ok(known) = self.rendered_unit.lock()
&& let Some((seen, cached)) = known.as_ref()
&& *seen == key
{
return Some(cached.clone());
}
if let Some(cached) = self.store.cached_unit(key) {
if crate::probes::units() {
eprintln!("unit render: key {key:016x} served from the cache");
}
if let Ok(mut known) = self.rendered_unit.lock() {
*known = Some((key, cached.clone()));
}
return Some(cached);
}
if crate::probes::units() {
eprintln!(
"unit render: key {key:016x} MISSED ({} source(s) to cook)",
sources.len()
);
}
let definitions = unit.definitions();
let seed = MacroTable::from_marked(self.store.index().macros());
let search = crate::preprocess::cooked::Search::new(&self.files, &self.config);
let stream =
{ unit.cook_the_unit(&sources, &definitions, Some(&seed), true, Some(&search)) };
self.store.remember_unit(key, &stream);
if let Ok(mut known) = self.rendered_unit.lock() {
*known = Some((key, stream.clone()));
}
Some(stream)
}
fn the_rendering_still_describes(&self, rendered: &crate::RenderedUnit, stored: u64) -> bool {
let mut inputs: Vec<(&std::path::Path, u64)> = Vec::new();
for path in &rendered.walked {
let Some(text) = self.files.read(path) else {
return false;
};
inputs.push((path.as_path(), crate::cache::content_hash(&text)));
}
inputs.sort_unstable_by(|left, right| left.0.cmp(right.0));
let mut folded = String::new();
for (path, hash) in &inputs {
folded.push_str(&path.to_string_lossy());
folded.push('|');
folded.push_str(&hash.to_string());
folded.push(';');
}
crate::cache::content_hash(&folded) == stored
}
fn the_units_inputs(&self, unit: &crate::TranslationUnit) -> (HashMap<PathBuf, String>, u64) {
let mut sources: HashMap<PathBuf, String> = HashMap::new();
for path in unit.files() {
if let Some(text) = self.files.read(path) {
sources.insert(path.to_path_buf(), text);
}
}
let mut inputs: Vec<(&std::path::Path, u64)> = sources
.iter()
.map(|(path, text)| (path.as_path(), crate::cache::content_hash(text)))
.collect();
inputs.sort_unstable_by(|left, right| left.0.cmp(right.0));
let key = {
let mut folded = String::new();
for (path, hash) in &inputs {
folded.push_str(&path.to_string_lossy());
folded.push('|');
folded.push_str(&hash.to_string());
folded.push(';');
}
crate::cache::content_hash(&folded)
};
(sources, key)
}
pub fn gather_a_unit_read(&self, root: &Path) -> Option<UnitReadMaterials<SessionFiles<F>>> {
let started = std::time::Instant::now();
let revision = self.revision();
#[cfg(feature = "perf-probe")]
crate::perf::mark("read start");
let root = self.store.index().summary(root)?.path.clone();
let context = self.store.context_hash(&root);
if let Some(units_key) = self.the_units_key_of(&root, context)
&& let Some(rendered) = self
.rendered_unit
.lock()
.ok()
.and_then(|known| {
known
.as_ref()
.filter(|(seen, _)| *seen == units_key)
.map(|(_, cached)| cached.clone())
})
.or_else(|| self.store.cached_unit(units_key))
&& self.the_rendering_still_describes(&rendered, units_key)
{
if crate::probes::units() {
eprintln!(
"unit gather: key {units_key:016x} served without the walk in {:?} ({} walked, {} token file(s))",
started.elapsed(),
rendered.walked.len(),
rendered.files.len(),
);
}
let program = crate::cache::content_hash(&rendered.text);
let known = self
.unit_index
.lock()
.ok()
.and_then(|memo| {
memo.as_ref()
.filter(|(seen, _)| *seen == program)
.map(|(_, cached)| cached.clone())
})
.or_else(|| self.store.cached_indexed_unit(program));
return Some(UnitReadMaterials {
root,
revision,
key: SummaryKey::new(0, context),
units_key,
unit: None,
sources: HashMap::new(),
macros: self.store.index().macros().clone(),
config: self.config.clone(),
files: self.files.clone(),
database: self.store.database(),
program: Some(program),
rendered: Some(rendered),
known,
});
}
let unit = self.translation_unit_of(&root)?;
let walked = started.elapsed();
let (sources, units_key) = self.the_units_inputs(&unit);
let hashed = started.elapsed() - walked;
let key = SummaryKey::new(0, context);
let rendered = self
.rendered_unit
.lock()
.ok()
.and_then(|known| {
known
.as_ref()
.filter(|(seen, _)| *seen == units_key)
.map(|(_, cached)| cached.clone())
})
.or_else(|| self.store.cached_unit(units_key));
if crate::probes::units() {
eprintln!(
"unit gather: key {units_key:016x} {} — walked {walked:?}, key {hashed:?} ({} file(s))",
if rendered.is_some() {
"served"
} else {
"MISSED"
},
unit.files().count(),
);
}
let (program, known) = match &rendered {
Some(rendered) => {
let program = crate::cache::content_hash(&rendered.text);
let known = self
.unit_index
.lock()
.ok()
.and_then(|memo| {
memo.as_ref()
.filter(|(seen, _)| *seen == program)
.map(|(_, cached)| cached.clone())
})
.or_else(|| self.store.cached_indexed_unit(program));
(Some(program), known)
}
None => (None, None),
};
Some(UnitReadMaterials {
root,
revision,
key,
units_key,
unit: Some(unit),
sources,
macros: self.store.index().macros().clone(),
config: self.config.clone(),
files: self.files.clone(),
database: self.store.database(),
program,
rendered,
known,
})
}
fn remember_the_walked_closure(&self, root: &Path, walked: &[PathBuf]) {
if walked.is_empty() {
return;
}
let Some(module) = self
.store
.index()
.summary(root)
.and_then(|summary| summary.modules.module.clone())
else {
return;
};
let files: Vec<String> = walked
.iter()
.map(|path| path.to_string_lossy().to_string())
.collect();
self.store.index().remember_the_unit_closure(&module, files);
}
pub fn prepare_a_unit_reading(&self, root: &Path) -> Option<PreparedUnitReading> {
let materials = self.gather_a_unit_read(root)?;
let spelling = self
.store
.index()
.summary(root)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| root.to_path_buf());
let context = self.store.context_hash(&spelling);
let units_key = materials.units_key();
if let Ok(mut known) = self.unit_roots.lock() {
known.insert((spelling.clone(), context), units_key);
}
self.store
.remember_unit_root(&format!("{context}:{}", spelling.display()), units_key);
materials.read_the_program()
}
fn the_units_key_of(&self, root: &Path, context: u64) -> Option<u64> {
if let Some(known) = self
.unit_roots
.lock()
.ok()
.and_then(|known| known.get(&(root.to_path_buf(), context)).copied())
{
return Some(known);
}
self.store
.cached_unit_root(&format!("{context}:{}", root.display()))
}
fn remember_the_unit_index(&self, program: u64, indexed: &crate::IndexedUnit) {
if let Ok(mut memo) = self.unit_index.lock() {
*memo = Some((program, indexed.clone()));
}
}
fn the_completion_budget(&self) -> usize {
self.project_config()
.config
.completion
.max_items
.unwrap_or(crate::completion::ITEM_BUDGET)
.clamp(1, MAX_COMPLETION_ITEMS)
}
pub fn commit_a_unit_reading(&mut self, prepared: PreparedUnitReading) -> Option<UnitReading> {
if !self.is_current(&prepared.revision) {
log::debug!("discarding a unit reading prepared for an older session revision");
return None;
}
#[cfg(feature = "perf-probe")]
crate::perf::mark("before the commit");
let PreparedUnitReading {
root,
program,
indexed,
walked,
revision: _,
} = prepared;
self.remember_the_walked_closure(&root, &walked);
self.remember_the_unit_index(program, &indexed);
let mut placed = 0usize;
if crate::probes::units() {
let names: Vec<String> = indexed
.files
.iter()
.map(|(path, _)| {
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_default()
})
.filter(|name| name == "format" || name == "string" || name == "vector")
.collect();
eprintln!(
"unit commit: {} file(s); interesting: {names:?}",
indexed.files.len()
);
}
for (path, cooked) in indexed.files {
placed += 1;
if crate::probes::units()
&& path.file_name().and_then(|name| name.to_str()) == Some("format")
{
let in_std = cooked
.declarations
.iter()
.filter(|fact| fact.name.as_str() == "format")
.map(|fact| fact.scope.clone().unwrap_or_default())
.take(4)
.collect::<Vec<_>>();
eprintln!(
"unit commit: <format> {} declaration(s); format in {in_std:?}",
cooked.declarations.len()
);
}
self.queue.mark_worked(&path);
self.store
.index_mut()
.insert_cooked_in_a_program(&path, program, cooked);
if let Ok(mut refined) = self.refined_readings.lock() {
refined.insert(queue_key(&path));
}
}
Some(UnitReading {
root,
files: placed,
declared: 0,
tokens: indexed.tokens,
files_with_tokens: indexed.files_with_tokens,
missing: indexed.missing,
unplaced: indexed.unplaced,
unbalanced: indexed.unbalanced,
braces: indexed.braces,
errors: indexed.errors,
})
}
pub fn read_the_unit(&mut self, root: &Path) -> Option<UnitReading> {
let gathering = std::time::Instant::now();
let prepared = self.prepare_a_unit_reading(root)?;
let gathered = gathering.elapsed();
let committing = std::time::Instant::now();
let reading = self.commit_a_unit_reading(prepared)?;
if crate::probes::units() {
eprintln!(
"unit read: prepare {gathered:?} (walk + cook + parse), commit {:?} ({} file(s) filed)",
committing.elapsed(),
reading.files
);
}
Some(reading)
}
fn a_unit_may_be_read_now(&self) -> bool {
self.last_edit.elapsed() >= UNTIL_A_UNIT_MAY_BE_READ
}
#[cfg(test)]
pub(crate) fn pretend_a_pause(&mut self, for_how_long: std::time::Duration) {
self.last_edit = std::time::Instant::now()
.checked_sub(for_how_long)
.unwrap_or_else(std::time::Instant::now);
}
pub fn the_wait_before_a_program_may_be_read(&self) -> Option<std::time::Duration> {
self.the_next_unit_to_read()
.map(|_| UNTIL_A_UNIT_MAY_BE_READ.saturating_sub(self.last_edit.elapsed()))
}
pub fn the_program_to_read(&self) -> Option<PathBuf> {
let open = self.the_next_unit_to_read()?;
self.a_unit_may_be_read_now().then_some(open)
}
fn the_next_unit_to_read(&self) -> Option<PathBuf> {
let mut wanted: Vec<PathBuf> = self
.programs_wanted
.iter()
.filter(|key| !self.units_read.contains(*key))
.map(PathBuf::from)
.collect();
wanted.sort();
if let Some(unit) = wanted.into_iter().next() {
return Some(unit);
}
let open = self.documents.paths().into_iter().find(|path| {
!self.units_read.contains(&queue_key(path))
&& !self.the_program_is_too_large_to_read_at_once(path)
&& self
.store
.index()
.summary(path)
.is_none_or(|summary| summary.modules.imports.is_empty())
});
if open.is_some() {
return open;
}
let mut wanted: Vec<PathBuf> = self
.programs_wanted
.iter()
.filter(|key| !self.units_read.contains(*key))
.map(PathBuf::from)
.collect();
wanted.sort();
wanted.into_iter().next()
}
fn the_program_is_too_large_to_read_at_once(&self, root: &Path) -> bool {
let bytes: usize = self
.closure_paths_from(root, None)
.iter()
.map(|path| self.text(path).map_or(0, |text| text.len()))
.sum();
bytes > THE_LARGEST_PROGRAM_READ_AT_ONCE
}
pub fn read_a_unit_of_the_project(&mut self) -> Option<UnitReading> {
let root = self
.project
.iter()
.find(|root| !self.units_read.contains(&queue_key(root)))
.cloned()?;
let reading = self.read_the_unit(&root);
self.units_read.insert(queue_key(&root));
reading
}
pub fn a_module_unit_is_waiting_for_its_program(&self) -> bool {
self.programs_wanted
.iter()
.any(|key| !self.units_read.contains(key.as_str()))
}
pub fn the_class_after_the_operator(
&self,
view: &crate::FileView,
written: &str,
arrow: bool,
) -> Option<String> {
let written = crate::sema::types::parse_type_spelling(written);
if arrow && let crate::sema::types::Type::Named { name, arguments } = &written {
let class = match name.contains("::") {
true => name.clone(),
false => crate::index::project::qualified_from_the_index(
self.store.index(),
&view.path,
name,
)
.unwrap_or_else(|| name.clone()),
};
if let Some((pointee, _)) = crate::index::project::the_pointee_of_a_handle(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
&class,
arguments,
) {
return Some(pointee);
}
}
crate::completion::the_class_after_the_operator(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
&written,
arrow,
)
.map(|(class, _)| class)
}
pub fn read_a_module_unit(&mut self) -> bool {
let Some(key) = self
.programs_wanted
.iter()
.find(|key| !self.units_read.contains(*key))
.cloned()
else {
return false;
};
let path = std::path::PathBuf::from(&key);
if self.store.index().summary(&path).is_none() {
self.load(&path);
}
self.want_the_closure_cooked(&path);
let mut rounds = 0;
while self.pending() > 0 && rounds < 2_000 {
self.advance(256);
rounds += 1;
}
let reading = self.read_the_unit(&path);
if reading.is_some() {
self.units_read.insert(key);
}
reading.is_some()
}
pub fn read_a_looked_at_unit(&mut self) -> Option<UnitReading> {
let root = self.the_next_unit_to_read()?;
if self.store.index().summary(&root).is_none() {
let ready = self.prefetched.remove(&queue_key(&root));
self.index_one(root.clone(), Priority::Open, 0, ready);
}
let reading = self.read_the_unit(&root);
let key = queue_key(&root);
let attempts = self.units_attempted.entry(key.clone()).or_insert(0);
*attempts += 1;
if reading.is_some() || *attempts >= 3 {
self.units_read.insert(key);
}
reading
}
pub fn queue_the_modules_the_open_files_import(&mut self) -> usize {
let mut queued = 0;
for path in self.documents.paths() {
let wanted = self.modules_the_index_cannot_name(&path);
if wanted.is_empty() {
continue;
}
for unit in self.module_interface_units_of(&wanted, &path) {
self.add_project_files([unit.clone()]);
self.programs_wanted.insert(queue_key(&unit));
self.want_cooked_reading(&unit);
queued += 1;
}
}
queued
}
pub fn queue_the_modules_a_file_imports(&mut self, path: &Path) -> usize {
if self.project_config().config.index.modules == Some(false) {
return 0;
}
let wanted = self.modules_the_index_cannot_name(path);
if wanted.is_empty() {
return 0;
}
let found = self.module_interface_units_of(&wanted, path);
for unit in &found {
self.add_project_files([unit.clone()]);
self.want_cooked_reading(unit);
self.programs_wanted.insert(queue_key(unit));
self.queue.again(unit.clone(), Priority::Open, 0);
}
found.len()
}
pub fn read_the_modules_a_file_imports(&mut self, path: &Path) -> usize {
let mut read_in = 0;
let tracing = crate::probes::units();
let started = std::time::Instant::now();
let mut last = started;
for _ in 0..MAX_MODULES_READ_AT_ONCE {
let wanted = self.modules_the_index_cannot_name(path);
if tracing {
eprintln!(
"module read: wanted {:?} after {:?}",
wanted,
last.elapsed()
);
last = std::time::Instant::now();
}
if wanted.is_empty() {
break;
}
let found = self.module_interface_units_of(&wanted, path);
if tracing {
eprintln!(
"module read: resolved {} file(s) {:?} after {:?}",
found.len(),
found,
last.elapsed()
);
last = std::time::Instant::now();
}
if found.is_empty() {
break;
}
read_in += found.len();
self.add_project_files(found.clone());
for unit in found {
self.programs_wanted.insert(queue_key(&unit));
}
self.index_everything();
let due: Vec<PathBuf> = {
let mut due: Vec<PathBuf> = self
.programs_wanted
.iter()
.filter(|key| !self.units_read.contains(*key))
.map(PathBuf::from)
.collect();
due.sort();
due
};
for unit in due {
let reading = std::time::Instant::now();
let read = self.read_the_unit(&unit);
if tracing {
eprintln!(
"module read: unit {} in {:?} ({} file(s))",
unit.display(),
reading.elapsed(),
read.map_or(0, |reading| reading.files),
);
}
}
}
read_in
}
fn modules_the_index_cannot_name(&self, path: &Path) -> Vec<String> {
self.modules_in_view_of(path)
.into_iter()
.filter(|module| self.store.index().interface_unit_of(module).is_none())
.collect()
}
pub fn imports_a_module_that_is_not_read(&self, path: &Path) -> bool {
!self.modules_the_index_cannot_name(path).is_empty()
}
fn module_interface_units_of(&self, wanted: &[String], importing: &Path) -> Vec<PathBuf> {
let mut found: Vec<PathBuf> = Vec::new();
let mut interner = PathInterner::new(self.files.is_case_insensitive());
let mut scanner = ModuleScanner::new(&self.files, &self.config);
let directory =
crate::paths::parent_normalized(importing, self.files.is_case_insensitive());
for module in wanted {
if let ImportOutcome::Resolved(unit) =
scanner.resolve_module(module, &directory, &mut interner)
&& let Some(entry) = scanner.units().iter().find(|entry| entry.file == unit)
{
found.push(entry.path.clone());
}
}
found.retain(|unit| self.store.index().summary(unit).is_none());
found.sort();
found.dedup();
found
}
fn modules_in_view_of(&self, path: &Path) -> Vec<String> {
let mut wanted: Vec<String> = Vec::new();
let mut pending: Vec<PathBuf> = vec![path.to_path_buf()];
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
while let Some(current) = pending.pop() {
if !seen.insert(queue_key(¤t)) {
continue;
}
let Some(summary) = self.store.index().summary(¤t) else {
continue;
};
for imported in &summary.modules.imports {
if !wanted.iter().any(|held| held == imported.as_ref()) {
wanted.push(imported.to_string());
}
if let Some(interface) = self.store.index().interface_unit_of(imported) {
pending.push(PathBuf::from(interface));
}
}
if !summary.modules.is_interface
&& summary.modules.partition.is_none()
&& let Some(module) = &summary.modules.module
&& !wanted.iter().any(|held| held == module.as_ref())
{
wanted.push(module.to_string());
if let Some(interface) = self.store.index().interface_unit_of(module) {
pending.push(PathBuf::from(interface));
}
}
}
wanted
}
pub fn unread_units(&self) -> usize {
self.documents
.paths()
.iter()
.filter(|path| !self.units_read.contains(&queue_key(path)))
.count()
}
pub fn cook(&mut self, path: impl AsRef<Path>) -> Option<CookedReading> {
let materials = self.cooking_materials(path.as_ref())?;
let rendered = self.render_a_cooked(&materials)?;
self.commit_a_cooked(rendered)
}
pub fn cooking_materials(&self, path: &Path) -> Option<CookingMaterials> {
let path = self.store.index().summary(path)?.path.clone();
let key = SummaryKey::new(0, self.store.context_hash(&path));
let already_declared: std::collections::HashSet<String> = self
.store
.index()
.summary(&path)
.into_iter()
.flat_map(|summary| summary.declarations.iter())
.map(crate::DeclFact::qualified_name)
.collect();
let held = {
let held_units = self.held_units();
held_units
.values()
.find(|held| !held.provisional && held.unit.environment_of(&path).is_some())
.map(|held| std::sync::Arc::clone(&held.unit))
};
let can_be_a_root = {
let included = !self.store.index().includers_of(&path).is_empty();
let has_a_command = self
.config
.database
.as_ref()
.is_some_and(|database| database.command_for(&path).is_some());
!included || has_a_command || self.documents.is_open(&path)
};
let mut parsed = self
.parsed_definitions
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let mut composed = || {
self.store
.the_indexed_environment(&path)
.map(|environment| {
crate::preprocess::cooked::configuration_from_environment_and(
&environment,
true,
&mut parsed,
)
})
};
let walked = if held.is_none() && can_be_a_root {
self.unit_and_state_of(&path).map(|(unit, _)| unit)
} else {
None
};
let (unit, configuration) = match held.or(walked) {
Some(unit) => (Some(unit), None),
None => (None, Some(composed()?)),
};
drop(parsed);
let text = self.files.read(&path)?;
let text_hash = crate::cache::content_hash(&text);
let seed = MacroTable::from_marked(self.store.index().macros());
Some(CookingMaterials {
path,
key,
unit,
configuration,
text,
text_hash,
seed,
already_declared,
revision: self.revision(),
})
}
pub fn gather_a_cook(&self, path: &Path) -> Option<DetachedCook<F>> {
Some(DetachedCook {
files: self.files.clone(),
config: self.config.clone(),
materials: self.cooking_materials(path)?,
})
}
pub fn render_a_cooked(&self, materials: &CookingMaterials) -> Option<RenderedCook> {
if !self.is_current(&materials.revision) {
return None;
}
Self::render_cook(&self.files, &self.config, materials)
}
fn render_cook(
files: &SessionFiles<F>,
config: &CompilerConfig,
materials: &CookingMaterials,
) -> Option<RenderedCook> {
let CookingMaterials {
path,
key,
unit,
configuration,
text,
text_hash,
seed,
already_declared,
revision,
} = materials;
let (tokens, _) = { cpp_parser::lex(text, &cpp_parser::LexerConfig::default()) };
let rendered = match (unit, configuration) {
(Some(unit), _) => {
let definitions = unit.definitions();
let macros = crate::preprocess::cooked::FileMacros::new(
unit.environment_of(path)?,
&definitions,
Some(seed),
true,
);
crate::preprocess::cooked::cook_with(text, &tokens, ¯os).render()
}
(None, Some(configuration)) => {
let start = crate::preprocess::cooked::Over::new(&configuration.table, seed);
crate::preprocess::cooked::cook_with(text, &tokens, &start).render()
}
(None, None) => return None,
};
let indexer = FileIndexer::new(files, config);
let indexed = indexer.index_rendering(path, &rendered, *key);
let reading = CookedReading {
declarations: indexed.summary.declarations.len(),
only_after_expansion: indexed
.summary
.declarations
.iter()
.filter(|fact| !already_declared.contains(&fact.qualified_name()))
.count(),
diagnostics: indexed.diagnostics.len(),
unplaced: indexed.unplaced,
mapped: indexed.mapped,
};
Some(RenderedCook {
text_hash: *text_hash,
indexed,
reading,
revision: revision.clone(),
})
}
pub fn commit_a_cooked(&mut self, rendered: RenderedCook) -> Option<CookedReading> {
let RenderedCook {
text_hash,
indexed,
reading,
revision,
} = rendered;
let path = indexed.summary.path.clone();
let current = self
.text(&path)
.map(|text| crate::cache::content_hash(&text));
if !self.is_current(&revision) || current != Some(text_hash) {
if crate::probes::cooking() {
eprintln!(
"commit_a_cooked: dropped {} | the text changed while it was being rendered",
path.display()
);
}
if Arc::ptr_eq(&self.identity, &revision.session) && self.files.exists(&path) {
self.want_cooked_reading(&path);
}
return None;
}
if let Ok(mut known) = self.renderings.lock() {
let key = (path.clone(), text_hash);
if crate::probes::view() {
eprintln!("commit: path {:?} hash {:?}", path, text_hash);
}
let before = known.len();
let weight = indexed.rendered.weight();
known.insert(key, std::sync::Arc::clone(&indexed.rendered), weight);
if crate::probes::cooking() {
eprintln!(
"commit_a_cooked: inserted {} | cache {} -> {}",
path.display(),
before,
known.len()
);
}
} else if crate::probes::cooking() {
eprintln!(
"commit_a_cooked: NOT inserted {} | lock {}",
path.display(),
self.renderings.lock().is_ok()
);
}
{
let cooked: crate::index::CookedFile = indexed.into();
self.store.put_the_cooked_reading(&path, &cooked);
self.store.index_mut().insert_cooked(&path, cooked);
}
Some(reading)
}
pub fn claim_cooking(&mut self, how_many: usize) -> Vec<PathBuf> {
self.cooking.take(how_many)
}
pub fn refuse_the_cooks_that_built_nothing(&mut self, asked: &[PathBuf], built: &[PathBuf]) {
let Ok(mut refused) = self.cooking_refused.lock() else {
return;
};
for path in asked {
if !built.iter().any(|made| made == path) {
refused.insert(queue_key(path));
}
}
}
pub fn materials_for(&self, paths: Vec<PathBuf>) -> Vec<CookingMaterials> {
paths
.into_iter()
.filter_map(|path| {
let built = self.cooking_materials(&path);
if built.is_none() && crate::probes::cooking() {
let indexed = self.store.index().summary(&path).is_some();
let (unit, provisional) = match self.unit_and_state_of(&path) {
Some((_, provisional)) => ("held", provisional),
None => ("none", false),
};
eprintln!(
"cooking: {} yields no materials | summary indexed {indexed} | unit {unit} \
provisional {provisional}",
path.display()
);
}
built
})
.collect()
}
pub fn render_them(&self, materials: Vec<CookingMaterials>) -> Vec<RenderedCook> {
let tracing = crate::probes::cooking();
materials
.into_iter()
.filter_map(|materials| {
let path = materials.path.clone();
let built = self.render_a_cooked(&materials);
if built.is_none() && tracing {
eprintln!("cooking: {} did not render", path.display());
}
built
})
.collect()
}
pub fn commit_them(&mut self, rendered: Vec<RenderedCook>) -> Vec<CookedReading> {
rendered
.into_iter()
.filter_map(|reading| self.commit_a_cooked(reading))
.collect()
}
pub fn rendered_text_of(&self, path: impl AsRef<Path>) -> Option<String> {
Some(self.rendering_of(path)?.text)
}
pub fn rendering_of(
&self,
path: impl AsRef<Path>,
) -> Option<crate::preprocess::cooked::RenderedCooked> {
let path = self.store.index().summary(path.as_ref())?.path.clone();
let unit = self.translation_unit_of(&path)?;
let text = self.files.read(&path)?;
let seed = MacroTable::from_marked(self.store.index().macros());
let (tokens, _) = cpp_parser::lex(&text, &cpp_parser::LexerConfig::default());
let unit_definitions = unit.definitions();
let macros = crate::preprocess::cooked::FileMacros::new(
unit.environment_of(&path)?,
&unit_definitions,
Some(&seed),
true,
);
Some(crate::preprocess::cooked::cook_with(&text, &tokens, ¯os).render())
}
pub fn index_the_rendering(&mut self, path: impl AsRef<Path>) -> Option<crate::FileSummary> {
let path = self.store.index().summary(path.as_ref())?.path.clone();
let unit = self.translation_unit_of(&path)?;
let environment = unit.environment_of(&path)?;
let written = self.files.read(&path)?;
let rendered = match self.known_rendering_of(&path, &written) {
Some(rendered) => rendered,
None => std::sync::Arc::new(self.rendering_of(&path)?),
};
self.store
.index_the_rendering(&path, &rendered, Some(&environment))
.cloned()
}
pub fn fact_from_the_rendering(
&self,
path: impl AsRef<Path>,
name: &str,
scope: Option<&str>,
) -> Option<crate::DeclFact> {
let path = self.store.index().summary(path.as_ref())?.path.clone();
let text = self.files.read(&path)?;
let rendered = match self.known_rendering_of(&path, &text) {
Some(rendered) => rendered,
None => {
let built = std::sync::Arc::new(self.rendering_of(&path)?);
if let Ok(mut known) = self.renderings.lock() {
let weight = built.weight();
known.insert(
(path.clone(), crate::cache::content_hash(&text)),
built.clone(),
weight,
);
}
built
}
};
let key = (path.clone(), crate::cache::content_hash(&rendered.text));
let remembered = self
.rendering_summaries
.lock()
.ok()
.and_then(|mut known| known.get(&key).cloned());
let summary = match remembered {
Some(summary) => summary,
None => {
let built = std::sync::Arc::new(crate::summarize(
&path,
&rendered.text,
crate::SummaryKey::new(0, 0),
));
if let Ok(mut known) = self.rendering_summaries.lock() {
let weight = built.declarations.len() + built.macros.len();
known.insert(key, built.clone(), weight);
}
built
}
};
summary
.declarations
.iter()
.find(|fact| {
fact.name == name && fact.scope.as_deref() == scope && !fact.split_by_a_directive
})
.cloned()
}
pub fn cook_the_open_files(&mut self) -> Vec<(PathBuf, CookedReading)> {
let open = self.documents.paths();
let mut done = Vec::new();
for path in open {
if let Some(reading) = self.cook(&path) {
done.push((path, reading));
}
}
done
}
fn closure_with_text(&self, root: &Path) -> (Vec<(PathBuf, String)>, HashMap<String, usize>) {
let seed = self.store.index().summary(root);
self.closure_with_text_from(root, seed)
}
fn closure_with_text_from(
&self,
root: &Path,
seed: Option<&FileSummary>,
) -> (Vec<(PathBuf, String)>, HashMap<String, usize>) {
let mut out: Vec<(PathBuf, String)> = Vec::new();
let mut by_key: HashMap<String, usize> = HashMap::new();
for path in self.closure_paths_from(root, seed) {
if let Some(text) = self.text(&path) {
by_key.insert(queue_key(&path), out.len());
out.push((path, text));
}
}
(out, by_key)
}
fn closure_paths_from(&self, root: &Path, seed: Option<&FileSummary>) -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
let mut queue: std::collections::VecDeque<PathBuf> = std::collections::VecDeque::new();
queue.push_back(root.to_path_buf());
let mut edges: Option<&FileSummary> = seed;
while let Some(path) = queue.pop_front() {
if !seen.insert(queue_key(&path)) {
continue;
}
out.push(path.clone());
let summary = match edges.take() {
Some(seed) => Some(seed),
None => self.store.index().summary(&path),
};
if let Some(summary) = summary {
for include in &summary.includes {
if let Some(resolved) = &include.resolved {
queue.push_back(resolved.clone());
}
}
}
}
out
}
fn the_closure_of_the_directives(
&self,
root: &Path,
directives: &FileSummary,
) -> Option<Vec<PathBuf>> {
let index = self.store.index();
let root_key = queue_key(root);
let reached = self.closure_paths_from(root, Some(directives));
reached
.iter()
.all(|path| queue_key(path) == root_key || index.summary(path).is_some())
.then_some(reached)
}
pub fn definition(&self, view: &FileView, offset: ReadingOffset) -> Known<ProjectDefinition> {
let found = definition_across_files(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
offset.get(),
);
let found = self.with_the_reflection(view, found);
if let Known::Yes(found) = &found {
self.want_refinement(&found.file);
}
found
}
pub fn is_refined(&self, path: &Path) -> bool {
if self
.refined_readings
.lock()
.is_ok_and(|refined| refined.contains(&queue_key(path)))
{
return true;
}
self.store.is_marked_refined(path)
|| self
.store
.index()
.summary(path)
.is_some_and(|summary| summary.key.env_hash != 0)
}
fn with_the_reflection(
&self,
view: &FileView,
found: Known<ProjectDefinition>,
) -> Known<ProjectDefinition> {
let Known::Yes(mut definition) = found else {
return found;
};
if definition.fact.reflection.is_some() {
return Known::Yes(definition);
}
let Some(in_the_file) =
view.file_offset_of(ReadingOffset::new(definition.fact.name_range.start_offset))
else {
return Known::Yes(definition);
};
let Some(summary) = self.parsed_summary(&view.path) else {
return Known::Yes(definition);
};
let recorded = summary.declarations.iter().find(|fact| {
fact.name == definition.fact.name && fact.name_range.start_offset == in_the_file.get()
});
if let Some(recorded) = recorded {
definition.fact.reflection = recorded.reflection.clone();
}
Known::Yes(definition)
}
pub fn definitions(&self, view: &FileView, offset: ReadingOffset) -> Known<ProjectDefinitions> {
let mut answer = definitions_across_files(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
offset.get(),
);
if let Known::Yes(definitions) = &mut answer {
for definition in &mut definitions.found {
if let Some(path) = self.project_paths.get(&queue_key(&definition.file)) {
definition.file.clone_from(path);
}
}
}
answer
}
pub fn symbol_references(
&self,
view: &FileView,
offset: ReadingOffset,
budget: crate::ReferenceBudget,
) -> Known<crate::SymbolReferences> {
let Known::Yes(found) = self.definitions(view, offset) else {
let written = crate::sema::resolve::name_position_at(&view.root, offset.get())
.map(|name| name.written.clone())
.unwrap_or_default();
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from(written)));
};
let Some(first) = found.found.first() else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
};
let symbol = crate::SymbolToFind::of(&first.fact, first.file.clone());
crate::index::references::symbol_references(
self.store.index(),
&self.files,
&symbol,
budget,
)
}
pub fn macro_at_a_file_offset(
&self,
view: &FileView,
offset: crate::file::coordinates::FileOffset,
) -> Known<ProjectMacro> {
let Some(written) = view.the_written_text() else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("no written text")));
};
let at = offset.get();
let is_a_word = |byte: u8| byte.is_ascii_alphanumeric() || byte == b'_';
let bytes = written.as_bytes();
let mut start = at.min(bytes.len());
while start > 0 && is_a_word(bytes[start - 1]) {
start -= 1;
}
let mut end = at.min(bytes.len());
while end < bytes.len() && is_a_word(bytes[end]) {
end += 1;
}
let name = &written[start..end];
if name.is_empty() {
return Known::Unknown(UnknownReason::UnparsableName);
}
self.store.index().macro_definition(name, &view.path, at)
}
pub fn macro_definition_text(&self, path: &Path, offset: usize, name: &str) -> Option<String> {
let text = self.text(path)?;
the_directive_defining(name, &text, offset)
}
pub fn module_names(&self) -> Vec<(String, PathBuf)> {
let mut found: Vec<(String, PathBuf)> = self
.store
.index()
.interface_units()
.into_iter()
.map(|(name, unit)| (name, PathBuf::from(unit)))
.collect();
const A_MODULE_FILE: &[&str] = &["cppm", "ixx", "ccm", "cxxm"];
for path in &self.project {
let Some(extension) = path.extension().and_then(|extension| extension.to_str()) else {
continue;
};
if !A_MODULE_FILE
.iter()
.any(|known| known.eq_ignore_ascii_case(extension))
{
continue;
}
let Some(name) = path
.file_stem()
.and_then(|stem| stem.to_str())
.filter(|stem| !stem.is_empty())
else {
continue;
};
found.push((name.to_string(), path.clone()));
}
found.sort_by(|left, right| left.0.cmp(&right.0));
found.dedup_by(|later, earlier| later.0 == earlier.0);
found
}
fn module_completions(
&self,
written: &str,
range: cpp_parser::SourceRange,
) -> crate::CompletionSet {
let mut items: Vec<crate::completion::CompletionItem> = Vec::new();
for (name, unit) in self.module_names() {
if !written.is_empty()
&& !name
.to_ascii_lowercase()
.starts_with(&written.to_ascii_lowercase())
{
continue;
}
items.push(crate::completion::CompletionItem {
label: name.clone(),
insert: name.clone(),
snippet: false,
kind: crate::completion::ItemKind::Namespace,
detail: Some(format!("module {}", unit.display())),
identity: None,
filter: None,
fact: None,
replace: range,
inclusion: None,
additional_edits: Vec::new(),
});
}
crate::CompletionSet {
scope: String::new(),
prefix: written.to_string(),
replace: range,
truncated: false,
incomplete: false,
items,
}
}
pub fn constant_value_at(&self, view: &FileView, offset: ReadingOffset) -> Option<i64> {
let path = view.path.clone();
crate::sema::constant::value_at(view, offset.get(), &|name| {
self.macro_body_at(&path, offset.get(), name)
})
}
pub fn macro_body_at(&self, path: &Path, offset: usize, name: &str) -> Option<String> {
let environment = self.store.index().macro_environment(name, path);
let Known::Yes(found) = environment.at(offset) else {
return None;
};
let fact = found.fact();
if fact.function_like {
return None;
}
let range = fact.body_range?;
let text = self.text(found.file())?;
let body = text.get(range.start_offset..range.end_offset())?;
(!body.trim().is_empty()).then(|| body.trim().to_string())
}
pub fn macro_definition(&self, view: &FileView, offset: ReadingOffset) -> Known<ProjectMacro> {
macro_across_files(self.store.index(), &view.root, &view.path, offset.get())
}
pub fn predefined_macro(&self, name: &str, path: &std::path::Path) -> Option<String> {
let language = self.config_for(path).language_for(path);
if let Some(value) = crate::session::the_standards_own_macro(name, &language) {
return Some(value);
}
let toolchain = self.toolchain.as_ref()?;
if toolchain.builtin_macros.is_empty() {
return None;
}
toolchain
.macros()
.into_iter()
.find(|(defined, _)| *defined == name)
.map(|(_, value)| value.unwrap_or_default().to_string())
}
pub fn the_expansion_of_a_macro(
&self,
view: &FileView,
offset: crate::file::coordinates::FileOffset,
) -> Option<String> {
let Known::Yes(found) = self.macro_at_a_file_offset(view, offset) else {
return None;
};
let fact = &found.fact;
if fact.kind != crate::MacroKind::Definition {
return None;
}
let body_range = fact.body_range?;
let defining =
if crate::file::paths::normalize_path(&found.file, self.files.is_case_insensitive())
== crate::file::paths::normalize_path(&view.path, self.files.is_case_insensitive())
{
view.the_written_text().map(|text| text.to_string())
} else {
self.view_of_the_file(&found.file)
.map(|other| other.source.to_string())
}?;
let body = defining
.get(body_range.start_offset..body_range.end_offset())?
.trim();
if !fact.function_like {
return Some(body.to_string());
}
let between = defining.get(fact.range.end_offset()..body_range.start_offset)?;
let open = between.find('(')?;
let close = between.rfind(')')?;
if close < open {
return None;
}
let parameters: Vec<&str> = between[open + 1..close]
.split(',')
.map(str::trim)
.filter(|parameter| !parameter.is_empty())
.collect();
fn the_arguments_of_a_call(text: &str) -> Option<Vec<String>> {
let bytes = text.as_bytes();
if bytes.first() != Some(&b'(') {
return None;
}
let mut depth = 0i32;
let mut quote: Option<u8> = None;
let mut arguments: Vec<String> = Vec::new();
let mut current_start = 1usize;
let mut at = 0usize;
while at < bytes.len() {
let byte = bytes[at];
match quote {
Some(ending) => {
if byte == b'\\' {
at += 2;
continue;
}
if byte == ending {
quote = None;
}
}
None => match byte {
b'"' | b'\'' => quote = Some(byte),
b'(' | b'[' | b'{' => depth += 1,
b')' | b']' | b'}' => {
depth -= 1;
if depth == 0 {
let last = text[current_start..at].trim();
if !last.is_empty() || !arguments.is_empty() {
arguments.push(last.to_string());
}
return Some(arguments);
}
}
b',' if depth == 1 => {
arguments.push(text[current_start..at].trim().to_string());
current_start = at + 1;
}
_ => {}
},
}
at += 1;
}
None
}
let written = view.the_written_text()?;
let bytes = written.as_bytes();
let is_a_word = |byte: u8| byte.is_ascii_alphanumeric() || byte == b'_';
let mut start = offset.get().min(bytes.len());
while start > 0 && is_a_word(bytes[start - 1]) {
start -= 1;
}
let mut end = offset.get().min(bytes.len());
while end < bytes.len() && is_a_word(bytes[end]) {
end += 1;
}
if written.get(start..end)? != fact.name {
return None;
}
let after = &written[end..];
let paren = after.find(|character: char| !character.is_whitespace())?;
if !after[paren..].starts_with('(') {
return None;
}
let arguments = the_arguments_of_a_call(&written[end + paren..])?;
if arguments.len() != parameters.len() && !(parameters.len() == 1 && parameters[0] == "...")
{
return None;
}
let mut expanded = String::with_capacity(body.len());
let body_bytes = body.as_bytes();
let mut at = 0usize;
while at < body_bytes.len() {
if !is_a_word(body_bytes[at]) || (at > 0 && is_a_word(body_bytes[at - 1])) {
expanded.push(body_bytes[at] as char);
at += 1;
continue;
}
let word_start = at;
while at < body_bytes.len() && is_a_word(body_bytes[at]) {
at += 1;
}
let word = &body[word_start..at];
match parameters.iter().position(|parameter| *parameter == word) {
Some(position) => expanded.push_str(arguments.get(position)?.trim()),
None => expanded.push_str(word),
}
}
Some(expanded)
}
pub fn module_imports_of(&self, view: &FileView) -> Vec<ModuleImport> {
let mut imports: Vec<ModuleImport> = Vec::new();
let directory =
crate::paths::parent_normalized(&view.path, self.files.is_case_insensitive());
let mut interner = PathInterner::new(self.files.is_case_insensitive());
let mut scanner: Option<ModuleScanner<'_, OverlayFiles<OpenDocuments, F>>> = None;
for node in view.root.descendants() {
if cpp_parser::CppSyntaxKind::from(node.kind()) != cpp_parser::CppSyntaxKind::ImportDecl
{
continue;
}
let Some(name_node) = node.descendants().find(|inner| {
cpp_parser::CppSyntaxKind::from(inner.kind())
== cpp_parser::CppSyntaxKind::ModuleName
}) else {
continue;
};
let name = name_node.text().to_string().trim().to_string();
if name.is_empty() {
continue;
}
let range = cpp_parser::source_range(name_node.text_range());
let resolved = match self.store.index().interface_unit_of(&name) {
Some(path) => Some(PathBuf::from(path)),
None => {
let scanner = scanner
.get_or_insert_with(|| ModuleScanner::new(&self.files, &self.config));
match scanner.resolve_module(&name, &directory, &mut interner) {
ImportOutcome::Resolved(unit) => scanner
.units()
.iter()
.find(|entry| entry.file == unit)
.map(|entry| entry.path.clone()),
_ => None,
}
}
};
imports.push(ModuleImport {
name,
range,
resolved,
});
}
imports
}
pub fn header_at(&self, view: &FileView, offset: FileOffset) -> Known<crate::HeaderTarget> {
let index = self.store.index();
let Some(written) = view.written_text() else {
return crate::header_at(index, &view.root, &view.path, offset.get());
};
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
crate::header_at(index, &tree.get_red_root(), &view.path, offset.get())
}
pub fn macro_references(
&self,
view: &FileView,
offset: ReadingOffset,
) -> Known<MacroReferences> {
let Some((name, _)) =
crate::sema::resolve::name_at_including_directives(&view.root, offset.get())
else {
return Known::Unknown(UnknownReason::UnparsableName);
};
macro_references(
self.store.index(),
&self.files,
&name,
ReferenceBudget::default(),
)
}
pub fn member_completions(
&self,
view: &FileView,
offset: FileOffset,
) -> Known<MemberCompletions> {
let Some(offset) = view.reading_offset_of(offset) else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
};
member_completions_at(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
offset.get(),
)
}
pub fn name_completions(&self, view: &FileView, offset: FileOffset) -> Known<NameCompletions> {
if let Some(written) = view.written_text()
&& crate::file::view::in_prose(written, offset.get())
{
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
}
let Some(offset) = view.reading_offset_of(offset) else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
};
crate::index::project::name_completions_traced(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
)
}
pub fn completions(&self, view: &FileView, offset: FileOffset) -> crate::CompletionSet {
let mut offered = self.completions_without_the_edits(view, offset);
self.attach_the_inclusions(&mut offered, &view.path);
offered
}
fn the_files_this_file_could_include(
&self,
path: &std::path::Path,
) -> Vec<crate::completion::Unincluded> {
if !self
.project_config()
.config
.completion
.suggest_unincluded
.unwrap_or(true)
{
return Vec::new();
}
let include_paths = self.config_for(path).include_paths;
let reached = self.store.index().include_closure(path);
let directory = path.parent().map(|parent| parent.to_path_buf());
let libraries: Vec<PathBuf> = self
.project_config()
.config
.libraries
.iter()
.map(|library| library.root(&self.root))
.collect();
const MOST: usize = 64;
let mut found = Vec::new();
for file in &self.project {
if found.len() >= MOST {
break;
}
if file == path {
continue;
}
let extension = file
.extension()
.and_then(|extension| extension.to_str())
.map(|extension| extension.to_lowercase());
let is_header = matches!(
extension.as_deref(),
Some("h" | "hpp" | "hxx" | "h++" | "inl" | "ipp")
);
let is_interface = matches!(extension.as_deref(), Some("cppm"));
if !is_header && !is_interface {
continue;
}
let key = crate::file::paths::normalize_path(file, cfg!(windows));
if reached.contains(&key) {
continue;
}
let related = directory.as_deref() == file.parent()
|| libraries.iter().any(|root| file.starts_with(root))
|| include_paths
.iter()
.any(|held| file.starts_with(&held.directory));
if !related {
continue;
}
let inclusion = match self
.store
.index()
.summary(file)
.and_then(|summary| summary.modules.module.clone())
{
Some(module) => crate::completion::Inclusion::Import(module.to_string()),
None => crate::completion::Inclusion::Include(
crate::completion::the_include_spelling(file, path, &include_paths),
),
};
found.push(crate::completion::Unincluded {
file: file.clone(),
inclusion,
});
}
found
}
fn attach_the_inclusions(&self, offered: &mut crate::CompletionSet, path: &std::path::Path) {
if !offered.items.iter().any(|item| item.inclusion.is_some()) {
return;
}
let Some(text) = self.vfs.held(path).map(|held| held.text.to_string()) else {
return;
};
let after_includes = after_the_includes(&text);
let after_imports = after_the_module_imports(&text);
for item in &mut offered.items {
let Some(inclusion) = &item.inclusion else {
continue;
};
let at = match inclusion {
crate::completion::Inclusion::Include(_) => after_includes,
crate::completion::Inclusion::Import(_) => after_imports,
};
item.additional_edits
.push((cpp_parser::SourceRange::new(at, 0), inclusion.line()));
}
}
fn completions_without_the_edits(
&self,
view: &FileView,
offset: FileOffset,
) -> crate::CompletionSet {
let unincluded = self.the_files_this_file_could_include(&view.path);
{
let source: &str = view.written_text().unwrap_or(&view.source);
if let Some((written, range)) =
crate::completion::context::module_name_from_the_text(source, offset.get())
{
return self.module_completions(&written, range);
}
}
let written_text = view.written_text();
let text: &str = written_text.unwrap_or(&view.source);
{
let written_root = match written_text {
Some(written) => {
cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default())
.get_red_root()
}
None => view.root.clone(),
};
if let Some(
context @ (crate::completion::CompletionContext::DocComment { .. }
| crate::completion::CompletionContext::DocLink { .. }),
) = crate::completion::doc_comment_at(&written_root, offset.get())
{
return crate::completion::completion_with(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
context,
self.config.cuda(),
self.the_completion_budget(),
&[],
);
}
if crate::doc::inside_a_code_block(text, offset.get()) {
return crate::completion::completion_at(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
0,
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
self.config.cuda(),
self.the_completion_budget(),
&[],
);
}
if crate::doc::is_a_documentation_comment(text, offset.get())
|| crate::file::view::in_prose(text, offset.get())
{
return crate::CompletionSet::default();
}
let context = crate::completion::context_at(&written_root, offset.get());
match context {
crate::completion::CompletionContext::DirectiveArgument { .. }
| crate::completion::CompletionContext::Qualified(_) => {
let in_the_reading = view
.reading_offset_of(offset)
.map_or(view.source.len(), |at| at.get());
return crate::completion::completion_with(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
in_the_reading,
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
context,
self.config.cuda(),
self.the_completion_budget(),
&[],
);
}
context @ (crate::completion::CompletionContext::DirectiveName { .. }
| crate::completion::CompletionContext::Include { .. }
| crate::completion::CompletionContext::MacroBody { .. }
| crate::completion::CompletionContext::Condition { .. }) => {
return crate::completion::completion_with(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
context,
self.config.cuda(),
self.the_completion_budget(),
&[],
);
}
crate::completion::CompletionContext::Nothing => {
return crate::CompletionSet::default();
}
_ => {}
}
}
let Some(offset) = view.reading_offset_of(offset) else {
return crate::CompletionSet::default();
};
let headers = self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default();
crate::completion::completion_at(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
&headers,
self.config.cuda(),
self.the_completion_budget(),
&unincluded,
)
}
pub fn headers(&self) -> crate::HeaderIndex {
self.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default()
}
pub fn documentation(
&self,
view: &FileView,
file: &Path,
offset: usize,
) -> Option<cpp_parser::CppDocComment> {
if file == view.path {
let Some(written) = view.written_text() else {
return crate::file::view::documentation_at(&view.root, offset);
};
if !crate::file::view::might_be_documented(written, offset) {
return None;
}
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
return crate::file::view::documentation_at(&tree.get_red_root(), offset);
}
let declared_in = self.vfs.held(file)?;
if !crate::file::view::might_be_documented(&declared_in.text, offset) {
return None;
}
let tree =
cpp_parser::CppParser::parse(&declared_in.text, cpp_parser::ParserConfig::default());
crate::file::view::documentation_at(&tree.get_red_root(), offset)
}
pub fn type_of_the_expression(
&self,
view: &FileView,
expression: &cpp_parser::CppSyntaxNode,
) -> Known<ExpressionType> {
let written = expression.text().to_string().trim().to_string();
match crate::index::project::type_of_expression(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
expression,
0,
) {
Known::Yes((type_of, file)) => Known::Yes(ExpressionType {
expression: written,
type_of: type_of.to_string(),
file,
}),
Known::Unknown(reason) => Known::Unknown(reason),
Known::No => Known::No,
}
}
pub fn type_at(&self, view: &FileView, offset: ReadingOffset) -> Known<ExpressionType> {
let Some(expression) = crate::sema::resolve::expression_at(&view.root, offset.get()) else {
return Known::No;
};
let written = expression.text().to_string().trim().to_string();
match crate::index::project::type_of_expression(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
&expression,
0,
) {
Known::Yes((type_of, file)) => Known::Yes(ExpressionType {
expression: written,
type_of: type_of.to_string(),
file,
}),
Known::Unknown(reason) => Known::Unknown(reason),
Known::No => Known::No,
}
}
pub fn member_of_the_enclosing_class(
&self,
view: &FileView,
offset: ReadingOffset,
) -> Option<ProjectDefinition> {
let callee = crate::sema::resolve::name_node_at(&view.root, offset.get())?;
crate::index::project::a_member_of_the_enclosing_class(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
&callee,
false,
)
}
pub fn inlay_hints(
&self,
view: &FileView,
range: cpp_parser::SourceRange,
) -> Vec<ParameterHint> {
let mut hints = crate::inlay::parameter_hints(self.store.index(), view, range, |path| {
self.view_of_the_file(path)
});
for hint in &mut hints {
let Some(placed) = view.file_offset_of(ReadingOffset::new(hint.offset)) else {
continue;
};
hint.offset = placed.get();
}
hints
}
pub fn type_hints(
&self,
view: &FileView,
range: cpp_parser::SourceRange,
) -> Vec<crate::inlay::TypeHint> {
let mut hints = crate::inlay::type_hints(
view,
range,
|at| match self.type_at(view, ReadingOffset::new(at)) {
Known::Yes(found) => Some(found.type_of),
_ => None,
},
|at| {
let local = self.definition(view, ReadingOffset::new(at)).value();
let indexed = local.as_ref().and_then(|found| {
self.store
.index()
.definition(&found.fact.qualified_name(), &view.path)
.value()
});
indexed
.and_then(|found| found.fact.returns)
.or_else(|| local.and_then(|found| found.fact.returns))
},
);
for hint in &mut hints {
let Some(placed) = view.file_offset_of(ReadingOffset::new(hint.offset)) else {
continue;
};
hint.offset = placed.get();
}
hints
}
pub fn classified_names(&self, view: &FileView) -> Vec<crate::semantic::Name> {
use cpp_parser::CppTokenKind;
let mut names = crate::semantic::classified_names(self.store.index(), view);
let language = self.config_for(&view.path).language_for(&view.path);
let mut claimed: std::collections::HashSet<usize> =
names.iter().map(|name| name.range.start_offset).collect();
for token in view.tree.get_tokens() {
if token.kind != CppTokenKind::Identifier {
continue;
}
let Some(text) = view.get_token_text(token.range) else {
continue;
};
if !text.starts_with('_') {
continue;
}
let start = token.range.start_offset;
if claimed.contains(&start) {
continue;
}
let is_predefined = the_standards_own_macro(text, &language).is_some()
|| a_macro_whose_value_depends_on_the_use(text)
|| self.predefined_macro(text, &view.path).is_some();
if !is_predefined {
continue;
}
claimed.insert(start);
names.push(crate::semantic::Name {
range: token.range,
kind: crate::semantic::NameKind::Macro,
declaration: false,
modifiers: Vec::new(),
provenance: crate::semantic::Provenance::Macro,
});
}
let mut placed = Vec::with_capacity(names.len());
for mut name in names {
let Some(start) = view.file_offset_of(ReadingOffset::new(name.range.start_offset))
else {
continue;
};
let end = view
.file_offset_of(ReadingOffset::new(name.range.end_offset()))
.unwrap_or(start);
name.range =
cpp_parser::SourceRange::new(start.get(), end.get().saturating_sub(start.get()));
placed.push(name);
}
names = placed;
if view
.written_text()
.is_some_and(|written| written != &*view.source)
&& let Some(own) = self.view_of_the_file(&view.path)
{
let mut at_offset: std::collections::HashMap<usize, usize> = names
.iter()
.enumerate()
.map(|(index, name)| (name.range.start_offset, index))
.collect();
for name in crate::semantic::classified_names(self.store.index(), &own) {
match at_offset.get(&name.range.start_offset) {
Some(&index) => names[index] = name,
None => {
at_offset.insert(name.range.start_offset, names.len());
names.push(name);
}
}
}
}
{
let text: &str = view.written_text().unwrap_or(&view.source);
for target in crate::doc::targets(text) {
if !crate::doc::is_a_documentation_comment(text, target.range.start_offset) {
continue;
}
let spelling = target.spelled_for_a_lookup();
let kind = self
.store
.index()
.definition(spelling, &view.path)
.value()
.and_then(|found| {
crate::semantic::kind_of_index(crate::index::project::IndexedKind::of(
&found.fact,
))
})
.or_else(|| {
self.store
.index()
.macro_names_in_force(&view.path)
.contains(spelling)
.then_some(crate::semantic::NameKind::Macro)
});
let Some(kind) = kind else {
continue;
};
names.push(crate::semantic::Name {
range: target.range,
kind,
declaration: false,
modifiers: Vec::new(),
provenance: crate::semantic::Provenance::Found,
});
}
}
{
let text: &str = view.written_text().unwrap_or(&view.source);
for block in crate::doc::code_blocks(text) {
let body = &text[block.start_offset..block.end_offset()];
let (tokens, _) = cpp_parser::lex(body, &cpp_parser::LexerConfig::default());
for token in tokens {
let Some(spelling) =
body.get(token.range.start_offset..token.range.end_offset())
else {
continue;
};
let is_a_keyword = cpp_parser::is_keyword(token.kind);
if !is_a_keyword && token.kind != cpp_parser::CppTokenKind::Identifier {
continue;
}
let kind = if is_a_keyword {
Some(crate::semantic::NameKind::Keyword)
} else {
self.store
.index()
.definition(spelling, &view.path)
.value()
.and_then(|found| {
crate::semantic::kind_of_index(
crate::index::project::IndexedKind::of(&found.fact),
)
})
};
let Some(kind) = kind else {
continue;
};
names.push(crate::semantic::Name {
range: cpp_parser::SourceRange::new(
block.start_offset + token.range.start_offset,
token.range.length,
),
kind,
declaration: false,
modifiers: Vec::new(),
provenance: crate::semantic::Provenance::Found,
});
}
}
}
names.sort_by_key(|name| (name.range.start_offset, name.range.end_offset()));
names
}
pub fn format_literals(
&self,
view: &FileView,
config: &crate::format::FormatConfig,
) -> Vec<crate::format::FormatLiteral> {
if view.written_text().is_some() {
return Vec::new();
}
let mut qualified_name = |offset: usize| -> Option<String> {
let reading = view.reading_offset_of(FileOffset::new(offset))?;
let Known::Yes(binding) =
crate::sema::resolve::definition_at(&view.scopes, &view.root, reading.get())
else {
return None;
};
let name = binding.name.identifier_text()?;
Some(match view.scopes.qualified_name_of(binding.scope) {
Some(scope) if !scope.is_empty() => format!("{scope}::{name}"),
_ => name.to_string(),
})
};
crate::format::literals_in(
&view.source,
view.tree.get_tokens(),
config,
&mut qualified_name,
)
}
pub fn selection_chain(
&self,
view: &FileView,
offset: FileOffset,
) -> Vec<cpp_parser::SourceRange> {
let Some(written) = view.written_text() else {
return view.selection_chain(ReadingOffset::new(offset.get()));
};
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
crate::file::view::selection_chain_of(&tree.get_red_root(), offset.get())
}
pub fn folding_ranges(&self, view: &FileView) -> Vec<crate::folding::Fold> {
let Some(written) = view.written_text() else {
return crate::folding::folding_ranges(&view.source, view.tree.get_tokens());
};
let (tokens, _) = cpp_parser::lex(written, &cpp_parser::LexerConfig::default());
crate::folding::folding_ranges(written, &tokens)
}
pub fn signatures_at(
&self,
view: &FileView,
offset: ReadingOffset,
) -> Vec<crate::signature::CallSignature> {
let mut signatures =
crate::signature::signatures_at(self.store.index(), view, offset.get(), |path| {
self.view(path)
});
for signature in &mut signatures {
let declared = self.view(&signature.declared_in);
let Some(in_the_file) = declared.as_ref().and_then(|declared| {
declared.file_offset_of(ReadingOffset::new(signature.declared_at))
}) else {
continue;
};
signature.documentation =
self.documentation(view, &signature.declared_in, in_the_file.get());
}
signatures
}
pub fn the_declarations_a_call_means(
&self,
view: &FileView,
offset: ReadingOffset,
parameter_lists: &[Option<String>],
template_parameters: &[Vec<String>],
) -> Option<Vec<usize>> {
let (types, candidates) =
self.the_candidates_of_a_call(view, offset, parameter_lists, template_parameters)?;
match crate::sema::overload::choose(&types, &candidates) {
crate::sema::overload::Choice::One(chosen) => Some(vec![chosen]),
crate::sema::overload::Choice::Ambiguous => Some(
crate::sema::overload::viable_candidates(&types, &candidates),
),
crate::sema::overload::Choice::None => None,
}
}
pub fn the_declaration_a_call_means(
&self,
view: &FileView,
offset: ReadingOffset,
parameter_lists: &[Option<String>],
template_parameters: &[Vec<String>],
) -> Option<usize> {
let (types, candidates) =
self.the_candidates_of_a_call(view, offset, parameter_lists, template_parameters)?;
match crate::sema::overload::choose(&types, &candidates) {
crate::sema::overload::Choice::One(chosen) => Some(chosen),
_ => None,
}
}
fn the_candidates_of_a_call(
&self,
view: &FileView,
offset: ReadingOffset,
parameter_lists: &[Option<String>],
template_parameters: &[Vec<String>],
) -> Option<(
Vec<crate::Known<String>>,
Vec<crate::sema::overload::Candidate>,
)> {
let token = cpp_parser::token_at(&view.root, offset.get())?;
let call = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::CallExpr
})?;
let arguments = crate::inlay::arguments_of(&call)?;
let session: &Session<F> = self;
let index: &crate::ProjectIndex = session.store.index();
let resolver =
Box::new(move |path: &Path| -> Option<FileView> { session.view_of_the_file(path) });
let model = crate::sema::model::SemanticModel::new(
index,
&view.root,
&view.scopes,
&view.source,
&view.path,
resolver,
);
let types: Vec<crate::Known<String>> = arguments
.iter()
.map(|argument| match model.type_of(argument) {
crate::Known::Yes((a_type, _)) => crate::Known::Yes(a_type.to_string()),
crate::Known::No => crate::Known::No,
crate::Known::Unknown(reason) => crate::Known::Unknown(reason),
})
.collect();
let candidates: Vec<crate::sema::overload::Candidate> = parameter_lists
.iter()
.enumerate()
.map(|(at, parameters)| {
let (declared, variadic) = parameters
.as_deref()
.map(crate::sema::overload::parameters_in)
.unwrap_or_default();
let names = template_parameters.get(at).cloned().unwrap_or_default();
let declared_for_ordering = declared.clone();
let (types, is_a_template) =
match crate::sema::deduction::deduce(&names, &declared, &types) {
Some(arguments) => (
declared
.iter()
.map(|spelling| {
crate::sema::types::substituted_spelling(
spelling, &names, &arguments,
)
})
.collect(),
true,
),
None => (declared, !names.is_empty()),
};
let candidate = crate::sema::overload::Candidate::new(
types,
variadic,
format!("declaration {at}"),
);
if is_a_template {
candidate.with_declaration(declared_for_ordering, names.clone())
} else {
candidate
}
})
.collect();
Some((types, candidates))
}
pub fn facts_of_the_view(&self, view: &FileView) -> Vec<crate::DeclFact> {
let preprocessing = crate::preprocess(&view.source, view.tree.get_tokens());
let errors: Vec<cpp_parser::SourceRange> = view
.tree
.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range))
.collect();
crate::build_facts(&view.scopes, &preprocessing, &view.root, &errors, true).0
}
pub fn the_declaration_node(
&self,
file: &Path,
offset: usize,
) -> Option<cpp_parser::CppDeclaration> {
let view = self.view_of_the_file(file)?;
let token = cpp_parser::token_at(&view.root, offset)?;
use cpp_parser::CppAstNode as _;
token
.parent_ancestors()
.find_map(cpp_parser::CppDeclaration::cast)
}
pub fn the_deduced_arguments(
&self,
view: &FileView,
offset: ReadingOffset,
template_parameters: &[String],
parameters: &[String],
) -> Option<Vec<String>> {
if template_parameters.is_empty() {
return None;
}
let token = cpp_parser::token_at(&view.root, offset.get())?;
let call = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::CallExpr
})?;
let arguments = crate::inlay::arguments_of(&call)?;
let session: &Session<F> = self;
let index: &crate::ProjectIndex = session.store.index();
let resolver =
Box::new(move |path: &Path| -> Option<FileView> { session.view_of_the_file(path) });
let model = crate::sema::model::SemanticModel::new(
index,
&view.root,
&view.scopes,
&view.source,
&view.path,
resolver,
);
let types: Vec<crate::Known<String>> = arguments
.iter()
.map(|argument| match model.type_of(argument) {
crate::Known::Yes((a_type, _)) => crate::Known::Yes(a_type.to_string()),
crate::Known::No => crate::Known::No,
crate::Known::Unknown(reason) => crate::Known::Unknown(reason),
})
.collect();
crate::sema::deduction::deduce(template_parameters, parameters, &types)
}
pub fn members_of(&self, view: &FileView, written_type: &str) -> Known<MemberList> {
let listed = members_of(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
written_type,
);
let listed = substituted_members(self.store.index(), &view.path, written_type, listed);
self.the_generated_members(written_type, &view.path, listed)
}
fn the_generated_members(
&self,
written_type: &str,
path: &std::path::Path,
listed: Known<crate::index::MemberList>,
) -> Known<crate::index::MemberList> {
let unreal = self
.project_config()
.config
.unreal
.as_ref()
.is_some_and(|unreal| unreal.is_on());
if !unreal {
return listed;
}
let Known::Yes(mut list) = listed else {
return listed;
};
let declares_static_class = list
.members
.iter()
.any(|member| member.fact.name == "StaticClass");
let already_generated = list
.members
.iter()
.any(|member| member.fact.name == "ThisClass");
if !declares_static_class || already_generated {
return Known::Yes(list);
}
let Known::Yes(declared) = self.store.index().definition(written_type, path) else {
return Known::Yes(list);
};
let class_fact = declared.fact.clone();
let class_name = class_fact.name.clone();
for (name, type_of) in crate::project::ue::generated_members(&class_name, &class_fact.bases)
{
let mut fact = class_fact.clone();
fact.name = name.to_string();
fact.kind = crate::DeclKind::Type;
fact.type_of = Some(type_of);
fact.scope = Some(class_name.clone());
list.members.push(crate::index::ProjectMember {
file: declared.file.clone(),
fact,
declared_in: class_name.clone(),
depth: 0,
ambiguous: false,
});
}
Known::Yes(list)
}
pub fn definition_of_a_written_type(
&self,
view: &FileView,
written: &str,
in_scope: Option<&str>,
at: usize,
) -> Known<ProjectDefinition> {
let index = self.store.index();
let enclosing = match in_scope {
Some(scope) => Some(scope.to_string()),
None => enclosing_namespace_at(view, at),
};
let direct = index.definition_where_written(written, enclosing.as_deref(), &view.path);
if !matches!(direct, Known::Unknown(UnknownReason::NotDeclaredHere(_))) {
return direct;
}
let in_scope = enclosing.as_deref();
let Some((class, member)) = written.rsplit_once("::") else {
return direct;
};
let mut tried: Vec<String> = Vec::new();
if let Some(scope) = in_scope.filter(|_| !class.starts_with("::") && !class.contains("::"))
{
tried.push(format!("{scope}::{class}"));
}
tried.push(class.to_string());
let mut list = None;
for spelling in &tried {
if let Known::Yes(found) = self.members_of(view, spelling) {
list = Some(found);
break;
}
}
let Some(list) = list else {
return direct;
};
let mut found = list
.members
.iter()
.filter(|found| found.fact.name == member);
let Some(only) = found.next() else {
return direct;
};
if found.next().is_some() || only.ambiguous {
return Known::Unknown(UnknownReason::Ambiguous(Box::from(written)));
}
Known::Yes(ProjectDefinition {
file: only.file.clone(),
fact: only.fact.clone(),
})
}
pub fn outline(&self, view: &FileView) -> Vec<OutlineSymbol> {
let preprocessing = crate::preprocess(&view.source, view.tree.get_tokens());
let errors: Vec<cpp_parser::SourceRange> = view
.tree
.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range))
.collect();
let (facts, _guards) =
crate::build_facts(&view.scopes, &preprocessing, &view.root, &errors, true);
crate::outline_of(&facts)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticReading {
Cooked,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpressionType {
pub expression: String,
pub type_of: String,
pub file: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiagnostic {
pub start: usize,
pub end: usize,
pub message: String,
pub code: crate::sema::check::DiagnosticCode,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleNote {
pub message: String,
pub start: usize,
pub end: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiagnostics {
pub reading: DiagnosticReading,
pub unplaced: usize,
pub errors: Vec<FileDiagnostic>,
pub notes: Vec<ModuleNote>,
pub checks: Vec<crate::sema::check::Finding>,
pub checks_withheld: bool,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CookedReading {
pub declarations: usize,
pub only_after_expansion: usize,
pub diagnostics: usize,
pub unplaced: usize,
pub mapped: crate::MapReport,
}
const MAX_UNITS: usize = 8;
const MAX_UNIT_BYTES: usize = 64 * 1024 * 1024;
const MAX_WALKS_BY_CONTENT_ENTRIES: usize = 32;
const MAX_WALKS_BY_CONTENT_BYTES: usize = 16 * 1024 * 1024;
const CONFIG_FOR_FILE_ENTRIES: usize = 256;
const MAX_MODULES_READ_AT_ONCE: usize = 16;
#[derive(Default)]
struct UnitTable {
held: std::collections::HashMap<String, HeldUnit>,
clock: u64,
bytes: usize,
}
impl UnitTable {
fn values(&self) -> impl Iterator<Item = &HeldUnit> {
self.held.values()
}
fn get(&mut self, key: &str) -> Option<(bool, std::sync::Arc<crate::TranslationUnit>)> {
self.clock += 1;
let held = self.held.get_mut(key)?;
held.used = self.clock;
Some((held.provisional, held.unit.clone()))
}
fn insert(
&mut self,
key: String,
unit: std::sync::Arc<crate::TranslationUnit>,
provisional: bool,
) {
self.clock += 1;
let weight = unit.heap_bytes();
if let Some(replaced) = self.held.remove(&key) {
self.bytes = self.bytes.saturating_sub(replaced.weight);
}
while self.held.len() + 1 > MAX_UNITS || self.bytes + weight > MAX_UNIT_BYTES {
if self.held.is_empty() {
break;
}
let smallest = self
.held
.iter()
.min_by_key(|(_, held)| (held.weight, held.used))
.map(|(key, _)| key.clone());
let Some(smallest) = smallest else {
break;
};
if let Some(removed) = self.held.remove(&smallest) {
self.bytes = self.bytes.saturating_sub(removed.weight);
}
}
self.bytes = self.bytes.saturating_add(weight);
self.held.insert(
key,
HeldUnit {
used: self.clock,
provisional,
unit,
weight,
},
);
}
fn remove(&mut self, key: &str) {
if let Some(removed) = self.held.remove(key) {
self.bytes = self.bytes.saturating_sub(removed.weight);
}
}
fn clear(&mut self) {
self.held.clear();
self.bytes = 0;
}
fn accounting(&self) -> (usize, usize) {
(self.held.len(), self.bytes)
}
fn timelines(&self) -> impl Iterator<Item = &std::sync::Arc<crate::TranslationUnit>> {
self.held.values().map(|held| &held.unit)
}
}
struct HeldUnit {
used: u64,
provisional: bool,
unit: std::sync::Arc<crate::TranslationUnit>,
weight: usize,
}
#[derive(Debug, Clone, Copy)]
struct DirectivesMoved {
environment: bool,
layout: bool,
}
#[derive(Debug, Default)]
struct Cooking {
wanted: VecDeque<PathBuf>,
held: HashSet<String>,
}
impl Cooking {
fn want(&mut self, path: &Path) -> bool {
if !self.held.insert(queue_key(path)) {
return false;
}
self.wanted.push_front(path.to_path_buf());
true
}
fn wants(&self, path: &Path) -> bool {
self.held.contains(&queue_key(path))
}
fn forget(&mut self, path: &Path) {
let key = queue_key(path);
if self.held.remove(&key) {
self.wanted.retain(|queued| queue_key(queued) != key);
}
}
fn take(&mut self, how_many: usize) -> Vec<PathBuf> {
let mut taken = Vec::new();
for _ in 0..how_many {
let Some(path) = self.wanted.pop_front() else {
break;
};
self.held.remove(&queue_key(&path));
taken.push(path);
}
taken
}
fn len(&self) -> usize {
self.wanted.len()
}
fn abandon(&mut self) -> usize {
let held = self.wanted.len();
self.wanted.clear();
self.held.clear();
held
}
}
#[derive(Debug, Default)]
struct Work {
open: VecDeque<(PathBuf, usize)>,
rest: VecDeque<(PathBuf, usize)>,
standing: HashMap<String, Standing>,
queued: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Standing {
Queued(Priority),
Worked,
}
impl Work {
fn add(&mut self, path: PathBuf, priority: Priority, depth: usize) -> bool {
let key = queue_key(&path);
match self.standing.get(&key) {
None => {
self.standing.insert(key, Standing::Queued(priority));
self.push(path, priority, depth);
self.queued += 1;
true
}
Some(Standing::Queued(Priority::Rest)) if priority == Priority::Open => {
self.standing.insert(key, Standing::Queued(Priority::Open));
self.push(path, priority, depth);
true
}
Some(_) => false,
}
}
fn again(&mut self, path: PathBuf, priority: Priority, depth: usize) {
self.rearm(path, priority, depth, true);
}
fn requeue(&mut self, path: PathBuf, priority: Priority, depth: usize) {
self.rearm(path, priority, depth, false);
}
fn rearm(&mut self, path: PathBuf, priority: Priority, depth: usize, urgent: bool) {
let key = queue_key(&path);
if !matches!(self.standing.get(&key), Some(Standing::Queued(_))) {
self.queued += 1;
}
self.standing.insert(key, Standing::Queued(priority));
match (priority, urgent) {
(Priority::Open, true) => self.open.push_front((path, depth)),
(Priority::Open, false) => self.open.push_back((path, depth)),
(Priority::Rest, true) => self.rest.push_front((path, depth)),
(Priority::Rest, false) => self.rest.push_back((path, depth)),
}
}
fn forget(&mut self, path: &Path) -> bool {
if !self.is_queued(path) {
return false;
}
self.standing.insert(queue_key(path), Standing::Worked);
self.queued -= 1;
true
}
fn is_queued(&self, path: &Path) -> bool {
matches!(
self.standing.get(&queue_key(path)),
Some(Standing::Queued(_))
)
}
fn pop(&mut self) -> Option<(PathBuf, Priority, usize)> {
loop {
let (path, priority, depth) = match self.open.pop_front() {
Some((path, depth)) => (path, Priority::Open, depth),
None => {
let (path, depth) = self.rest.pop_front()?;
(path, Priority::Rest, depth)
}
};
let key = queue_key(&path);
match self.standing.get(&key) {
Some(Standing::Queued(held)) if *held == priority => {
self.standing.insert(key, Standing::Worked);
self.queued -= 1;
return Some((path, priority, depth));
}
_ => continue,
}
}
}
fn push(&mut self, path: PathBuf, priority: Priority, depth: usize) {
match priority {
Priority::Open => self.open.push_back((path, depth)),
Priority::Rest => self.rest.push_back((path, depth)),
}
}
fn pending(&self) -> usize {
self.queued
}
fn is_worked(&self, path: &Path) -> bool {
matches!(self.standing.get(&queue_key(path)), Some(Standing::Worked))
}
fn mark_worked(&mut self, path: &Path) {
let key = queue_key(path);
match self.standing.get(&key) {
Some(Standing::Queued(_)) => {
self.standing.insert(key, Standing::Worked);
self.queued -= 1;
}
Some(Standing::Worked) => {}
None => {
self.standing.insert(key, Standing::Worked);
}
}
}
fn peek(&self, how_many: usize) -> Vec<PathBuf> {
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
let mut ahead = Vec::new();
let halves = [(&self.open, Priority::Open), (&self.rest, Priority::Rest)];
for (half, priority) in halves {
for (path, _) in half {
if ahead.len() >= how_many {
return ahead;
}
let key = queue_key(path);
let queued_here = matches!(
self.standing.get(&key),
Some(Standing::Queued(held)) if *held == priority
);
if queued_here && seen.insert(key) {
ahead.push(path.clone());
}
}
}
ahead
}
}
fn enclosing_namespace_at(view: &FileView, at: usize) -> Option<String> {
let scope = view.scopes.scope_at(at)?;
let mut segments: Vec<&str> = Vec::new();
for id in view.scopes.scope_chain(scope) {
let Some(data) = view.scopes.scope(id) else {
continue;
};
if data.kind == crate::ScopeKind::Namespace
&& let Some(name) = data.name.as_deref()
{
segments.push(name);
}
}
if segments.is_empty() {
return None;
}
segments.reverse();
Some(segments.join("::"))
}
fn queue_key(path: &Path) -> String {
normalize_path(path, cfg!(windows))
}
fn the_compilation_is_known(toolchain: Option<&Toolchain>) -> bool {
toolchain.is_some_and(|found| !found.builtin_macros.is_empty())
}
fn library_files_to_read(
libraries: &[crate::project::LibrarySection],
workspace: &std::path::Path,
) -> Vec<PathBuf> {
const HEADER_EXTENSIONS: &[&str] = &["h", "hpp", "hxx", "h++", "inl", "ipp"];
const MAX_DEPTH: usize = 12;
const MAX_FILES: usize = 20_000;
let mut found: Vec<PathBuf> = Vec::new();
for library in libraries {
let mut roots: Vec<PathBuf> = library.include_directories(workspace);
roots.extend(library.module_directories(workspace));
let mut stack: Vec<(PathBuf, usize)> = roots.into_iter().map(|root| (root, 0)).collect();
while let Some((directory, depth)) = stack.pop() {
if depth > MAX_DEPTH || found.len() >= MAX_FILES {
continue;
}
let Ok(entries) = std::fs::read_dir(&directory) else {
continue;
};
for entry in entries.flatten() {
let path = entry.path();
let Ok(kind) = entry.file_type() else {
continue;
};
if kind.is_dir() {
stack.push((path, depth + 1));
continue;
}
let extension = path
.extension()
.and_then(|extension| extension.to_str())
.map(|extension| extension.to_lowercase());
let wanted = match extension.as_deref() {
Some("cppm") => true,
Some(other) => HEADER_EXTENSIONS.contains(&other),
None => false,
};
if wanted {
found.push(path);
}
}
}
}
found.sort();
found.dedup();
found
}
fn after_the_includes(text: &str) -> usize {
let mut end = 0usize;
let mut offset = 0usize;
for line in text.split_inclusive('\n') {
if line.trim_start().starts_with("#include") {
end = offset + line.len();
}
offset += line.len();
}
end
}
fn after_the_module_imports(text: &str) -> usize {
let mut end = 0usize;
let mut offset = 0usize;
for line in text.split_inclusive('\n') {
let trimmed = line.trim_start();
let imports = trimmed.starts_with("import ") && trimmed.trim_end().ends_with(';');
let declares_a_module = (trimmed.starts_with("module ")
|| trimmed.starts_with("export module "))
&& trimmed.trim_end().ends_with(';');
if imports || declares_a_module {
end = offset + line.len();
}
offset += line.len();
}
end
}
fn apply_project_config(filter: WatchFilter, report: &ConfigReport) -> WatchFilter {
let mut filter = filter;
for pattern in &report.config.workspace.exclude {
if let Ok(pattern) = crate::PathPattern::new(pattern) {
filter = filter.ignore_pattern(pattern);
}
}
if let Some(cache_dir) = &report.config.index.cache_dir {
filter = filter.with_cache_directory(cache_dir);
}
if report
.config
.unreal
.as_ref()
.is_some_and(|unreal| unreal.is_on())
{
for pattern in [
"Intermediate/**",
"**/Intermediate/**",
"Binaries/**",
"Saved/**",
"DerivedDataCache/**",
] {
if let Ok(pattern) = crate::PathPattern::new(pattern) {
filter = filter.ignore_pattern(pattern);
}
}
}
filter
}
fn project_files(
files: &impl FileProvider,
filter: &WatchFilter,
root: &Path,
database: Option<&CompileCommands>,
extra_extensions: &[String],
max_files: usize,
) -> Vec<PathBuf> {
let mut found = Vec::new();
if let Some(database) = database {
for command in &database.commands {
let path = match (&command.directory, command.file.is_relative()) {
(Some(directory), true) => directory.join(&command.file),
_ => command.file.clone(),
};
if files.exists(&path) && !filter.is_ignored(&path) {
found.push(path);
}
}
}
if found.is_empty() {
scan(root, filter, &mut found, extra_extensions, max_files);
}
found.sort();
found.dedup();
found
}
fn scan(
root: &Path,
filter: &WatchFilter,
found: &mut Vec<PathBuf>,
extensions: &[String],
max_files: usize,
) {
let mut pending = vec![root.to_path_buf()];
while let Some(directory) = pending.pop() {
if found.len() >= max_files {
return;
}
let Ok(entries) = std::fs::read_dir(&directory) else {
continue;
};
for entry in entries.flatten() {
if found.len() >= max_files {
return;
}
let path = entry.path();
if filter.is_ignored(&path) {
continue;
}
let Ok(kind) = entry.file_type() else {
continue;
};
if kind.is_symlink() {
continue;
}
if kind.is_dir() {
pending.push(path);
} else if kind.is_file() && is_a_source_file(&path, extensions) {
found.push(path);
}
}
}
}
fn is_a_source_file(path: &Path, extra: &[String]) -> bool {
path.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| {
let lower = extension.to_ascii_lowercase();
SOURCE_EXTENSIONS.contains(&lower.as_str())
|| extra
.iter()
.any(|listed| listed.trim_start_matches('.').eq_ignore_ascii_case(&lower))
})
}
#[cfg(test)]
mod tests;
const RENDERING_SUMMARY_ENTRIES: usize = 16;
const RENDERING_SUMMARY_BYTES: usize = 40_000;