Skip to content

Commit

Permalink
fmt: format two way bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Mar 11, 2024
1 parent b7e437f commit c3a2ad1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/compiler/parser/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,9 @@ fn parse_callback_declaration(p: &mut impl Parser) {
parse_type(&mut *p);
}

if p.test(SyntaxKind::DoubleArrow) {
if p.peek().kind() == SyntaxKind::DoubleArrow {
let mut p = p.start_node(SyntaxKind::TwoWayBinding);
p.expect(SyntaxKind::DoubleArrow);
parse_expression(&mut *p);
}

Expand Down
48 changes: 46 additions & 2 deletions tools/lsp/fmt/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ fn format_node(
SyntaxKind::Binding => {
return format_binding(node, writer, state);
}
SyntaxKind::TwoWayBinding => {
return format_two_way_binding(node, writer, state);
}
SyntaxKind::CallbackConnection => {
return format_callback_connection(node, writer, state);
}
Expand Down Expand Up @@ -388,13 +391,16 @@ fn format_property_declaration(
_ => continue,
}
}
let need_newline =
if node.child_node(SyntaxKind::TwoWayBinding).is_some() { false } else { true };

state.skip_all_whitespace = true;
// FIXME: more formatting
for s in sub {
fold(s, writer, state)?;
}
state.new_line();
if need_newline {
state.new_line();
}
Ok(())
}

Expand All @@ -415,6 +421,27 @@ fn format_binding(
Ok(())
}

fn format_two_way_binding(
node: &SyntaxNode,
writer: &mut impl TokenWriter,
state: &mut FormatState,
) -> Result<(), std::io::Error> {
let mut sub = node.children_with_tokens();
if node.child_token(SyntaxKind::Identifier).is_some() {
whitespace_to(&mut sub, SyntaxKind::Identifier, writer, state, "")?;
}
let _ok = whitespace_to(&mut sub, SyntaxKind::DoubleArrow, writer, state, " ")?
&& whitespace_to(&mut sub, SyntaxKind::Expression, writer, state, " ")?;
if node.child_token(SyntaxKind::Semicolon).is_some() {
whitespace_to(&mut sub, SyntaxKind::Semicolon, writer, state, "")?;
state.new_line();
}
for s in sub {
fold(s, writer, state)?;
}
Ok(())
}

fn format_callback_declaration(
node: &SyntaxNode,
writer: &mut impl TokenWriter,
Expand Down Expand Up @@ -443,6 +470,9 @@ fn format_callback_declaration(
fold(n, writer, state)?;
whitespace_to(&mut sub, SyntaxKind::ReturnType, writer, state, " ")?;
}
SyntaxKind::TwoWayBinding => {
fold(n, writer, state)?;
}
_ => {
fold(n, writer, state)?;
}
Expand Down Expand Up @@ -1745,6 +1775,20 @@ export component MainWindow2 inherits Rectangle {
export component MainWindow2 inherits Rectangle {
in property <[string]> model: [];
}
"#,
);
}

#[test]
fn two_way_binding() {
assert_formatting(
"export component Foobar{foo<=>xx.bar ; property<int\n>xx <=> ff . mm ; callback doo <=> moo\n;\nproperty e-e<=>f-f; }",
r#"export component Foobar {
foo <=> xx.bar;
property <int> xx <=> ff.mm;
callback doo <=> moo;
property e-e <=> f-f;
}
"#,
);
}
Expand Down

0 comments on commit c3a2ad1

Please sign in to comment.