代码拉取完成,页面将自动刷新
在 Vim 编辑器上集成 PHP CS Fixer
支持在目录或文件上执行 PHP CS Fixer 命令
Note: 插件使用
vim9script
脚本语言实现,需要vim9.0
以上版本
在 .vimrc
上你可以这样配置:
vim9script
# php-cs-fixer 文件路径(必须)
# 官网下载: https://cs.symfony.com/
g:phpCsFixerPath = expand('~/php/php-cs-fixer-v3.phar')
# 应用规则,内容跟 PHP CS Fixer 的 --rules 选项保持一致
# 常见规则组 @PSR2,@PSR12,@PER-CS,@Symfony,@PhpCsFixer
# 默认: '@PSR12'
g:phpCsFixerRules = '@PhpCsFixer'
# php解释器路径(默认: 'php')
# php解释器所在目录加入环境变量后,无需设置此项
# g:phpCsFixerPhpPath = expand('~/php')
# --dry-run 选项(默认: false)
# g:phpCsFixerIsDryRun = true
# --verbose 选项(默认: false)
# g:phpCsFixerIsVerbose = true
# PHP_CS_FIXER_IGNORE_ENV 是否启用环境忽略
# 在不兼容的php版本或缺少扩展的情况下,请启用
# g:phpCsFixerIgnoreEnv = true
# 重新映射
nnoremap <unique><silent><Leader>f <Plug>PhpCsFixerFixFile;
nnoremap <unique><silent><Leader>d <Plug>PhpCsFixerFixDir;
PHP Path
'php'
g:phpCsFixerPhpPath = '/usr/local/bin/php'
php-cs-fixer 路径
''
g:phpCsFixerPath = 'expadn(~/php-cs-fixer.phar')
下载 PHP CS Fixer
--dry-run
--dry-run
false
g:phpCsFixerIsDryRun = true
--verbose
--verbose
false
g:phpCsFixerIsVerbose = true
--rules
--rules
'$PSR12'
g:phpCsFixerRules = '@PhpCsFixer'
常见规则组:
@PSR2
,@PSR12
,@PER-CS
,@Symfony
,@PhpCsFixer
PHP_CS_FIXER_IGNORE_ENV
false
g:phpCsFixerIgnoreEnv = true
g:PhpCsFixerFixFile()
g:PhpCsFixerFixDir()
单文件修复映射
<leader>pcf
filetype
必须是 php假如
phpCsFixerFixCacheDir
设置路径异常,单文件修复会失败
目录修复映射
<leader>pcd
重新映射
这是一个简单的重新映射示例:
nnoremap <unique><silent><Leader>f <Plug>PhpCsFixerFixFile;
nnoremap <unique><silent><Leader>d <Plug>PhpCsFixerFixDir;
更好的映射,如果你有多个代码格式化工具,可以参考下面这种方式:
vim9script
def g:RunCodeFormat()
# prettier 支持的文件类型有很多,这里你可以自行增减文件类型
var prettierSupportTypes = [
'javascript',
'typescript',
'json',
'markdown',
'css',
'html'
]
var currentFiletype = &filetype
if currentFiletype == 'php'
execute 'call PhpCsFixerFixFile()'
elseif prettierSupportTypes->index(currentFiletype) != -1
execute 'Prettier'
endif
enddef
nnoremap <silent><Leader>f :call g:RunCodeFormat()<CR>
如果您使用的是 Vim 版本 8 或更高版本,您可以使用其内置的包管理;请参阅 :help packages
以获取更多信息。在你的终端上运行这些命令:
git clone https://github.com/linjialiang/vim-php-cs-fixer.git <vimfilesPath>/pack/store/start/vim-php-cs-fixer
vim -u NONE -c "helptags <vimfilesPath>/pack/store/start/vim-php-cs-fixer/doc" -c q
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。