DEV Community

matsuikazuto
matsuikazuto

Posted on

Neovim and neoformat setting for WordPress Coding Standards

The Coding Standards are important

I'm WordPress Developer.
I am conscious of writing readable code, but since I am a human, I make mistakes.

Let's use formatter. And I want to run phpcbf from Neovim.

Great plugin is neoformat

Below are my settings.

let g:neoformat_php_phpcbf = {
      \ 'exe': 'phpcbf',
      \ 'args': [
      \ '--standard=WordPress',
      \ '--extensions=php',
      \ '%',
      \ '||',
      \ 'true'
      \ ],
      \ 'stdin': 1,
      \ 'no_append': 1
      \ }
let g:neoformat_enabled_php = ['phpcbf']
nnoremap <leader>nf :Neoformat<cr>
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
cdayjr profile image
Chad Wade Day, Jr.

Thanks for sharing this! I found this on Google searching for using phpcbf with
Neoformat and this got me in the right direction getting phpcbf working with
Neoformat with my setup. I ran into some odd issues when having Neoformat run
on save with:

autocmd BufWritePre *.php Neoformat

It did work fine with the <leader>nf shortcut but I prefer Neoformat to run
on save. I looked into it a bit more and it looks like there was
a feature to deal with non-standard error codes made due to how phpcbf handles error codes,
and I was able to adjust your code to take advantage of that and remove the
need for the || true (which I think was causing my problem). In addition, I
don't think the --extensions=php is needed since it's already set as a PHP
formatter, so it shouldn't run on anything else. Here's an update to your code
with the fixes:

let g:neoformat_php_phpcbf = {
      \ 'exe': 'phpcbf',
      \ 'args': [
      \ '--standard=WordPress',
      \ ],
      \ 'stdin': 1,
      \ 'valid_exit_codes': [0,1]
      \ }
let g:neoformat_enabled_php = ['phpcbf']
nnoremap <leader>nf :Neoformat<cr>

My use case is that I use a phpcs.xml file for my rules, so I don't need to
set a standard via a cli argument; and as mentioned before I like to have it
run on save, so my setup is like this:

let g:neoformat_php_phpcbf = {
      \ 'exe': 'phpcbf',
      \ 'stdin': 1,
      \ 'valid_exit_codes': [0,1]
      \ }
let g:neoformat_enabled_php = ['phpcbf']
nnoremap <leader>nf :Neoformat<cr>
autocmd BufWritePre *.php Neoformat

Once again, thanks for sharing, and I hope this helps anyone else coming here via searching for how to use phpcbf with Neoformat like I did.