20
20
// FIXME spec the JSON output properly.
21
21
22
22
23
- use codemap:: { Span , MultiSpan , CodeMap } ;
23
+ use codemap:: { self , Span , MultiSpan , CodeMap } ;
24
24
use diagnostics:: registry:: Registry ;
25
25
use errors:: { Level , DiagnosticBuilder , SubDiagnostic , RenderSpan , CodeSuggestion } ;
26
26
use errors:: emitter:: Emitter ;
@@ -197,8 +197,8 @@ impl DiagnosticSpan {
197
197
198
198
fn from_render_span ( rsp : & RenderSpan , je : & JsonEmitter ) -> Vec < DiagnosticSpan > {
199
199
match * rsp {
200
- // FIXME(#30701) handle Suggestion properly
201
200
RenderSpan :: FullSpan ( ref msp) |
201
+ // FIXME(#30701) handle Suggestion properly
202
202
RenderSpan :: Suggestion ( CodeSuggestion { ref msp, .. } ) => {
203
203
DiagnosticSpan :: from_multispan ( msp, je)
204
204
}
@@ -207,13 +207,13 @@ impl DiagnosticSpan {
207
207
let end = je. cm . lookup_char_pos ( span. hi ) ;
208
208
DiagnosticSpan {
209
209
file_name : end. file . name . clone ( ) ,
210
- byte_start : span. lo . 0 ,
210
+ byte_start : span. hi . 0 ,
211
211
byte_end : span. hi . 0 ,
212
- line_start : 0 ,
212
+ line_start : end . line ,
213
213
line_end : end. line ,
214
- column_start : 0 ,
214
+ column_start : end . col . 0 + 1 ,
215
215
column_end : end. col . 0 + 1 ,
216
- text : DiagnosticSpanLine :: from_span ( span, je) ,
216
+ text : DiagnosticSpanLine :: from_span_end ( span, je) ,
217
217
}
218
218
} ) . collect ( )
219
219
}
@@ -237,25 +237,70 @@ impl DiagnosticSpan {
237
237
}
238
238
}
239
239
240
- impl DiagnosticSpanLine {
241
- fn from_span ( span : & Span , je : & JsonEmitter ) -> Vec < DiagnosticSpanLine > {
242
- let lines = match je. cm . span_to_lines ( * span) {
240
+ macro_rules! get_lines_for_span {
241
+ ( $ span: ident , $ je: ident ) = > {
242
+ match $ je. cm. span_to_lines( * $ span) {
243
243
Ok ( lines) => lines,
244
244
Err ( _) => {
245
245
debug!( "unprintable span" ) ;
246
246
return Vec :: new( ) ;
247
247
}
248
- } ;
248
+ }
249
+ }
250
+ }
251
+
252
+ impl DiagnosticSpanLine {
253
+ fn line_from_filemap ( fm : & codemap:: FileMap ,
254
+ index : usize ,
255
+ h_start : usize ,
256
+ h_end : usize )
257
+ -> DiagnosticSpanLine {
258
+ DiagnosticSpanLine {
259
+ text : fm. get_line ( index) . unwrap ( ) . to_owned ( ) ,
260
+ highlight_start : h_start,
261
+ highlight_end : h_end,
262
+ }
263
+ }
264
+
265
+ /// Create a list of DiagnosticSpanLines from span - each line with any part
266
+ /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
267
+ /// `span` within the line.
268
+ fn from_span ( span : & Span , je : & JsonEmitter ) -> Vec < DiagnosticSpanLine > {
269
+ let lines = get_lines_for_span ! ( span, je) ;
249
270
250
271
let mut result = Vec :: new ( ) ;
251
272
let fm = & * lines. file ;
252
273
253
274
for line in & lines. lines {
254
- result. push ( DiagnosticSpanLine {
255
- text : fm. get_line ( line. line_index ) . unwrap ( ) . to_owned ( ) ,
256
- highlight_start : line. start_col . 0 + 1 ,
257
- highlight_end : line. end_col . 0 + 1 ,
258
- } ) ;
275
+ result. push ( DiagnosticSpanLine :: line_from_filemap ( fm,
276
+ line. line_index ,
277
+ line. start_col . 0 + 1 ,
278
+ line. end_col . 0 + 1 ) ) ;
279
+ }
280
+
281
+ result
282
+ }
283
+
284
+ /// Create a list of DiagnosticSpanLines from span - the result covers all
285
+ /// of `span`, but the highlight is zero-length and at the end of `span`.
286
+ fn from_span_end ( span : & Span , je : & JsonEmitter ) -> Vec < DiagnosticSpanLine > {
287
+ let lines = get_lines_for_span ! ( span, je) ;
288
+
289
+ let mut result = Vec :: new ( ) ;
290
+ let fm = & * lines. file ;
291
+
292
+ for ( i, line) in lines. lines . iter ( ) . enumerate ( ) {
293
+ // Invariant - CodeMap::span_to_lines will not return extra context
294
+ // lines - the last line returned is the last line of `span`.
295
+ let highlight = if i == lines. lines . len ( ) - 1 {
296
+ ( line. end_col . 0 + 1 , line. end_col . 0 + 1 )
297
+ } else {
298
+ ( 0 , 0 )
299
+ } ;
300
+ result. push ( DiagnosticSpanLine :: line_from_filemap ( fm,
301
+ line. line_index ,
302
+ highlight. 0 ,
303
+ highlight. 1 ) ) ;
259
304
}
260
305
261
306
result
0 commit comments