diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2018-01-23 10:54:57 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-01-26 14:24:17 -0800 |
| commit | d0bd090efb574cda08647ac07e1870cdafe4ac7d (patch) | |
| tree | e4188458fa2636a08f7364eb1b437e4a0fb8b7c9 /src/libsyntax | |
| parent | a8f77e12fca0072d08745feb8c32900c5a46967a (diff) | |
| download | rust-d0bd090efb574cda08647ac07e1870cdafe4ac7d.tar.gz rust-d0bd090efb574cda08647ac07e1870cdafe4ac7d.zip | |
Consider all whitespace when preparing span
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/codemap.rs | 28 |
1 files changed, 26 insertions, 2 deletions
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) { |
