Skip to content

Commit

Permalink
Fix pretty printing signature and output
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslavsky committed Jun 1, 2024
1 parent a837ff2 commit 7b36db9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
21 changes: 17 additions & 4 deletions crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ impl WitPrinter {
}

/// Print a set of one or more WIT packages into a string.
pub fn print(&mut self, resolve: &Resolve, pkg_ids: Vec<PackageId>) -> Result<String> {
pub fn print(&mut self, resolve: &Resolve, pkg_ids: &[PackageId]) -> Result<String> {
let has_multiple_packages = pkg_ids.len() > 1;
for (i, pkg_id) in pkg_ids.into_iter().enumerate() {
if i > 0 {
self.output.push_str("\n\n");
}

let pkg = &resolve.packages[pkg_id];
let pkg = &resolve.packages[pkg_id.clone()];
self.print_docs(&pkg.docs);
self.output.push_str("package ");
self.print_name(&pkg.name.namespace);
Expand All @@ -66,8 +67,15 @@ impl WitPrinter {
if let Some(version) = &pkg.name.version {
self.output.push_str(&format!("@{version}"));
}
self.print_semicolon();
self.output.push_str("\n\n");

if has_multiple_packages {
self.output.push_str("{");
self.output.indent += 1
} else {
self.print_semicolon();
self.output.push_str("\n\n");
}

for (name, id) in pkg.interfaces.iter() {
self.print_docs(&resolve.interfaces[*id].docs);
self.print_stability(&resolve.interfaces[*id].stability);
Expand All @@ -87,6 +95,11 @@ impl WitPrinter {
self.print_world(resolve, *id)?;
writeln!(&mut self.output, "}}")?;
}

if has_multiple_packages {
self.output.push_str("}");
self.output.indent -= 1
}
}

Ok(std::mem::take(&mut self.output).into())
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn run_test(path: &Path) -> Result<()> {
}
};
let wit = WitPrinter::default()
.print(&resolve, vec![pkg])
.print(&resolve, &[pkg])
.context("failed to print WIT")?;
assert_output(&wit, &component_wit_path)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn assert_print(
path: &Path,
is_dir: bool,
) -> Result<()> {
let output = WitPrinter::default().print(resolve, pkg_ids.clone())?;
let output = WitPrinter::default().print(resolve, &pkg_ids)?;
for pkg_id in pkg_ids {
let pkg = &resolve.packages[pkg_id];
let expected = if is_dir {
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn merging() -> Result<()> {
.join("merge")
.join(&pkg.name.name)
.with_extension("wit");
let output = WitPrinter::default().print(&into, vec![id])?;
let output = WitPrinter::default().print(&into, &[id])?;
assert_output(&expected, &output)?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/wasm-tools/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl WitOpts {
}

for (id, pkg) in resolve.packages.iter() {
let output = printer.print(resolve, vec![id])?;
let output = printer.print(resolve, &[id])?;
let out_dir = if main.contains(&id) {
dir.clone()
} else {
Expand All @@ -664,7 +664,7 @@ impl WitOpts {
}
}
None => {
let output = printer.print(resolve, main)?;
let output = printer.print(resolve, &main)?;
self.output.output(Output::Wat(&output))?;
}
}
Expand Down

0 comments on commit 7b36db9

Please sign in to comment.