#[test]
#[ignore = "reads one real file; run with --ignored and CPPLS_FILE set"]
fn one_file_reports_its_findings_with_context() {
use cpp_code_analysis::file::coordinates::ReadingOffset;
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
use std::path::PathBuf;
let Ok(file) = std::env::var("CPPLS_FILE") else {
eprintln!("skipped: set CPPLS_FILE to a source file");
return;
};
let path = PathBuf::from(file);
let root = path.parent().expect("a file has a directory").to_path_buf();
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files([path.clone()]);
session.index_everything();
session.cook(&path);
let Some(view) = session.view(&path) else {
panic!("the file is readable");
};
let text = std::fs::read_to_string(&path).expect("the file is text");
let lines: Vec<&str> = text.lines().collect();
let Some(answer) = session.diagnostics(&path) else {
panic!("the file is analysed");
};
for finding in &answer.checks {
let at = view.file_offset_of(ReadingOffset::new(finding.range.start_offset));
let line = at.map(|at| text[..at.get().min(text.len())].matches('\n').count());
if let Some(at) = at
&& let Some(scope) = view.scopes.scope_at(finding.range.start_offset)
{
let chain: Vec<String> = view
.scopes
.scope_chain(scope)
.into_iter()
.filter_map(|id| {
view.scopes.scope(id).map(|data| {
format!("{:?}{}", data.kind, data.name.clone().unwrap_or_default())
})
})
.collect();
println!(
" {:?} at {:?} in scopes: {chain:?}",
finding.code,
line.map(|line| line + 1)
);
{
let at = at.get();
let spelled = text.get(
at..(at + (finding.range.end_offset() - finding.range.start_offset))
.min(text.len()),
);
println!(
" name={:?} spelled={spelled:?} reading={} written_here={}",
finding.name,
finding.range.start_offset,
view.was_written_here(finding.range)
);
}
}
if let Some(line) = line {
for (index, text) in lines
.iter()
.enumerate()
.skip(line.saturating_sub(2))
.take(5)
{
println!(" {}: {}", index + 1, text);
}
}
}
}