use super::*;
impl<F: FileProvider + Clone> Session<F> {
pub fn the_members_a_concept_requires(
&self,
view: &FileView,
at: usize,
replace: cpp_parser::SourceRange,
) -> Vec<crate::CompletionItem> {
let Some(access) = crate::sema::resolve::member_access_at(&view.root, at) else {
return Vec::new();
};
if !access.member.is_empty() {
return Vec::new();
}
let concepts = self.the_concepts_of_an_expression(view, &access.object, 0);
let mut items: Vec<crate::CompletionItem> = Vec::new();
for concept in &concepts {
let Some(required) = self.the_members_the_concept_names(concept, &view.path) else {
continue;
};
for (name, called) in required {
if items.iter().any(|item| item.label == name) {
continue;
}
let mut item = crate::CompletionItem::word(
&name,
if called {
crate::completion::ItemKind::Method
} else {
crate::completion::ItemKind::Field
},
&format!("required by concept `{concept}`"),
);
item.replace = replace;
items.push(item);
}
}
items
}
fn the_concepts_of_an_expression(
&self,
view: &FileView,
expression: &cpp_parser::CppSyntaxNode,
depth: usize,
) -> Vec<String> {
if depth > 8 {
return Vec::new();
}
if cpp_parser::CppSyntaxKind::from(expression.kind()) == cpp_parser::CppSyntaxKind::CallExpr
&& let Some(member) = the_member_a_call_names(expression)
&& let Some(callee) = expression.children().next()
&& let Some(receiver) = callee.children().next()
{
let mut found: Vec<String> = Vec::new();
for concept in self.the_concepts_of_an_expression(view, &receiver, depth + 1) {
for result in self.the_result_concepts_of(&concept, &view.path, &member) {
if !found.contains(&result) {
found.push(result);
}
}
}
return found;
}
let at = usize::from(expression.text_range().start());
let Known::Yes(binding) = crate::sema::resolve::definition_at(&view.scopes, &view.root, at)
else {
return Vec::new();
};
the_concepts_constraining(&view.root, binding.name_range.start_offset)
}
fn the_result_concepts_of(&self, concept: &str, path: &Path, member: &str) -> Vec<String> {
let Some(found) = self.store.index().definition(concept, path).value() else {
return Vec::new();
};
let Some(view) = self.view_of_the_file(&found.file) else {
return Vec::new();
};
let Some(token) = cpp_parser::token_at(&view.root, found.fact.name_range.start_offset)
else {
return Vec::new();
};
let Some(concept_node) = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::ConceptDecl
}) else {
return Vec::new();
};
let mut concepts: Vec<String> = Vec::new();
for requirement in concept_node.descendants().filter(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::Requirement
}) {
let text = requirement.text().to_string();
let Some(arrow) = text.find("->") else {
continue;
};
let expression = text[..arrow].trim();
if !expression.contains(&format!(".{member}("))
&& !expression.contains(&format!("->{member}("))
{
continue;
}
let constraint = text[arrow + 2..].trim().trim_end_matches(';').trim();
for concept in the_concepts_a_constraint_names(constraint) {
if !concepts.contains(&concept) {
concepts.push(concept);
}
}
}
concepts
}
fn the_members_the_concept_names(
&self,
concept: &str,
path: &Path,
) -> Option<Vec<(String, bool)>> {
let mut names: Vec<(String, bool)> = Vec::new();
let mut visited: Vec<String> = Vec::new();
self.collect_the_members_of_a_concept(concept, path, 0, &mut visited, &mut names);
Some(names)
}
fn collect_the_members_of_a_concept(
&self,
concept: &str,
path: &Path,
depth: usize,
visited: &mut Vec<String>,
names: &mut Vec<(String, bool)>,
) {
if depth > 8 || visited.iter().any(|seen| seen == concept) {
return;
}
visited.push(concept.to_string());
let Some(found) = self.store.index().definition(concept, path).value() else {
return;
};
let Some(view) = self.view_of_the_file(&found.file) else {
return;
};
let Some(token) = cpp_parser::token_at(&view.root, found.fact.name_range.start_offset)
else {
return;
};
let Some(concept_node) = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::ConceptDecl
}) else {
return;
};
for requires in concept_node.descendants().filter(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::RequiresExpr
}) {
let parameters = the_parameter_names_of(&requires);
if parameters.is_empty() {
continue;
}
for node in requires.descendants() {
let kind = cpp_parser::CppSyntaxKind::from(node.kind());
if !matches!(
kind,
cpp_parser::CppSyntaxKind::IndexExpr | cpp_parser::CppSyntaxKind::ArrowExpr
) {
continue;
}
let Some(access) = crate::sema::resolve::member_access_of(&node) else {
continue;
};
if access.member.is_empty() {
continue;
}
let Some(object) = node.children().next() else {
continue;
};
let object = object.text().to_string();
let object = object.trim();
if !parameters.iter().any(|parameter| parameter == object) {
continue;
}
let called = node.parent().is_some_and(|parent| {
cpp_parser::CppSyntaxKind::from(parent.kind())
== cpp_parser::CppSyntaxKind::CallExpr
});
if !names.iter().any(|(name, _)| name == &access.member) {
names.push((access.member.clone(), called));
}
}
}
for other in the_concepts_a_body_names(&concept_node) {
self.collect_the_members_of_a_concept(&other, &found.file, depth + 1, visited, names);
}
}
}
fn the_concepts_constraining(root: &cpp_parser::CppSyntaxNode, name: usize) -> Vec<String> {
let mut concepts: Vec<String> = Vec::new();
let Some(token) = cpp_parser::token_at(root, name) else {
return concepts;
};
let Some(list) = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::ParameterList
}) else {
return concepts;
};
let text = list.text().to_string();
let start = usize::from(list.text_range().start());
let Some(before) = text.get(..name.saturating_sub(start)) else {
return concepts;
};
let parameter = before.rsplit(',').next().unwrap_or_default().trim();
let words = the_words(parameter);
if let Some(at) = words.iter().position(|word| *word == "auto") {
if let Some(constraint) = at.checked_sub(1).and_then(|at| words.get(at)) {
concepts.push((*constraint).to_string());
}
} else if let Some(written_type) = words.last()
&& let Some(constraint) = the_head_constraint_of(&list, written_type)
{
concepts.push(constraint);
}
let declaration = list.ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::Declaration
});
if let Some(clause) = declaration.and_then(|declaration| {
declaration.descendants().find(|child| {
cpp_parser::CppSyntaxKind::from(child.kind())
== cpp_parser::CppSyntaxKind::RequiresClause
})
}) {
for concept in the_concepts_a_constraint_names(&clause.text().to_string()) {
if !concepts.contains(&concept) {
concepts.push(concept);
}
}
}
concepts
}
fn the_head_constraint_of(list: &cpp_parser::CppSyntaxNode, written_type: &str) -> Option<String> {
let declaration = list.ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::Declaration
})?;
let head = declaration.ancestors().find_map(|ancestor| {
let head = ancestor.children().find(|child| {
cpp_parser::CppSyntaxKind::from(child.kind())
== cpp_parser::CppSyntaxKind::TemplateParameterList
});
head.or_else(|| {
ancestor
.children()
.find(|child| {
cpp_parser::CppSyntaxKind::from(child.kind())
== cpp_parser::CppSyntaxKind::TemplateDecl
})
.and_then(|introduced| {
introduced.children().find(|child| {
cpp_parser::CppSyntaxKind::from(child.kind())
== cpp_parser::CppSyntaxKind::TemplateParameterList
})
})
})
})?;
let mut heads: Vec<String> = head
.children()
.map(|parameter| parameter.text().to_string())
.collect();
if heads.is_empty() {
let text = head.text().to_string();
let inside = text.trim_start_matches('<').trim_end_matches('>');
heads = inside.split(',').map(|part| part.to_string()).collect();
}
for parameter in heads {
let words = the_words(¶meter);
let Some(last) = words.last() else {
continue;
};
if *last != written_type || words.len() < 2 {
continue;
}
let constraint = words[words.len() - 2];
if matches!(constraint, "class" | "typename" | "template" | "concept") {
continue;
}
return Some(constraint.to_string());
}
None
}
fn the_concepts_a_constraint_names(text: &str) -> Vec<String> {
let body = text.trim().trim_start_matches("requires").trim();
let mut names: Vec<String> = Vec::new();
for term in body.split("&&").flat_map(|part| part.split("||")) {
let term = term.trim_matches(|character: char| {
character == '(' || character == ')' || character.is_whitespace()
});
let name: String = term
.chars()
.take_while(|character| character.is_alphanumeric() || *character == '_')
.collect();
if !name.is_empty()
&& !matches!(name.as_str(), "true" | "false" | "requires")
&& !names.contains(&name)
{
names.push(name);
}
}
names
}
fn the_member_a_call_names(call: &cpp_parser::CppSyntaxNode) -> Option<String> {
let callee = call.children().next()?;
let access = crate::sema::resolve::member_access_of(&callee)?;
(!access.member.is_empty()).then_some(access.member.clone())
}
fn the_words(text: &str) -> Vec<&str> {
text.split(|character: char| !character.is_alphanumeric() && character != '_')
.filter(|word| !word.is_empty())
.collect()
}
fn the_concepts_a_body_names(node: &cpp_parser::CppSyntaxNode) -> Vec<String> {
let text = node.text().to_string();
let after_the_keyword = text.find("concept").map_or(0, |at| at + "concept".len());
let Some(equals) = text[after_the_keyword..].find('=') else {
return Vec::new();
};
let body = text[after_the_keyword + equals + 1..].trim();
let body = body.trim_end_matches(';').trim();
if body.starts_with("requires") {
return Vec::new();
}
body.split("&&")
.flat_map(|part| part.split("||"))
.filter_map(|term| {
let term = term.trim_matches(|character: char| {
character == '(' || character == ')' || character.is_whitespace()
});
let name: String = term
.chars()
.take_while(|character| character.is_alphanumeric() || *character == '_')
.collect();
(!name.is_empty() && !matches!(name.as_str(), "true" | "false" | "requires"))
.then_some(name)
})
.collect()
}
fn the_parameter_names_of(requires: &cpp_parser::CppSyntaxNode) -> Vec<String> {
let Some(open) = requires.text().to_string().find('(') else {
return Vec::new();
};
let Some(close) = requires.text().to_string()[open..].find(')') else {
return Vec::new();
};
let inside = &requires.text().to_string()[open + 1..open + close];
inside
.split(',')
.filter_map(|parameter| {
parameter
.rsplit(|character: char| !character.is_alphanumeric() && character != '_')
.find(|word| !word.is_empty())
.map(|word| word.to_string())
})
.collect()
}