use std::fmt;
use crate::{SourceSpan, SyntaxKind, SyntaxNode};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Root {
syntax: SyntaxNode,
}
impl Root {
pub(crate) fn new(syntax: SyntaxNode) -> Self {
debug_assert_eq!(syntax.kind(), SyntaxKind::Root);
Self { syntax }
}
pub fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
pub fn records(&self) -> impl Iterator<Item = Record> + '_ {
self.syntax.children().filter_map(Record::cast)
}
pub fn source_range(&self) -> SourceSpan {
SourceSpan::from_text_range(self.syntax.text_range())
}
}
impl fmt::Display for Root {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.syntax, formatter)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Record {
syntax: SyntaxNode,
}
impl Record {
pub(crate) fn cast(syntax: SyntaxNode) -> Option<Self> {
(syntax.kind() == SyntaxKind::Record).then_some(Self { syntax })
}
pub fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
pub fn fields(&self) -> impl Iterator<Item = Field> + '_ {
self.syntax.children().filter_map(Field::cast)
}
pub fn fields_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = Field> + 'a {
self.fields()
.filter(move |field| field.name().as_deref() == Some(name))
}
pub fn field(&self, name: &str) -> Option<Field> {
self.fields_named(name).next()
}
pub fn last_field(&self, name: &str) -> Option<Field> {
self.fields_named(name).last()
}
pub fn source_range(&self) -> SourceSpan {
SourceSpan::from_text_range(self.syntax.text_range())
}
pub fn raw_text(&self) -> String {
self.syntax.to_string()
}
}
impl fmt::Display for Record {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.syntax, formatter)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Field {
syntax: SyntaxNode,
}
impl Field {
pub(crate) fn cast(syntax: SyntaxNode) -> Option<Self> {
(syntax.kind() == SyntaxKind::Field).then_some(Self { syntax })
}
pub fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
pub fn name(&self) -> Option<String> {
self.syntax
.children_with_tokens()
.filter_map(rowan::NodeOrToken::into_token)
.find(|token| token.kind() == SyntaxKind::Name)
.map(|token| token.text().to_owned())
}
pub fn value(&self) -> ValueText {
let raw = self.raw_text();
let tokens: Vec<_> = self
.syntax
.children_with_tokens()
.filter_map(rowan::NodeOrToken::into_token)
.collect();
let value_start = tokens
.iter()
.find(|token| token.kind() == SyntaxKind::Colon)
.map_or(self.syntax.text_range().start(), |token| {
token.text_range().end()
});
let value_end = tokens
.iter()
.rev()
.find(|token| token.kind() != SyntaxKind::LineEnding)
.map_or(value_start, |token| token.text_range().end());
let range = SourceSpan::from_text_range(rowan::TextRange::new(value_start, value_end));
let mut logical = Vec::new();
for (index, line) in split_bodies(&raw).into_iter().enumerate() {
let content = if index == 0 {
line.split_once(':').map_or("", |(_, value)| value)
} else {
line.trim_start_matches([' ', '\t'])
};
let content = content.trim_matches([' ', '\t']);
logical.push(if index > 0 && content == "." {
""
} else {
content
});
}
ValueText {
text: logical.join("\n"),
range,
}
}
pub fn source_range(&self) -> SourceSpan {
SourceSpan::from_text_range(self.syntax.text_range())
}
pub fn raw_text(&self) -> String {
self.syntax.to_string()
}
}
impl fmt::Display for Field {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.syntax, formatter)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ValueText {
text: String,
range: SourceSpan,
}
impl ValueText {
pub fn as_str(&self) -> &str {
&self.text
}
pub const fn source_range(&self) -> SourceSpan {
self.range
}
pub fn into_string(self) -> String {
self.text
}
}
impl fmt::Display for ValueText {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.text)
}
}
fn split_bodies(text: &str) -> Vec<&str> {
let bytes = text.as_bytes();
let mut result = Vec::new();
let mut start = 0;
let mut cursor = 0;
while cursor < bytes.len() {
if matches!(bytes[cursor], b'\r' | b'\n') {
result.push(&text[start..cursor]);
if bytes[cursor] == b'\r' && bytes.get(cursor + 1) == Some(&b'\n') {
cursor += 1;
}
cursor += 1;
start = cursor;
} else {
cursor += 1;
}
}
if start < text.len() {
result.push(&text[start..]);
}
result
}