Bug 439568 - Comments formatting inside parameters/elements list should be improved
Summary: Comments formatting inside parameters/elements list should be improved
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: PDT (show other bugs)
Version: unspecified   Edit
Hardware: PC All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: PHP Core CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 468155
Blocks:
  Show dependency tree
 
Reported: 2014-07-14 17:12 EDT by Michal Niewrzal CLA
Modified: 2020-05-14 10:17 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michal Niewrzal CLA 2014-07-14 17:12:21 EDT
Right now comments formatting inside such structures like if statement, array constructor, method invocations, etc. is not perfect. Very often comment is moved outside brackets after formatting. This state should be improved.

Examples:

return array(
//this is a comment
);

or this:

if ($link->getTarget() === $target->getName()
// && $link->getConstraint()->matches($test))) {
) {
return true;
}

or:

print($var
//,$nex_var
);
Comment 1 Michal Niewrzal CLA 2014-07-14 17:13:39 EDT
Thierry said also:

Let me explain: in many methods, tokens ( like ()[] or -> ) are appended to buffer (using appendToBuffer()) BEFORE comments are treated (using handleChars() or directly handleComments()).
So for example, something like: ( COMMENT ) will appear as () COMMENT after formating.
For the test case attached to this patch, look at call sequence in CodeFormatterVisitor::visit(ArrayCreation arrayCreation) :
1. appendToBuffer(OPEN_PARN);
2. appendToBuffer(CLOSE_PARN);
3. handleChars(lastPosition, arrayCreation.getEnd()); which
   calls handleComments()
Ideally, every visit() method should do : 1. handleComments() 2. appendToBuffer() 3. handleComments() but it would be necessary to rewrite handleComments() to treat comments BEFORE and AFTER tokens separatly, by splitting the list of comments retrieved using astLexer.getCommentList().
Comment 2 Sylvia Tancheva CLA 2015-05-25 07:22:59 EDT
Another example from Bug 440820
<?php
function foo() {
	$a->	// comment
$b () // comment
		-> /* comment */ $c  (); // comment
}

After formatting '//comment' and '->' after b() have switched places like this:
<?php
function foo() {
	$a-> // comment
$b ()-> // comment
	/* comment */
	$c (); // comment
}
Comment 3 Sylvia Tancheva CLA 2015-05-26 11:35:55 EDT
Also
$x->call1()
  ->call2() //my comment
  ->call3();

will result into

$x->call1()
    ->call2()
    -> // my comment
call3();
Comment 4 Sylvia Tancheva CLA 2015-05-28 07:15:12 EDT
One more example from Bug 439891:
<?php
$a = [
1=>'one',
2=>'two',//a comment
//a comment
];

Result:

<?php
$a = [ 
		1 => 'one',
		2 => 'two' 
] // a comment
          // a comment
;
Comment 6 Thierry BLIND CLA 2018-03-18 18:51:33 EDT
My first patch solved following problems from Michal's original post:

return array(
//this is a comment
);

and:

print($var
//,$nex_var
);

Still needs to be done:

if ($link->getTarget() === $target->getName()
// && $link->getConstraint()->matches($test))) {
) {
return true;
}

And globally the problems from "comment 1" are also still valid.

Thierry.