about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorEsteban Kuber <esteban@kuber.com.ar>2021-08-23 12:42:08 +0000
committerEsteban Kuber <esteban@kuber.com.ar>2021-08-23 14:31:48 +0000
commit955e913612648056853b4021a5d9046775538fd7 (patch)
treee5fc65792fa28ff725697d23d644f7bb64b7cbc9 /compiler/rustc_errors/src
parent31d07edc94814069a02bea7341edfa35c7068786 (diff)
downloadrust-955e913612648056853b4021a5d9046775538fd7.tar.gz
rust-955e913612648056853b4021a5d9046775538fd7.zip
review comments
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/lib.rs31
1 files changed, 26 insertions, 5 deletions
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index 8c0a9d5aae3..a48d4fe8bb5 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -321,21 +321,42 @@ impl CodeSuggestion {
                         }
                     }
                     // Add a whole line highlight per line in the snippet.
-                    let len = part.snippet.split('\n').next().unwrap_or(&part.snippet).len();
+                    let len: isize = part
+                        .snippet
+                        .split('\n')
+                        .next()
+                        .unwrap_or(&part.snippet)
+                        .chars()
+                        .map(|c| match c {
+                            '\t' => 4,
+                            _ => 1,
+                        })
+                        .sum();
                     line_highlight.push(SubstitutionHighlight {
                         start: (cur_lo.col.0 as isize + acc) as usize,
-                        end: (cur_lo.col.0 as isize + acc + len as isize) as usize,
+                        end: (cur_lo.col.0 as isize + acc + len) as usize,
                     });
                     buf.push_str(&part.snippet);
-                    prev_hi = sm.lookup_char_pos(part.span.hi());
+                    let cur_hi = sm.lookup_char_pos(part.span.hi());
                     if prev_hi.line == cur_lo.line {
-                        acc += len as isize - (prev_hi.col.0 - cur_lo.col.0) as isize;
+                        // Account for the difference between the width of the current code and the
+                        // snippet being suggested, so that the *later* suggestions are correctly
+                        // aligned on the screen.
+                        acc += len as isize - (cur_hi.col.0 - cur_lo.col.0) as isize;
                     }
+                    prev_hi = cur_hi;
                     prev_line = sf.get_line(prev_hi.line - 1);
                     for line in part.snippet.split('\n').skip(1) {
                         acc = 0;
                         highlights.push(std::mem::take(&mut line_highlight));
-                        line_highlight.push(SubstitutionHighlight { start: 0, end: line.len() });
+                        let end: usize = line
+                            .chars()
+                            .map(|c| match c {
+                                '\t' => 4,
+                                _ => 1,
+                            })
+                            .sum();
+                        line_highlight.push(SubstitutionHighlight { start: 0, end });
                     }
                 }
                 highlights.push(std::mem::take(&mut line_highlight));