Skip to content

Commit

Permalink
fmt: Improve formatting in codeblocks
Browse files Browse the repository at this point in the history
- No longer inserts extra new lines
- multiple statements get formatted correctly
  • Loading branch information
Waqar144 authored and ogoffart committed Feb 15, 2024
1 parent 24f5bb5 commit 05fb6d0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tools/lsp/fmt/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,23 @@ fn format_codeblock(
state.indentation_level += 1;
state.new_line();
for n in sub {
state.skip_all_whitespace = true;
if n.kind() == SyntaxKind::RBrace {
state.indentation_level -= 1;
state.whitespace_to_add = None;
state.new_line();
}

let is_semicolon = n.kind() == SyntaxKind::Semicolon;

fold(n, writer, state)?;

if is_semicolon {
state.whitespace_to_add = None;
state.new_line();
}
}
state.skip_all_whitespace = true;
Ok(())
}

Expand Down Expand Up @@ -1160,6 +1170,39 @@ A := B {
}
}
}
"#,
);
}

#[test]
fn code_block() {
assert_formatting(
r#"
component ABC {
in-out property <bool> logged_in: false;
function clicked() -> bool {
if (logged_in) {
logged_in = false;
return true;
} else {
logged_in = false; return false;
}
}
}
"#,
r#"
component ABC {
in-out property <bool> logged_in: false;
function clicked() -> bool{
if (logged_in) {
logged_in = false;
return true;
} else {
logged_in = false;
return false;
}
}
}
"#,
);
}
Expand Down

0 comments on commit 05fb6d0

Please sign in to comment.