Consolidate variable pretty printing

This commit is contained in:
Ayaz Hafiz 2022-05-10 11:24:41 -04:00
parent 3de35f7aa2
commit a9507cf917
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
7 changed files with 28 additions and 43 deletions

View file

@ -1937,7 +1937,7 @@ pub mod test_constrain {
use roc_parse::parser::{SourceError, SyntaxError}; use roc_parse::parser::{SourceError, SyntaxError};
use roc_region::all::Region; use roc_region::all::Region;
use roc_types::{ use roc_types::{
pretty_print::{content_to_string, name_all_type_vars}, pretty_print::name_and_print_var,
solved_types::Solved, solved_types::Solved,
subs::{Subs, VarStore, Variable}, subs::{Subs, VarStore, Variable},
}; };
@ -2050,11 +2050,6 @@ pub mod test_constrain {
let subs = solved.inner_mut(); let subs = solved.inner_mut();
// name type vars
let named_result = name_all_type_vars(var, subs);
let content = subs.get_content_without_compacting(var);
// Connect the ModuleId to it's IdentIds // Connect the ModuleId to it's IdentIds
dep_idents.insert(mod_id, env.ident_ids); dep_idents.insert(mod_id, env.ident_ids);
@ -2063,7 +2058,7 @@ pub mod test_constrain {
all_ident_ids: dep_idents, all_ident_ids: dep_idents,
}; };
let actual_str = content_to_string(content, subs, mod_id, &interns, named_result); let actual_str = name_and_print_var(var, subs, mod_id, &interns);
assert_eq!(actual_str, expected_str); assert_eq!(actual_str, expected_str);
} }

View file

@ -29,7 +29,7 @@ mod test_load {
use roc_reporting::report::RenderTarget; use roc_reporting::report::RenderTarget;
use roc_reporting::report::RocDocAllocator; use roc_reporting::report::RocDocAllocator;
use roc_target::TargetInfo; use roc_target::TargetInfo;
use roc_types::pretty_print::{content_to_string, name_all_type_vars}; use roc_types::pretty_print::name_and_print_var;
use roc_types::subs::Subs; use roc_types::subs::Subs;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -237,10 +237,7 @@ mod test_load {
expected_types: &mut HashMap<&str, &str>, expected_types: &mut HashMap<&str, &str>,
) { ) {
for (symbol, expr_var) in &def.pattern_vars { for (symbol, expr_var) in &def.pattern_vars {
let named_result = name_all_type_vars(*expr_var, subs); let actual_str = name_and_print_var(*expr_var, subs, home, interns);
let content = subs.get_content_without_compacting(*expr_var);
let actual_str = content_to_string(content, subs, home, interns, named_result);
let fully_qualified = symbol.fully_qualified(interns, home).to_string(); let fully_qualified = symbol.fully_qualified(interns, home).to_string();
let expected_type = expected_types let expected_type = expected_types
.remove(fully_qualified.as_str()) .remove(fully_qualified.as_str())

View file

@ -19,7 +19,7 @@ mod solve_expr {
use roc_region::all::{LineColumn, LineColumnRegion, LineInfo, Region}; use roc_region::all::{LineColumn, LineColumnRegion, LineInfo, Region};
use roc_reporting::report::{can_problem, type_problem, RocDocAllocator}; use roc_reporting::report::{can_problem, type_problem, RocDocAllocator};
use roc_solve::solve::TypeError; use roc_solve::solve::TypeError;
use roc_types::pretty_print::{content_to_string, name_all_type_vars}; use roc_types::pretty_print::name_and_print_var;
use std::path::PathBuf; use std::path::PathBuf;
// HELPERS // HELPERS
@ -174,10 +174,7 @@ mod solve_expr {
debug_assert!(exposed_to_host.len() == 1); debug_assert!(exposed_to_host.len() == 1);
let (_symbol, variable) = exposed_to_host.into_iter().next().unwrap(); let (_symbol, variable) = exposed_to_host.into_iter().next().unwrap();
let named_result = name_all_type_vars(variable, subs); let actual_str = name_and_print_var(variable, subs, home, &interns);
let content = subs.get_content_without_compacting(variable);
let actual_str = content_to_string(content, subs, home, &interns, named_result);
Ok((type_problems, can_problems, actual_str)) Ok((type_problems, can_problems, actual_str))
} }
@ -276,9 +273,7 @@ mod solve_expr {
let var = find_type_at(region, &decls) let var = find_type_at(region, &decls)
.expect(&format!("No type for {} ({:?})!", &text, region)); .expect(&format!("No type for {} ({:?})!", &text, region));
let named_result = name_all_type_vars(var, subs); let actual_str = name_and_print_var(var, subs, home, &interns);
let content = subs.get_content_without_compacting(var);
let actual_str = content_to_string(content, subs, home, &interns, named_result);
solved_queries.push(format!("{} : {}", text, actual_str)); solved_queries.push(format!("{} : {}", text, actual_str));
} }

View file

@ -336,7 +336,7 @@ struct Context<'a> {
recursion_structs_to_expand: Vec<Variable>, recursion_structs_to_expand: Vec<Variable>,
} }
pub fn content_to_string( fn content_to_string(
content: &Content, content: &Content,
subs: &Subs, subs: &Subs,
home: ModuleId, home: ModuleId,
@ -364,6 +364,17 @@ pub fn content_to_string(
buf buf
} }
pub fn name_and_print_var(
var: Variable,
subs: &mut Subs,
home: ModuleId,
interns: &Interns,
) -> String {
let named_result = name_all_type_vars(var, subs);
let content = subs.get_content_without_compacting(var);
content_to_string(content, subs, home, interns, named_result)
}
pub fn get_single_arg<'a>(subs: &'a Subs, args: &'a AliasVariables) -> &'a Content { pub fn get_single_arg<'a>(subs: &'a Subs, args: &'a AliasVariables) -> &'a Content {
debug_assert_eq!(args.len(), 1); debug_assert_eq!(args.len(), 1);

View file

@ -59,10 +59,9 @@ use roc_collections::all::MutMap;
use roc_module::ident::Lowercase; use roc_module::ident::Lowercase;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_region::all::Region; use roc_region::all::Region;
use roc_types::pretty_print::name_all_type_vars; use roc_types::pretty_print::name_and_print_var;
use roc_types::solved_types::Solved; use roc_types::solved_types::Solved;
use roc_types::subs::{Subs, Variable}; use roc_types::subs::{Subs, VarStore, Variable};
use roc_types::{pretty_print::content_to_string, subs::VarStore};
use snafu::OptionExt; use snafu::OptionExt;
use threadpool::ThreadPool; use threadpool::ThreadPool;
use winit::event::VirtualKeyCode; use winit::event::VirtualKeyCode;
@ -463,20 +462,10 @@ impl<'a> EdModel<'a> {
let subs = solved.inner_mut(); let subs = solved.inner_mut();
let named_result = name_all_type_vars(var, subs); let pretty_var =
name_and_print_var(var, subs, self.module.env.home, &self.loaded_module.interns);
let content = subs.get_content_without_compacting(var); PoolStr::new(&pretty_var, self.module.env.pool)
PoolStr::new(
&content_to_string(
content,
subs,
self.module.env.home,
&self.loaded_module.interns,
named_result,
),
self.module.env.pool,
)
} }
fn run_solve( fn run_solve(

View file

@ -23,7 +23,7 @@ use roc_repl_eval::{ReplApp, ReplAppMemory};
use roc_reporting::report::DEFAULT_PALETTE; use roc_reporting::report::DEFAULT_PALETTE;
use roc_std::RocStr; use roc_std::RocStr;
use roc_target::TargetInfo; use roc_target::TargetInfo;
use roc_types::pretty_print::{content_to_string, name_all_type_vars}; use roc_types::pretty_print::name_and_print_var;
const BLUE: &str = "\u{001b}[36m"; const BLUE: &str = "\u{001b}[36m";
const PINK: &str = "\u{001b}[35m"; const PINK: &str = "\u{001b}[35m";
@ -227,9 +227,8 @@ fn gen_and_eval_llvm<'a>(
let main_fn_var = *main_fn_var; let main_fn_var = *main_fn_var;
// pretty-print the expr type string for later. // pretty-print the expr type string for later.
let named_result = name_all_type_vars(main_fn_var, &mut subs); let expr_type_str = name_and_print_var(main_fn_var, &mut subs, home, &interns);
let content = subs.get_content_without_compacting(main_fn_var); let content = subs.get_content_without_compacting(main_fn_var);
let expr_type_str = content_to_string(content, &subs, home, &interns, named_result);
let (_, main_fn_layout) = match procedures.keys().find(|(s, _)| *s == main_fn_symbol) { let (_, main_fn_layout) = match procedures.keys().find(|(s, _)| *s == main_fn_symbol) {
Some(layout) => *layout, Some(layout) => *layout,

View file

@ -12,7 +12,7 @@ use roc_repl_eval::{
}; };
use roc_reporting::report::DEFAULT_PALETTE_HTML; use roc_reporting::report::DEFAULT_PALETTE_HTML;
use roc_target::TargetInfo; use roc_target::TargetInfo;
use roc_types::pretty_print::{content_to_string, name_all_type_vars}; use roc_types::pretty_print::name_and_print_var;
use crate::{js_create_app, js_get_result_and_memory, js_run_app}; use crate::{js_create_app, js_get_result_and_memory, js_run_app};
@ -184,9 +184,8 @@ pub async fn entrypoint_from_js(src: String) -> Result<String, String> {
let main_fn_var = *main_fn_var; let main_fn_var = *main_fn_var;
// pretty-print the expr type string for later. // pretty-print the expr type string for later.
let named_result = name_all_type_vars(main_fn_var, &mut subs); let expr_type_str = name_and_print_var(main_fn_var, &mut subs, module_id, &interns);
let content = subs.get_content_without_compacting(main_fn_var); let content = subs.get_content_without_compacting(main_fn_var);
let expr_type_str = content_to_string(content, &subs, module_id, &interns, named_result);
let (_, main_fn_layout) = match procedures.keys().find(|(s, _)| *s == main_fn_symbol) { let (_, main_fn_layout) = match procedures.keys().find(|(s, _)| *s == main_fn_symbol) {
Some(layout) => *layout, Some(layout) => *layout,