about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-02-02 04:19:12 +0000
committerbors <bors@rust-lang.org>2018-02-02 04:19:12 +0000
commit616b66dca25a67321b1654e5a65acc6337d63cf4 (patch)
tree7c1ba7adbdb7a15c9bb7137c99f156254d82dcf1 /src/libsyntax
parent6741e416feb54b18de41c348ecc70ba5cbc961ce (diff)
parentdf412ce2083308cbe7c21ce2fc5622bfa8518993 (diff)
downloadrust-616b66dca25a67321b1654e5a65acc6337d63cf4.tar.gz
rust-616b66dca25a67321b1654e5a65acc6337d63cf4.zip
Auto merge of #47465 - estebank:include-space-after-mut, r=nikomatsakis
Include space in suggestion `mut` in bindings

Fix #46614.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/codemap.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs
index a6a7f9e20b3..3601b9ba8a8 100644
--- a/src/libsyntax/codemap.rs
+++ b/src/libsyntax/codemap.rs
@@ -593,8 +593,30 @@ impl CodeMap {
         }
     }
 
-    /// Given a `Span`, try to get a shorter span ending just after the first
-    /// occurrence of `char` `c`.
+    /// Given a `Span`, get a new `Span` covering the first token and all its trailing whitespace or
+    /// the original `Span`.
+    ///
+    /// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
+    pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
+        if let Ok(snippet) = self.span_to_snippet(sp) {
+            let mut offset = 0;
+            // get the bytes width of all the non-whitespace characters
+            for c in snippet.chars().take_while(|c| !c.is_whitespace()) {
+                offset += c.len_utf8();
+            }
+            // get the bytes width of all the whitespace characters after that
+            for c in snippet[offset..].chars().take_while(|c| c.is_whitespace()) {
+                offset += c.len_utf8();
+            }
+            if offset > 1 {
+                return sp.with_hi(BytePos(sp.lo().0 + offset as u32));
+            }
+        }
+        sp
+    }
+
+    /// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
+    /// `c`.
     pub fn span_through_char(&self, sp: Span, c: char) -> Span {
         if let Ok(snippet) = self.span_to_snippet(sp) {
             if let Some(offset) = snippet.find(c) {