Add opt_snippet() and opt_span_after()

This commit is contained in:
topecongiro 2017-11-10 17:08:57 +09:00
parent 0922f3f427
commit 6ce42823cb
2 changed files with 12 additions and 0 deletions

View File

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

View File

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