Skip to content

Commit

Permalink
fix bugs; switch function to fn; add export keywords; semis to separa…
Browse files Browse the repository at this point in the history
…te let bindings
  • Loading branch information
sezna committed Jul 21, 2024
1 parent e834445 commit b16cf37
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 315 deletions.
12 changes: 6 additions & 6 deletions petr-bind/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,33 +600,33 @@ mod tests {
#[test]
fn bind_function_decl() {
check(
"function add(a in 'Int, b in 'Int) returns 'Int + 1 2",
"fn add(a in 'Int, b in 'Int) returns 'Int + 1 2",
expect![[r#"
__Scopes__
0: Root (parent none):
test: Module Module { root_scope: ScopeId(1), exports: {} }
1: Module test (parent scopeid0):
add: Function FunctionId(0)
2: Function (parent scopeid1):
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(19), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(30), length: 3 } } })
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(13), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(24), length: 3 } } })
"#]],
);
}

#[test]
fn bind_list_new_scope() {
check(
"function add(a in 'Int, b in 'Int) returns 'Int [ 1, 2, 3, 4, 5, 6 ]",
"fn add(a in 'Int, b in 'Int) returns 'Int [ 1, 2, 3, 4, 5, 6 ]",
expect![[r#"
__Scopes__
0: Root (parent none):
test: Module Module { root_scope: ScopeId(1), exports: {} }
1: Module test (parent scopeid0):
add: Function FunctionId(0)
2: Function (parent scopeid1):
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(19), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(31), length: 3 } } })
a: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(13), length: 3 } } })
b: FunctionParameter Named(Identifier { id: SymbolId(3), span: Span { source: SourceId(0), span: SourceSpan { offset: SourceOffset(25), length: 3 } } })
"#]],
);
}
Expand Down
20 changes: 10 additions & 10 deletions petr-fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct FormatterConfig {
tab_size: usize,
max_line_length: usize,
put_fn_args_on_new_lines: bool,
put_trailing_commas_on_let_bindings: bool,
put_trailing_semis_on_let_bindings: bool,
backup: bool,
}

Expand Down Expand Up @@ -60,8 +60,8 @@ impl FormatterConfig {
self.put_fn_args_on_new_lines
}

pub fn put_trailing_commas_on_let_bindings(&self) -> bool {
self.put_trailing_commas_on_let_bindings
pub fn put_trailing_semis_on_let_bindings(&self) -> bool {
self.put_trailing_semis_on_let_bindings
}

pub fn backup(&self) -> bool {
Expand All @@ -81,7 +81,7 @@ impl FormatterConfig {
tab_size: self.tab_size,
max_line_length: self.max_line_length,
put_fn_args_on_new_lines: self.put_fn_args_on_new_lines,
put_trailing_commas_on_let_bindings: self.put_trailing_commas_on_let_bindings,
put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings,
backup: self.backup,
}
}
Expand All @@ -105,7 +105,7 @@ pub struct FormatterConfigBuilder {
tab_size: usize,
max_line_length: usize,
put_fn_args_on_new_lines: bool,
put_trailing_commas_on_let_bindings: bool,
put_trailing_semis_on_let_bindings: bool,
backup: bool,
}

Expand Down Expand Up @@ -211,12 +211,12 @@ impl FormatterConfigBuilder {
}
}

pub fn put_trailing_commas_on_let_bindings(
pub fn put_trailing_semis_on_let_bindings(
self,
put_trailing_commas_on_let_bindings: bool,
put_trailing_semis_on_let_bindings: bool,
) -> Self {
Self {
put_trailing_commas_on_let_bindings,
put_trailing_semis_on_let_bindings,
..self
}
}
Expand All @@ -241,7 +241,7 @@ impl FormatterConfigBuilder {
tab_size: self.tab_size,
max_line_length: self.max_line_length,
put_fn_args_on_new_lines: self.put_fn_args_on_new_lines,
put_trailing_commas_on_let_bindings: self.put_trailing_commas_on_let_bindings,
put_trailing_semis_on_let_bindings: self.put_trailing_semis_on_let_bindings,
backup: self.backup,
}
}
Expand All @@ -261,7 +261,7 @@ impl Default for FormatterConfigBuilder {
tab_size: 2,
max_line_length: 80,
put_fn_args_on_new_lines: false,
put_trailing_commas_on_let_bindings: false,
put_trailing_semis_on_let_bindings: false,
backup: false,
}
}
Expand Down
11 changes: 3 additions & 8 deletions petr-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ impl Formattable for FunctionDeclaration {
ctx: &mut FormatterContext,
) -> FormattedLines {
let mut lines: Vec<Line> = Vec::new();
let mut buf: String = if self.visibility == Visibility::Exported {
"Function "
} else {
"function "
}
.to_string();
let mut buf: String = if self.visibility == Visibility::Exported { "export fn " } else { "fn " }.to_string();

buf.push_str(&ctx.interner.get(self.name.id));

Expand Down Expand Up @@ -245,11 +240,11 @@ impl Formattable for ExpressionWithBindings {
lines.append(&mut expr_lines[1..].to_vec());
}
// add comma to the end of the last line
if !is_last || ctx.config.put_trailing_commas_on_let_bindings() {
if !is_last || ctx.config.put_trailing_semis_on_let_bindings() {
let last_line = lines.last().expect("invariant");
let last_line_indentation = last_line.indentation;
let mut last_line_content = last_line.content.to_string();
last_line_content.push(',');
last_line_content.push(';');
*(lines.last_mut().expect("invariant")) = Line {
content: Rc::from(last_line_content),
indentation: last_line_indentation,
Expand Down
Loading

0 comments on commit b16cf37

Please sign in to comment.