Skip to content

Commit 483cff7

Browse files
committed
Add SourceMap::indentation_before.
1 parent d914f17 commit 483cff7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

compiler/rustc_span/src/source_map.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,19 @@ impl SourceMap {
593593
}
594594

595595
pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
596-
match self.span_to_prev_source(sp) {
597-
Err(_) => None,
598-
Ok(source) => {
599-
let last_line = source.rsplit_once('\n').unwrap_or(("", &source)).1;
596+
Some(self.indentation_before(sp)?.len())
597+
}
600598

601-
Some(last_line.len() - last_line.trim_start().len())
602-
}
603-
}
599+
pub fn indentation_before(&self, sp: Span) -> Option<String> {
600+
self.span_to_source(sp, |src, start_index, _| {
601+
let before = &src[..start_index];
602+
let last_line = before.rsplit_once('\n').map_or(before, |(_, last)| last);
603+
Ok(last_line
604+
.split_once(|c: char| !c.is_whitespace())
605+
.map_or(last_line, |(indent, _)| indent)
606+
.to_string())
607+
})
608+
.ok()
604609
}
605610

606611
/// Returns the source snippet as `String` before the given `Span`.

0 commit comments

Comments
 (0)