Skip to content

Commit

Permalink
fix comment indent
Browse files Browse the repository at this point in the history
  • Loading branch information
SWW13 committed Jun 13, 2024
1 parent 9d59701 commit 09b4ea4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
24 changes: 15 additions & 9 deletions crates/rune/src/fmt/indent_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::str;
use crate::alloc::fmt::TryWrite;
use crate::alloc::prelude::*;
use crate::alloc::{self, try_vec, Vec};
use crate::ast::Span;
use crate::ast::{ByteIndex, Span};

use super::comments::Comment;
use super::error::FormattingError;
Expand Down Expand Up @@ -195,18 +195,12 @@ impl<'a> SpanInjectionWriter<'a> {
self.write_spanned(Span::new(0, 0), text, false, false)
}

pub(super) fn write_spanned(
&mut self,
span: Span,
text: &str,
newline: bool,
space: bool,
) -> Result<(), FormattingError> {
pub(super) fn write_queued_spans(&mut self, until: ByteIndex) -> Result<(), FormattingError> {
// The queued recovered spans are ordered so we can pop them from the front if they're before the current span.
// If the current span is before the first queued span, we need to inject the queued span.

while let Some(queued_span) = self.queued_spans.first() {
if queued_span.span().start > span.start {
if queued_span.span().start > until {
break;
}

Expand All @@ -226,6 +220,18 @@ impl<'a> SpanInjectionWriter<'a> {
}
}

Ok(())
}

pub(super) fn write_spanned(
&mut self,
span: Span,
text: &str,
newline: bool,
space: bool,
) -> Result<(), FormattingError> {
self.write_queued_spans(span.start)?;

write!(self.writer, "{}", text)?;

if space {
Expand Down
1 change: 1 addition & 0 deletions crates/rune/src/fmt/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ impl<'a> Printer<'a> {
self.visit_statement(statement)?;
}

self.writer.write_queued_spans(close.span.start)?;
self.writer.dedent();
self.writer.write_spanned_raw(close.span, false, false)?;

Expand Down
44 changes: 41 additions & 3 deletions crates/rune/src/tests/format_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,58 @@ fn bug_684() -> Result<()> {
/*
test
*/
}"#;
}
"#;

assert_format_source(source, None)
}

#[test]
fn fmt_block_comment() -> Result<()> {
let source = r#"//test1
/*test2*/"#;
let expected = format!("{source}\n");

assert_format_source(source, Some(&expected))
}

#[test]
fn fmt_block_comment_indent() -> Result<()> {
let source = r#"struct Test {
a, /* test1
test2
test 3*/
}
"#;

assert_format_source(source, None)
}

/// https://github.com/rune-rs/rune/issues/693
#[test]
#[ignore]
fn bug_693() -> Result<()> {
let source = r#"pub fn main() {
if true {
// test
}
}"#;
}
"#;

assert_format_source(source, None)
}

#[test]
fn fmt_comment_line() -> Result<()> {
let source = r#"pub fn main() {
// test 1
if true {
// test 2.1
let a = 1;
// test 2.2
}
// test 3
}
"#;

assert_format_source(source, None)
}
Expand Down

0 comments on commit 09b4ea4

Please sign in to comment.