Merge pull request #2912 from rtfeldman/issue-2875/feedback

Exclude filename from repl reports.
This commit is contained in:
Richard Feldman 2022-04-22 14:00:38 -04:00 committed by GitHub
commit 951ecdcc53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 17 deletions

View file

@ -47,7 +47,7 @@ pub fn compile_to_mono<'a>(
target_info: TargetInfo,
palette: Palette,
) -> Result<MonomorphizedModule<'a>, Vec<String>> {
let filename = PathBuf::from("REPL.roc");
let filename = PathBuf::from("");
let src_dir = Path::new("fake/test/path");
let module_src = arena.alloc(promote_expr_to_module(src));

View file

@ -575,7 +575,7 @@ fn too_few_args() {
"Num.add 2",
indoc!(
r#"
TOO FEW ARGS REPL.roc
TOO FEW ARGS
The add function expects 2 arguments, but it got only 1:
@ -596,7 +596,7 @@ fn type_problem() {
"1 + \"\"",
indoc!(
r#"
TYPE MISMATCH REPL.roc
TYPE MISMATCH
The 2nd argument to add is not what I expect:
@ -882,7 +882,7 @@ fn parse_problem() {
"add m n = m + n",
indoc!(
r#"
ARGUMENTS BEFORE EQUALS REPL.roc
ARGUMENTS BEFORE EQUALS
I am partway through parsing a definition, but I got stuck here:
@ -912,7 +912,7 @@ fn mono_problem() {
"#,
indoc!(
r#"
UNSAFE PATTERN REPL.roc
UNSAFE PATTERN
This when does not cover all the possibilities:
@ -948,7 +948,7 @@ fn issue_2343_complete_mono_with_shadowed_vars() {
),
indoc!(
r#"
DUPLICATE NAME REPL.roc
DUPLICATE NAME
The b name is first defined here:

View file

@ -3,7 +3,7 @@ use roc_module::ident::{Lowercase, ModuleName, TagName, Uppercase};
use roc_module::symbol::{Interns, ModuleId, Symbol};
use roc_region::all::LineColumnRegion;
use std::fmt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use ven_pretty::{BoxAllocator, DocAllocator, DocBuilder, Render, RenderAnnotated};
pub use crate::error::canonicalize::can_problem;
@ -60,7 +60,15 @@ pub fn cycle<'b>(
.annotate(Annotation::TypeBlock)
}
pub fn pretty_header(title: &str, path: &std::path::Path) -> String {
const HEADER_WIDTH: usize = 80;
pub fn pretty_header(title: &str) -> String {
let title_width = title.len() + 4;
let header = format!("── {} {}", title, "".repeat(HEADER_WIDTH - title_width));
header
}
pub fn pretty_header_with_path(title: &str, path: &Path) -> String {
let cwd = std::env::current_dir().unwrap();
let relative_path = match path.strip_prefix(cwd) {
Ok(p) => p,
@ -69,17 +77,12 @@ pub fn pretty_header(title: &str, path: &std::path::Path) -> String {
.to_str()
.unwrap();
let header_width = 80;
let title_width = title.len() + 4;
let relative_path_width = relative_path.len() + 3;
let available_path_width = header_width - title_width - 1;
let available_path_width = HEADER_WIDTH - title_width - 1;
// If path is too long to fit in 80 characters with everything else then truncate it
let path_width = if relative_path_width <= available_path_width {
relative_path_width
} else {
available_path_width
};
let path_width = relative_path_width.min(available_path_width);
let path_trim = relative_path_width - path_width;
let path = if path_trim > 0 {
format!("...{}", &relative_path[(path_trim + 3)..])
@ -90,7 +93,7 @@ pub fn pretty_header(title: &str, path: &std::path::Path) -> String {
let header = format!(
"── {} {} {} ─",
title,
"".repeat(header_width - (title_width + path_width)),
"".repeat(HEADER_WIDTH - (title_width + path_width)),
path
);
@ -166,7 +169,11 @@ impl<'b> Report<'b> {
if self.title.is_empty() {
self.doc
} else {
let header = crate::report::pretty_header(&self.title, &self.filename);
let header = if self.filename == PathBuf::from("") {
crate::report::pretty_header(&self.title)
} else {
crate::report::pretty_header_with_path(&self.title, &self.filename)
};
alloc.stack([alloc.text(header).annotate(Annotation::Header), self.doc])
}