code_offset: usize,
margin: Margin,
) {
+ // Tabs are assumed to have been replaced by spaces in calling code.
+ debug_assert!(!source_string.contains('\t'));
let line_len = source_string.len();
// Create the source line we will highlight.
let left = margin.left(line_len);
}
let source_string = match file.get_line(line.line_index - 1) {
- Some(s) => s,
+ Some(s) => replace_tabs(&*s),
None => return Vec::new(),
};
let file = annotated_file.file.clone();
let line = &annotated_file.lines[line_idx];
if let Some(source_string) = file.get_line(line.line_index - 1) {
- let leading_whitespace =
- source_string.chars().take_while(|c| c.is_whitespace()).count();
+ let leading_whitespace = source_string
+ .chars()
+ .take_while(|c| c.is_whitespace())
+ .map(|c| {
+ match c {
+ // Tabs are displayed as 4 spaces
+ '\t' => 4,
+ _ => 1,
+ }
+ })
+ .sum();
if source_string.chars().any(|c| !c.is_whitespace()) {
whitespace_margin = min(whitespace_margin, leading_whitespace);
}
self.draw_line(
&mut buffer,
- &unannotated_line,
+ &replace_tabs(&unannotated_line),
annotated_file.lines[line_idx + 1].line_index - 1,
last_buffer_line_num,
width_offset,
);
// print the suggestion
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
- buffer.append(row_num, line, Style::NoStyle);
+ buffer.append(row_num, &replace_tabs(line), Style::NoStyle);
row_num += 1;
}
}
}
+fn replace_tabs(str: &str) -> String {
+ str.replace('\t', " ")
+}
+
fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
buffer.puts(line, col, "| ", Style::LineNumber);
}