Skip to content

Commit 20900ba

Browse files
authored
fix: #46 - Single line if statement going to multiple lines should keep the header on one line if able
1 parent ff4a5dc commit 20900ba

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/parsing/parser.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6174,7 +6174,7 @@ fn parse_header_with_conditional_brace_body<'a>(opts: ParseHeaderWithConditional
61746174
let mut items = PrintItems::new();
61756175

61766176
items.push_info(start_header_info);
6177-
items.extend(opts.parsed_header);
6177+
items.extend(new_line_group(opts.parsed_header));
61786178
items.push_info(end_header_info);
61796179
let result = parse_conditional_brace_body(ParseConditionalBraceBodyOptions {
61806180
parent: opts.parent,
@@ -6222,7 +6222,6 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
62226222
let end_header_info = opts.end_header_info;
62236223
let requires_braces_condition = opts.requires_braces_condition_ref;
62246224
let start_inner_text_info = Info::new("startInnerText");
6225-
let end_first_line_comments_info = Info::new("endFirstLineComments");
62266225
let start_statements_info = Info::new("startStatements");
62276226
let end_statements_info = Info::new("endStatements");
62286227
let header_trailing_comments = get_header_trailing_comments(&opts.body_node, context);
@@ -6238,17 +6237,10 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
62386237
let open_brace_token = get_open_brace_token(&opts.body_node, context);
62396238
let use_braces = opts.use_braces;
62406239
let is_body_empty_stmt = opts.body_node.kind() == NodeKind::EmptyStmt;
6241-
let mut space_condition = if_true(
6240+
let mut inner_brace_space_condition = if_true(
62426241
"spaceCondition",
62436242
move |condition_context| {
62446243
if is_body_empty_stmt { return Some(false); }
6245-
6246-
if let Some(has_first_line_comments) = condition_resolvers::are_infos_not_equal(condition_context, &start_inner_text_info, &end_first_line_comments_info) {
6247-
if has_first_line_comments {
6248-
return Some(true);
6249-
}
6250-
}
6251-
62526244
let start_inner_text_info = condition_context.get_resolved_info(&start_inner_text_info)?;
62536245
let end_statements_info = condition_context.get_resolved_info(&end_statements_info)?;
62546246
if start_inner_text_info.line_number < end_statements_info.line_number {
@@ -6258,7 +6250,7 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
62586250
},
62596251
Signal::SpaceOrNewLine.into(),
62606252
);
6261-
let space_condition_ref = space_condition.get_reference();
6253+
let inner_brace_space_condition_ref = inner_brace_space_condition.get_reference();
62626254
let mut newline_condition = if_true(
62636255
"newLineCondition",
62646256
move |condition_context| {
@@ -6338,6 +6330,7 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
63386330
start_header_info,
63396331
}, context));
63406332
items.push_str("{");
6333+
items.push_condition(inner_brace_space_condition);
63416334
Some(items)
63426335
},
63436336
false_path: None,
@@ -6348,13 +6341,14 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
63486341
let mut items = PrintItems::new();
63496342
items.push_info(start_info);
63506343
items.push_condition(open_brace_condition);
6351-
items.push_condition(space_condition);
63526344
items.push_info(start_inner_text_info);
63536345
let parsed_comments = parse_comment_collection(header_trailing_comments.into_iter(), None, None, context);
63546346
if !parsed_comments.is_empty() {
6355-
items.push_condition(conditions::indent_if_start_of_line(parsed_comments));
6347+
items.push_signal(Signal::StartForceNoNewLines);
6348+
items.push_str(" ");
6349+
items.extend(parsed_comments);
6350+
items.push_signal(Signal::FinishForceNoNewLines);
63566351
}
6357-
items.push_info(end_first_line_comments_info);
63586352
items.push_condition(newline_condition);
63596353
items.push_info(start_statements_info);
63606354

@@ -6371,6 +6365,13 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
63716365
items.extend(parser_helpers::with_indent({
63726366
let mut items = PrintItems::new();
63736367
let body_node_span = opts.body_node.span();
6368+
if !is_body_empty_stmt {
6369+
items.push_condition(if_true(
6370+
"spaceIfAtStart",
6371+
move |context| condition_resolvers::is_at_same_position(context, &start_info),
6372+
Signal::SpaceOrNewLine.into(),
6373+
));
6374+
}
63746375
items.extend(parse_node(opts.body_node, context));
63756376
items.extend(parse_trailing_comments(&body_node_span, context));
63766377
items
@@ -6398,7 +6399,7 @@ fn parse_conditional_brace_body<'a>(opts: ParseConditionalBraceBodyOptions<'a>,
63986399
if condition_resolvers::is_at_same_position(condition_context, &start_inner_text_info)? {
63996400
return Some(false);
64006401
}
6401-
let had_space = condition_context.get_resolved_condition(&space_condition_ref)?;
6402+
let had_space = condition_context.get_resolved_condition(&inner_brace_space_condition_ref)?;
64026403
return Some(had_space);
64036404
},
64046405
" ".into(),

tests/specs/issues/issue0046.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
~~ lineWidth: 40 ~~
2+
== should move a single line if statement to be multi-line when exceeding line width ==
3+
if (test && other && test && other) testing;
4+
5+
[expect]
6+
if (test && other && test && other) {
7+
testing;
8+
}

tests/specs/statements/ifStatement/IfStatement_UseBraces_WhenNotSingleLine.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ if (testingThisOut && otherTest) { // testing this out
4242
logger;
4343
}
4444

45-
== for now, it does this when going over the line width on the second space ==
45+
== should maintain the comment when going over the line width on the comment space ==
4646
if (testingThisOut && otherTestTesttt) { // testing this out
4747
logger;
4848
}
4949

5050
[expect]
51-
if (testingThisOut && otherTestTesttt) {
52-
// testing this out
51+
if (testingThisOut && otherTestTesttt) { // testing this out
5352
logger;
5453
}
5554

0 commit comments

Comments
 (0)