File tree Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Original file line number Diff line number Diff 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`.
You can’t perform that action at this time.
0 commit comments