Skip to content

Commit

Permalink
Fix expressions indent
Browse files Browse the repository at this point in the history
Closes GH-150.
Closes GH-152.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
bnchi authored Jan 28, 2025
1 parent 5c9eba1 commit d5a0f56
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/construct/partial_mdx_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@
//! [mdx_expression_text]: crate::construct::mdx_expression_text
//! [interleaving]: https://mdxjs.com/docs/what-is-mdx/#interleaving
use crate::construct::partial_space_or_tab::space_or_tab_min_max;
use crate::event::Name;
use crate::message;
use crate::state::{Name as StateName, State};
use crate::tokenizer::Tokenizer;
use crate::util::{constant::TAB_SIZE, mdx_collect::collect};
use crate::util::mdx_collect::collect;
use crate::{MdxExpressionKind, MdxExpressionParse, MdxSignal};
use alloc::boxed::Box;

pub const INDENT_SIZE: usize = 4;

/// Start of an MDX expression.
///
/// ```markdown
Expand Down Expand Up @@ -180,7 +181,6 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State {
}
)
} else if matches!(tokenizer.current, Some(b'\t' | b' ')) {
tokenizer.attempt(State::Next(StateName::MdxExpressionBefore), State::Nok);
// Idea: investigate if we’d need to use more complex stripping.
// Take this example:
//
Expand All @@ -200,12 +200,27 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State {
// the start of the expression and move past whitespace.
// For future lines, we’d move at most to
// `line_start_shifted.column + 4`.
State::Retry(space_or_tab_min_max(tokenizer, 0, TAB_SIZE))
tokenizer.enter(Name::LinePrefix);
State::Retry(StateName::MdxExpressionPrefix)
} else {
State::Retry(StateName::MdxExpressionBefore)
}
}

pub fn prefix(tokenizer: &mut Tokenizer) -> State {
tokenizer.tokenize_state.size_c += 1;
if matches!(tokenizer.current, Some(b'\t' | b' '))
&& tokenizer.tokenize_state.size_c < INDENT_SIZE - 1
{
tokenizer.consume();
return State::Next(StateName::MdxExpressionPrefix);
}

tokenizer.exit(Name::LinePrefix);
tokenizer.tokenize_state.size_c = 0;
State::Retry(StateName::MdxExpressionBefore)
}

/// Parse an expression with a given function.
fn parse_expression(tokenizer: &mut Tokenizer, parse: &MdxExpressionParse) -> State {
// Collect the body of the expression and positional info for each run of it.
Expand Down
2 changes: 2 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,8 @@ pub enum Name {
/// ^ ^ ^
/// ```
ThematicBreakSequence,

LinePrefix,
}

/// List of void events, used to make sure everything is working well.
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ pub enum Name {

MdxExpressionStart,
MdxExpressionBefore,
MdxExpressionPrefix,
MdxExpressionInside,
MdxExpressionEolAfter,

Expand Down Expand Up @@ -835,6 +836,7 @@ pub fn call(tokenizer: &mut Tokenizer, name: Name) -> State {
Name::MdxEsmAtEnd => construct::mdx_esm::at_end,

Name::MdxExpressionStart => construct::partial_mdx_expression::start,
Name::MdxExpressionPrefix => construct::partial_mdx_expression::prefix,
Name::MdxExpressionBefore => construct::partial_mdx_expression::before,
Name::MdxExpressionInside => construct::partial_mdx_expression::inside,
Name::MdxExpressionEolAfter => construct::partial_mdx_expression::eol_after,
Expand Down
13 changes: 13 additions & 0 deletions tests/mdx_expression_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ fn mdx_expression_flow_agnostic() -> Result<(), message::Message> {
"should support mdx expressions (flow) as `MdxFlowExpression`s in mdast"
);

assert_eq!(
to_mdast(" {`\n a\n `}", &mdx.parse)?,
Node::Root(Root {
children: vec![Node::MdxFlowExpression(MdxFlowExpression {
value: "`\n a\n`".into(),
position: Some(Position::new(1, 3, 2, 3, 5, 15)),
stops: vec![(0, 3), (1, 4), (2, 7), (5, 10), (6, 13)]
})],
position: Some(Position::new(1, 1, 0, 3, 5, 15))
}),
"should support indent in `MdxFlowExpression` in mdast"
);

Ok(())
}

Expand Down

0 comments on commit d5a0f56

Please sign in to comment.