diff --git a/crates/lib/src/utils/reflow/elements.rs b/crates/lib/src/utils/reflow/elements.rs index 1a6559ec..7c9e6ba4 100644 --- a/crates/lib/src/utils/reflow/elements.rs +++ b/crates/lib/src/utils/reflow/elements.rs @@ -1,3 +1,4 @@ +use std::cell::OnceCell; use std::iter::zip; use std::ops::Deref; use std::rc::Rc; @@ -35,7 +36,7 @@ fn get_consumed_whitespace(segment: Option<&ErasedSegment>) -> Option { pub struct ReflowPointData { segments: Vec, stats: IndentStats, - class_types: SyntaxSet, + class_types: OnceCell, } #[derive(Debug, Clone, Default, PartialEq)] @@ -54,13 +55,12 @@ impl Deref for ReflowPoint { impl ReflowPoint { pub fn new(segments: Vec) -> 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(), }), } } @@ -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 {