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 for tailing comments #34

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod tests {
/// comment1
/// comment2
a = { "a" }

/// comment3
"#,
};
Expand All @@ -110,7 +110,7 @@ mod tests {
r#"
//comment0
a = { "a" // comment1
~
~
"b" //comment2
//comment2.1
~ "c" //comment3
Expand All @@ -136,7 +136,7 @@ mod tests {
r#"
/*comment1*/
a = { "a" }

/*comment1
comment2*/
b = { "b" }
Expand Down
79 changes: 54 additions & 25 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl Formatter<'_> {
}

fn group_output(&self, nodes: Vec<Node>) -> String {
// println!("------ nodes: {:?}", nodes);

let hardbreak = Node::Str("".to_string());

let mut groups = vec![];
Expand Down Expand Up @@ -135,8 +133,17 @@ impl Formatter<'_> {

let start_line = pair.as_span().start_pos().line_col().0;
let end_line = pair.as_span().end_pos().line_col().0;
let mut inner = pair.into_inner();
let mut last_comment: Option<String> = None;

while let Some(pair) = inner.next() {
// ensure new line after comment
if last_comment.is_some() {
if !code.ends_with('\n') {
code.push('\n');
}
}

for pair in pair.into_inner() {
match pair.as_rule() {
Rule::WHITESPACE => continue,
Rule::assignment_operator => continue,
Expand All @@ -149,33 +156,33 @@ impl Formatter<'_> {
Rule::compound_atomic_modifier => modifier = pair.as_str().to_string(),
Rule::expression => match self.format_expression(pair) {
Ok(parts) => {
if start_line == end_line {
code.push(' ');
code.push_str(&parts.join(" | "));
code.push(' ');
} else {
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));
if parts.len() > 0 {
if start_line == end_line {
code.push(' ');
code.push_str(&parts.join(" | "));
code.push(' ');
} else {
code.push('\n');
code.push_str(" ");

let mut expr_code = dbg!(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 {
if !code.ends_with('\n') {
code.push('\n');
code.push_str(&" ".repeat(4));
}

let comment = self.format_comment(pair);
code.push_str(" ");
code.push_str(&comment);
last_comment = Some(comment);
}
Rule::line_doc => {
return Ok(Node::LineDoc(self.format_line_doc(pair, "///")));
Expand All @@ -188,10 +195,10 @@ impl Formatter<'_> {
Ok(Node::Rule(GrammarRule { identifier, modifier, code, lines: (start_line, end_line) }))
}

fn format_expression(&self, pairs: Pair<Rule>) -> PestResult<Vec<String>> {
fn format_expression(&self, pair: Pair<Rule>) -> PestResult<Vec<String>> {
let mut code = vec![];
let mut term = String::new();
for pair in pairs.into_inner() {
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::WHITESPACE => continue,
Rule::COMMENT => {
Expand Down Expand Up @@ -221,7 +228,7 @@ impl Formatter<'_> {
};
}
code.push(term.clone());
Ok(code)
Ok(dbg!(code))
}

fn format_term(&self, pairs: Pair<Rule>) -> PestResult<String> {
Expand Down Expand Up @@ -438,4 +445,26 @@ mod tests {
"#,
}
}

#[test]
fn test_tailing_comments() {
expect_correction! {
r#"
number = {
"-"? ~ // sign
("0" | '1'..'9' ~ ASCII_DIGIT*) ~ ("." ~ ASCII_DIGIT*)? ~ // fraction
"E"|"e" ~ ("+" | "-")? ~ASCII_DIGIT* // exponent
}
"#,
r#"
number = {
"-"? ~ // sign
("0" | '1'..'9' ~ ASCII_DIGIT*) ~ ("." ~ ASCII_DIGIT*)? ~ // fraction
"E"
| "e" ~ ("+" | "-")? ~ ASCII_DIGIT*
// exponent
}
"#,
}
}
}
Loading