Community
Participate
Working Groups
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 );
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().
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 }
Also $x->call1() ->call2() //my comment ->call3(); will result into $x->call1() ->call2() -> // my comment call3();
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 ;
Gerrit change https://git.eclipse.org/r/119637 was merged to [master]. Commit: http://git.eclipse.org/c/pdt/org.eclipse.pdt.git/commit/?id=8080607dfd05b3ce6b9fa0c0e106b40d12c8d0dc
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.