syn-rsx icon indicating copy to clipboard operation
syn-rsx copied to clipboard

Node method to get span for the whole node

Open stoically opened this issue 5 years ago • 4 comments

This should probably span all children nodes as well? Like in the case of e.g. <><div /></> the fragment span should be 0 to 12? Is that what you'd expect from such method, @ZetaNumbers?

If possible add a field containing the span to node types.

stoically avatar Jan 07 '21 17:01 stoically

I will link to this issue this inside of my code. Thank you.

zetanumbers avatar Jan 07 '21 17:01 zetanumbers

Btw I saw for example NodeName straightforward implementing span method. You should consider implementing the syn::spanned::Spanned trait instead.

zetanumbers avatar Jan 07 '21 17:01 zetanumbers

Btw I saw for example NodeName straightforward implementing span method. You should consider implementing the syn::spanned::Spanned trait instead.

Thanks for the hint. While reading https://docs.rs/syn/latest/syn/spanned/trait.Spanned.html, I realized that Spanned is automatically implemented if ToTokens is implemented, which is already the case for NodeName. So I can just drop the manual span implementation.

stoically avatar Oct 22 '22 16:10 stoically

It seems to implement a span method for a whole node it'd need the https://docs.rs/proc-macro2/latest/proc_macro2/struct.Span.html#method.join method, which currently is still nightly only, since it depends on https://github.com/rust-lang/rust/issues/54725.

Unless I'm missing a different way to construct a span from a start and end span.

stoically avatar Oct 24 '22 08:10 stoically

While this can't be implemented properly in stable until Span::join() is stabilized, it would be nice for diagnostic purposes to be able to get some span for every kind of node even if it doesn't actually cover the entire node. I think the worst case here is fragment, but even there we can default to the start of the opening tag which definitely beats using Span::call_site(). Happy to attempt a PR if you want.

danheuck avatar Oct 26 '22 16:10 danheuck

@danheuck I like the idea and would be happy to review a PR for it. This could probably also help with #12, where the span would help with formatting custom errors.

stoically avatar Oct 26 '22 17:10 stoically

Only thing I'm a bit unsure about is in which cases it actually makes sense to store an additional span, given that the Expressions already give access to spans.

stoically avatar Oct 26 '22 17:10 stoically