File tree Expand file tree Collapse file tree 6 files changed +49
-1
lines changed
Expand file tree Collapse file tree 6 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,33 @@ impl<'arena> FormatterState<'_, 'arena> {
7474 || ( remaining_content. starts_with ( '#' ) && !remaining_content. starts_with ( "#[" ) )
7575 }
7676
77+ /// Checks if a node has a trailing line comment on the same line.
78+ ///
79+ /// This is different from `is_followed_by_comment_on_next_line` which checks
80+ /// for comments on the subsequent line. This method detects trailing comments
81+ /// that are on the same line as the node (e.g., `} // comment`).
82+ ///
83+ /// # Arguments
84+ ///
85+ /// * `span` - The span of the node to check for same-line trailing comments.
86+ ///
87+ /// # Returns
88+ ///
89+ /// `true` if there's a line comment on the same line after the node, `false` otherwise.
90+ pub ( crate ) fn has_same_line_trailing_comment ( & self , span : Span ) -> bool {
91+ let Some ( first_char_offset) = self . skip_spaces ( Some ( span. end_offset ( ) ) , false ) else {
92+ return false ;
93+ } ;
94+
95+ // If there's a newline before the next content, the comment is on the next line, not same line
96+ if self . has_newline ( first_char_offset, /* backwards */ true ) {
97+ return false ;
98+ }
99+
100+ let remaining = & self . source_text [ first_char_offset as usize ..] ;
101+ remaining. starts_with ( "//" ) || ( remaining. starts_with ( '#' ) && !remaining. starts_with ( "#[" ) )
102+ }
103+
77104 pub ( crate ) fn has_leading_own_line_comment ( & self , range : Span ) -> bool {
78105 self . has_comment_with_filter ( range, CommentFlags :: Leading , |comment| {
79106 self . has_newline ( comment. end , /* backwards */ false )
Original file line number Diff line number Diff line change @@ -565,7 +565,10 @@ pub(super) fn adjust_clause<'arena>(
565565 } ;
566566
567567 if has_trailing_segment {
568- if !is_block || f. is_followed_by_comment_on_next_line ( node. span ( ) ) {
568+ if !is_block
569+ || f. is_followed_by_comment_on_next_line ( node. span ( ) )
570+ || f. has_same_line_trailing_comment ( node. span ( ) )
571+ {
569572 Document :: Array ( vec ! [ in f. arena; clause, Document :: Line ( Line :: hard( ) ) ] )
570573 } else {
571574 Document :: Array ( vec ! [ in f. arena; clause, Document :: space( ) ] )
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ if ($ x ) {
4+ } // foo
5+ elseif (
6+ $ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
7+ ) {
8+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ if ($ x ) {
4+ } // foo
5+ elseif ($ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) {
6+ }
Original file line number Diff line number Diff line change 1+ FormatSettings {
2+ ..Default ::default ()
3+ }
Original file line number Diff line number Diff line change @@ -308,6 +308,7 @@ test_case!(issue_738);
308308test_case ! ( issue_788) ;
309309test_case ! ( issue_796) ;
310310test_case ! ( issue_812) ;
311+ test_case ! ( issue_813) ;
311312
312313#[ test]
313314fn test_all_test_cases_are_ran ( ) {
You can’t perform that action at this time.
0 commit comments