Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format 'unit as symbol "epsilon" #159

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions petr-fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand All @@ -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,
}
}
}
15 changes: 10 additions & 5 deletions petr-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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)])
}
Expand Down Expand Up @@ -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)])
}
}
Expand All @@ -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!(
Expand Down
27 changes: 25 additions & 2 deletions petr-fmt/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"#]],
);
Expand All @@ -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!")
"#]],
);
Expand Down Expand Up @@ -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
"#]],
)
}