junstyle / vscode-php-cs-fixer

PHP CS Fixer extension for VS Code

Home Page:https://marketplace.visualstudio.com/items?itemName=junstyle.php-cs-fixer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix on save does not respect finder

cartok opened this issue · comments

Looks like fix on save does not respect the finder object.
I've created a finder with in('/foo/bar'), created a file /baz.php and it gets auto fixed on save, but it should not. This can make the plugin unusable, can someone confirm this?

I guess it's because the plugin has an extra setting (exclude). I think it should rather be include but why have it at all?

I also tried to use a negated glob (ext glob is enabled by default in anymatch) and a negative lookahead regex but without success.

Tried it again with:

"php-cs-fixer.exclude": [
  "**/src/**",
  "!(**/src/foo/bar/**)",
]

Which seems to work files outside of src/foo/bar get ignored on save but inside still gets fixed, but it's not a nice solution I think.

when use this extension to fix a file, it create a tmp file and then pass to php-cs-fixer, not the current edited file, maybe the config in is useless.

 "php-cs-fixer.pathMode": {
          "type": "string",
          "enum": [
            "override",
            "intersection"
          ],
          "default": "override",
          "description": "--path-mode can be override or intersection, intersection only works on explorer context menu action, not works for current focused file. detail see:https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage"
        },

and see this extension's config: pathMode, this option always override when you fix the current edited file. so the config in
has been overridden, if you open the vscode's Developer Tools, you will see

Paths from configuration file have been overridden by paths provided as command arguments.

if you want the config in to work, you can use the command php-cs-fixer.fix, or context menu -> php-cs-fixer.fix

so i think the best way is using the option: php-cs-fixer.exclude, if you fix the current edited file.

Thanks, so ok I get it, you documented that it pathMode=intersection is not possible atm. Can't it get improved? I imagine that it would be good if by default mode would be intersection and the config would get used (including the rules). I tried it with intersection and get Cannot create intersection with not-fully defined Finder in configuration file., see:
image
The screenshot also shows that --config is not passed.

My finder configuration is:

$fullPath = getcwd() . '/app';
$finder = PhpCsFixer\Finder::create()
   ->in($fullPath)
   ->files()
   ->name(['*.php', '*.phtml'])
;

To the topic about the exclude option: What about adding an include option aswell?

Why the temp file?

because use tmp file, so the config in can't match the tmp file path generally.

why i use tmp file?
this extension not only provide to format a whole file, but also format partial codes, such as selected range formatting...
and if you want to format an unsaved file, and not change the behavior to save first, need a temp file to do that.