use crate::models::EntityKind;
use crate::pipeline::parser::utils::{find_parent_by_kind, node_text};
use tree_sitter::Node;
pub(crate) struct CsharpCapture<'a> {
pub name: String,
pub kind: EntityKind,
pub start_line: usize,
pub entity_node: Node<'a>,
}
const SIMPLE_CAPTURES: &[(&str, EntityKind, &str)] = &[
(
"csharp.class.name",
EntityKind::CSharpClass,
"class_declaration",
),
(
"csharp.interface.name",
EntityKind::CSharpInterface,
"interface_declaration",
),
(
"csharp.struct.name",
EntityKind::CSharpStruct,
"struct_declaration",
),
(
"csharp.record.name",
EntityKind::CSharpRecord,
"record_declaration",
),
(
"csharp.enum.name",
EntityKind::CSharpEnum,
"enum_declaration",
),
(
"csharp.method.name",
EntityKind::CSharpMethod,
"method_declaration",
),
(
"csharp.constructor.name",
EntityKind::CSharpConstructor,
"constructor_declaration",
),
(
"csharp.property.name",
EntityKind::CSharpProperty,
"property_declaration",
),
(
"csharp.delegate.name",
EntityKind::CSharpDelegate,
"delegate_declaration",
),
(
"csharp.local_function.name",
EntityKind::CSharpLocalFunction,
"local_function_statement",
),
];
pub(crate) fn handle_csharp_capture<'a>(
cap_name: &str,
node: Node<'a>,
source: &[u8],
) -> Option<CsharpCapture<'a>> {
if let Some((_, kind, decl_kind)) = SIMPLE_CAPTURES.iter().find(|(cap, _, _)| *cap == cap_name)
{
let entity_node = find_parent_by_kind(node, decl_kind)?;
return Some(capture_from(
node_text(node, source),
kind.clone(),
entity_node,
));
}
let text = node_text(node, source);
let (name, kind, entity_node) = match cap_name {
"csharp.namespace.name" => (
text,
EntityKind::CSharpNamespace,
find_parent_by_kind(node, "namespace_declaration")
.or_else(|| find_parent_by_kind(node, "file_scoped_namespace_declaration"))?,
),
"csharp.field.name" => {
let decl = find_parent_by_kind(node, "field_declaration")?;
let kind = if has_modifier(decl, source, "const") {
EntityKind::CSharpConstant
} else {
EntityKind::CSharpField
};
(text, kind, decl)
}
"csharp.event.name" => {
let decl = find_parent_by_kind(node, "event_declaration")
.or_else(|| find_parent_by_kind(node, "event_field_declaration"))?;
(text, EntityKind::CSharpEvent, decl)
}
"csharp.indexer" => {
let decl = declaration_or_self(node, "indexer_declaration");
("this[]".to_string(), EntityKind::CSharpIndexer, decl)
}
"csharp.operator" => {
let decl = declaration_or_self(node, "operator_declaration");
let op_token = decl
.child_by_field_name("operator")
.map(|t| node_text(t, source))
.unwrap_or_default();
let name = if op_token.is_empty() {
"operator".to_string()
} else {
format!("operator {op_token}")
};
(name, EntityKind::CSharpOperator, decl)
}
_ => return None,
};
Some(capture_from(name, kind, entity_node))
}
fn capture_from(name: String, kind: EntityKind, entity_node: Node) -> CsharpCapture {
CsharpCapture {
name,
kind,
start_line: entity_node.start_position().row + 1,
entity_node,
}
}
fn declaration_or_self<'a>(node: Node<'a>, kind: &str) -> Node<'a> {
if node.kind() == kind {
node
} else {
find_parent_by_kind(node, kind).unwrap_or(node)
}
}
pub(crate) fn has_modifier(decl: Node<'_>, source: &[u8], keyword: &str) -> bool {
let mut child = decl.child(0);
while let Some(c) = child {
if c.kind() == "modifier" && node_text(c, source) == keyword {
return true;
}
child = c.next_sibling();
}
false
}