use std::path::PathBuf;
use cpp_code_analysis::file::coordinates::FileOffset;
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
struct Project {
root: PathBuf,
}
impl Project {
fn new(name: &str) -> Project {
let root = std::env::temp_dir()
.join("cpp_ls-completion-config")
.join(format!("{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("a scratch directory");
Project { root }
}
fn root(&self) -> &std::path::Path {
&self.root
}
fn write(&self, name: &str, text: &str) -> PathBuf {
let path = self.root.join(name);
std::fs::write(&path, text).expect("the file is written");
path
}
}
impl Drop for Project {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.root);
}
}
const HEADER: &str = "\
namespace wide {
struct One {};
struct Two {};
struct Three {};
struct Four {};
struct Five {};
struct Six {};
struct Seven {};
struct Eight {};
}
";
const SOURCE: &str = "\
#include \"wide.h\"
void f() {
wide::
}
";
fn completions(project: &Project) -> (usize, bool) {
let main = project.write("main.cpp", SOURCE);
let header = project.write("wide.h", HEADER);
let mut session = Session::open(
project.root().to_path_buf(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(project.root()),
);
session.did_open(&main, SOURCE);
session.add_project_files([main.clone(), header]);
session.index_everything();
let view = session.view_of_the_file(&main).expect("a view");
let at = SOURCE.find("wide::").expect("the fixture") + "wide::".len();
let offered = session.completions(&view, FileOffset::new(at));
(offered.items.len(), offered.truncated)
}
#[test]
fn the_completion_budget_is_a_project_setting() {
let unconfigured = Project::new("unconfigured");
let (count, truncated) = completions(&unconfigured);
assert_eq!(count, 8, "all eight names are offered, and only those");
assert!(!truncated, "and the list is complete");
let configured = Project::new("configured");
configured.write(".cppls.toml", "[completion]\nmax_items = 3\n");
let report = cpp_code_analysis::project::load_config(&DiskFiles, configured.root(), None);
assert_eq!(
report.config.completion.max_items,
Some(3),
"`[completion] max_items` is a section the parser reads: {:?}",
report.problems
);
let (count, truncated) = completions(&configured);
assert_eq!(count, 3, "the project asked for three");
assert!(
truncated,
"and a cut list reports itself, so the client asks again as the reader types"
);
}