mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-06 13:44:13 +00:00
Add opt_snippet() and opt_span_after()
This commit is contained in:
parent
0922f3f427
commit
6ce42823cb
@ -34,6 +34,7 @@ pub trait SpanUtils {
|
||||
fn span_after(&self, original: Span, needle: &str) -> BytePos;
|
||||
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
|
||||
fn span_before(&self, original: Span, needle: &str) -> BytePos;
|
||||
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
|
||||
}
|
||||
|
||||
pub trait LineRangeUtils {
|
||||
@ -70,6 +71,13 @@ impl SpanUtils for CodeMap {
|
||||
|
||||
original.lo() + BytePos(offset as u32)
|
||||
}
|
||||
|
||||
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
|
||||
let snippet = self.span_to_snippet(original).ok()?;
|
||||
let offset = snippet.find_uncommented(needle)? + needle.len();
|
||||
|
||||
Some(original.lo() + BytePos(offset as u32))
|
||||
}
|
||||
}
|
||||
|
||||
impl LineRangeUtils for CodeMap {
|
||||
|
@ -510,6 +510,10 @@ impl<'a> FmtVisitor<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn opt_snippet(&self, span: Span) -> Option<String> {
|
||||
self.codemap.span_to_snippet(span).ok()
|
||||
}
|
||||
|
||||
pub fn snippet(&self, span: Span) -> String {
|
||||
match self.codemap.span_to_snippet(span) {
|
||||
Ok(s) => s,
|
||||
|
Loading…
Reference in New Issue
Block a user