@@ -132,7 +132,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
132
132
Diagnostic :: spanned ( resource_span, Level :: Error , "could not parse Fluent resource" )
133
133
. help ( "see additional errors emitted" )
134
134
. emit ( ) ;
135
- for ParserError { pos, slice : _ , kind } in errs {
135
+ for ParserError { pos, slice, kind } in errs {
136
136
let mut err = kind. to_string ( ) ;
137
137
// Entirely unnecessary string modification so that the error message starts
138
138
// with a lowercase as rustc errors do.
@@ -142,17 +142,16 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
142
142
. chain (
143
143
this. source ( )
144
144
. char_indices ( )
145
- . filter_map ( |( i, c) | Some ( i + 1 ) . filter ( |_| c == '\n' ) ) ,
145
+ . filter_map ( |( i, c) | ( c == '\n' ) . then_some ( i + 1 ) ) ,
146
146
)
147
147
. collect ( ) ;
148
- let line_start = line_starts
149
- . iter ( )
150
- . enumerate ( )
151
- . map ( |( line, idx) | ( line + 1 , idx) )
152
- . filter ( |( _, idx) | * * idx <= pos. start )
153
- . last ( )
154
- . unwrap ( )
155
- . 0 ;
148
+ let get_line = |pos| match line_starts. binary_search ( & pos) {
149
+ Ok ( n) => n,
150
+ Err ( n) => n. saturating_sub ( 1 ) ,
151
+ } ;
152
+
153
+ let start_line = get_line ( slice. as_ref ( ) . unwrap_or ( & pos) . start ) ;
154
+ let start_line_pos = line_starts[ start_line] ;
156
155
157
156
let snippet = Snippet {
158
157
title : Some ( Annotation {
@@ -162,14 +161,15 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
162
161
} ) ,
163
162
footer : vec ! [ ] ,
164
163
slices : vec ! [ Slice {
165
- source: this. source( ) ,
166
- line_start,
164
+ source: & this. source( ) [ start_line_pos..] ,
165
+ // lines are one-indexed
166
+ line_start: start_line + 1 ,
167
167
origin: Some ( & relative_ftl_path) ,
168
168
fold: true ,
169
169
annotations: vec![ SourceAnnotation {
170
170
label: "" ,
171
171
annotation_type: AnnotationType :: Error ,
172
- range: ( pos. start, pos. end - 1 ) ,
172
+ range: ( pos. start - start_line_pos , pos. end - start_line_pos ) ,
173
173
} ] ,
174
174
} ] ,
175
175
} ;
0 commit comments