From d316eba54d6778593ec87cdeffc32a89d0064ae9 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 6 Mar 2018 19:46:03 +0900 Subject: [PATCH] Add opt_span_before() to SpanUtils trait With some refactorings to avoid duplicated code. --- src/codemap.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/codemap.rs b/src/codemap.rs index 224b2083561..f6c05f5be40 100644 --- a/src/codemap.rs +++ b/src/codemap.rs @@ -22,6 +22,7 @@ pub trait SpanUtils { 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; + fn opt_span_before(&self, original: Span, needle: &str) -> Option; } pub trait LineRangeUtils { @@ -35,10 +36,7 @@ pub trait LineRangeUtils { impl<'a> SpanUtils for SnippetProvider<'a> { fn span_after(&self, original: Span, needle: &str) -> BytePos { - let snippet = self.span_to_snippet(original).expect("Bad snippet"); - let offset = snippet.find_uncommented(needle).expect("Bad offset") + needle.len(); - - original.lo() + BytePos(offset as u32) + self.opt_span_after(original, needle).expect("bad span") } fn span_after_last(&self, original: Span, needle: &str) -> BytePos { @@ -53,15 +51,17 @@ impl<'a> SpanUtils for SnippetProvider<'a> { } fn span_before(&self, original: Span, needle: &str) -> BytePos { - let snippet = self.span_to_snippet(original).unwrap(); - let offset = snippet.find_uncommented(needle).unwrap(); - - original.lo() + BytePos(offset as u32) + self.opt_span_before(original, needle).expect("bad span") } fn opt_span_after(&self, original: Span, needle: &str) -> Option { + self.opt_span_before(original, needle) + .map(|bytepos| bytepos + BytePos(needle.len() as u32)) + } + + fn opt_span_before(&self, original: Span, needle: &str) -> Option { let snippet = self.span_to_snippet(original)?; - let offset = snippet.find_uncommented(needle)? + needle.len(); + let offset = snippet.find_uncommented(needle)?; Some(original.lo() + BytePos(offset as u32)) }