lkrms / vscode-pretty-php

A Visual Studio Code extension for PrettyPHP
https://marketplace.visualstudio.com/items?itemName=lkrms.pretty-php
MIT License
5 stars 2 forks source link

[VSCode] Is it possible to exclude some paths? #11

Closed pczerkas closed 2 months ago

pczerkas commented 2 months ago

For example vendor directory?

lkrms commented 2 months ago

When you run pretty-php on the command line, vendor is excluded by default (along with any other directories that match /\/(\.git|\.hg|\.svn|_?build|dist|vendor)\/$/, see here for the particulars), but when you're using the VS Code extension, the only files changed are those you're editing in VS Code, so it shouldn't be necessary to exclude anything.

Are you having issues with files being formatted unexpectedly?

pczerkas commented 2 months ago

Well, my usecase is that I want to edit files in vendor, but for all of these edits ommit auto-formatting. Why editing in vendor? It is handy for example when tesing and preparing patches,

From log I see, that file path is passed to pretty-php (along with file contents on stdin, I suppose):

2024-08-24 08:32:58.676 [info] Spawned: /home/przemek/.phpbrew/php/php-8.2.16/bin/php -ddisplay_errors=stderr -dshort_open_tag=On /home/przemek/.vscode/extensions/lkrms.pretty-php-0.4.67/bin/pretty-php.phar --preset=symfony -r blank-before-return -m name -F /home/przemek/Some/Project/magento/vendor/magento/framework/Acl/AclResource.php -s4 -q -- -
2024-08-24 08:32:58.788 [info] /home/przemek/.phpbrew/php/php-8.2.16/bin/php reported:
✔ Formatted 1 file successfully in 0,071s (8,309MiB memory used)

2024-08-24 08:32:58.788 [info] /home/przemek/.phpbrew/php/php-8.2.16/bin/php succeeded (output length: 300)

Maybe it would be possible to honor exclusions also in this case ?

pczerkas commented 2 months ago

I also tried with added --exclude parameter in extension configuration:

2024-08-24 09:01:15.065 [info] Spawned: /home/przemek/.phpbrew/php/php-8.2.16/bin/php -ddisplay_errors=stderr -dshort_open_tag=On /home/przemek/.vscode/extensions/lkrms.pretty-php-0.4.67/bin/pretty-php.phar --preset=symfony --exclude="/\/vendor\//" -r blank-before-return -m name -F /home/przemek/Some/Project/magento/vendor/magento/framework/Acl/AclResourceFactory.php -s4 -q -- -
2024-08-24 09:01:15.149 [info] /home/przemek/.phpbrew/php/php-8.2.16/bin/php reported:
✔ Formatted 1 file successfully in 0,061s (8,388MiB memory used)

2024-08-24 09:01:15.149 [info] /home/przemek/.phpbrew/php/php-8.2.16/bin/php succeeded (output length: 895)

but formatting still occurs

lkrms commented 2 months ago

I also tried with added --exclude parameter in extension configuration:

2024-08-24 09:01:15.065 [info] Spawned: /home/przemek/.phpbrew/php/php-8.2.16/bin/php -ddisplay_errors=stderr -dshort_open_tag=On /home/przemek/.vscode/extensions/lkrms.pretty-php-0.4.67/bin/pretty-php.phar --preset=symfony --exclude="/\/vendor\//" -r blank-before-return -m name -F /home/przemek/Some/Project/magento/vendor/magento/framework/Acl/AclResourceFactory.php -s4 -q -- -
2024-08-24 09:01:15.149 [info] /home/przemek/.phpbrew/php/php-8.2.16/bin/php reported:
✔ Formatted 1 file successfully in 0,061s (8,388MiB memory used)

2024-08-24 09:01:15.149 [info] /home/przemek/.phpbrew/php/php-8.2.16/bin/php succeeded (output length: 895)

but formatting still occurs

--exclude (which defaults to the regex I mentioned earlier) is only used to limit files discovered by pretty-php when formatting entire directories. When individual files are given (as the VS Code extension does--via STDIN, as you guessed), they are always formatted, regardless of their path.

We could address your use case by changing this behaviour and making pretty-php fail or output unmodified code in paths that match --exclude, but this would make it difficult to perform ad-hoc formatting in vendor directories, which other users might need.

I think adding a setting to the extension to exclude particular folders from formatting triggered by VS Code would be better. Do you foresee any issues with inheriting from files.exclude by default?

Meanwhile, in case you're not aware of it, there is a VS Code command called "Save without Formatting", bound to "Command+K S" or "Ctrl+K S" by default.

lkrms commented 2 months ago

I've added this feature to v0.4.69 (with no inheritance from files.exclude--on reflection, I thought keeping it completely separate made more sense). Let me know if you have any issues 😊