A little more refactoring inside emitter.rs

This commit is contained in:
Nick Cameron 2015-12-15 16:42:05 +13:00
parent 7a0334944b
commit e2371518c4

View File

@ -39,6 +39,16 @@ pub enum ColorConfig {
Never, Never,
} }
impl ColorConfig {
fn use_color(&self) -> bool {
match *self {
ColorConfig::Always => true,
ColorConfig::Never => false,
ColorConfig::Auto => stderr_isatty(),
}
}
}
// A basic emitter for when we don't have access to a codemap or registry. Used // A basic emitter for when we don't have access to a codemap or registry. Used
// for reporting very early errors, etc. // for reporting very early errors, etc.
pub struct BasicEmitter { pub struct BasicEmitter {
@ -64,24 +74,12 @@ impl Emitter for BasicEmitter {
} }
impl BasicEmitter { impl BasicEmitter {
// TODO refactor
pub fn stderr(color_config: ColorConfig) -> BasicEmitter { pub fn stderr(color_config: ColorConfig) -> BasicEmitter {
let stderr = io::stderr(); if color_config.use_color() {
let dst = Destination::from_stderr();
let use_color = match color_config {
ColorConfig::Always => true,
ColorConfig::Never => false,
ColorConfig::Auto => stderr_isatty(),
};
if use_color {
let dst = match term::stderr() {
Some(t) => Terminal(t),
None => Raw(Box::new(stderr)),
};
BasicEmitter { dst: dst } BasicEmitter { dst: dst }
} else { } else {
BasicEmitter { dst: Raw(Box::new(stderr)) } BasicEmitter { dst: Raw(Box::new(io::stderr())) }
} }
} }
} }
@ -139,22 +137,11 @@ impl EmitterWriter {
registry: Option<diagnostics::registry::Registry>, registry: Option<diagnostics::registry::Registry>,
code_map: Rc<codemap::CodeMap>) code_map: Rc<codemap::CodeMap>)
-> EmitterWriter { -> EmitterWriter {
let stderr = io::stderr(); if color_config.use_color() {
let dst = Destination::from_stderr();
let use_color = match color_config {
ColorConfig::Always => true,
ColorConfig::Never => false,
ColorConfig::Auto => stderr_isatty(),
};
if use_color {
let dst = match term::stderr() {
Some(t) => Terminal(t),
None => Raw(Box::new(stderr)),
};
EmitterWriter { dst: dst, registry: registry, cm: code_map } EmitterWriter { dst: dst, registry: registry, cm: code_map }
} else { } else {
EmitterWriter { dst: Raw(Box::new(stderr)), registry: registry, cm: code_map } EmitterWriter { dst: Raw(Box::new(io::stderr())), registry: registry, cm: code_map }
} }
} }
@ -476,22 +463,18 @@ impl EmitterWriter {
loop { loop {
let span_name_span = self.cm.with_expn_info(span.expn_id, |expn_info| { let span_name_span = self.cm.with_expn_info(span.expn_id, |expn_info| {
match expn_info { expn_info.map(|ei| {
Some(ei) => { let (pre, post) = match ei.callee.format {
let (pre, post) = match ei.callee.format { codemap::MacroAttribute(..) => ("#[", "]"),
codemap::MacroAttribute(..) => ("#[", "]"), codemap::MacroBang(..) => ("", "!"),
codemap::MacroBang(..) => ("", "!"), };
}; let macro_decl_name = format!("in this expansion of {}{}{}",
let macro_decl_name = format!("in this expansion of {}{}{}", pre,
pre, ei.callee.name(),
ei.callee.name(), post);
post); let def_site_span = ei.callee.span;
let def_site_span = ei.callee.span; (ei.call_site, macro_decl_name, def_site_span)
Some((ei.call_site, macro_decl_name, def_site_span)) })
}
// TODO map
None => None,
}
}); });
let (macro_decl_name, def_site_span) = match span_name_span { let (macro_decl_name, def_site_span) = match span_name_span {
None => break, None => break,
@ -573,6 +556,13 @@ enum Destination {
} }
impl Destination { impl Destination {
fn from_stderr() -> Destination {
match term::stderr() {
Some(t) => Terminal(t),
None => Raw(Box::new(io::stderr())),
}
}
fn print_maybe_styled(&mut self, fn print_maybe_styled(&mut self,
args: fmt::Arguments, args: fmt::Arguments,
color: term::Attr, color: term::Attr,