rust-analyzer / rowan

Apache License 2.0
704 stars 58 forks source link

Is that possible to mutate the SyntaxNode #123

Closed IWANABETHATGUY closed 2 years ago

IWANABETHATGUY commented 2 years ago

I am using rowan to write a transpiler , but the rowan's syntax node is immutable, so convert ast is very hard. So i am wonder if there has some best practice to do these thing.

Veykril commented 2 years ago

There is a mutable api, see https://docs.rs/rowan/0.14.1/rowan/api/struct.SyntaxNode.html#method.splice_children and for some example usages https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/syntax/src/ted.rs. To be able to mutate a node you have to first clone_for_update it though, https://docs.rs/rowan/0.14.1/rowan/api/struct.SyntaxNode.html#method.clone_for_update, if this isn't done you will panic at runtime when trying to mutate an immutable syntax node.

IWANABETHATGUY commented 2 years ago

There is a mutable api, see https://docs.rs/rowan/0.14.1/rowan/api/struct.SyntaxNode.html#method.splice_children and for some example usages https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/syntax/src/ted.rs. To be able to mutate a node you have to first clone_for_update it though, https://docs.rs/rowan/0.14.1/rowan/api/struct.SyntaxNode.html#method.clone_for_update, if this isn't done you will panic at runtime when trying to mutate an immutable syntax node.

Still have some question to ask, Could you leave me a way to get in touch, maybe like discord?If not, That's fine too, and thank you again.

lnicola commented 2 years ago

You can ask on Zulip.

IWANABETHATGUY commented 2 years ago

You can ask on Zulip.

Thanks a lot

IWANABETHATGUY commented 2 years ago

You can ask on Zulip.

It seems that Zulip don't have rowan stream, so which stream should i choose when i have a question?

lnicola commented 2 years ago

You can make a new topic in the rust-analyzer stream (which I linked to).

IWANABETHATGUY commented 2 years ago

You can make a new topic in the rust-analyzer stream (which I linked to).

Nice, Thank you.