use tree_sitter::Node;
use super::{collect_path_idents, field_name, GrammarSpec};
use crate::types::SymbolKind;
pub(super) fn spec() -> GrammarSpec {
GrammarSpec {
language: || tree_sitter_rust::LANGUAGE.into(),
symbol_kind,
symbol_name: field_name,
is_function_kind: |kind| kind == "function_item",
is_call_kind: |kind| kind == "call_expression",
callee_name,
collect_imports,
scope_name,
}
}
fn scope_name(node: Node, bytes: &[u8]) -> Option<String> {
match node.kind() {
"mod_item" | "trait_item" | "function_item" => field_name(node, bytes),
"impl_item" => {
let ty = node
.child_by_field_name("type")
.and_then(|n| n.utf8_text(bytes).ok())?;
match node
.child_by_field_name("trait")
.and_then(|n| n.utf8_text(bytes).ok())
{
Some(tr) => Some(format!("{ty}@{tr}")),
None => Some(ty.to_string()),
}
}
_ => None,
}
}
fn symbol_kind(node: Node, _bytes: &[u8]) -> Option<SymbolKind> {
let kind = match node.kind() {
"function_item" => SymbolKind::Function,
"struct_item" => SymbolKind::Struct,
"enum_item" => SymbolKind::Enum,
"union_item" => SymbolKind::Struct,
"trait_item" => SymbolKind::Interface,
"mod_item" => SymbolKind::Module,
"const_item" | "static_item" => SymbolKind::Constant,
"type_item" => SymbolKind::TypeAlias,
_ => return None,
};
Some(kind)
}
fn callee_name(call: Node, bytes: &[u8]) -> Option<String> {
let func = call.child_by_field_name("function")?;
match func.kind() {
"identifier" => func.utf8_text(bytes).ok().map(str::to_string),
"field_expression" => func
.child_by_field_name("field")
.and_then(|n| n.utf8_text(bytes).ok())
.map(str::to_string),
"scoped_identifier" => func
.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok())
.map(str::to_string),
"generic_function" => func.child_by_field_name("function").and_then(|f| {
callee_of(f, bytes)
}),
_ => None,
}
}
fn callee_of(func: Node, bytes: &[u8]) -> Option<String> {
match func.kind() {
"identifier" => func.utf8_text(bytes).ok().map(str::to_string),
"field_expression" => func
.child_by_field_name("field")
.and_then(|n| n.utf8_text(bytes).ok())
.map(str::to_string),
"scoped_identifier" => func
.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok())
.map(str::to_string),
_ => None,
}
}
fn collect_imports(node: Node, bytes: &[u8], out: &mut Vec<String>) {
match node.kind() {
"use_declaration" => collect_path_idents(node, bytes, &["crate", "self", "super"], out),
"mod_item" if node.child_by_field_name("body").is_none() => {
if let Some(name) = field_name(node, bytes) {
out.push(name);
}
}
_ => {}
}
}