提交 1599d226 编写于 作者: F Felix S. Klock II

Added session.fileline_note() method and support infrastucture for it.

Add way to print notes with just file:linenum prefix (preserving
integration with source lookup for e.g. vi and emacs) but don't repeat
the other span info.
上级 3ccbffac
...@@ -232,6 +232,9 @@ pub fn span_note(&self, sp: Span, msg: &str) { ...@@ -232,6 +232,9 @@ pub fn span_note(&self, sp: Span, msg: &str) {
pub fn span_end_note(&self, sp: Span, msg: &str) { pub fn span_end_note(&self, sp: Span, msg: &str) {
self.diagnostic().span_end_note(sp, msg) self.diagnostic().span_end_note(sp, msg)
} }
pub fn fileline_note(&self, sp: Span, msg: &str) {
self.diagnostic().fileline_note(sp, msg)
}
pub fn note(&self, msg: &str) { pub fn note(&self, msg: &str) {
self.diagnostic().handler().note(msg) self.diagnostic().handler().note(msg)
} }
......
...@@ -20,11 +20,37 @@ ...@@ -20,11 +20,37 @@
// maximum number of lines we will print for each error; arbitrary. // maximum number of lines we will print for each error; arbitrary.
static MAX_LINES: uint = 6u; static MAX_LINES: uint = 6u;
#[deriving(Clone)]
pub enum RenderSpan {
/// A FullSpan renders with both with an initial line for the
/// message, prefixed by file:linenum, followed by a summary of
/// the source code covered by the span.
FullSpan(Span),
/// A FileLine renders with just a line for the message prefixed
/// by file:linenum.
FileLine(Span),
}
impl RenderSpan {
fn span(self) -> Span {
match self {
FullSpan(s) | FileLine(s) => s
}
}
fn is_full_span(&self) -> bool {
match self {
&FullSpan(..) => true,
&FileLine(..) => false,
}
}
}
pub trait Emitter { pub trait Emitter {
fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>, fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>,
msg: &str, lvl: Level); msg: &str, lvl: Level);
fn custom_emit(&mut self, cm: &codemap::CodeMap, fn custom_emit(&mut self, cm: &codemap::CodeMap,
sp: Span, msg: &str, lvl: Level); sp: RenderSpan, msg: &str, lvl: Level);
} }
/// This structure is used to signify that a task has failed with a fatal error /// This structure is used to signify that a task has failed with a fatal error
...@@ -60,7 +86,10 @@ pub fn span_note(&self, sp: Span, msg: &str) { ...@@ -60,7 +86,10 @@ pub fn span_note(&self, sp: Span, msg: &str) {
self.handler.emit(Some((&self.cm, sp)), msg, Note); self.handler.emit(Some((&self.cm, sp)), msg, Note);
} }
pub fn span_end_note(&self, sp: Span, msg: &str) { pub fn span_end_note(&self, sp: Span, msg: &str) {
self.handler.custom_emit(&self.cm, sp, msg, Note); self.handler.custom_emit(&self.cm, FullSpan(sp), msg, Note);
}
pub fn fileline_note(&self, sp: Span, msg: &str) {
self.handler.custom_emit(&self.cm, FileLine(sp), msg, Note);
} }
pub fn span_bug(&self, sp: Span, msg: &str) -> ! { pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
self.handler.emit(Some((&self.cm, sp)), msg, Bug); self.handler.emit(Some((&self.cm, sp)), msg, Bug);
...@@ -132,7 +161,7 @@ pub fn emit(&self, ...@@ -132,7 +161,7 @@ pub fn emit(&self,
self.emit.borrow_mut().emit(cmsp, msg, lvl); self.emit.borrow_mut().emit(cmsp, msg, lvl);
} }
pub fn custom_emit(&self, cm: &codemap::CodeMap, pub fn custom_emit(&self, cm: &codemap::CodeMap,
sp: Span, msg: &str, lvl: Level) { sp: RenderSpan, msg: &str, lvl: Level) {
self.emit.borrow_mut().custom_emit(cm, sp, msg, lvl); self.emit.borrow_mut().custom_emit(cm, sp, msg, lvl);
} }
} }
...@@ -258,7 +287,7 @@ fn emit(&mut self, ...@@ -258,7 +287,7 @@ fn emit(&mut self,
msg: &str, msg: &str,
lvl: Level) { lvl: Level) {
let error = match cmsp { let error = match cmsp {
Some((cm, sp)) => emit(self, cm, sp, msg, lvl, false), Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, lvl, false),
None => print_diagnostic(self, "", lvl, msg), None => print_diagnostic(self, "", lvl, msg),
}; };
...@@ -269,7 +298,7 @@ fn emit(&mut self, ...@@ -269,7 +298,7 @@ fn emit(&mut self,
} }
fn custom_emit(&mut self, cm: &codemap::CodeMap, fn custom_emit(&mut self, cm: &codemap::CodeMap,
sp: Span, msg: &str, lvl: Level) { sp: RenderSpan, msg: &str, lvl: Level) {
match emit(self, cm, sp, msg, lvl, true) { match emit(self, cm, sp, msg, lvl, true) {
Ok(()) => {} Ok(()) => {}
Err(e) => fail!("failed to print diagnostics: {}", e), Err(e) => fail!("failed to print diagnostics: {}", e),
...@@ -277,8 +306,9 @@ fn custom_emit(&mut self, cm: &codemap::CodeMap, ...@@ -277,8 +306,9 @@ fn custom_emit(&mut self, cm: &codemap::CodeMap,
} }
} }
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, sp: Span, fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> { msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> {
let sp = rsp.span();
let ss = cm.span_to_str(sp); let ss = cm.span_to_str(sp);
let lines = cm.span_to_lines(sp); let lines = cm.span_to_lines(sp);
if custom { if custom {
...@@ -288,10 +318,14 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, sp: Span, ...@@ -288,10 +318,14 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, sp: Span,
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info}; let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
let ses = cm.span_to_str(span_end); let ses = cm.span_to_str(span_end);
try!(print_diagnostic(dst, ses, lvl, msg)); try!(print_diagnostic(dst, ses, lvl, msg));
try!(custom_highlight_lines(dst, cm, sp, lvl, lines)); if rsp.is_full_span() {
try!(custom_highlight_lines(dst, cm, sp, lvl, lines));
}
} else { } else {
try!(print_diagnostic(dst, ss, lvl, msg)); try!(print_diagnostic(dst, ss, lvl, msg));
try!(highlight_lines(dst, cm, sp, lvl, lines)); if rsp.is_full_span() {
try!(highlight_lines(dst, cm, sp, lvl, lines));
}
} }
print_macro_backtrace(dst, cm, sp) print_macro_backtrace(dst, cm, sp)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册