Skip to content

Indent code past the widest line number #26540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions src/libsyntax/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,22 @@ fn highlight_lines(err: &mut EmitterWriter,
let display_line_infos = &lines.lines[..display_lines];
let display_line_strings = &line_strings[..display_lines];

// Calculate the widest number to format evenly and fix #11715
assert!(display_line_infos.len() > 0);
let mut max_line_num = display_line_infos[display_line_infos.len() - 1].line_index + 1;
let mut digits = 0;
while max_line_num > 0 {
max_line_num /= 10;
digits += 1;
}

// Print the offending lines
for (line_info, line) in display_line_infos.iter().zip(display_line_strings) {
try!(write!(&mut err.dst, "{}:{} {}\n",
try!(write!(&mut err.dst, "{}:{:>width$} {}\n",
fm.name,
line_info.line_index + 1,
line));
line,
width=digits));
}

// If we elided something, put an ellipsis.
Expand Down Expand Up @@ -795,3 +805,64 @@ pub fn expect<T, M>(diag: &SpanHandler, opt: Option<T>, msg: M) -> T where
None => diag.handler().bug(&msg()),
}
}

#[cfg(test)]
mod test {
use super::{EmitterWriter, highlight_lines, Level};
use codemap::{mk_sp, CodeMap, BytePos};
use std::sync::{Arc, Mutex};
use std::io::{self, Write};
use std::str::from_utf8;

// Diagnostic doesn't align properly in span where line number increases by one digit
#[test]
fn test_hilight_suggestion_issue_11715() {
struct Sink(Arc<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
Write::write(&mut *self.0.lock().unwrap(), data)
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
let data = Arc::new(Mutex::new(Vec::new()));
let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())), None);
let cm = CodeMap::new();
let content = "abcdefg
koksi
line3
line4
cinq
line6
line7
line8
line9
line10
e-lä-vän
tolv
dreizehn
";
let file = cm.new_filemap("dummy.txt".to_string(), content.to_string());
for (i, b) in content.bytes().enumerate() {
if b == b'\n' {
file.next_line(BytePos(i as u32));
}
}
let start = file.lines.borrow()[7];
let end = file.lines.borrow()[11];
let sp = mk_sp(start, end);
let lvl = Level::Error;
println!("span_to_lines");
let lines = cm.span_to_lines(sp);
println!("highlight_lines");
highlight_lines(&mut ew, &cm, sp, lvl, lines).unwrap();
println!("done");
let vec = data.lock().unwrap().clone();
let vec: &[u8] = &vec;
println!("{}", from_utf8(vec).unwrap());
assert_eq!(vec, "dummy.txt: 8 \n\
dummy.txt: 9 \n\
dummy.txt:10 \n\
dummy.txt:11 \n\
dummy.txt:12 \n".as_bytes());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk this test is giving me trouble after making some changes to codemap, the output I get is

dummy.txt: 8         line8
dummy.txt: 9         line9
dummy.txt:10         line10
dummy.txt:11         e-lä-vän
dummy.txt:12         tolv

Which is what I expect from looking at the test. Why does the test not expect the content in vec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because hilight_lines only prints the dummy.txt:line prefix, I couldn't figure out where the actual text gets appended

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured out what is going on here, new_line declares the start of a line, but the code here finds the end of a line. get_line is a weird function and goes not to the next registered line but to the next \n, therefore, each line you get from the codemap is the zero length string from the end of the previous line. This is fixed in my PR.

}