use techy::core::node::NodeRef;
use crate::lang::Flm;
use crate::lang::structural;
use crate::lang::text::is_blank;
use crate::model::element::element_value;
use crate::stdlib::features::refs::{self, LabelUse};
use super::syntax::{item_options, list_options, ItemTag, OwnLabel};
use super::{ListElement, ITEM};
#[derive(Debug, Clone)]
pub struct ListItem<'t> {
pub node: NodeRef<'t, Flm>,
pub tag: Option<ItemTag<'t>>,
pub labels: Vec<LabelUse>,
pub content: Vec<NodeRef<'t, Flm>>,
}
#[derive(Debug, Clone)]
pub struct ListBody<'t> {
pub depth: usize,
pub own_label: Option<OwnLabel<'t>>,
pub own_ref: Option<String>,
pub kind_label: Option<String>,
pub before: Vec<NodeRef<'t, Flm>>,
pub items: Vec<ListItem<'t>>,
}
pub fn is_item(node: &NodeRef<'_, Flm>) -> bool {
node.ext().element.name.as_str() == ITEM
}
fn is_layout(node: &NodeRef<'_, Flm>) -> bool {
node.is_comment() || is_blank(node) || node.ext().element.name.as_str() == structural::PARBREAK
}
pub fn enclosing_list<'t>(node: &NodeRef<'t, Flm>) -> Option<NodeRef<'t, Flm>> {
let region = node.parent()?;
let list = region.parent()?;
element_value::<ListElement>(&list)?;
let first = list.body()?.first()?;
(first.parent()?.id() == region.id()).then_some(list)
}
pub fn nesting_chain<'t>(list: &NodeRef<'t, Flm>) -> Vec<NodeRef<'t, Flm>> {
let Some(value) = element_value::<ListElement>(list) else { return Vec::new() };
let mut chain: Vec<NodeRef<'t, Flm>> = std::iter::successors(Some(*list), |node| node.parent())
.filter(|node| {
element_value::<ListElement>(node).is_some_and(|other| other.name == value.name)
})
.collect();
chain.reverse();
chain
}
pub(super) fn instance_chain<'t>(list: &NodeRef<'t, Flm>) -> Vec<NodeRef<'t, Flm>> {
let Some(value) = element_value::<ListElement>(list) else { return Vec::new() };
let mut chain = vec![*list];
let (mut child, mut region) = match list.parent() {
Some(region) => (*list, region),
None => return chain,
};
while let Some(node) = region.parent() {
let same_kind =
element_value::<ListElement>(&node).is_some_and(|other| other.name == value.name);
if same_kind
&& holding_item(&node, ®ion, &child)
.is_some_and(|item| item_options(&item).tag.is_none())
{
chain.push(node);
}
child = region;
region = node;
}
chain.reverse();
chain
}
fn holding_item<'t>(
list: &NodeRef<'t, Flm>,
region: &NodeRef<'t, Flm>,
child: &NodeRef<'t, Flm>,
) -> Option<NodeRef<'t, Flm>> {
if list.body()?.first()?.parent()?.id() != region.id() {
return None;
}
let siblings = region.children();
(0..child.index_in_parent()?).rev().filter_map(|index| siblings.get(index)).find(is_item)
}
pub fn item_of<'t>(node: &NodeRef<'t, Flm>) -> Option<ListItem<'t>> {
if !is_item(node) {
return None;
}
Some(ListItem {
node: *node,
tag: item_options(node).tag,
labels: refs::label_uses(node),
content: Vec::new(),
})
}
pub fn list_items<'t>(node: &NodeRef<'t, Flm>) -> ListBody<'t> {
let depth = nesting_chain(node).len();
let kind_label = element_value::<ListElement>(node)
.and_then(|value| value.kind.label_at(depth))
.map(String::from);
let mut body = ListBody {
depth,
own_label: None,
own_ref: None,
kind_label,
before: Vec::new(),
items: Vec::new(),
};
if depth == 0 {
return body;
}
let options = list_options(node);
body.own_ref = options.ref_template().map(String::from);
body.own_label = options.label;
let Some(children) = node.body() else { return body };
let mut before = Vec::new();
for child in children.iter() {
if let Some(item) = item_of(&child) {
body.items.push(item);
} else if let Some(item) = body.items.last_mut() {
item.content.push(child);
} else {
before.push(child);
}
}
if !before.iter().all(is_layout) {
body.before = before;
}
body
}