@@ -26,7 +26,7 @@ fn new_codemap() -> codemap {
26
26
}
27
27
28
28
fn new_filemap( filename filename, uint start_pos) -> filemap {
29
- ret @rec( name=filename, start_pos=start_pos, mutable lines=~ [ 0 u ] ) ;
29
+ ret @rec( name=filename, start_pos=start_pos, mutable lines=[ start_pos ] ) ;
30
30
}
31
31
32
32
fn next_line( filemap file, uint pos) { file. lines += ~[ pos ] ; }
@@ -170,10 +170,18 @@ fn span_to_lines(span sp, codemap::codemap cm) -> @file_lines {
170
170
fn get_line ( filemap fm, int line , & str file ) -> str {
171
171
let uint begin = fm. lines . ( line) - fm. start_pos ;
172
172
let uint end;
173
- if ( ( line as uint ) + 1 u >= ivec:: len ( fm. lines ) ) {
174
- end = str:: byte_len ( file) ;
175
- } else {
173
+ if ( line as uint < ivec:: len ( fm. lines ) - 1 u) {
176
174
end = fm. lines . ( line + 1 ) - fm. start_pos ;
175
+ } else {
176
+ // If we're not done parsing the file, we're at the limit of what's
177
+ // parsed. If we just slice the rest of the string, we'll print out
178
+ // the remainder of the file, which is undesirable.
179
+ end = str:: byte_len ( file) ;
180
+ auto rest = str:: slice ( file, begin, end) ;
181
+ auto newline = str:: index ( rest, '\n' as u8 ) ;
182
+ if ( newline != -1 ) {
183
+ end = begin + ( newline as uint ) ;
184
+ }
177
185
}
178
186
ret str:: slice ( file, begin, end) ;
179
187
}
0 commit comments