about summary refs log tree commit diff
path: root/src/librustc_errors/styled_buffer.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-12-06 21:05:53 +0000
committerbors <bors@rust-lang.org>2017-12-06 21:05:53 +0000
commit5a2465e2b44ecdd3b5d835c0abe29e9a4c9dcfe4 (patch)
tree76b353607669b391077b7230d0c0c13966bbbc29 /src/librustc_errors/styled_buffer.rs
parentcf30759a8409bee031ac252ee207452ab4804467 (diff)
parent9d80e2200af22bb4532a7cc4738b22e408072ffd (diff)
downloadrust-5a2465e2b44ecdd3b5d835c0abe29e9a4c9dcfe4.tar.gz
rust-5a2465e2b44ecdd3b5d835c0abe29e9a4c9dcfe4.zip
Auto merge of #45953 - estebank:tab-4, r=nikomatsakis
Display `\t` in diagnostics code as four spaces

Follow up to #44386 using the unicode variable width machinery from #45711 to replace tabs in the source code when displaying a diagnostic error with four spaces (instead of only one), while properly accounting for this when calculating underlines.

Partly addresses #44618.
Diffstat (limited to 'src/librustc_errors/styled_buffer.rs')
-rw-r--r--src/librustc_errors/styled_buffer.rs17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/librustc_errors/styled_buffer.rs b/src/librustc_errors/styled_buffer.rs
index ceb94f27dc3..2c33f805203 100644
--- a/src/librustc_errors/styled_buffer.rs
+++ b/src/librustc_errors/styled_buffer.rs
@@ -27,10 +27,21 @@ impl StyledBuffer {
     }
 
     fn replace_tabs(&mut self) {
-        for line in self.text.iter_mut() {
-            for c in line.iter_mut() {
+        for (line_pos, line) in self.text.iter_mut().enumerate() {
+            let mut tab_pos = vec![];
+            for (pos, c) in line.iter().enumerate() {
                 if *c == '\t' {
-                    *c = ' ';
+                    tab_pos.push(pos);
+                }
+            }
+            // start with the tabs at the end of the line to replace them with 4 space chars
+            for pos in tab_pos.iter().rev() {
+                assert_eq!(line.remove(*pos), '\t');
+                // fix the position of the style to match up after replacing the tabs
+                let s = self.styles[line_pos].remove(*pos);
+                for _ in 0..4 {
+                    line.insert(*pos, ' ');
+                    self.styles[line_pos].insert(*pos, s);
                 }
             }
         }