git.delta.rocks / jrsonnet / refs/commits / bc97bc99f5fd

difftreelog

source

crates/jrsonnet-evaluator/src/trace/location.rs2.0 KiBsourcehistory
1#[derive(Clone, PartialEq, Debug)]2pub struct CodeLocation {3	pub offset: usize,45	pub line: usize,6	pub column: usize,78	pub line_start_offset: usize,9	pub line_end_offset: usize,10}1112pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {13	if offsets.is_empty() {14		return vec![];15	}16	let mut line = 1;17	let mut column = 1;18	let max_offset = *offsets.iter().max().unwrap();1920	let mut offset_map = offsets21		.iter()22		.enumerate()23		.map(|(pos, offset)| (*offset, pos))24		.collect::<Vec<_>>();25	offset_map.sort_by_key(|v| v.0);26	offset_map.reverse();2728	let mut out = vec![29		CodeLocation {30			offset: 0,31			column: 0,32			line: 0,33			line_start_offset: 0,34			line_end_offset: 035		};36		offsets.len()37	];38	let mut with_no_known_line_ending = vec![];39	let mut this_line_offset = 0;40	for (pos, ch) in file.chars().enumerate() {41		column += 1;42		match offset_map.last() {43			Some(x) if x.0 == pos => {44				let out_idx = x.1;45				with_no_known_line_ending.push(out_idx);46				out[out_idx].offset = pos;47				out[out_idx].line = line;48				out[out_idx].column = column;49				out[out_idx].line_start_offset = this_line_offset;50				offset_map.pop();51			}52			_ => {}53		}54		if ch == '\n' {55			line += 1;56			column = 1;5758			for idx in with_no_known_line_ending.drain(..) {59				out[idx].line_end_offset = pos;60			}61			this_line_offset = pos + 1;6263			if pos == max_offset + 1 {64				break;65			}66		}67	}68	let file_end = file.chars().count();69	for idx in with_no_known_line_ending {70		out[idx].line_end_offset = file_end;71	}7273	out74}7576#[cfg(test)]77pub mod tests {78	use super::{offset_to_location, CodeLocation};7980	#[test]81	fn test() {82		assert_eq!(83			offset_to_location(84				"hello world\n_______________________________________________________",85				&[0, 14]86			),87			vec![88				CodeLocation {89					offset: 0,90					line: 1,91					column: 2,92					line_start_offset: 0,93					line_end_offset: 11,94				},95				CodeLocation {96					offset: 14,97					line: 2,98					column: 4,99					line_start_offset: 12,100					line_end_offset: 67101				}102			]103		)104	}105}