Skip to content

Commit

Permalink
buck2/superconsole: factor out span style equality
Browse files Browse the repository at this point in the history
Summary: Consecutive `Span`s with the same style are merged in a `Line`. Deciding if two spans have the same style should be a function on `Span`, instead of coded into `Line.push()`.

Reviewed By: ndmitchell

Differential Revision: D68901431

fbshipit-source-id: 0eec5dfa8353aaaf716a4424f99eabd7f66ebf1f
  • Loading branch information
zsol authored and facebook-github-bot committed Jan 31, 2025
1 parent b353872 commit d2ef83d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/content/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Line {
return;
}
if let Some(last) = self.0.last_mut() {
if last.style == span.style && last.hyperlink == span.hyperlink {
if last.is_mergeable_with(&span) {
last.content.to_mut().push_str(&span.content);
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/content/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl Span {
}
}

/// Determine if this span is mergeable with another span, i.e. if they
/// are equal except for content.
pub fn is_mergeable_with(&self, other: &Span) -> bool {
self.style == other.style && self.hyperlink == other.hyperlink
}

/// Attempt to create a new, unstyled span equivalent to the underlying stringlike.
/// This will fail if the input string is not [`valid`](Span::valid).
pub fn new_unstyled<S: std::fmt::Display>(stringlike: S) -> anyhow::Result<Self> {
Expand Down

0 comments on commit d2ef83d

Please sign in to comment.