mirror of
https://github.com/roc-lang/roc.git
synced 2025-07-28 00:43:47 +00:00
Correct indentation for when expressions
... and simultaneously remove the need for State::indent_column field / multiline tracking in blankspace.rs. Fixes #2889
This commit is contained in:
parent
19436cc58a
commit
ada8af25cc
8 changed files with 151 additions and 54 deletions
|
@ -197,11 +197,7 @@ where
|
|||
Err((MadeProgress, indent_problem(state.pos()), state))
|
||||
} else {
|
||||
let comments_and_newlines = Vec::with_capacity_in(newlines, arena);
|
||||
let mut spaces = eat_spaces(state, false, comments_and_newlines);
|
||||
|
||||
if spaces.multiline {
|
||||
spaces.state.indent_column = spaces.state.column();
|
||||
}
|
||||
let spaces = eat_spaces(state, comments_and_newlines);
|
||||
|
||||
Ok((
|
||||
MadeProgress,
|
||||
|
@ -324,13 +320,11 @@ fn fast_eat_spaces(state: &State) -> FastSpaceState {
|
|||
|
||||
struct SpaceState<'a> {
|
||||
state: State<'a>,
|
||||
multiline: bool,
|
||||
comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
|
||||
}
|
||||
|
||||
fn eat_spaces<'a>(
|
||||
mut state: State<'a>,
|
||||
mut multiline: bool,
|
||||
mut comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
|
||||
) -> SpaceState<'a> {
|
||||
for c in state.bytes() {
|
||||
|
@ -340,7 +334,6 @@ fn eat_spaces<'a>(
|
|||
}
|
||||
b'\n' => {
|
||||
state = state.advance_newline();
|
||||
multiline = true;
|
||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||
}
|
||||
b'\r' => {
|
||||
|
@ -350,7 +343,7 @@ fn eat_spaces<'a>(
|
|||
|
||||
b'#' => {
|
||||
state = state.advance(1);
|
||||
return eat_line_comment(state, multiline, comments_and_newlines);
|
||||
return eat_line_comment(state, comments_and_newlines);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
|
@ -358,14 +351,12 @@ fn eat_spaces<'a>(
|
|||
|
||||
SpaceState {
|
||||
state,
|
||||
multiline,
|
||||
comments_and_newlines,
|
||||
}
|
||||
}
|
||||
|
||||
fn eat_line_comment<'a>(
|
||||
mut state: State<'a>,
|
||||
mut multiline: bool,
|
||||
mut comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
|
||||
) -> SpaceState<'a> {
|
||||
let mut index = state.pos().offset as usize;
|
||||
|
@ -388,7 +379,6 @@ fn eat_line_comment<'a>(
|
|||
index += 2;
|
||||
|
||||
comments_and_newlines.push(CommentOrNewline::DocComment(""));
|
||||
multiline = true;
|
||||
|
||||
for c in state.bytes() {
|
||||
match c {
|
||||
|
@ -397,7 +387,6 @@ fn eat_line_comment<'a>(
|
|||
}
|
||||
b'\n' => {
|
||||
state = state.advance_newline();
|
||||
multiline = true;
|
||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||
}
|
||||
b'\r' => {
|
||||
|
@ -417,7 +406,6 @@ fn eat_line_comment<'a>(
|
|||
|
||||
return SpaceState {
|
||||
state,
|
||||
multiline,
|
||||
comments_and_newlines,
|
||||
};
|
||||
}
|
||||
|
@ -427,7 +415,6 @@ fn eat_line_comment<'a>(
|
|||
|
||||
return SpaceState {
|
||||
state,
|
||||
multiline,
|
||||
comments_and_newlines,
|
||||
};
|
||||
}
|
||||
|
@ -483,7 +470,6 @@ fn eat_line_comment<'a>(
|
|||
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
||||
}
|
||||
state = state.advance_newline();
|
||||
multiline = true;
|
||||
|
||||
index += 1;
|
||||
while index < length {
|
||||
|
@ -493,7 +479,6 @@ fn eat_line_comment<'a>(
|
|||
}
|
||||
b'\n' => {
|
||||
state = state.advance_newline();
|
||||
multiline = true;
|
||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||
}
|
||||
b'\r' => {
|
||||
|
@ -513,7 +498,6 @@ fn eat_line_comment<'a>(
|
|||
|
||||
return SpaceState {
|
||||
state,
|
||||
multiline,
|
||||
comments_and_newlines,
|
||||
};
|
||||
}
|
||||
|
@ -550,7 +534,6 @@ fn eat_line_comment<'a>(
|
|||
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
||||
}
|
||||
state = state.advance_newline();
|
||||
multiline = true;
|
||||
|
||||
index += 1;
|
||||
while index < length {
|
||||
|
@ -560,7 +543,6 @@ fn eat_line_comment<'a>(
|
|||
}
|
||||
b'\n' => {
|
||||
state = state.advance_newline();
|
||||
multiline = true;
|
||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||
}
|
||||
b'\r' => {
|
||||
|
@ -580,7 +562,6 @@ fn eat_line_comment<'a>(
|
|||
|
||||
return SpaceState {
|
||||
state,
|
||||
multiline,
|
||||
comments_and_newlines,
|
||||
};
|
||||
}
|
||||
|
@ -606,7 +587,6 @@ fn eat_line_comment<'a>(
|
|||
|
||||
return SpaceState {
|
||||
state,
|
||||
multiline,
|
||||
comments_and_newlines,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1992,9 +1992,10 @@ mod when {
|
|||
/// Parsing when with indentation.
|
||||
fn when_with_indent<'a>() -> impl Parser<'a, u32, EWhen<'a>> {
|
||||
move |arena, state: State<'a>| {
|
||||
let min_indent = state.column();
|
||||
parser::keyword_e(keyword::WHEN, EWhen::When)
|
||||
.parse(arena, state)
|
||||
.map(|(progress, (), state)| (progress, state.indent_column, state))
|
||||
.map(|(progress, (), state)| (progress, min_indent, state))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2003,14 +2004,12 @@ mod when {
|
|||
options: ExprParseOptions,
|
||||
) -> impl Parser<'a, Vec<'a, &'a WhenBranch<'a>>, EWhen<'a>> {
|
||||
move |arena, state: State<'a>| {
|
||||
let when_indent = state.indent_column;
|
||||
|
||||
let mut branches: Vec<'a, &'a WhenBranch<'a>> = Vec::with_capacity_in(2, arena);
|
||||
|
||||
// 1. Parse the first branch and get its indentation level. (It must be >= min_indent.)
|
||||
// 2. Parse the other branches. Their indentation levels must be == the first branch's.
|
||||
|
||||
let (_, ((pattern_indent_level, loc_first_patterns), loc_first_guard), mut state): (
|
||||
let (_, ((pattern_indent_level, loc_first_patterns), loc_first_guard), state): (
|
||||
_,
|
||||
((_, _), _),
|
||||
State<'a>,
|
||||
|
@ -2018,8 +2017,6 @@ mod when {
|
|||
|
||||
let original_indent = pattern_indent_level;
|
||||
|
||||
state.indent_column = pattern_indent_level;
|
||||
|
||||
// Parse the first "->" and the expression after it.
|
||||
let (_, loc_first_expr, mut state) =
|
||||
branch_result(original_indent + 1).parse(arena, state)?;
|
||||
|
@ -2078,9 +2075,6 @@ mod when {
|
|||
}
|
||||
}
|
||||
|
||||
let mut state = state;
|
||||
state.indent_column = when_indent;
|
||||
|
||||
Ok((MadeProgress, branches, state))
|
||||
}
|
||||
}
|
||||
|
@ -2382,7 +2376,7 @@ where
|
|||
E: 'a,
|
||||
{
|
||||
move |arena, state: State<'a>| {
|
||||
let indent_column = state.indent_column;
|
||||
let indent_column = state.column();
|
||||
|
||||
let (progress, _, state) = parser.parse(arena, state)?;
|
||||
|
||||
|
|
|
@ -14,10 +14,6 @@ pub struct State<'a> {
|
|||
|
||||
/// Position of the start of the current line
|
||||
pub(crate) line_start: Position,
|
||||
|
||||
/// Current indentation level, in columns
|
||||
/// (so no indent is col 1 - this saves an arithmetic operation.)
|
||||
pub(crate) indent_column: u32,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
|
@ -26,7 +22,6 @@ impl<'a> State<'a> {
|
|||
original_bytes: bytes,
|
||||
offset: 0,
|
||||
line_start: Position::zero(),
|
||||
indent_column: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,7 +81,6 @@ impl<'a> fmt::Debug for State<'a> {
|
|||
}
|
||||
|
||||
write!(f, "\n\t(offset): {:?},", self.pos())?;
|
||||
write!(f, "\n\tindent_column: {}", self.indent_column)?;
|
||||
write!(f, "\n}}")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue