Skip to content

Commit

Permalink
skip automatic indentation inside comments
Browse files Browse the repository at this point in the history
This fixes broken indentation due to
e.g. `wasi:io/streams#output-stream.blocking-write-and-flush`, which has a
doc comment that contains curly brackets.

This isn't generally a problem for Rust, which uses `rustfmt` if available, but
leads to headaches for C if `clang-format` isn't handy.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Jan 4, 2024
1 parent 8825a3b commit 99ac19c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
35 changes: 23 additions & 12 deletions crates/core/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,46 @@ impl Files {
pub struct Source {
s: String,
indent: usize,
in_line_comment: bool,
}

impl Source {
pub fn append_src(&mut self, src: &Source) {
self.s.push_str(&src.s);
self.indent += src.indent;
self.in_line_comment = src.in_line_comment;
}

pub fn push_str(&mut self, src: &str) {
let lines = src.lines().collect::<Vec<_>>();
for (i, line) in lines.iter().enumerate() {
let trimmed = line.trim();
if trimmed.starts_with('}') && self.s.ends_with(" ") {
self.s.pop();
self.s.pop();
if trimmed.starts_with("//") {
self.in_line_comment = true;
}

if !self.in_line_comment {
if trimmed.starts_with('}') && self.s.ends_with(" ") {
self.s.pop();
self.s.pop();
}
}
self.s.push_str(if lines.len() == 1 {
line
} else {
line.trim_start()
});
if trimmed.ends_with('{') {
self.indent += 1;
}
if trimmed.starts_with('}') {
// Note that a `saturating_sub` is used here to prevent a panic
// here in the case of invalid code being generated in debug
// mode. It's typically easier to debug those issues through
// looking at the source code rather than getting a panic.
self.indent = self.indent.saturating_sub(1);
if !self.in_line_comment {
if trimmed.ends_with('{') {
self.indent += 1;
}
if trimmed.starts_with('}') {
// Note that a `saturating_sub` is used here to prevent a panic
// here in the case of invalid code being generated in debug
// mode. It's typically easier to debug those issues through
// looking at the source code rather than getting a panic.
self.indent = self.indent.saturating_sub(1);
}
}
if i != lines.len() - 1 || src.ends_with('\n') {
self.newline();
Expand All @@ -83,6 +93,7 @@ impl Source {
}

fn newline(&mut self) {
self.in_line_comment = false;
self.s.push('\n');
for _ in 0..self.indent {
self.s.push_str(" ");
Expand Down
1 change: 0 additions & 1 deletion crates/go/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,6 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
self.preamble
.push_str(&format!("// typedef struct {c_typedef_target} "));
self.preamble.push_str("{");
self.preamble.deindent(1);
self.preamble.push_str("\n");
self.preamble.push_str("// int32_t __handle; \n");
self.preamble.push_str("// ");
Expand Down

0 comments on commit 99ac19c

Please sign in to comment.