ariadne icon indicating copy to clipboard operation
ariadne copied to clipboard

How to use this crate with syn/proc-macro2?

Open msrd0 opened this issue 2 years ago • 5 comments

How can I use this crate when using syn and proc-macro2 for parsing my input? proc_macro2::Span gives me a start and end proc_macro2::LineColumn, not an offset as this crate expects. It seems like there's no obvious way to use offsets that are broken down into line and column already with this crate. How can I accomplish this?

msrd0 avatar Jun 19 '22 13:06 msrd0

You'll probably need to count through the input to get the offset.

zesterer avatar Jul 04 '22 21:07 zesterer

@msrd0 I'm also curious about this. @zesterer's suggestion seems right to me, and so it seems like std::iter::fold is useful, so that a (/* line: */ mut usize, /* column: */ mut usize) can be updated at each successive input element.·

dmgolembiowski avatar Jul 12 '22 22:07 dmgolembiowski

@dmgolembiowski I'm using this code as a workaround:

fn offset(&self, at: proc_macro2::LineColumn) -> usize {
	let line_offset: usize = self
		.code
		.split('\n')
		.take(at.line - 1)
		.map(|line| line.chars().count() + 1)
		.sum();
	line_offset + at.column
}

(Source: https://github.com/msrd0/cargo-doc2readme/blob/990595f11ad3459309b1c3da9d46ac096d5bbb14/src/diagnostic.rs#L39-L47)

msrd0 avatar Jul 13 '22 07:07 msrd0

Is this addressed?

zesterer avatar Jul 18 '22 06:07 zesterer

@zesterer There exists a way to use this crate by calculating the offset from the line and column information from proc-macro. I'd still prefer a way to just pass the information directly. If this is something you'd like to incorporate into the api, and if this is something that could potentially save some computation on your end since you do output line and column information in the error message, is something you need to decide.

msrd0 avatar Jul 18 '22 10:07 msrd0