Skip to content

Commit

Permalink
Modify the codemap code to use more slices and to information about
Browse files Browse the repository at this point in the history
columns within a line, not just the line numbers. Also try to clarify
and use the term `line_index` when 0-based.
  • Loading branch information
nikomatsakis committed Apr 9, 2015
1 parent f93279e commit 7314c1a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 36 deletions.
48 changes: 39 additions & 9 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,20 @@ impl ExpnId {

pub type FileName = String;

pub struct LineInfo {
/// Index of line, starting from 0.
pub line_index: usize,

/// Column in line where span begins, starting from 0.
pub start_col: CharPos,

/// Column in line where span ends, starting from 0, exclusive.
pub end_col: CharPos,
}

pub struct FileLines {
pub file: Rc<FileMap>,
pub lines: Vec<usize>
pub lines: Vec<LineInfo>
}

/// Identifies an offset of a multi-byte character in a FileMap
Expand Down Expand Up @@ -467,9 +478,9 @@ impl FileMap {
lines.push(pos);
}

/// get a line from the list of pre-computed line-beginnings
///
pub fn get_line(&self, line_number: usize) -> Option<String> {
/// get a line from the list of pre-computed line-beginnings.
/// line-number here is 0-based.
pub fn get_line(&self, line_number: usize) -> Option<&str> {
match self.src {
Some(ref src) => {
let lines = self.lines.borrow();
Expand All @@ -480,7 +491,7 @@ impl FileMap {
match slice.find('\n') {
Some(e) => &slice[..e],
None => slice
}.to_string()
}
})
}
None => None
Expand Down Expand Up @@ -649,10 +660,29 @@ impl CodeMap {
pub fn span_to_lines(&self, sp: Span) -> FileLines {
let lo = self.lookup_char_pos(sp.lo);
let hi = self.lookup_char_pos(sp.hi);
let mut lines = Vec::new();
for i in lo.line - 1..hi.line {
lines.push(i);
};
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);

// The span starts partway through the first line,
// but after that it starts from offset 0.
let mut start_col = lo.col;

// For every line but the last, it extends from `start_col`
// and to the end of the line. Be careful because the line
// numbers in Loc are 1-based, so we subtract 1 to get 0-based
// lines.
for line_index in lo.line-1 .. hi.line-2 {
let line_len = lo.file.get_line(line_index).map(|s| s.len()).unwrap_or(0);
lines.push(LineInfo { line_index: line_index,
start_col: start_col,
end_col: CharPos::from_usize(line_len) });
start_col = CharPos::from_usize(0);
}

// For the last line, it extends from `start_col` to `hi.col`:
lines.push(LineInfo { line_index: hi.line - 1,
start_col: start_col,
end_col: hi.col });

FileLines {file: lo.file, lines: lines}
}

Expand Down
68 changes: 41 additions & 27 deletions src/libsyntax/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,25 +483,39 @@ fn highlight_lines(err: &mut EmitterWriter,
cm: &codemap::CodeMap,
sp: Span,
lvl: Level,
lines: codemap::FileLines) -> io::Result<()> {
lines: codemap::FileLines)
-> io::Result<()>
{
let fm = &*lines.file;

let mut elided = false;
let mut display_lines = &lines.lines[..];
if display_lines.len() > MAX_LINES {
display_lines = &display_lines[0..MAX_LINES];
elided = true;
}
let line_strings: Option<Vec<&str>> =
lines.lines.iter()
.map(|info| fm.get_line(info.line_index))
.collect();

let line_strings = match line_strings {
None => { return Ok(()); }
Some(line_strings) => line_strings
};

// Display only the first MAX_LINES lines.
let all_lines = lines.lines.len();
let display_lines = cmp::min(all_lines, MAX_LINES);
let display_line_infos = &lines.lines[..display_lines];
let display_line_strings = &line_strings[..display_lines];

// Print the offending lines
for &line_number in display_lines {
if let Some(line) = fm.get_line(line_number) {
try!(write!(&mut err.dst, "{}:{} {}\n", fm.name,
line_number + 1, line));
}
for (line_info, line) in display_line_infos.iter().zip(display_line_strings.iter()) {
try!(write!(&mut err.dst, "{}:{} {}\n",
fm.name,
line_info.line_index + 1,
line));
}
if elided {
let last_line = display_lines[display_lines.len() - 1];
let s = format!("{}:{} ", fm.name, last_line + 1);

// If we elided something, put an ellipsis.
if display_lines < all_lines {
let last_line_index = display_line_infos.last().unwrap().line_index;
let s = format!("{}:{} ", fm.name, last_line_index + 1);
try!(write!(&mut err.dst, "{0:1$}...\n", "", s.len()));
}

Expand All @@ -510,7 +524,7 @@ fn highlight_lines(err: &mut EmitterWriter,
if lines.lines.len() == 1 {
let lo = cm.lookup_char_pos(sp.lo);
let mut digits = 0;
let mut num = (lines.lines[0] + 1) / 10;
let mut num = (lines.lines[0].line_index + 1) / 10;

// how many digits must be indent past?
while num > 0 { num /= 10; digits += 1; }
Expand All @@ -522,7 +536,7 @@ fn highlight_lines(err: &mut EmitterWriter,
for _ in 0..skip {
s.push(' ');
}
if let Some(orig) = fm.get_line(lines.lines[0]) {
if let Some(orig) = fm.get_line(lines.lines[0].line_index) {
let mut col = skip;
let mut lastc = ' ';
let mut iter = orig.chars().enumerate();
Expand Down Expand Up @@ -597,32 +611,32 @@ fn end_highlight_lines(w: &mut EmitterWriter,

let lines = &lines.lines[..];
if lines.len() > MAX_LINES {
if let Some(line) = fm.get_line(lines[0]) {
if let Some(line) = fm.get_line(lines[0].line_index) {
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
lines[0] + 1, line));
lines[0].line_index + 1, line));
}
try!(write!(&mut w.dst, "...\n"));
let last_line_number = lines[lines.len() - 1];
if let Some(last_line) = fm.get_line(last_line_number) {
let last_line_index = lines[lines.len() - 1].line_index;
if let Some(last_line) = fm.get_line(last_line_index) {
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
last_line_number + 1, last_line));
last_line_index + 1, last_line));
}
} else {
for &line_number in lines {
if let Some(line) = fm.get_line(line_number) {
for line_info in lines {
if let Some(line) = fm.get_line(line_info.line_index) {
try!(write!(&mut w.dst, "{}:{} {}\n", fm.name,
line_number + 1, line));
line_info.line_index + 1, line));
}
}
}
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1]+1);
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1].line_index + 1);
let hi = cm.lookup_char_pos(sp.hi);
let skip = last_line_start.width(false);
let mut s = String::new();
for _ in 0..skip {
s.push(' ');
}
if let Some(orig) = fm.get_line(lines[0]) {
if let Some(orig) = fm.get_line(lines[0].line_index) {
let iter = orig.chars().enumerate();
for (pos, ch) in iter {
// Span seems to use half-opened interval, so subtract 1
Expand Down

0 comments on commit 7314c1a

Please sign in to comment.