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

fmt: format two way bindings #4804

Merged
merged 1 commit into from
Mar 11, 2024
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
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
Loading