Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix choice indent #16

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
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
53 changes: 50 additions & 3 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl Formatter<'_> {

fn format_grammar_rule(&self, pair: Pair<Rule>) -> PestResult<Node> {
let mut code = String::new();
code.push('{');
let mut modifier = " ".to_string();
let mut identifier = String::new();

Expand All @@ -148,19 +149,40 @@ impl Formatter<'_> {
Rule::expression => match self.format_expression(pair) {
Ok(parts) => {
if start_line == end_line {
code = format!("{{ {} }}", parts.join(" | "));
code.push(' ');
code.push_str(&parts.join(" | "));
code.push(' ');
} else {
code = format!("{{\n {}}}", indent(parts.join("\n| "), 2));
code.push_str("\n ");
let mut expr_code = parts.join("\n| ");

// Remove leading whitespace: " |" to "|"
expr_code = expr_code.split('\n').map(|part| part.trim()).collect::<Vec<_>>().join("\n");

code.push_str(&indent(expr_code, 2));
}
}
Err(e) => return Err(e),
},
Rule::COMMENT => {
let comment = self.format_comment(pair);

if start_line == end_line {
code.push(' ');
} else {
code.push_str("\n ");
}

code.push_str(&comment);
}
Rule::line_doc => {
return Ok(Node::LineDoc(self.format_line_doc(pair, "///")));
}
_ => {}
_ => return Err(Unreachable(unreachable_rule!())),
};
}

code.push('}');
Ok(Node::Rule(GrammarRule { identifier, modifier, code, lines: (start_line, end_line) }))
}

Expand Down Expand Up @@ -376,4 +398,29 @@ mod tests {
"#
}
}

#[test]
fn test_choice_alignment() {
expect_correction! {
r#"
a = {
"0" ~ ( "0" //comment1
| "0" // comment2
| "1" // comment3
| "2" //comment4
| "a" | "b" | "c" | "d" | "e")
}
"#,

r#"
a = {
"0" ~ ("0" // comment1
| "0" // comment2
| "1" // comment3
| "2" // comment4
| "a" | "b" | "c" | "d" | "e")
}
"#,
}
}
}
36 changes: 36 additions & 0 deletions tests/fixtures/bad_cases.actual.pest
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,39 @@ arguments = {
blah = {
("w") ~ &("z")
}

// https://github.com/pest-parser/pest-fmt/issues/15
log_line1 = {
"0" ~ (
"1"
// a comment
| "2" // another comment
| "3" // longer comment
| "4"
// final comment
| "a"
| "b"
| "c"
| "d"
)
}

log_line2 = {
"0" ~ ( "0" // a comment
| "0" // another comment
| "1" // longer comment
| "2" // final comment
| "a" | "b" | "c" | "d" | "e")
}

log_line3 = {
// lead comment
"1" ~ ("2"
// another comment
| "3"
// longer comment
| "4"
// final comment
| "5"
| "a" | "b" | "c" | "d" | "e") // comment
}
25 changes: 25 additions & 0 deletions tests/fixtures/bad_cases.expected.pest
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,29 @@ arguments = {
}
blah = {
("w") ~ &("z")
}

// https://github.com/pest-parser/pest-fmt/issues/15
log_line1 = {
"0" ~ ("1" // a comment
| "2" // another comment
| "3" // longer comment
| "4" // final comment
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if correct to align the comment like that -- the original feedback was that the intention was to put the "final comment" on top of "a" (or "5" in log_line3), but formatting makes it look like that it's attached to the previous line ("4" instead of "a" or "5")

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't try to improve the comment style for this case, but focused on ensuring alignment and avoiding loss.

| "a" | "b" | "c" | "d")
}

log_line2 = {
"0" ~ ("0" // a comment
| "0" // another comment
| "1" // longer comment
| "2" // final comment
| "a" | "b" | "c" | "d" | "e")
}

log_line3 = {
// lead comment
"1" ~ ("2" // another comment
| "3" // longer comment
| "4" // final comment
| "5" | "a" | "b" | "c" | "d" | "e") // comment
Copy link
Contributor

Choose a reason for hiding this comment

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

will this split up the choices if there was more of them, so that it is not too long vertically?
The original feedback mentioned there may be too many choices:

the line that starts with pair35 in the output doesn't scale if there are lots and lots of rules.
In my real output - I ended up with a vertical formatted input with one of the output lines being 634 characters long (593 up to the comment).

}