diff --git a/src/comment.rs b/src/comment.rs index a55dcae..22c730e 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -84,7 +84,7 @@ mod tests { /// comment1 /// comment2 a = { "a" } - + /// comment3 "#, }; @@ -110,7 +110,7 @@ mod tests { r#" //comment0 a = { "a" // comment1 - ~ + ~ "b" //comment2 //comment2.1 ~ "c" //comment3 @@ -136,7 +136,7 @@ mod tests { r#" /*comment1*/ a = { "a" } - + /*comment1 comment2*/ b = { "b" } diff --git a/src/formatter.rs b/src/formatter.rs index 5c3134f..5e56ba2 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -45,8 +45,6 @@ impl Formatter<'_> { } fn group_output(&self, nodes: Vec) -> String { - // println!("------ nodes: {:?}", nodes); - let hardbreak = Node::Str("".to_string()); let mut groups = vec![]; @@ -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 = 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, @@ -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::>().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::>().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, "///"))); @@ -188,10 +195,10 @@ impl Formatter<'_> { Ok(Node::Rule(GrammarRule { identifier, modifier, code, lines: (start_line, end_line) })) } - fn format_expression(&self, pairs: Pair) -> PestResult> { + fn format_expression(&self, pair: Pair) -> PestResult> { 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 => { @@ -221,7 +228,7 @@ impl Formatter<'_> { }; } code.push(term.clone()); - Ok(code) + Ok(dbg!(code)) } fn format_term(&self, pairs: Pair) -> PestResult { @@ -445,15 +452,17 @@ mod tests { r#" number = { "-"? ~ // sign - ("0" | '1'..'9' ~ ASCII_DIGIT*) ~ ("." ~ ASCII_DIGIT*)? ~ // fraction - "E"|"e" ~ ("+" | "-")? ~ASCII_DIGIT* // exponent + ("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 + "E" + | "e" ~ ("+" | "-")? ~ ASCII_DIGIT* + // exponent } "#, }