OVYA / php-cs-fixer

php-cs-fixer wrapper for the Emacs editor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow for path-mode option to be used

nloyola opened this issue · comments

In my php-cs-fixer configuration I am excluding directories and files. When edit one of these files and save it, this package reformats the file when it shouldn't.

If the configuration for this package allows the --path-mode=intersection option to be used, then excluded files will not reformatted.

See the manual for a description for this option: https://cs.symfony.com/doc/usage.html

I solved this by adding my own before-save-hook:

(defun nl/php-before-save-hook ()
    (when (string-match-p (regexp-quote ".php") buffer-file-name)
        (let ((file-name (replace-regexp-in-string (projectile-project-root) "" buffer-file-name))
              (command (concat php-cs-fixer-command
                               " list-files --config "
                               (shell-quote-argument php-cs-fixer-config-option)))
              (default-directory (projectile-project-root)))
          (when (string-match-p (regexp-quote file-name) (shell-command-to-string command))
            (php-cs-fixer-fix)
          ))))

It uses php-cs-fixer's list-files command to list all the files that php-cs-fixer should examine.

Note that this code uses Projectile to get the project's root folder.