use super::super::range::{Position, Range};
use super::super::traits::{AstNode, Visitor, VisualStructure};
use crate::lex::lexing::Token;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub struct BlankLineGroup {
pub count: usize,
pub source_tokens: Vec<Token>,
pub location: Range,
}
impl BlankLineGroup {
fn default_location() -> Range {
Range::new(0..0, Position::new(0, 0), Position::new(0, 0))
}
pub fn new(count: usize, source_tokens: Vec<Token>) -> Self {
Self {
count,
source_tokens,
location: Self::default_location(),
}
}
pub fn at(mut self, location: Range) -> Self {
self.location = location;
self
}
}
impl AstNode for BlankLineGroup {
fn node_type(&self) -> &'static str {
"BlankLineGroup"
}
fn display_label(&self) -> String {
if self.count == 1 {
"1 blank line".to_string()
} else {
format!("{} blank lines", self.count)
}
}
fn range(&self) -> &Range {
&self.location
}
fn accept(&self, visitor: &mut dyn Visitor) {
visitor.visit_blank_line_group(self);
visitor.leave_blank_line_group(self);
}
}
impl VisualStructure for BlankLineGroup {
fn is_source_line_node(&self) -> bool {
true
}
}
impl fmt::Display for BlankLineGroup {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BlankLineGroup({})", self.count)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_blank_line_group_creation() {
let group = BlankLineGroup::new(3, vec![]);
assert_eq!(group.count, 3);
assert_eq!(group.display_label(), "3 blank lines");
}
#[test]
fn test_blank_line_group_single() {
let group = BlankLineGroup::new(1, vec![]);
assert_eq!(group.count, 1);
assert_eq!(group.display_label(), "1 blank line");
}
}