Skip to content

Commit

Permalink
feat: reduce nesting of InlineContent when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nfejzic committed Aug 1, 2022
1 parent cf58e5c commit 7946da0
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion inline/src/inlines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,39 @@ impl Inline {
/// [`Inline`]: self::Inline
/// [`TokenKind`]: crate::TokenKind
/// [`InlineContent`]: self::content::InlineContent
pub fn new(content: InlineContent<PlainContent, NestedContent>, kind: TokenKind) -> Self {
pub fn new(mut content: InlineContent<PlainContent, NestedContent>, kind: TokenKind) -> Self {
let consume_as_plain = |content| match content {
InlineContent::Plain(plain_content) => Self::Plain(plain_content),
InlineContent::Nested(nested_content) => Self::Multiple(nested_content),
};

let is_plain = matches!(
kind,
TokenKind::Verbatim
| TokenKind::Newline
| TokenKind::EndOfLine
| TokenKind::Whitespace
| TokenKind::Plain
| TokenKind::UnderlineSubscript
| TokenKind::ItalicBold
| TokenKind::CloseParens
| TokenKind::CloseBracket
| TokenKind::CloseBrace
);

if !is_plain {
let span = content.span();
// try to flatten content more
if let InlineContent::Nested(ref mut nested) = content {
if nested.content.len() == 1 {
if let Inline::Multiple(ref mut nested) = nested.content[0] {
content = InlineContent::Nested(std::mem::take(nested));
content.set_span(span);
}
}
}
}

match kind {
TokenKind::Bold => Self::Bold(content.into()),
TokenKind::Italic => Self::Italic(content.into()),
Expand Down

0 comments on commit 7946da0

Please sign in to comment.