Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust whitespace during WIT printing #1073

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use anyhow::{anyhow, bail, Result};
use std::fmt::{self, Write};
use std::mem;
use wit_parser::*;

/// A utility for printing WebAssembly interface definitions to a string.
#[derive(Default)]
pub struct WitPrinter {
output: Output,

// Count of how many items in this current block have been printed to print
// a blank line between each item, but not the first item.
any_items: bool,
}

impl WitPrinter {
Expand Down Expand Up @@ -39,8 +44,16 @@ impl WitPrinter {
Ok(std::mem::take(&mut self.output).into())
}

fn new_item(&mut self) {
if self.any_items {
self.output.push_str("\n");
}
self.any_items = true;
}

/// Print the given WebAssembly interface to a string.
fn print_interface(&mut self, resolve: &Resolve, id: InterfaceId) -> Result<()> {
let prev_items = mem::replace(&mut self.any_items, false);
let interface = &resolve.interfaces[id];

self.print_types(
Expand All @@ -52,16 +65,16 @@ impl WitPrinter {
.map(|(name, id)| (name.as_str(), *id)),
)?;

for (i, (name, func)) in interface.functions.iter().enumerate() {
if i > 0 {
self.output.push_str("\n");
}
for (name, func) in interface.functions.iter() {
self.new_item();
self.print_name(name);
self.output.push_str(": ");
self.print_function(resolve, func)?;
self.output.push_str("\n");
}

self.any_items = prev_items;

Ok(())
}

Expand Down Expand Up @@ -108,8 +121,8 @@ impl WitPrinter {
TypeOwner::World(id) => resolve.worlds[id].package.unwrap(),
TypeOwner::None => unreachable!(),
};
let amt_to_import = types_to_import.len();
for (owner, tys) in types_to_import {
self.any_items = true;
write!(&mut self.output, "use ")?;
let id = match owner {
TypeOwner::Interface(id) => id,
Expand All @@ -134,11 +147,8 @@ impl WitPrinter {
writeln!(&mut self.output, "}}")?;
}

if amt_to_import > 0 && types_to_declare.len() > 0 {
self.output.push_str("\n");
}

for id in types_to_declare {
self.new_item();
self.declare_type(resolve, &Type::Id(id))?;
}

Expand Down Expand Up @@ -182,6 +192,7 @@ impl WitPrinter {
}

fn print_world(&mut self, resolve: &Resolve, id: WorldId) -> Result<()> {
let prev_items = mem::replace(&mut self.any_items, false);
let world = &resolve.worlds[id];
let pkgid = world.package.unwrap();
let mut types = Vec::new();
Expand All @@ -193,13 +204,21 @@ impl WitPrinter {
},
_ => {
self.print_world_item(resolve, name, import, pkgid, "import")?;
// Don't put a blank line between imports, but count
// imports as having printed something so if anything comes
// after them then a blank line is printed after imports.
self.any_items = true;
}
}
}
self.print_types(resolve, TypeOwner::World(id), types.into_iter())?;
if !world.exports.is_empty() {
self.new_item();
}
for (name, export) in world.exports.iter() {
self.print_world_item(resolve, name, export, pkgid, "export")?;
}
self.any_items = prev_items;
Ok(())
}

Expand Down Expand Up @@ -466,7 +485,7 @@ impl WitPrinter {
self.print_name(name);
self.output.push_str(" = ");
self.print_type_name(resolve, inner)?;
self.output.push_str("\n\n");
self.output.push_str("\n");
}
None => bail!("unnamed type in document"),
},
Expand All @@ -490,7 +509,7 @@ impl WitPrinter {
self.print_name(name);
self.output.push_str(" = ");
self.print_handle_type(resolve, handle)?;
self.output.push_str("\n\n");
self.output.push_str("\n");

Ok(())
}
Expand Down Expand Up @@ -597,7 +616,7 @@ impl WitPrinter {
self.print_type_name(resolve, &field.ty)?;
self.output.push_str(",\n");
}
self.output.push_str("}\n\n");
self.output.push_str("}\n");
Ok(())
}
None => bail!("document has unnamed record type"),
Expand All @@ -615,7 +634,7 @@ impl WitPrinter {
self.print_name(name);
self.output.push_str(" = ");
self.print_tuple_type(resolve, tuple)?;
self.output.push_str("\n\n");
self.output.push_str("\n");
}
Ok(())
}
Expand All @@ -630,7 +649,7 @@ impl WitPrinter {
self.print_name(&flag.name);
self.output.push_str(",\n");
}
self.output.push_str("}\n\n");
self.output.push_str("}\n");
}
None => bail!("document has unnamed flags type"),
}
Expand Down Expand Up @@ -659,7 +678,7 @@ impl WitPrinter {
}
self.output.push_str(",\n");
}
self.output.push_str("}\n\n");
self.output.push_str("}\n");
Ok(())
}

Expand All @@ -681,7 +700,7 @@ impl WitPrinter {
self.print_type_name(resolve, &case.ty)?;
self.output.push_str(",\n");
}
self.output.push_str("}\n\n");
self.output.push_str("}\n");
Ok(())
}

Expand All @@ -696,7 +715,7 @@ impl WitPrinter {
self.print_name(name);
self.output.push_str(" = ");
self.print_option_type(resolve, payload)?;
self.output.push_str("\n\n");
self.output.push_str("\n");
}
Ok(())
}
Expand All @@ -712,7 +731,7 @@ impl WitPrinter {
self.print_name(name);
self.output.push_str(" = ");
self.print_result_type(resolve, result)?;
self.output.push_str("\n\n");
self.output.push_str("\n");
}
Ok(())
}
Expand All @@ -729,7 +748,7 @@ impl WitPrinter {
self.print_name(&case.name);
self.output.push_str(",\n");
}
self.output.push_str("}\n\n");
self.output.push_str("}\n");
Ok(())
}

Expand All @@ -739,7 +758,7 @@ impl WitPrinter {
self.print_name(name);
self.output.push_str(" = list<");
self.print_type_name(resolve, ty)?;
self.output.push_str(">\n\n");
self.output.push_str(">\n");
return Ok(());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ world root {
import new: interface {
read: func(amt: u32) -> list<u8>
}

export entrypoint: func(args: list<string>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package root:component
world root {
import foo:foo/adapter-imports
import foo: func(x: string)

export bar: func()
export adapter-bar: func(x: string)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package root:component
world root {
import foo: func()
import bar: func() -> string

export baz: func()
export foo2: func(x: string) -> option<list<u8>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package root:component

world root {
import foo:foo/foo

export a: func(b: u8)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root:component

world root {
import foo:foo/foo

export x: interface {
use foo:foo/foo.{r}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ world root {
export foo:foo/name
export name: interface {
use foo:foo/name.{foo}

a: func(f: foo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package root:component

world root {
import foo:foo/foo

export bar: interface {
use foo:foo/foo.{foo as bar}

foo: func() -> bar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package root:component
world root {
import foo:dep/the-name
import foo:foo/the-name

export foo:foo/the-name
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ world root {
import foo: interface {
a: func() -> string
}

export bar: interface {
a: func()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ world root {
import foo:shared-dependency/types
import main-dep: interface {
use foo:shared-dependency/types.{a-typedef}

foo: func() -> a-typedef
}
import adapter-dep: interface {
use foo:shared-dependency/types.{a-typedef}

foo: func() -> a-typedef
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package root:component

world root {
import foo:foo/name

export foo:foo/name
export name: interface {
use foo:foo/name.{r2}

a: func()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ world root {
import foo:foo/foo
import other-name: interface {
use foo:foo/foo.{bar}

a: func() -> bar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package root:component
world root {
import foo:foo/name1
import foo:foo/name2

export name: interface {
use foo:foo/name1.{name}
use foo:foo/name2.{name as name1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package root:component
world root {
import foo:foo/i
use foo:foo/i.{some-type as other-name}

export foo:foo/i
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package foo:foo

interface shared2 {
type t2 = u8

}

interface shared1 {
type t1 = u8

}

world w1 {
Expand Down
3 changes: 2 additions & 1 deletion crates/wit-component/tests/interfaces/diamond.wit.print
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ interface shared-items {
enum the-enum {
a,
}

}

world w3 {
import shared-items

export bar: interface {
use shared-items.{the-enum}
}
Expand All @@ -18,6 +18,7 @@ world w2 {
import foo: interface {
use shared-items.{the-enum}
}

export bar: interface {
use shared-items.{the-enum}
}
Expand Down
1 change: 1 addition & 0 deletions crates/wit-component/tests/interfaces/empty.wit.print
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ world empty-world {
import empty
import empty: interface {
}

export empty
export empty2: interface {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ interface bar {

world import-and-export {
import foo

export bar
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package foo:foo
interface b {
record the-type {
}

}

interface b2 {
Expand Down
2 changes: 0 additions & 2 deletions crates/wit-component/tests/interfaces/multiple-use.wit.print
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ interface foo {
type t2 = u8

type t1 = u8

}

interface bar {
type u = u8

}

interface baz {
Expand Down
Loading