Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::cell::OnceCell;
use std::io;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -346,21 +347,20 @@ pub(crate) enum TabExpandedString {
NoTabs(Cow<'static, str>),
WithTabs {
original: Cow<'static, str>,
expanded: String,
expanded: OnceCell<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use Option instead of OnceCell for this? Seems simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think so: We don't have mut access in expanded. AFAIK, that's essentially what OnceCell is there for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

tab_width: usize,
},
}

impl TabExpandedString {
pub(crate) fn new(s: Cow<'static, str>, tab_width: usize) -> Self {
let expanded = s.replace('\t', &" ".repeat(tab_width));
if s == expanded {
if !s.contains('\t') {
Self::NoTabs(s)
} else {
Self::WithTabs {
original: s,
expanded,
tab_width,
expanded: OnceCell::new(),
}
}
}
Expand All @@ -371,20 +371,24 @@ impl TabExpandedString {
debug_assert!(!s.contains('\t'));
s
}
Self::WithTabs { expanded, .. } => expanded,
Self::WithTabs {
original,
tab_width,
expanded,
} => expanded.get_or_init(|| original.replace('\t', &" ".repeat(*tab_width))),
}
}

pub(crate) fn set_tab_width(&mut self, new_tab_width: usize) {
if let Self::WithTabs {
original,
expanded,
tab_width,
..
} = self
{
if *tab_width != new_tab_width {
*tab_width = new_tab_width;
*expanded = original.replace('\t', &" ".repeat(new_tab_width));
expanded.take();
}
}
}
Expand Down
Loading