Skip to content

Commit

Permalink
refactor(reflow): lazily initialize class_types in ReflowPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu authored and benfdking committed Nov 30, 2024
1 parent 5c353c0 commit c87657a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/lib/src/utils/reflow/elements.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::OnceCell;
use std::iter::zip;
use std::ops::Deref;
use std::rc::Rc;
Expand Down Expand Up @@ -35,7 +36,7 @@ fn get_consumed_whitespace(segment: Option<&ErasedSegment>) -> Option<String> {
pub struct ReflowPointData {
segments: Vec<ErasedSegment>,
stats: IndentStats,
class_types: SyntaxSet,
class_types: OnceCell<SyntaxSet>,
}

#[derive(Debug, Clone, Default, PartialEq)]
Expand All @@ -54,13 +55,12 @@ impl Deref for ReflowPoint {
impl ReflowPoint {
pub fn new(segments: Vec<ErasedSegment>) -> Self {
let stats = Self::generate_indent_stats(&segments);
let class_types = segments.iter().flat_map(|it| it.class_types()).collect();

Self {
value: Rc::new(ReflowPointData {
segments,
stats,
class_types,
class_types: OnceCell::new(),
}),
}
}
Expand All @@ -70,7 +70,12 @@ impl ReflowPoint {
}

pub fn class_types(&self) -> &SyntaxSet {
&self.class_types
self.class_types.get_or_init(|| {
self.segments
.iter()
.flat_map(|it| it.class_types())
.collect()
})
}

fn generate_indent_stats(segments: &[ErasedSegment]) -> IndentStats {
Expand Down

0 comments on commit c87657a

Please sign in to comment.