mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 05:43:53 +01:00
improve function type display
This commit is contained in:
parent
3b179ba2d4
commit
36e0f69d0c
@ -133,6 +133,11 @@ impl Display for Function {
|
||||
}
|
||||
impl Display for FunctionT {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Function")
|
||||
match (self.0)(&Type::empty_tuple()) {
|
||||
Ok(t) => write!(f, "Function /* () -> {t} */"),
|
||||
Err(_) => {
|
||||
write!(f, "Function",)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ pub struct CheckErrorHRConfig {
|
||||
}
|
||||
pub struct CheckErrorDisplay<'a> {
|
||||
e: &'a CheckError,
|
||||
src: &'a Source,
|
||||
src: Option<&'a Source>,
|
||||
}
|
||||
impl Display for CheckErrorDisplay<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@ -123,13 +123,19 @@ impl CheckError {
|
||||
self.add(CheckErrorComponent::Source(s))
|
||||
}
|
||||
pub fn display<'a>(&'a self, src: &'a Source) -> CheckErrorDisplay<'a> {
|
||||
CheckErrorDisplay { e: self, src }
|
||||
CheckErrorDisplay {
|
||||
e: self,
|
||||
src: Some(src),
|
||||
}
|
||||
}
|
||||
pub fn display_no_src<'a>(&'a self) -> CheckErrorDisplay<'a> {
|
||||
CheckErrorDisplay { e: self, src: None }
|
||||
}
|
||||
// will, unless empty, end in a newline
|
||||
fn human_readable(
|
||||
&self,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
src: &Source,
|
||||
src: Option<&Source>,
|
||||
cfg: &CheckErrorHRConfig,
|
||||
) -> std::fmt::Result {
|
||||
let len = self.0.len();
|
||||
@ -155,50 +161,55 @@ impl CheckError {
|
||||
err.human_readable(f, src, &cfg)?;
|
||||
}
|
||||
CheckErrorComponent::Source(highlights) => {
|
||||
let start = highlights.iter().map(|v| v.0.start.pos()).min();
|
||||
let end = highlights.iter().map(|v| v.0.start.pos()).max();
|
||||
if let (Some(start), Some(end)) = (start, end) {
|
||||
writeln!(f, "{}Line(s) [?] ({start}..{end})", indent!())?;
|
||||
let start = src.get_line_start(start);
|
||||
let end = src.get_line_end(end);
|
||||
let lines = src.src()[start..end].line_spans().collect::<Vec<_>>();
|
||||
for line in lines {
|
||||
let line_start = line.start();
|
||||
let line_end = line.end();
|
||||
let line = line.as_str();
|
||||
writeln!(f, "{} {line}", indent!())?;
|
||||
let mut right = 0;
|
||||
for (pos, color) in highlights {
|
||||
if let Some(color) = color {
|
||||
let highlight_start = pos.start.pos() - start;
|
||||
let highlight_end = pos.end.pos() - start;
|
||||
if highlight_start < line_end && highlight_end > line_start {
|
||||
let hl_start = highlight_start.saturating_sub(line_start);
|
||||
if hl_start < right {
|
||||
right = 0;
|
||||
writeln!(f)?;
|
||||
if let Some(src) = src {
|
||||
let start = highlights.iter().map(|v| v.0.start.pos()).min();
|
||||
let end = highlights.iter().map(|v| v.0.start.pos()).max();
|
||||
if let (Some(start), Some(end)) = (start, end) {
|
||||
writeln!(f, "{}Line(s) [?] ({start}..{end})", indent!())?;
|
||||
let start = src.get_line_start(start);
|
||||
let end = src.get_line_end(end);
|
||||
let lines = src.src()[start..end].line_spans().collect::<Vec<_>>();
|
||||
for line in lines {
|
||||
let line_start = line.start();
|
||||
let line_end = line.end();
|
||||
let line = line.as_str();
|
||||
writeln!(f, "{} {line}", indent!())?;
|
||||
let mut right = 0;
|
||||
for (pos, color) in highlights {
|
||||
if let Some(color) = color {
|
||||
let highlight_start = pos.start.pos() - start;
|
||||
let highlight_end = pos.end.pos() - start;
|
||||
if highlight_start < line_end && highlight_end > line_start
|
||||
{
|
||||
let hl_start =
|
||||
highlight_start.saturating_sub(line_start);
|
||||
if hl_start < right {
|
||||
right = 0;
|
||||
writeln!(f)?;
|
||||
}
|
||||
let hl_len = highlight_end
|
||||
.saturating_sub(line_start)
|
||||
.saturating_sub(hl_start);
|
||||
let hl_space = hl_start - right;
|
||||
let print_indent = right == 0;
|
||||
right += hl_space + hl_len;
|
||||
let hl_len =
|
||||
hl_len.min(highlight_end - highlight_start);
|
||||
if print_indent && right != 0 {
|
||||
write!(f, "{} ", indent!())?;
|
||||
}
|
||||
write!(
|
||||
f,
|
||||
"{}{}",
|
||||
" ".repeat(hl_space),
|
||||
"^".repeat(hl_len).color(*color)
|
||||
)?;
|
||||
}
|
||||
let hl_len = highlight_end
|
||||
.saturating_sub(line_start)
|
||||
.saturating_sub(hl_start);
|
||||
let hl_space = hl_start - right;
|
||||
let print_indent = right == 0;
|
||||
right += hl_space + hl_len;
|
||||
let hl_len = hl_len.min(highlight_end - highlight_start);
|
||||
if print_indent && right != 0 {
|
||||
write!(f, "{} ", indent!())?;
|
||||
}
|
||||
write!(
|
||||
f,
|
||||
"{}{}",
|
||||
" ".repeat(hl_space),
|
||||
"^".repeat(hl_len).color(*color)
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
if right != 0 {
|
||||
writeln!(f)?;
|
||||
if right != 0 {
|
||||
writeln!(f)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user