Skip to content

Commit f8eee76

Browse files
authored
Merge pull request #159 from ch-systems/mohammadfawaz/epsilon_as_unit
format `'unit` as symbol "epsilon"
2 parents d5338ba + 4edb1a6 commit f8eee76

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

petr-fmt/src/config.rs

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct FormatterConfig {
1414
put_trailing_semis_on_let_bindings: bool,
1515
backup: bool,
1616
use_symbol_for_fn_return_type: bool,
17+
use_symbol_for_unit_type: bool,
1718
}
1819

1920
impl FormatterConfig {
@@ -85,12 +86,17 @@ impl FormatterConfig {
8586
put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings,
8687
backup: self.backup,
8788
use_symbol_for_fn_return_type: self.use_symbol_for_fn_return_type,
89+
use_symbol_for_unit_type: self.use_symbol_for_unit_type,
8890
}
8991
}
9092

9193
pub(crate) fn use_symbol_for_fn_return_type(&self) -> bool {
9294
self.use_symbol_for_fn_return_type
9395
}
96+
97+
pub(crate) fn use_symbol_for_unit_type(&self) -> bool {
98+
self.use_symbol_for_unit_type
99+
}
94100
}
95101

96102
impl Default for FormatterConfig {
@@ -114,6 +120,7 @@ pub struct FormatterConfigBuilder {
114120
put_trailing_semis_on_let_bindings: bool,
115121
backup: bool,
116122
use_symbol_for_fn_return_type: bool,
123+
use_symbol_for_unit_type: bool,
117124
}
118125

119126
impl FormatterConfigBuilder {
@@ -251,6 +258,7 @@ impl FormatterConfigBuilder {
251258
put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings,
252259
backup: self.backup,
253260
use_symbol_for_fn_return_type: self.use_symbol_for_fn_return_type,
261+
use_symbol_for_unit_type: self.use_symbol_for_unit_type,
254262
}
255263
}
256264

@@ -263,6 +271,16 @@ impl FormatterConfigBuilder {
263271
..self
264272
}
265273
}
274+
275+
pub fn use_symbol_for_unit_type(
276+
self,
277+
use_symbol_for_unit_type: bool,
278+
) -> Self {
279+
Self {
280+
use_symbol_for_unit_type,
281+
..self
282+
}
283+
}
266284
}
267285

268286
impl Default for FormatterConfigBuilder {
@@ -282,6 +300,7 @@ impl Default for FormatterConfigBuilder {
282300
put_trailing_semis_on_let_bindings: false,
283301
backup: false,
284302
use_symbol_for_fn_return_type: true,
303+
use_symbol_for_unit_type: true,
285304
}
286305
}
287306
}

petr-fmt/src/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Formattable for FunctionDeclaration {
141141
buf.push_str("returns ");
142142
}
143143

144-
buf.push_str(&self.return_type.pretty_print(&ctx.interner, ctx.indentation()));
144+
buf.push_str(&self.return_type.format(ctx).into_single_line().content);
145145

146146
lines.push(ctx.new_line(buf));
147147

@@ -170,8 +170,7 @@ impl Formattable for FunctionParameter {
170170
let ty_in = if ctx.config.use_set_notation_for_types() { "∈" } else { "in" };
171171

172172
buf.push_str(&format!(" {ty_in} "));
173-
let ty = self.ty.format(ctx).into_single_line().content;
174-
buf.push_str(&ty);
173+
buf.push_str(&self.ty.format(ctx).into_single_line().content);
175174

176175
FormattedLines::new(vec![ctx.new_line(buf)])
177176
}
@@ -513,7 +512,7 @@ impl Formattable for TypeField {
513512
let name = ctx.interner.get(self.name.id);
514513
let mut buf = name.to_string();
515514
buf.push(' ');
516-
buf.push_str(&self.ty.pretty_print(&ctx.interner, ctx.indentation()));
515+
buf.push_str(&self.ty.format(ctx).into_single_line().content);
517516
FormattedLines::new(vec![ctx.new_line(buf)])
518517
}
519518
}
@@ -527,7 +526,13 @@ impl Formattable for Ty {
527526
Ty::Bool => "'bool".to_string(),
528527
Ty::Int => "'int".to_string(),
529528
Ty::String => "'string".to_string(),
530-
Ty::Unit => "'unit".to_string(),
529+
Ty::Unit => {
530+
if ctx.config.use_symbol_for_unit_type() {
531+
"ε".to_string()
532+
} else {
533+
"'unit".to_string()
534+
}
535+
},
531536
Ty::Named(name) => format!("'{}", ctx.interner.get(name.id)),
532537
Ty::Literal(lit) => lit.to_string(),
533538
Ty::Sum(tys) => format!(

petr-fmt/src/tests.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ fn intrinsic() {
563563
fn string_literals() → 'string
564564
"This is a string literal."
565565
566-
fn my_func() → 'unit
566+
fn my_func() → ε
567567
@puts(~string_literal)
568568
"#]],
569569
);
@@ -576,7 +576,7 @@ fn intrinsic_2() {
576576
r#"
577577
fn my_func() returns 'unit @puts("hello, world!")"#,
578578
expect![[r#"
579-
fn my_func() → 'unit
579+
fn my_func() → ε
580580
@puts("hello, world!")
581581
"#]],
582582
);
@@ -647,3 +647,26 @@ fn sum_ty_formatting() {
647647
"#]],
648648
)
649649
}
650+
651+
#[test]
652+
fn unit_ty_formatting() {
653+
check(
654+
Default::default(),
655+
r#"
656+
type bar = a f1 'unit f2 'unit
657+
658+
fn my_func(x in 'unit, y in 'unit, z in 'sum 'unit | 'unit) returns 'unit foo
659+
"#,
660+
expect![[r#"
661+
type bar = a f1 ε f2 ε
662+
663+
664+
fn my_func(
665+
x ∈ ε,
666+
y ∈ ε,
667+
z ∈ 'Σ ε | ε,
668+
) → ε
669+
foo
670+
"#]],
671+
)
672+
}

0 commit comments

Comments
 (0)