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

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

Open
msrd0 opened this issue Jun 19, 2022 · 5 comments
Open

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

msrd0 opened this issue Jun 19, 2022 · 5 comments

Comments

@msrd0
Copy link
Contributor

msrd0 commented Jun 19, 2022

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?

@zesterer
Copy link
Owner

zesterer commented Jul 4, 2022

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

@dmgolembiowski
Copy link

@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.·

@msrd0
Copy link
Contributor Author

msrd0 commented Jul 13, 2022

@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)

@zesterer
Copy link
Owner

Is this addressed?

@msrd0
Copy link
Contributor Author

msrd0 commented Jul 18, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants