Skip to content

Commit 8aa6cb2

Browse files
committed
fixup! Fix formatting of comments after statements without semicolons
1 parent 62592fd commit 8aa6cb2

5 files changed

Lines changed: 75 additions & 18 deletions

File tree

core/datatests/generators/logical_line_parser.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,13 @@ mod comments {
508508
_| //
509509
_|end
510510
",
511+
anonymous = "
512+
_1 |A := procedure
513+
_1 |begin{1}
514+
_^1 | A := B + C
515+
_^1 | //
516+
_1 |end;
517+
",
511518
if_else = "
512519
_ |begin
513520
_1 | if A then{1}
@@ -518,6 +525,18 @@ mod comments {
518525
_ | //
519526
_ |end
520527
",
528+
if_else_begin = "
529+
_ |begin
530+
_1 | if A then{1}
531+
_^1 | //
532+
_^1 | begin
533+
_^1 | //
534+
_^1 | end
535+
_1 | else{2}
536+
_^2 | B := C + D
537+
_ | //
538+
_ |end
539+
",
521540
for_in = "
522541
_ |begin
523542
_1 | for A in B do{1}

core/datatests/generators/optimising_line_formatter.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,25 @@ mod comments {
534534
end
535535
);
536536
",
537+
anonymous_content = "
538+
A(
539+
procedure
540+
begin
541+
//
542+
end,
543+
procedure
544+
begin
545+
A
546+
//
547+
end,
548+
procedure
549+
begin
550+
A;
551+
//
552+
A
553+
end,
554+
);
555+
",
537556
if_else = "
538557
if AAAAAA then //
539558
BBBBBBB;
@@ -554,6 +573,32 @@ mod comments {
554573
else //
555574
begin
556575
end;
576+
if A then
577+
//
578+
begin
579+
//
580+
end
581+
//
582+
;
583+
if A then
584+
//
585+
begin
586+
A
587+
//
588+
end
589+
//
590+
;
591+
if A then begin
592+
//
593+
end
594+
//
595+
;
596+
if A then begin
597+
A
598+
//
599+
end
600+
//
601+
;
557602
",
558603
after_do = "
559604
try
@@ -613,16 +658,6 @@ mod comments {
613658
);
614659
end;
615660
",
616-
middle_line = "
617-
A(
618-
procedure
619-
begin
620-
A;
621-
//
622-
A;
623-
end
624-
);
625-
",
626661
);
627662
}
628663
}

core/src/defaults/parser.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
611611
|parser| {
612612
parser.parse_structures();
613613
parser.finish_logical_line();
614-
parser.take_individual_comments();
614+
// Comments after the `if` content are seen to be referring to the `else`
615+
parser.take_individual_comments(Some(LLT::ParentLineChildComment));
615616
},
616617
);
617618

@@ -809,7 +810,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
809810
self.current_line.pop();
810811
}
811812

812-
fn take_individual_comments(&mut self) {
813+
fn take_individual_comments(&mut self, line_type: Option<LogicalLineType>) {
813814
trace!("Take individual comments");
814815
while let Some(TT::Comment(
815816
CommentKind::IndividualBlock
@@ -819,7 +820,9 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
819820
{
820821
trace!("Taking individual comment line");
821822
self.next_token();
822-
self.set_logical_line_type(LLT::TrailingChildComment);
823+
if let Some(line_type) = line_type {
824+
self.set_logical_line_type(line_type);
825+
}
823826
self.finish_logical_line();
824827
}
825828
}
@@ -1230,7 +1233,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
12301233
}
12311234
}
12321235

1233-
self.take_individual_comments();
1236+
self.take_individual_comments(None);
12341237
}
12351238
fn parse_block(&mut self, context: ParserContext) {
12361239
self.do_with_context(context, |parser| {
@@ -1718,7 +1721,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
17181721
self.next_token(); // Begin
17191722
self.parse_statement_list_block(ParserContext {
17201723
context_type: ContextType::StatementBlock(BlockKind::Begin),
1721-
context_ending_predicate: CEP::Opaque(end),
1724+
context_ending_predicate: CEP::Opaque(statement_list_end),
17221725
level: context_level,
17231726
});
17241727
self.next_token(); // End

core/src/lang.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ pub enum LogicalLineType {
461461
CaseArm,
462462
Declaration,
463463
VariantRecordCaseArm,
464-
TrailingChildComment,
464+
ParentLineChildComment,
465465
Unknown,
466466
Voided,
467467
}

core/src/rules/optimising_line_formatter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ impl<'this> InternalOptimisingLineFormatter<'this, '_> {
10641064
ChildLineOption::BreakAll(ws) | ChildLineOption::ContinueThenBreak(ws) => ws,
10651065
};
10661066
/*
1067-
This is to ensure that comments before the end of a context are
1067+
Lines with the `LLT::ParentLineChildComment` line type are
10681068
formatted to the parent's level.
10691069
10701070
E.g.,
@@ -1077,7 +1077,7 @@ impl<'this> InternalOptimisingLineFormatter<'this, '_> {
10771077
```
10781078
*/
10791079
let get_child_starting_ws = |line_type| match line_type {
1080-
LLT::TrailingChildComment => parent_base_ws,
1080+
LLT::ParentLineChildComment => parent_base_ws,
10811081
_ => child_starting_ws,
10821082
};
10831083
let get_first_token_decision = |index, line_length| match option {

0 commit comments

Comments
 (0)