_From @websquared on September 15, 2016 18:56_
Great extension, but is it possible to configure formatting options to follow PSR-2 standard - like do not remove empty lines, align assignments (by = sign), do not convert "else if" to "else } if", etc.?
Thanks.
_Copied from original issue: felixfbecker/vscode-php-intellisense#10_
This early implementation of the formatting feature is using he standard PrettyPrinter of PHPParser: https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/PrettyPrinter/Standard.php.
I am also not satisfied with the output, for example how it handles use and declare statements and the empty lines. I'm sure the class can be customized more. We could
contributions welcome!
I heard from @mniewrzal that the PrettyPrinter can't be used for range formatting, but only for formatting the whole document.
I wonder if we can use either the CS Fixer or PHPCBF. AFAIK they already support a variety of coding standards.
I am not sure if they are better for range formatting, but perhaps it's worth taking a look.
Range formatting is probably less problematic than lack of configuration. PrettyPrinter should be able to format just selected range if user will select complete AST node. To make it configurable we need to extend it and add own logic.
I will take a look at CS Fixer and PHPCBF.
I played with CS Fixer today and I was able to use it with language server. If someone want to take a look here is branch with changes. Its just a fast hack ;-)
Pros:
Cons:
@mniewrzal always feel free to open a PR, it is a nice way to start a discussion :)
can be very slow, with enabled xdebug formatting 1600 LOC can take more than 10s, The same operation without xdebug takes under 1s.
I would like to implement some solution in the client that spawns the language server without XDebug, XDebug is performance killer. Some ideas here: http://stackoverflow.com/questions/38506140/how-to-run-composer-without-xdebug-when-installed-with-homebrew
On startup, read the php.ini, remove lines containing "xdebug", write to temp folder, start php with the ini file.
@mniewrzal always feel free to open a PR, it is a nice way to start a discussion :)
If there is an option to overcome xdebug performance issue making such PR make sense :) I will prepare something soon.
I meant that I prefer to discuss changes on an open PR that is not yet finished, because you can better comment on the changes, even when it won't get merged in the current state of the branch :)
Yes, I understand. I just wanted to say that without hope for better performance I didn't see a chance for using cs-fixer with language server. Actual implementation is too messy to make PR from it ;)
@mniewrzal can you check what is the performance impact if having Zend Debugger instead of Xdebug?
PHP CS Fixer times for https://github.com/composer/composer/blob/master/src/Composer/Installer.php:
I abandon my previous PR for CS-Fixer. I created PR #35 with PHPCBF integration. PHPCBF formatting looks slightly better and performance is good even with XDebug enabled. Like CS-Fixer it has some gaps to fill (e.g. whitespaces in namespace declaration) but I think it will be easier to add missing rules than writing formatter from scratch (e.g. based on PrettyPrinter).
Solved with PHP_CodeSniffer
And how to configure the formatting now?
Can you give an example?
Please refer to the PHP CodeSniffer documentation on how to configure it with an XML file - you can also find one in this repository
Most helpful comment
I abandon my previous PR for CS-Fixer. I created PR #35 with PHPCBF integration. PHPCBF formatting looks slightly better and performance is good even with XDebug enabled. Like CS-Fixer it has some gaps to fill (e.g. whitespaces in namespace declaration) but I think it will be easier to add missing rules than writing formatter from scratch (e.g. based on PrettyPrinter).