Skip to content

Commit 95ad6df

Browse files
committed
add span_extend_to_prev_char_before() to SourceMap
1 parent e643f59 commit 95ad6df

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

compiler/rustc_span/src/source_map.rs

+18
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,24 @@ impl SourceMap {
633633
sp
634634
}
635635

636+
/// Extends the given `Span` to just before the previous occurrence of `c`. Return the same span
637+
/// if an error occurred while retrieving the code snippet.
638+
pub fn span_extend_to_prev_char_before(
639+
&self,
640+
sp: Span,
641+
c: char,
642+
accept_newlines: bool,
643+
) -> Span {
644+
if let Ok(prev_source) = self.span_to_prev_source(sp) {
645+
let prev_source = prev_source.rsplit(c).next().unwrap_or("");
646+
if accept_newlines || !prev_source.contains('\n') {
647+
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32 - 1_u32));
648+
}
649+
}
650+
651+
sp
652+
}
653+
636654
/// Extends the given `Span` to just after the previous occurrence of `pat` when surrounded by
637655
/// whitespace. Returns None if the pattern could not be found or if an error occurred while
638656
/// retrieving the code snippet.

0 commit comments

Comments
 (0)