@@ -20,11 +20,37 @@ use term;
20
20
// maximum number of lines we will print for each error; arbitrary.
21
21
static MAX_LINES : uint = 6 u;
22
22
23
+ #[ deriving( Clone ) ]
24
+ pub enum RenderSpan {
25
+ /// A FullSpan renders with both with an initial line for the
26
+ /// message, prefixed by file:linenum, followed by a summary of
27
+ /// the source code covered by the span.
28
+ FullSpan ( Span ) ,
29
+
30
+ /// A FileLine renders with just a line for the message prefixed
31
+ /// by file:linenum.
32
+ FileLine ( Span ) ,
33
+ }
34
+
35
+ impl RenderSpan {
36
+ fn span ( self ) -> Span {
37
+ match self {
38
+ FullSpan ( s) | FileLine ( s) => s
39
+ }
40
+ }
41
+ fn is_full_span ( & self ) -> bool {
42
+ match self {
43
+ & FullSpan ( ..) => true ,
44
+ & FileLine ( ..) => false ,
45
+ }
46
+ }
47
+ }
48
+
23
49
pub trait Emitter {
24
50
fn emit ( & mut self , cmsp : Option < ( & codemap:: CodeMap , Span ) > ,
25
51
msg : & str , lvl : Level ) ;
26
52
fn custom_emit ( & mut self , cm : & codemap:: CodeMap ,
27
- sp : Span , msg : & str , lvl : Level ) ;
53
+ sp : RenderSpan , msg : & str , lvl : Level ) ;
28
54
}
29
55
30
56
/// This structure is used to signify that a task has failed with a fatal error
@@ -60,7 +86,10 @@ impl SpanHandler {
60
86
self . handler . emit ( Some ( ( & self . cm , sp) ) , msg, Note ) ;
61
87
}
62
88
pub fn span_end_note ( & self , sp : Span , msg : & str ) {
63
- self . handler . custom_emit ( & self . cm , sp, msg, Note ) ;
89
+ self . handler . custom_emit ( & self . cm , FullSpan ( sp) , msg, Note ) ;
90
+ }
91
+ pub fn fileline_note ( & self , sp : Span , msg : & str ) {
92
+ self . handler . custom_emit ( & self . cm , FileLine ( sp) , msg, Note ) ;
64
93
}
65
94
pub fn span_bug ( & self , sp : Span , msg : & str ) -> ! {
66
95
self . handler . emit ( Some ( ( & self . cm , sp) ) , msg, Bug ) ;
@@ -132,7 +161,7 @@ impl Handler {
132
161
self.emit.borrow_mut().emit(cmsp, msg, lvl);
133
162
}
134
163
pub fn custom_emit(&self, cm: &codemap::CodeMap,
135
- sp: Span , msg: &str, lvl: Level) {
164
+ sp: RenderSpan , msg: &str, lvl: Level) {
136
165
self.emit.borrow_mut().custom_emit(cm, sp, msg, lvl);
137
166
}
138
167
}
@@ -258,7 +287,7 @@ impl Emitter for EmitterWriter {
258
287
msg : & str ,
259
288
lvl : Level ) {
260
289
let error = match cmsp {
261
- Some ( ( cm, sp) ) => emit ( self , cm, sp , msg, lvl, false ) ,
290
+ Some ( ( cm, sp) ) => emit ( self , cm, FullSpan ( sp ) , msg, lvl, false ) ,
262
291
None => print_diagnostic ( self , "" , lvl, msg) ,
263
292
} ;
264
293
@@ -269,16 +298,17 @@ impl Emitter for EmitterWriter {
269
298
}
270
299
271
300
fn custom_emit ( & mut self , cm : & codemap:: CodeMap ,
272
- sp : Span , msg : & str , lvl : Level ) {
301
+ sp : RenderSpan , msg : & str , lvl : Level ) {
273
302
match emit ( self , cm, sp, msg, lvl, true ) {
274
303
Ok ( ( ) ) => { }
275
304
Err ( e) => fail ! ( "failed to print diagnostics: {}" , e) ,
276
305
}
277
306
}
278
307
}
279
308
280
- fn emit ( dst : & mut EmitterWriter , cm : & codemap:: CodeMap , sp : Span ,
309
+ fn emit ( dst : & mut EmitterWriter , cm : & codemap:: CodeMap , rsp : RenderSpan ,
281
310
msg : & str , lvl : Level , custom : bool ) -> io:: IoResult < ( ) > {
311
+ let sp = rsp. span ( ) ;
282
312
let ss = cm. span_to_str ( sp) ;
283
313
let lines = cm. span_to_lines ( sp) ;
284
314
if custom {
@@ -288,10 +318,14 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, sp: Span,
288
318
let span_end = Span { lo : sp. hi , hi : sp. hi , expn_info : sp. expn_info } ;
289
319
let ses = cm. span_to_str ( span_end) ;
290
320
try!( print_diagnostic ( dst, ses, lvl, msg) ) ;
291
- try!( custom_highlight_lines ( dst, cm, sp, lvl, lines) ) ;
321
+ if rsp. is_full_span ( ) {
322
+ try!( custom_highlight_lines ( dst, cm, sp, lvl, lines) ) ;
323
+ }
292
324
} else {
293
325
try!( print_diagnostic ( dst, ss, lvl, msg) ) ;
294
- try!( highlight_lines ( dst, cm, sp, lvl, lines) ) ;
326
+ if rsp. is_full_span ( ) {
327
+ try!( highlight_lines ( dst, cm, sp, lvl, lines) ) ;
328
+ }
295
329
}
296
330
print_macro_backtrace ( dst, cm, sp)
297
331
}
0 commit comments