Skip to content
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

Refactor write.rs #119

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f73e10a
Ignore temp files generated by `cargo insta`
ISSOtm Nov 28, 2024
ba7b721
Factor out a `nb_digits` function
ISSOtm May 25, 2024
59790f3
Dedicate a struct name to repeating chars
ISSOtm May 25, 2024
da3e437
Make obtaining its colour a method of `ReportKind`
ISSOtm May 25, 2024
e215cc6
Factor out a `fecth_source` function
ISSOtm May 25, 2024
8ba9c61
Use `bool::then_some` to simplify a few exprs
ISSOtm May 25, 2024
9ea9f77
Remove dead `impl Display for Show`
ISSOtm May 25, 2024
07ceb6b
Mark `Show` and `Rept` as crate-local
ISSOtm May 25, 2024
465e3d6
Simplify computation of line numbers a bit
ISSOtm May 25, 2024
4d33284
Use `fetch_source` in `Report::get_source_groups` as well
ISSOtm May 25, 2024
7c37877
Factor out a `max_line_no` function
ISSOtm May 25, 2024
9b3a043
Force `Show` to only contain an `Option`
ISSOtm May 27, 2024
d199caa
Use more compact print!/write! syntax
ISSOtm May 27, 2024
c98d4f6
Use the `format!` padding feature to pad out line numbers
ISSOtm May 27, 2024
67215dc
Factor out a function to obtain the appropriate `vbar`
ISSOtm May 27, 2024
a2f5b69
Use a slightly more descriptive name for `is_line`
ISSOtm May 27, 2024
638c80c
[BREAKING] Factor out a `Location` struct to group that info
ISSOtm May 28, 2024
992c9f3
Factor out a separate function for `group_connector`
ISSOtm May 28, 2024
a50004c
Factor out a `is_referencing` method
ISSOtm May 29, 2024
12067f0
Condense the arrow-character-selection logic
ISSOtm May 29, 2024
e8d3706
Condense the arrow-char-selection margin logic a bit more
ISSOtm May 29, 2024
caada34
Tweak collection of multi-line labels
ISSOtm May 29, 2024
f4f5fa2
Elide one alloc per report emitted
ISSOtm May 29, 2024
6ca20a6
Factor out a function for writing margin characters
ISSOtm May 29, 2024
db16ba4
Switch snapshot tests to Unicode
ISSOtm May 29, 2024
bf18d02
Add a few more tests
ISSOtm May 29, 2024
1f8e9a3
Build the `line_labels` array in a single statement
ISSOtm May 29, 2024
531a5e0
Factor out a function for checking if a label is the margin one
ISSOtm May 29, 2024
138006f
Avoid trying to render labels when printing help or notes
ISSOtm May 29, 2024
53c0c8a
Split off `write_margin` into two functions
ISSOtm May 30, 2024
a14e462
Remove an unused `enumerate()`
ISSOtm May 30, 2024
059429f
Add multi-source tests
ISSOtm May 30, 2024
493d7b5
Factor out a function to write spacer lines
ISSOtm May 30, 2024
fa4b46a
Print spacers before groups, rather than after
ISSOtm May 30, 2024
2ea74f4
Iterate on groups by ref instead of by value
ISSOtm May 30, 2024
825d66e
Rearrange a bool expr like Clippy suggests
ISSOtm May 30, 2024
05524fd
Add a test for help and note but no labels
ISSOtm May 30, 2024
34fe426
Do not apply the report kind's colour to its colon
ISSOtm May 30, 2024
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
Cargo.lock
*.pending-snap
252 changes: 0 additions & 252 deletions src/.write.rs.pending-snap

This file was deleted.

22 changes: 8 additions & 14 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
use std::fmt::{self, Display};

#[derive(Copy, Clone, Debug)]
pub struct Show<T>(pub T);
pub(crate) struct Show<T>(pub Option<T>);

impl<T: Display> Display for Show<Option<T>> {
impl<T: Display> Display for Show<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.0 {
Some(x) => write!(f, "{}", x),
Some(x) => write!(f, "{x}"),
None => Ok(()),
}
}
}

impl<'a, T, F: Fn(&mut fmt::Formatter, &'a T) -> fmt::Result> Display for Show<(&'a [T], F)> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for x in self.0 .0 {
(self.0 .1)(f, x)?;
}
Ok(())
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct Rept<T>(pub T, pub usize);

impl<T: Display> Display for Show<(T, usize)> {
impl<T: Display> Display for Rept<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for _ in 0..self.0 .1 {
write!(f, "{}", self.0 .0)?;
for _ in 0..self.1 {
write!(f, "{}", self.0)?;
}
Ok(())
}
Expand Down
16 changes: 16 additions & 0 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ impl Characters {
underline: '^',
}
}

pub(crate) fn arrow_bend(&self, is_top: bool) -> char {
if is_top {
self.ltop
} else {
self.lbot
}
}

pub(crate) fn vbar(&self, is_gap: bool) -> char {
if is_gap {
self.vbar_gap
} else {
self.vbar
}
}
}

/// Output stream to check for whether color is enabled.
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,24 @@ pub enum ReportKind<'a> {
Custom(&'a str, Color),
}

impl ReportKind<'_> {
fn color(&self, config: &Config) -> Option<Color> {
match self {
ReportKind::Error => config.error_color(),
ReportKind::Warning => config.warning_color(),
ReportKind::Advice => config.advice_color(),
ReportKind::Custom(_, color) => Some(*color),
}
}
}

impl fmt::Display for ReportKind<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ReportKind::Error => write!(f, "Error"),
ReportKind::Warning => write!(f, "Warning"),
ReportKind::Advice => write!(f, "Advice"),
ReportKind::Custom(s, _) => write!(f, "{}", s),
ReportKind::Custom(s, _) => write!(f, "{s}"),
}
}
}
Expand Down
29 changes: 23 additions & 6 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<I: AsRef<str>> Source<I> {
/// Get the line that the given offset appears on, and the line/column numbers of the offset.
///
/// Note that the line/column numbers are zero-indexed.
pub fn get_offset_line(&self, offset: usize) -> Option<(Line, usize, usize)> {
pub fn get_offset_line(&self, offset: usize) -> Option<Location> {
if offset <= self.len {
let idx = self
.lines
Expand All @@ -222,7 +222,11 @@ impl<I: AsRef<str>> Source<I> {
offset,
line.offset
);
Some((line, idx, offset - line.offset))
Some(Location {
line,
line_idx: idx,
col_idx: offset - line.offset,
})
} else {
None
}
Expand All @@ -231,7 +235,7 @@ impl<I: AsRef<str>> Source<I> {
/// Get the line that the given byte offset appears on, and the line/byte column numbers of the offset.
///
/// Note that the line/column numbers are zero-indexed.
pub fn get_byte_line(&self, byte_offset: usize) -> Option<(Line, usize, usize)> {
pub fn get_byte_line(&self, byte_offset: usize) -> Option<Location> {
if byte_offset <= self.byte_len {
let idx = self
.lines
Expand All @@ -244,7 +248,11 @@ impl<I: AsRef<str>> Source<I> {
byte_offset,
line.byte_offset
);
Some((line, idx, byte_offset - line.byte_offset))
Some(Location {
line,
line_idx: idx,
col_idx: byte_offset - line.byte_offset,
})
} else {
None
}
Expand All @@ -255,10 +263,12 @@ impl<I: AsRef<str>> Source<I> {
/// The resulting range is guaranteed to contain valid line indices (i.e: those that can be used for
/// [`Source::line`]).
pub fn get_line_range<S: Span>(&self, span: &S) -> Range<usize> {
let start = self.get_offset_line(span.start()).map_or(0, |(_, l, _)| l);
let start = self
.get_offset_line(span.start())
.map_or(0, |location| location.line_idx);
let end = self
.get_offset_line(span.end().saturating_sub(1).max(span.start()))
.map_or(self.lines.len(), |(_, l, _)| l + 1);
.map_or(self.lines.len(), |location| location.line_idx + 1);
start..end
}

Expand All @@ -268,6 +278,13 @@ impl<I: AsRef<str>> Source<I> {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Location {
pub line: Line,
pub line_idx: usize,
pub col_idx: usize,
}

impl<I: AsRef<str>> Cache<()> for Source<I> {
type Storage = I;

Expand Down
Loading
Loading