diff --git a/petr-fmt/src/config.rs b/petr-fmt/src/config.rs index ddfc4a0..0e24a14 100644 --- a/petr-fmt/src/config.rs +++ b/petr-fmt/src/config.rs @@ -14,6 +14,7 @@ pub struct FormatterConfig { put_trailing_semis_on_let_bindings: bool, backup: bool, use_symbol_for_fn_return_type: bool, + use_symbol_for_unit_type: bool, } impl FormatterConfig { @@ -85,12 +86,17 @@ impl FormatterConfig { put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings, backup: self.backup, use_symbol_for_fn_return_type: self.use_symbol_for_fn_return_type, + use_symbol_for_unit_type: self.use_symbol_for_unit_type, } } pub(crate) fn use_symbol_for_fn_return_type(&self) -> bool { self.use_symbol_for_fn_return_type } + + pub(crate) fn use_symbol_for_unit_type(&self) -> bool { + self.use_symbol_for_unit_type + } } impl Default for FormatterConfig { @@ -114,6 +120,7 @@ pub struct FormatterConfigBuilder { put_trailing_semis_on_let_bindings: bool, backup: bool, use_symbol_for_fn_return_type: bool, + use_symbol_for_unit_type: bool, } impl FormatterConfigBuilder { @@ -251,6 +258,7 @@ impl FormatterConfigBuilder { put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings, backup: self.backup, use_symbol_for_fn_return_type: self.use_symbol_for_fn_return_type, + use_symbol_for_unit_type: self.use_symbol_for_unit_type, } } @@ -263,6 +271,16 @@ impl FormatterConfigBuilder { ..self } } + + pub fn use_symbol_for_unit_type( + self, + use_symbol_for_unit_type: bool, + ) -> Self { + Self { + use_symbol_for_unit_type, + ..self + } + } } impl Default for FormatterConfigBuilder { @@ -282,6 +300,7 @@ impl Default for FormatterConfigBuilder { put_trailing_semis_on_let_bindings: false, backup: false, use_symbol_for_fn_return_type: true, + use_symbol_for_unit_type: true, } } } diff --git a/petr-fmt/src/lib.rs b/petr-fmt/src/lib.rs index 65f7c6c..6b70bc7 100644 --- a/petr-fmt/src/lib.rs +++ b/petr-fmt/src/lib.rs @@ -141,7 +141,7 @@ impl Formattable for FunctionDeclaration { buf.push_str("returns "); } - buf.push_str(&self.return_type.pretty_print(&ctx.interner, ctx.indentation())); + buf.push_str(&self.return_type.format(ctx).into_single_line().content); lines.push(ctx.new_line(buf)); @@ -170,8 +170,7 @@ impl Formattable for FunctionParameter { let ty_in = if ctx.config.use_set_notation_for_types() { "∈" } else { "in" }; buf.push_str(&format!(" {ty_in} ")); - let ty = self.ty.format(ctx).into_single_line().content; - buf.push_str(&ty); + buf.push_str(&self.ty.format(ctx).into_single_line().content); FormattedLines::new(vec![ctx.new_line(buf)]) } @@ -513,7 +512,7 @@ impl Formattable for TypeField { let name = ctx.interner.get(self.name.id); let mut buf = name.to_string(); buf.push(' '); - buf.push_str(&self.ty.pretty_print(&ctx.interner, ctx.indentation())); + buf.push_str(&self.ty.format(ctx).into_single_line().content); FormattedLines::new(vec![ctx.new_line(buf)]) } } @@ -527,7 +526,13 @@ impl Formattable for Ty { Ty::Bool => "'bool".to_string(), Ty::Int => "'int".to_string(), Ty::String => "'string".to_string(), - Ty::Unit => "'unit".to_string(), + Ty::Unit => { + if ctx.config.use_symbol_for_unit_type() { + "ε".to_string() + } else { + "'unit".to_string() + } + }, Ty::Named(name) => format!("'{}", ctx.interner.get(name.id)), Ty::Literal(lit) => lit.to_string(), Ty::Sum(tys) => format!( diff --git a/petr-fmt/src/tests.rs b/petr-fmt/src/tests.rs index aa591a0..bcd922f 100644 --- a/petr-fmt/src/tests.rs +++ b/petr-fmt/src/tests.rs @@ -563,7 +563,7 @@ fn intrinsic() { fn string_literals() → 'string "This is a string literal." - fn my_func() → 'unit + fn my_func() → ε @puts(~string_literal) "#]], ); @@ -576,7 +576,7 @@ fn intrinsic_2() { r#" fn my_func() returns 'unit @puts("hello, world!")"#, expect![[r#" - fn my_func() → 'unit + fn my_func() → ε @puts("hello, world!") "#]], ); @@ -647,3 +647,26 @@ fn sum_ty_formatting() { "#]], ) } + +#[test] +fn unit_ty_formatting() { + check( + Default::default(), + r#" + type bar = a f1 'unit f2 'unit + + fn my_func(x in 'unit, y in 'unit, z in 'sum 'unit | 'unit) returns 'unit foo + "#, + expect![[r#" + type bar = a f1 ε f2 ε + + + fn my_func( + x ∈ ε, + y ∈ ε, + z ∈ 'Σ ε | ε, + ) → ε + foo + "#]], + ) +}