やりたいこと
vim上でphp-cs-fixerを実行した時、一度dry−runをかけて変更箇所(どこがどう変更されるのか)を確認してから実際にfixさせたい!
前提
vim上でphp-cs-fixerを実行できる状態まで持っていきます。手順は以下を参考にしてください。
php-cs-fixerのインストール
https://github.com/FriendsOfPHP/PHP-CS-Fixerphp-cs-fixerをvim上で実行させるプラグインのインストール
https://github.com/stephpy/vim-php-cs-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の結果を表示できるようにします。
プラグインの変更
デフォルトでは次のようになっています。
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.
なおこの方法では、diffの結果の表示色は全てオレンジとなってしまいます。ターミナル上でdiffの結果を表示させた時は変更前が赤、変更後は緑、みたいな感じで表示されているので、それに合わせに行きたいところですが、目的は達成されたので一旦ここまでとします。