From 871856e8312e7248d12f06f0faa1555f38e50a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 2 Oct 2017 15:28:52 -0700 Subject: [PATCH] Highlight code on diagnostics when underlined --- src/librustc_errors/emitter.rs | 8 +++++++- src/librustc_errors/styled_buffer.rs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 58f851aea38..289383088c8 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -551,7 +551,13 @@ impl EmitterWriter { code_offset + annotation.start_col, style); } - _ => (), + _ => { + buffer.set_style_range(line_offset, + code_offset + annotation.start_col, + code_offset + annotation.end_col, + style, + annotation.is_primary); + } } } diff --git a/src/librustc_errors/styled_buffer.rs b/src/librustc_errors/styled_buffer.rs index 2c33f805203..2c736ec22c3 100644 --- a/src/librustc_errors/styled_buffer.rs +++ b/src/librustc_errors/styled_buffer.rs @@ -144,4 +144,25 @@ impl StyledBuffer { pub fn num_lines(&self) -> usize { self.text.len() } + + pub fn set_style_range(&mut self, + line: usize, + col_start: usize, + col_end: usize, + style: Style, + overwrite: bool) { + for col in col_start..col_end { + self.set_style(line, col, style, overwrite); + } + } + + pub fn set_style(&mut self, line: usize, col: usize, style: Style, overwrite: bool) { + if let Some(ref mut line) = self.styles.get_mut(line) { + if let Some(s) = line.get_mut(col) { + if *s == Style::NoStyle || *s == Style::Quotation || overwrite { + *s = style; + } + } + } + } }