use std::path::PathBuf;
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
use cpp_ls::handlers::implementation::definitions_of;
fn project() -> PathBuf {
PathBuf::from(
std::env::var("CPPLS_REAL_PROJECT").unwrap_or_else(|_| r"E:\EmmyLuaCodeStyle".to_string()),
)
}
#[test]
#[ignore = "indexes a real project; run with --ignored"]
fn a_header_declaration_answers_the_source_that_defines_it() {
let project = project();
let header = project.join("CodeFormatCLib/src/codeformat.h");
let source = project.join("CodeFormatCLib/src/CodeFormat.cpp");
let header_text = std::fs::read_to_string(&header).expect("the header reads");
let mut session = Session::open(
project.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&project),
);
session.add_project_files([source.clone()]);
session.index_everything();
session.load(&header);
let Some(held) = session.files().held(&header) else {
panic!("the header is readable");
};
let view =
cpp_code_analysis::FileView::parse_with(held, &cpp_parser::MacroEnvironment::default());
let mut asked = 0;
for (at, _) in header_text.match_indices("UpdateCodeStyle") {
let line = header_text[..at].matches('\n').count();
let column = at
- header_text[..at]
.rfind('\n')
.map_or(0, |newline| newline + 1);
let offset = at + 1;
asked += 1;
let lexer_header = project.join("LuaParser/include/LuaParser/Lexer/LuaLexer.h");
let lexer_source = project.join("LuaParser/src/Lexer/LuaLexer.cpp");
if lexer_header.exists() && lexer_source.exists() {
let header = std::fs::read_to_string(&lexer_header).expect("the header reads");
let Some(raw) = header.find("GetErrors").map(|at| at + 1) else {
panic!("the header declares GetErrors");
};
let mut session = Session::open(
project.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&project),
);
session.add_project_files([lexer_header.clone(), lexer_source.clone()]);
session.index_everything();
let view = session
.view_of_the_file(&lexer_header)
.expect("the header parses");
let found = definitions_of(
&session,
&view,
cpp_code_analysis::file::coordinates::ReadingOffset::new(raw).get(),
);
println!(
"GetErrors at header offset {raw} → {:?}",
found
.iter()
.map(|location| format!(
"{}:{}",
location.uri.path(),
location.range.start.line + 1
))
.collect::<Vec<_>>()
);
assert!(
found
.iter()
.any(|location| location.uri.path().ends_with("LuaLexer.cpp")),
"the definition in the .cpp: {found:?}"
);
}
let found = definitions_of(&session, &view, offset);
println!(
"line {} col {} → {} location(s): {:?}",
line + 1,
column,
found.len(),
found
.iter()
.map(|location| format!(
"{}:{}",
location.uri.path(),
location.range.start.line + 1
))
.collect::<Vec<_>>()
);
assert!(
!found.is_empty(),
"`{}` declares `UpdateCodeStyle` and `{}` defines it out of line",
header.display(),
source.display()
);
assert!(
found.iter().any(|location| location
.uri
.path()
.to_ascii_lowercase()
.ends_with("codeformat.cpp")),
"the implementation is in the source file: {found:?}"
);
}
assert!(asked > 0, "the fixture names the function");
}