Sitelet https://github.com/starkware-libs/cairo/commit/4670b30228c3bb06acfdcb0567df2f415c961f97
Skip to content

Commit 4670b30

Browse files
committed
bugfix(formatter): Made all single line comment prevent removing line-breaks.
1 parent 219825c commit 4670b30

5 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎crates/cairo-lang-formatter/src/formatter_impl.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,10 @@ impl fmt::Display for CommentLine {
780780
}
781781
}
782782

783-
/// Formats a comment to fit in the line width. There are no merges of lines, as this is not clear
784-
/// when to merge two lines the user chose to write on separate lines, so all original line breaks
785-
/// are preserved.
783+
/// Formats a comment to fit in the line width. Line breaks the user wrote are preserved, as it is
784+
/// not clear when to merge two lines the user chose to write separately; the only merges are of
785+
/// continuation lines that the formatter itself broke off an over-long open line (see the
786+
/// `is_open_line` check below).
786787
fn format_leading_comment(content: &str, cur_indent: usize, max_line_width: usize) -> String {
787788
let mut formatted_comment = String::new();
788789
let mut prev_comment_line = CommentLine::from_string("".to_string());

‎crates/cairo-lang-formatter/src/node_properties.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,10 +1084,12 @@ fn is_statement_list_break_point_optional(db: &dyn Database, node: &SyntaxNode<'
10841084
node.grandparent_kind(db),
10851085
Some(SyntaxKind::MatchArm | SyntaxKind::GenericArgNamed | SyntaxKind::GenericArgUnnamed)
10861086
) && node.get_children(db).len() == 1
1087-
&& node.descendants(db).all(|d| {
1088-
d.kind(db) != SyntaxKind::Trivia
1089-
|| ast::Trivia::from_syntax_node(db, d)
1090-
.elements(db)
1091-
.all(|t| !matches!(t, ast::Trivium::SingleLineComment(_)))
1087+
&& !node.descendants(db).any(|d| {
1088+
matches!(
1089+
d.kind(db),
1090+
SyntaxKind::TokenSingleLineComment
1091+
| SyntaxKind::TokenSingleLineDocComment
1092+
| SyntaxKind::TokenSingleLineInnerComment
1093+
)
10921094
})
10931095
}

‎crates/cairo-lang-formatter/src/test.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ use crate::{FormatterConfig, get_formatted_file};
107107
false,
108108
false
109109
)]
110+
#[test_case(
111+
"test_data/cairo_files/match_arm_doc_comment.cairo",
112+
"test_data/expected_results/match_arm_doc_comment.cairo",
113+
false,
114+
false,
115+
false,
116+
false,
117+
false
118+
)]
110119
#[test_case(
111120
"test_data/cairo_files/use_merge.cairo",
112121
"test_data/expected_results/use_merge.cairo",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn inner(x: felt252) -> felt252 {
2+
match x {
3+
0 => {
4+
//! inner comment
5+
1
6+
},
7+
_ => 2,
8+
}
9+
}
10+
11+
fn doc(x: felt252) -> felt252 {
12+
match x {
13+
0 => {
14+
/// doc comment
15+
1
16+
},
17+
_ => 2,
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn inner(x: felt252) -> felt252 {
2+
match x {
3+
0 => {
4+
//! inner comment
5+
1
6+
},
7+
_ => 2,
8+
}
9+
}
10+
11+
fn doc(x: felt252) -> felt252 {
12+
match x {
13+
0 => {
14+
/// doc comment
15+
1
16+
},
17+
_ => 2,
18+
}
19+
}

0 commit comments

Comments
 (0)