From d0bd090efb574cda08647ac07e1870cdafe4ac7d Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 23 Jan 2018 10:54:57 -0800 Subject: Consider all whitespace when preparing span --- src/libsyntax/codemap.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index a58a61c3636..c9b524a0c89 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -592,8 +592,32 @@ 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; + let mut pos = 0; + // get the bytes width of all the non-whitespace characters + for (i, c) in snippet.chars().take_while(|c| !c.is_whitespace()).enumerate() { + offset += c.len_utf8(); + pos = i + 1; + } + // get the bytes width of all the whitespace characters after that + for c in snippet[pos..].chars().take_while(|c| c.is_whitespace()) { + offset += c.len_utf8(); + } + if offset != 0 { + 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) { -- cgit 1.4.1-3-g733a5 From fa7767e1ea825bb4cbadbb05c0bfa8ff8a9100e1 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 23 Jan 2018 13:53:08 -0800 Subject: review comment --- src/libsyntax/codemap.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index c9b524a0c89..fbd9088797e 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -598,18 +598,16 @@ impl CodeMap { /// 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; - let mut pos = 0; + let mut offset = 1; // get the bytes width of all the non-whitespace characters - for (i, c) in snippet.chars().take_while(|c| !c.is_whitespace()).enumerate() { + for c in snippet.chars().take_while(|c| !c.is_whitespace()) { offset += c.len_utf8(); - pos = i + 1; } // get the bytes width of all the whitespace characters after that - for c in snippet[pos..].chars().take_while(|c| c.is_whitespace()) { + for c in snippet[offset..].chars().take_while(|c| c.is_whitespace()) { offset += c.len_utf8(); } - if offset != 0 { + if offset > 1 { return sp.with_hi(BytePos(sp.lo().0 + offset as u32)); } } -- cgit 1.4.1-3-g733a5 From df412ce2083308cbe7c21ce2fc5622bfa8518993 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 1 Feb 2018 12:02:22 -0800 Subject: Change offset to `0` --- src/libsyntax/codemap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index fbd9088797e..e980d6e6793 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -598,7 +598,7 @@ impl CodeMap { /// 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 = 1; + 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(); -- cgit 1.4.1-3-g733a5