Last active
January 1, 2016 22:29
-
-
Save boonebgorges/8210459 to your computer and use it in GitHub Desktop.
.vimrc command for WordPress-style whitespace correction in current buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command! WPCodingStandards call WPCodingStandards() | |
function! WPCodingStandards() | |
" Space after opening paren | |
execute '%s/(\([^\t) ]\)/( \1/ge' | |
" Space before closing paren | |
execute '%s/\([^\t( ]\))/\1 )/ge' | |
" No spaces when casting types | |
execute '%s/(\s*\(array\|int\|object\|string\|float\)\s*)/(\1)/ge' | |
" Tabs not spaces | |
execute '%s/^ \([^\t ]\)/\t\1/ge' | |
execute '%s/^ \([^\t ]\)/\t\t\1/ge' | |
execute '%s/^ \([^\t ]\)/\t\t\t\1/ge' | |
execute '%s/^ \([^\t ]\)/\t\t\t\t\1/ge' | |
execute '%s/^ \([^\t ]\)/\t\t\t\t\t\1/ge' | |
execute '%s/^ \([^\t ]\)/\t\t\t\t\t\t\1/ge' | |
execute '%s/^ \([^\t ]\)/\t\t\t\t\t\t\t\1/ge' | |
" control structures | |
execute '%s/\(while\|foreach\|if\|else\|for\|do\|try\)(/\1 (/ge' | |
execute '%s/}else/} else/ge' | |
execute '%s/else{/else {/ge' | |
" brackets | |
execute '%s/){/) {/ge' | |
" commas should have trailing spaces in argument lists | |
execute '%s/,\$/, $/ge' | |
endfunction | |
noremap <F8> :WPCodingStandards<cr> |
Update: improved handling for ()
fixes that were canceling out other spaces
Hi. This is really useful, but the spaces inside braces breaks regular expressions; e.g. something like:
return preg_replace( '/-([0-9]*)x([0-9]*)\.(jpg|jpeg|png|gif|webp)$/', ".$3", $url );
becomes:
return preg_replace( '/-( [0-9]* )x( [0-9]* )\.( jpg|jpeg|png|gif|webp )$/', ".$3", $url );
and fails to match. Any ideas for how to leave these alone?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: better handling for leading tabs vs spaces; no spaces when type casting (eg
(int) $foo
)