Make 1-indexed display of LineNumber values explicit

This fixes .. (or 'missing') line numbers on boundaries, where we'd
show too few dots after 10 or 100.
pull/70/head
Wilfred Hughes 2021-11-18 23:21:30 +07:00
parent b65d9d8c90
commit 7ee37868a1
2 changed files with 13 additions and 3 deletions

@ -10,6 +10,12 @@ use std::{cmp::max, fmt};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LineNumber(pub usize);
impl LineNumber {
pub fn one_indexed(&self) -> usize {
self.0 + 1
}
}
impl fmt::Debug for LineNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("LineNumber: {}", self.0))
@ -23,7 +29,7 @@ impl From<usize> for LineNumber {
}
pub fn format_line_num(line_num: LineNumber) -> String {
format!("{} ", line_num.0 + 1)
format!("{} ", line_num.one_indexed())
}
/// A position in a single line of a string.

@ -37,11 +37,15 @@ fn split_lines_nonempty(s: &str) -> Vec<String> {
}
fn format_line_num_padded(line_num: LineNumber, column_width: usize) -> String {
format!("{:width$} ", line_num.0 + 1, width = column_width - 1)
format!(
"{:width$} ",
line_num.one_indexed(),
width = column_width - 1
)
}
fn format_missing_line_num(prev_num: LineNumber, column_width: usize) -> String {
let num_digits = format!("{}", prev_num.0).len();
let num_digits = format!("{}", prev_num.one_indexed()).len();
format!(
"{:>width$} ",
".".repeat(num_digits),