use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
fn session_with(files: &[(&str, &str)]) -> Session<MemoryFiles> {
let mut memory = MemoryFiles::new();
for (path, source) in files {
memory = memory.with_file(*path, *source);
}
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files(
files
.iter()
.map(|(path, _)| std::path::PathBuf::from(*path)),
);
for (path, _) in files {
session.want_cooked_reading(std::path::Path::new(path));
}
session.index_everything();
session
}
fn hints_in(files: &[(&str, &str)], path: &str) -> Vec<(usize, String)> {
let session = session_with(files);
let view = session.view(path).expect("the file is held");
let whole_file = cpp_parser::SourceRange::new(0, view.source.len());
session
.inlay_hints(&view, whole_file)
.into_iter()
.map(|hint| (hint.offset, hint.name))
.collect()
}
fn hints_of(source: &str) -> Vec<(usize, String)> {
hints_in(&[("/p/a.cpp", source)], "/p/a.cpp")
}
#[test]
fn a_call_reports_the_parameters_of_its_callee() {
let source = "\
double scale(int count, double factor);
double f() { return scale(3, 0.5); }
";
let call = source.find("scale(3").expect("the fixture") + "scale(".len();
assert_eq!(
hints_of(source),
vec![
(call, "count".to_string()),
(call + "3, ".len(), "factor".to_string()),
]
);
}
#[test]
fn a_call_into_an_uncooked_header_is_still_hinted() {
let declaration = "/p/stdio.h";
let source = "int printf(const char* __format, ...);\n";
let caller = "#include \"stdio.h\"\nint main() { printf(\"x = %d\\n\", 123); }\n";
let mut memory = MemoryFiles::new();
memory = memory.with_file(declaration, source);
memory = memory.with_file("/p/a.cpp", caller);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([
std::path::PathBuf::from(declaration),
std::path::PathBuf::from("/p/a.cpp"),
]);
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
let view = session.view("/p/a.cpp").expect("the caller is held");
assert!(
session.view(declaration).is_none(),
"the fixture is only meaningful while the declaration's file has no rendering"
);
let argument = caller
.find("\"x = %d")
.expect("the argument is in the file");
let whole_file = cpp_parser::SourceRange::new(0, view.source.len());
let hints: Vec<(usize, String)> = session
.inlay_hints(&view, whole_file)
.into_iter()
.map(|hint| (hint.offset, hint.name))
.collect();
assert_eq!(
hints,
vec![(argument, "__format".to_string())],
"one hint, for the named parameter; `...` has no name to show.\n\
rendering: {:?}",
view.source
);
}
#[test]
fn only_the_arguments_of_a_call_are_hinted() {
let source = "\
int twice(int value);
int f(int other) { return twice(other); }
";
let argument = source.find("twice(other").expect("the fixture") + "twice(".len();
assert_eq!(hints_of(source), vec![(argument, "value".to_string())]);
}
#[test]
fn an_argument_that_already_spells_the_parameter_is_not_hinted() {
let source = "\
double scale(int count, double factor);
double f(int count, double factor) { return scale(count, factor); }
";
assert!(hints_of(source).is_empty());
}
#[test]
fn an_unnamed_parameter_does_not_shift_the_others() {
let source = "\
void log(int, const char* message);
void f() { log(1, \"hi\"); }
";
let second = source.find("1, ").expect("the fixture") + "1, ".len();
assert_eq!(
hints_of(source),
vec![(second, "message".to_string())],
"the first parameter has no name, so only the second argument is hinted"
);
}
#[test]
fn a_variadic_tail_is_not_hinted() {
let source = "\
void log(const char* format, ...);
void f() { log(\"%d %d\", 1, 2); }
";
let format = source.find("\"%d %d\"").expect("the fixture");
assert_eq!(
hints_of(source),
vec![(format, "format".to_string())],
"the two integers have no parameter to be named after"
);
}
#[test]
fn a_member_call_reports_the_members_parameters() {
let source = "\
struct Widget {
int size_of(int scale) const;
};
int f(Widget& w) { return w.size_of(3); }
";
let argument = source.find("size_of(3").expect("the fixture") + "size_of(".len();
assert_eq!(hints_of(source), vec![(argument, "scale".to_string())]);
}
#[test]
fn a_call_of_a_class_is_not_hinted() {
let source = "\
struct Widget {
int width;
int height;
Widget(int w, int h);
};
Widget make() { return Widget(3, 4); }
";
assert!(hints_of(source).is_empty());
}
#[test]
fn an_unresolvable_callee_is_not_hinted() {
let source = "\
double f() { return unknown(1, 2); }
";
assert!(hints_of(source).is_empty());
}
#[test]
fn a_callee_in_another_file_reports_its_parameters() {
let header = "double scale(int count, double factor);\n";
let source = "#include \"b.h\"\ndouble f() { return scale(3, 0.5); }\n";
let call = source.find("scale(3").expect("the fixture") + "scale(".len();
assert_eq!(
hints_in(&[("/p/a.cpp", source), ("/p/b.h", header)], "/p/a.cpp"),
vec![
(call, "count".to_string()),
(call + "3, ".len(), "factor".to_string()),
]
);
}
#[test]
fn every_call_is_hinted_separately() {
let header = "double scale(int count, double factor);\n";
let source = "\
#include \"b.h\"
double f() { return scale(3, 0.5); }
double g() { return scale(4, 1.5); }
";
let hints = hints_in(&[("/p/a.cpp", source), ("/p/b.h", header)], "/p/a.cpp");
assert_eq!(hints.len(), 4, "{hints:?}");
assert_eq!(hints[0].1, "count");
assert_eq!(hints[1].1, "factor");
assert_eq!(hints[2].1, "count");
assert_eq!(hints[3].1, "factor");
}
#[test]
fn only_arguments_inside_the_range_are_hinted() {
let source = "\
double scale(int count, double factor);
double f() { return scale(3, 0.5); }
";
let session = session_with(&[("/p/a.cpp", source)]);
let view = session.view("/p/a.cpp").expect("the file is held");
let first = source.find("scale(3").expect("the fixture") + "scale(".len();
let partial = cpp_parser::SourceRange::new(0, first + 1);
let hints = session.inlay_hints(&view, partial);
assert_eq!(hints.len(), 1, "{hints:?}");
assert_eq!(hints[0].name, "count");
}
#[test]
fn a_call_of_a_variable_is_not_hinted() {
let source = "\
int f(int y) {
int x = 0;
return x(1);
}
";
assert!(hints_of(source).is_empty(), "{:?}", hints_of(source));
}
#[test]
fn a_call_of_a_lambda_variable_is_not_hinted() {
let source = "\
int f() {
auto g = [](int a) { return a; };
return g(1);
}
";
assert!(hints_of(source).is_empty(), "{:?}", hints_of(source));
}
#[test]
fn a_member_called_through_this_is_hinted() {
let source = "\
struct S {
int other(int c) const;
int go() { return this->other(2); }
};
";
let argument = source.find("other(2").expect("the fixture") + "other(".len();
assert_eq!(hints_of(source), vec![(argument, "c".to_string())]);
}
#[test]
fn a_declaration_the_scope_model_does_not_bind_is_not_hinted() {
let source = "void (*f(int a))(int b);\nvoid g() { f(1); }\n";
let session = session_with(&[("/p/a.cpp", source)]);
let view = session.view("/p/a.cpp").expect("the file is held");
assert!(
matches!(
session.definition(
&view,
cpp_code_analysis::file::coordinates::ReadingOffset::new(
source.rfind("f(1)").expect("the call")
)
),
cpp_code_analysis::Known::Unknown(_)
),
"this declaration shape is not bound, so nothing resolves the call"
);
assert!(hints_of(source).is_empty());
}
#[test]
fn a_call_through_a_function_pointer_is_not_hinted() {
let source = "\
void h(int (*cb)(int a)) {
cb(1);
}
";
assert!(hints_of(source).is_empty(), "{:?}", hints_of(source));
}
#[test]
fn a_call_with_no_arguments_has_no_hints() {
assert!(hints_of("int f();\nint g() { return f(); }\n").is_empty());
}