use crate::language::{CommentForm, CommentSlot, NodeRow, NodeTable, PrintOptions};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Comment<'t> {
pub text: &'t str,
pub style: CommentForm,
}
#[derive(Clone, Debug)]
pub struct Index<'t> {
table: &'t NodeTable,
children: Vec<Vec<u32>>,
comments: Vec<[Vec<Comment<'t>>; 3]>,
}
impl<'t> Index<'t> {
pub fn new(table: &'t NodeTable) -> Self {
let n = table.rows.len();
let mut children = vec![Vec::new(); n];
for (i, r) in table.rows.iter().enumerate() {
if let Some(p) = r.parent
&& (p as usize) < n
{
children[p as usize].push(i as u32);
}
}
let mut comments: Vec<[Vec<Comment<'t>>; 3]> = (0..n).map(|_| Default::default()).collect();
for c in &table.comments {
if let Some(slots) = comments.get_mut(c.node as usize) {
let slot = match c.slot {
CommentSlot::Leading => 0,
CommentSlot::Trailing => 1,
CommentSlot::Dangling => 2,
};
slots[slot].push(Comment {
text: &c.text,
style: c.style,
});
}
}
Index {
table,
children,
comments,
}
}
pub fn table(&self) -> &'t NodeTable {
self.table
}
pub fn root(&self) -> u32 {
0
}
pub fn row(&self, id: u32) -> &'t NodeRow {
&self.table.rows[id as usize]
}
pub fn text(&self, id: u32) -> &'t str {
self.row(id).text.as_deref().unwrap_or("")
}
pub fn children(&self, id: u32) -> &[u32] {
&self.children[id as usize]
}
pub fn key(&self, id: u32) -> u32 {
self.children[id as usize][0]
}
pub fn value(&self, id: u32) -> u32 {
self.children[id as usize][1]
}
pub fn comments(&self, id: u32, slot: CommentSlot) -> &[Comment<'t>] {
let i = match slot {
CommentSlot::Leading => 0,
CommentSlot::Trailing => 1,
CommentSlot::Dangling => 2,
};
&self.comments[id as usize][i]
}
pub fn leading(&self, id: u32) -> &[Comment<'t>] {
self.comments(id, CommentSlot::Leading)
}
pub fn trailing(&self, id: u32) -> &[Comment<'t>] {
self.comments(id, CommentSlot::Trailing)
}
pub fn dangling(&self, id: u32) -> &[Comment<'t>] {
self.comments(id, CommentSlot::Dangling)
}
}
#[derive(Clone, Debug)]
pub struct Writer {
out: String,
pretty: bool,
unit: String,
}
impl Writer {
pub fn new(options: &PrintOptions) -> Self {
Writer {
out: String::new(),
pretty: options.pretty,
unit: " ".repeat(options.indent as usize),
}
}
pub fn put(&mut self, s: &str) -> &mut Self {
self.out.push_str(s);
self
}
pub fn nl(&mut self) -> &mut Self {
if self.pretty {
self.out.push('\n');
}
self
}
pub fn indent(&mut self, depth: usize) -> &mut Self {
if self.pretty {
for _ in 0..depth {
self.out.push_str(&self.unit);
}
}
self
}
pub fn line(&mut self, depth: usize, s: &str) -> &mut Self {
self.indent(depth).put(s).nl()
}
pub fn comments(&mut self, comments: &[Comment<'_>], leader: &str, depth: usize) -> bool {
let mut wrote = false;
for c in comments {
for line in c.text.split('\n') {
let t = line.trim_matches(|ch| ch == ' ' || ch == '\t');
self.indent(depth).put(leader);
if !t.is_empty() {
self.put(" ").put(t);
}
self.out.push('\n');
wrote = true;
}
}
wrote
}
pub fn as_str(&self) -> &str {
&self.out
}
pub fn finish(self) -> String {
self.out
}
}