音楽とお酒とものづくりと

営業マンが元webエンジニアの経験を生かしてあれやこれやするやつ

vim上でphp-cs-fixerを実行するときにdry−runでdiffを確認してからfixを実施するための設定方法

はじめに

php−cs−fixerを実行する際、どこがどう変更されるのかを確認してからfixをかけたいですよね。
ターミナルから実行するときはオプションに--diffをつけてやれば問題ありませんが、vim上で実行するときはどうやったら確認できるのでしょうか。


php-cs-fixerのインストール

インストールはこちらを参考にして行いました。

php-cs-fixerのインストール
https://github.com/FriendsOfPHP/PHP-CS-Fixer

php-cs-fixerをvim上で実行させるプラグインのインストール
https://github.com/stephpy/vim-php-cs-fixer


vim上でのphp−cs−fixerの動作

f:id:takaaki_z:20160706223334p:plain

実際にvim上でfixerを動かすとこんな感じになります。どこをどう変更してくれるのかがわからないため、若干不安になります。

では、ひとつずつ見ていきましょう。


デフォルトのOption Available

まず、stephpy/vim-php-cs-fixerで指定できるオプションを確認してみると、diffの項目がありません。

" If php-cs-fixer is in $PATH, you don't need to define line below
" let g:php_cs_fixer_path = "~/php-cs-fixer.phar" " define the path to the php-cs-fixer.phar
let g:php_cs_fixer_level = "symfony"              " which level ?
let g:php_cs_fixer_config = "default"             " configuration
"let g:php_cs_fixer_config_file = '.php_cs'       " configuration file
let g:php_cs_fixer_php_path = "php"               " Path to PHP
" If you want to define specific fixers:
"let g:php_cs_fixer_fixers_list = "linefeed,short_tag,indentation"
let g:php_cs_fixer_enable_default_mapping = 1     " Enable the mapping by default (<leader>pcd)
let g:php_cs_fixer_dry_run = 0                    " Call command with dry-run option
let g:php_cs_fixer_verbose = 0                    " Return the output of command if 1, else an inline information.

どうやらデフォルトではdiffを有効化して変更箇所の確認を行うことは難しいようです。

なので今回はプラグイン本体に修正を加え、diffの結果を表示できるようにします。


プラグインの変更

プラグイン本体のファイルを確認してみます。
ファイルは~/.vim/bundle/vim-php-cs-fixer/plugin/php-cs-fixer.vimです。
デフォルトでは次のようになっています。

 44     if a:dry_run == 1
 45         echohl Title | echo "[DRY RUN MODE]" | echohl None
 46         let command = command.' --dry-run'
 47     endif


今回は一度dry−runを実行し、変更箇所を確認してから、実際にfixをかけるようにしたかったので、dry−run実行時にdiffを一緒に指定しました。実際には以下のように修正を加えます。

 44     if a:dry_run == 1
 45         echohl Title | echo "[DRY RUN MODE]" | echohl None
 46         let command = command.' --dry-run --diff'
 47     endif


このように変更を加えた後、オプションのdry_runとverboseの項目を1にしてやればOK。これでvim上からphp−cs−fixerを実行した時に、一度dry−runを走らせ変更箇所を確認したのちに、実際にfixをかけられるようになります。

" If php-cs-fixer is in $PATH, you don't need to define line below
" let g:php_cs_fixer_path = "~/php-cs-fixer.phar" " define the path to the php-cs-fixer.phar
let g:php_cs_fixer_level = "symfony"              " which level ?
let g:php_cs_fixer_config = "default"             " configuration
"let g:php_cs_fixer_config_file = '.php_cs'       " configuration file
let g:php_cs_fixer_php_path = "php"               " Path to PHP
" If you want to define specific fixers:
"let g:php_cs_fixer_fixers_list = "linefeed,short_tag,indentation"
let g:php_cs_fixer_enable_default_mapping = 1     " Enable the mapping by default (<leader>pcd)
let g:php_cs_fixer_dry_run = 1                    " Call command with dry-run option
let g:php_cs_fixer_verbose = 1                    " Return the output of command if 1, else an inline information.

修正後のfixerの動作

f:id:takaaki_z:20160706224121p:plain

このようにdry-runを実行したときに具体的な変更箇所(どこをどう変更するのか)を表示してくれるようになります。そしてこの変更でOKであればそのままコードをfixさせます。


注意点

なおこの方法では、diffの結果の表示色は全てオレンジとなってしまいます。ターミナル上でdiffの結果を表示させた時は変更前が赤、変更後は緑、みたいな感じで表示されているので、それに合わせに行きたいところですが、目的は達成されたので一旦ここまでとします。