Skip to content

Commit

Permalink
fix comment block indent
Browse files Browse the repository at this point in the history
  • Loading branch information
SWW13 committed Jun 13, 2024
1 parent 09b4ea4 commit b91d4ea
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
43 changes: 23 additions & 20 deletions crates/rune/src/fmt/indent_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,7 @@ impl<'a> SpanInjectionWriter<'a> {
pub(super) fn into_inner(mut self) -> Result<Vec<Vec<u8>>, FormattingError> {
while !self.queued_spans.is_empty() {
let span = self.queued_spans.remove(0);
match span {
ResolvedSpan::Empty(_) => {
writeln!(self.writer)?;
}
ResolvedSpan::Comment(comment) => {
if comment.on_new_line {
writeln!(self.writer, "{}", self.resolve(comment.span)?)?;
} else {
self.extend_previous_line(b" ")?;
self.extend_previous_line(self.resolve(comment.span)?.as_bytes())?;
}
}
}
self.write_span(span)?;
}

Ok(self.writer.into_inner())
Expand Down Expand Up @@ -205,18 +193,33 @@ impl<'a> SpanInjectionWriter<'a> {
}

let queued_span = self.queued_spans.remove(0);
match queued_span {
ResolvedSpan::Empty(_) => {
writeln!(self.writer)?;
}
ResolvedSpan::Comment(comment) => {
self.write_span(queued_span)?;
}

Ok(())
}

fn write_span(&mut self, span: ResolvedSpan) -> Result<(), FormattingError> {
match span {
ResolvedSpan::Empty(_) => {
writeln!(self.writer)?;
}
ResolvedSpan::Comment(comment) => {
let mut lines = self.resolve(comment.span)?.lines();

if let Some(first_line) = lines.next() {
if comment.on_new_line {
writeln!(self.writer, "{}", self.resolve(comment.span)?)?;
writeln!(self.writer, "{}", first_line)?;
} else {
self.extend_previous_line(b" ")?;
self.extend_previous_line(self.resolve(comment.span)?.as_bytes())?;
self.extend_previous_line(first_line.as_bytes())?;
}
}

for line in lines {
self.newline()?;
self.extend_previous_line(line.as_bytes())?;
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion crates/rune/src/tests/format_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ fn assert_format_source(source: &str, expected: Option<&str>) -> Result<()> {

/// https://github.com/rune-rs/rune/issues/684
#[test]
#[ignore]
fn bug_684() -> Result<()> {
let source = r#"pub fn main() {
/*
Expand Down Expand Up @@ -45,6 +44,31 @@ test 3*/
assert_format_source(source, None)
}

#[test]
fn fmt_block_comment_indent2() -> Result<()> {
let source = r#"fn test() {
/* test1
test2 */
if true {
/*
if false {
// test3
}
*/
} /* else {
// test 4
} */
}
/* test 5.1
test 5.2
test 5.3
*/
"#;

assert_format_source(source, None)
}

/// https://github.com/rune-rs/rune/issues/693
#[test]
fn bug_693() -> Result<()> {
Expand Down

0 comments on commit b91d4ea

Please sign in to comment.