diff options
| author | bors <bors@rust-lang.org> | 2018-01-31 07:53:57 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-01-31 07:53:57 +0000 |
| commit | 560a2f4faf4828ba5f48fe2bd7709265c2f5354d (patch) | |
| tree | 14d4693f6908a4f3f7ece6cf63c00cff4d540a9b /src/librustc_errors | |
| parent | b8f2674ea47f4f3cf5d90d0428bf8b8025d7f8d8 (diff) | |
| parent | 08287c1e26c6faa18c145a8a794fdd25408217a4 (diff) | |
| download | rust-560a2f4faf4828ba5f48fe2bd7709265c2f5354d.tar.gz rust-560a2f4faf4828ba5f48fe2bd7709265c2f5354d.zip | |
Auto merge of #45752 - estebank:highlight-primary, r=nikomatsakis
Highlight code on diagnostics when underlined Highlight the label's span with the respective color: <img width="692" alt="" src="https://user-images.githubusercontent.com/1606434/32411026-a1842482-c18d-11e7-9933-6510eefbad19.png"> Fix #42112.
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/emitter.rs | 25 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 2 | ||||
| -rw-r--r-- | src/librustc_errors/styled_buffer.rs | 21 |
3 files changed, 41 insertions, 7 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index ffb5efd93ed..a59f68a6442 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -106,6 +106,7 @@ pub struct EmitterWriter { dst: Destination, cm: Option<Rc<CodeMapper>>, short_message: bool, + teach: bool, } struct FileWithAnnotatedLines { @@ -117,32 +118,37 @@ struct FileWithAnnotatedLines { impl EmitterWriter { pub fn stderr(color_config: ColorConfig, code_map: Option<Rc<CodeMapper>>, - short_message: bool) + short_message: bool, + teach: bool) -> EmitterWriter { if color_config.use_color() { let dst = Destination::from_stderr(); EmitterWriter { dst, cm: code_map, - short_message: short_message, + short_message, + teach, } } else { EmitterWriter { dst: Raw(Box::new(io::stderr())), cm: code_map, - short_message: short_message, + short_message, + teach, } } } pub fn new(dst: Box<Write + Send>, code_map: Option<Rc<CodeMapper>>, - short_message: bool) + short_message: bool, + teach: bool) -> EmitterWriter { EmitterWriter { dst: Raw(dst), cm: code_map, - short_message: short_message, + short_message, + teach, } } @@ -551,7 +557,14 @@ impl EmitterWriter { code_offset + annotation.start_col, style); } - _ => (), + _ if self.teach => { + 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/lib.rs b/src/librustc_errors/lib.rs index 3d50c95d3f4..47eb04621a1 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -297,7 +297,7 @@ impl Handler { cm: Option<Rc<CodeMapper>>, flags: HandlerFlags) -> Handler { - let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false)); + let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false)); Handler::with_emitter_and_flags(emitter, flags) } 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; + } + } + } + } } |
