about summary refs log tree commit diff
path: root/compiler/rustc_span/src/source_map.rs
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2023-06-06 23:51:09 +0800
committeryukang <moorekang@gmail.com>2023-06-10 06:28:35 +0800
commit3983881d4e00c2b12d1b5b0319b4c61d72926917 (patch)
treeaccd8e6288d3730eabfb2c5e92843fa87cb712a1 /compiler/rustc_span/src/source_map.rs
parent43062c43d2a63cf4e261c6eddc417575c4f3062f (diff)
downloadrust-3983881d4e00c2b12d1b5b0319b4c61d72926917.tar.gz
rust-3983881d4e00c2b12d1b5b0319b4c61d72926917.zip
take care module name for suggesting surround the struct literal in parentheses
Diffstat (limited to 'compiler/rustc_span/src/source_map.rs')
-rw-r--r--compiler/rustc_span/src/source_map.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs
index 1824510a974..f354751112f 100644
--- a/compiler/rustc_span/src/source_map.rs
+++ b/compiler/rustc_span/src/source_map.rs
@@ -744,6 +744,18 @@ impl SourceMap {
         })
     }
 
+    /// Extends the given `Span` to previous character while the previous character matches the predicate
+    pub fn span_extend_prev_while(
+        &self,
+        span: Span,
+        f: impl Fn(char) -> bool,
+    ) -> Result<Span, SpanSnippetError> {
+        self.span_to_source(span, |s, start, _end| {
+            let n = s[..start].char_indices().rfind(|&(_, c)| !f(c)).map_or(start, |(i, _)| start - i - 1);
+            Ok(span.with_lo(span.lo() - BytePos(n as u32)))
+        })
+    }
+
     /// Extends the given `Span` to just before the next occurrence of `c`.
     pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
         if let Ok(next_source) = self.span_to_next_source(sp) {