use std::path::{Path, PathBuf};
use std::time::Instant;
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
fn project() -> PathBuf {
PathBuf::from(
std::env::var("CPPLS_REAL_PROJECT")
.unwrap_or_else(|_| r"C:\Users\zc\Desktop\test_cpp".to_string()),
)
}
#[test]
#[ignore = "a probe: opens a real project and prints instead of asserting"]
fn what_a_name_query_can_see() {
let root = project();
let entry = root.join(
std::env::var("CPPLS_VISIBILITY_FILE").unwrap_or_else(|_| "src/containers.cpp".to_string()),
);
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files([entry.clone()]);
session.index_everything();
let text = std::fs::read_to_string(&entry).expect("the entry file is readable");
session.did_open(&entry, &text);
let started = Instant::now();
let mut slices = 0usize;
while session.pending_work() > 0 && slices < 400 {
session.advance(64);
slices += 1;
}
println!(
"pumped {slices} slice(s) in {:?}, {} work item(s) left",
started.elapsed(),
session.pending_work()
);
let visible = session.index().visible_files_with_modules(&entry);
println!("closure: {} file(s)", visible.0.len());
let wanted = [
"iostream",
"string",
"bits/basic_string.h",
"bits/stl_vector.h",
];
for tail in wanted {
let found = visible.0.iter().any(|(path, _)| path.ends_with(tail));
println!(
" {tail:<24} {}",
if found { "visible" } else { "NOT VISIBLE" }
);
}
for name in [
"std::cout",
"std::to_string",
"std::string",
"std::vector",
"std::basic_string",
"substr",
"to_string",
] {
let started = Instant::now();
let answer = session.index().definitions(name, &entry);
let shown = match &answer {
cpp_code_analysis::Known::Yes(found) => format!(
"{} declaration(s), first in {}",
found.found.len(),
found
.found
.first()
.map(|one| Path::new(&one.file)
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string())
.unwrap_or_default()
),
cpp_code_analysis::Known::Unknown(reason) => format!("unknown — {reason:?}"),
cpp_code_analysis::Known::No => "no declaration".to_string(),
};
println!(
"{name:<20} {:>8.1} ms {shown}",
started.elapsed().as_secs_f64() * 1000.0
);
}
}