use crate::{
query::{adj_ast, match_ast_d},
syntax::{
builders::variable_declaration::build_variable_declaration_node,
readers::kotlin::class_declaration::extract_modifiers, SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let var_dec = match_ast_d(graph, n_id, "variable_declaration", Some(1));
let var_name = var_dec
.and_then(|vd| match_ast_d(graph, vd, "identifier", Some(1)))
.map_or_else(|| "Unnamed".to_owned(), |id| node_to_str(graph, id));
let var_type = var_dec
.and_then(|vd| match_ast_d(graph, vd, "user_type", Some(1)))
.map(|id| node_to_str(graph, id));
let (_, access_modifiers) = extract_modifiers(graph, n_id);
let value_id = adj_ast(graph, n_id, Some(1), &[])
.last()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let value = if matches!(
graph.label_type(value_id),
Some("property_delegate" | "variable_declaration")
) {
match_ast_d(graph, value_id, "call_expression", Some(1))
} else {
Some(value_id)
};
build_variable_declaration_node(
args,
n_id,
var_name,
var_type,
value,
None,
access_modifiers,
)
.map(Some)
}