bytecodealliance / wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Apache License 2.0
1.36k stars 248 forks source link

reformat wit files #1290

Open yamt opened 1 year ago

yamt commented 1 year ago

is there rustfmt-like tools for wit files? otherwise, this is a feature request.

moved from: https://github.com/bytecodealliance/wit-bindgen/issues/710

yamt commented 1 year ago

while wasm-tools component wit can perform wit-to-wit conversion, it doesn't seem to work for complex cases like multi-file definitions with use.

oovm commented 6 months ago

As a temporary solution, you can try Intellij WIT, even the community version can use this plugin.

It supports formatting the selected area, or the entire file, or the wit files in the entire directory.

And support using .editorconfig to control indentation.

This is a pure lexer-level formatter that does not check whether the symbol exists, and supports experimental features such as nested namespace.

oovm commented 6 months ago

The current formatter is written in pure kotlin.

In the long term, I may switch to a rust native implementation via the document/formatter protocol in lsp.

If you want to develop a native formatter, wit-parser should support prefix-tokens and postfix-tokens, like COMMENT_LINE, COMMENT_BLOCK, WHITE_SPACE, NEW_LINE.

alexcrichton commented 6 months ago

Nice @oovm! FWIW the Rust lexer lives here but isn't exposed at the crate root just yet. I don't think it'd be a problem exposing that from the wit-parser crate if you'd find that useful. I've not written many lexers for IDEs though so it may be too strict, but I think it'd be fine to change it to suit your use case as well too

cpetig commented 3 months ago

I was missing witfmt too and started a toy prototype in https://github.com/bytecodealliance/wasm-tools/commit/fc6a1656c32cdcc609ffce5fc18f4bff6c8c967b