Skip to content

Commit

Permalink
Only print up to six lines on error. Print ^~~~~ to highlight error s…
Browse files Browse the repository at this point in the history
…pan.
  • Loading branch information
jdm authored and brson committed Jul 12, 2011
1 parent b62fcdc commit fd24fd5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/comp/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import std::option;
import std::option::some;
import std::option::none;
import std::str;
import std::vec;

tag os { os_win32; os_macos; os_linux; }

Expand Down
50 changes: 49 additions & 1 deletion src/comp/syntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,65 @@ fn emit_diagnostic(&option::t[span] sp, &str msg, &str kind, u8 color,
io::stdout().write_str(#fmt(" %s\n", msg));
alt (maybe_lines) {
case (some(?lines)) {
// FIXME: reading in the entire file is the worst possible way to
// get access to the necessary lines.
auto rdr = io::file_reader(lines.name);
auto file = str::unsafe_from_bytes(rdr.read_whole_stream());
auto fm = codemap::get_filemap(cm, lines.name);
for (uint line in lines.lines) {

// arbitrarily only print up to six lines of the error
auto max_lines = 6u;
auto elided = false;
auto display_lines = lines.lines;
if (vec::len(display_lines) > max_lines) {
display_lines = vec::slice(display_lines, 0u, max_lines);
elided = true;
}
// Print the offending lines
for (uint line in display_lines) {
io::stdout().write_str(#fmt("%s:%u ", fm.name, line + 1u));
auto s = codemap::get_line(fm, line as int, file);
if (!str::ends_with(s, "\n")) {
s += "\n";
}
io::stdout().write_str(s);
}
if (elided) {
auto last_line = display_lines.(vec::len(display_lines) - 1u);
auto s = #fmt("%s:%u ", fm.name, last_line + 1u);
auto indent = str::char_len(s);
auto out = "";
while (indent > 0u) { out += " "; indent -= 1u; }
out += "...\n";
io::stdout().write_str(out);
}

// If there's one line at fault we can easily point to the problem
if (vec::len(lines.lines) == 1u) {
auto lo = codemap::lookup_pos(cm, option::get(sp).lo);
auto digits = 0u;
auto num = lines.lines.(0) / 10u;

// how many digits must be indent past?
while (num > 0u) { num /= 10u; digits += 1u; }

// indent past |name:## | and the 0-offset column location
auto left = str::char_len(fm.name) + digits + lo.col + 3u;
auto s = "";
while (left > 0u) { str::push_char(s, ' '); left -= 1u; }

s += "^";
auto hi = codemap::lookup_pos(cm, option::get(sp).hi);
if (hi.col != lo.col) {
// the ^ already takes up one space
auto width = hi.col - lo.col - 1u;
while (width > 0u) {
str::push_char(s, '~');
width -= 1u;
}
}
io::stdout().write_str(s + "\n");
}
}
case (_) {}
}
Expand Down

0 comments on commit fd24fd5

Please sign in to comment.