use crate::{
query::adj_ast,
syntax::{
builders::member_access::build_member_access_node, SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
CodeGraph, NodeId,
};
use alloc::vec::Vec;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let child_ids: Vec<NodeId> = adj_ast(graph, n_id, Some(1), &[])
.into_iter()
.filter(|&c_id| graph.label_type(c_id) != Some("line_comment"))
.collect();
let Some(&first_id) = child_ids.first() else {
return Ok(None);
};
let identifier_id = child_ids
.get(1)
.filter(|_| child_ids.len() == 2)
.filter(|&&suffix_id| graph.label_type(suffix_id) == Some("navigation_suffix"))
.and_then(|&suffix_id| adj_ast(graph, suffix_id, Some(1), &[]).get(1).copied());
let member = identifier_id.map_or_else(
|| node_to_str(graph, *child_ids.last().unwrap_or(&first_id)),
|id| node_to_str(graph, id),
);
let expression = node_to_str(graph, first_id);
build_member_access_node(args, n_id, member, expression, first_id).map(Some)
}