是否可以让 vim 阻止保存有解析错误的 php 文件?

发布于 2024-11-07 21:15:26 字数 253 浏览 0 评论 0原文

我使用 vim 并希望它能够阻止我保存有解析错误的 php 文件。如果我想使用例如“php -l <​​file>”为了实现这一点,.vimrc 中的 autocmd 会是什么样子?

我知道我可以用类似的东西挂接到 BufWritePre

autocmd FileType php autocmd BufWritePre * !php -l %

,但我希望它在 php 命令失败时中止“写入”。

I use vim and would like it to prevent me from saving php files that has parse errors. If I would like to use for instance "php -l <file>" to achieve this, how would the autocmd in .vimrc look like?

I know I can hook into BufWritePre with something like

autocmd FileType php autocmd BufWritePre * !php -l %

but I want it to abort the "write" if the php command fails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-11-14 21:15:26

你可以在钩子中抛出一个未捕获的异常,它会中止 write: try

function s:RunPHP()
    execute '!php -l' shellescape(@%, 1)
    if v:shell_error | throw 'PHP failed' | endif
endfunction

function s:DefineBWPphpevent()
    augroup Run_php
        autocmd! BufWritePre <buffer> :call s:RunPHP()
    augroup END
endfunction

augroup Run_php
    autocmd!
    autocmd FileType  *  :autocmd! Run_php BufWritePre <buffer>
    autocmd FileType php :call s:DefineBWPphpevent()
augroup END

另请注意

  1. shellescape: 你不应该使用 :!shell_command % 因为这里 vim 没有进行正确的转义,如果文件名包含特殊符号,如空格、引号、美元、换行符...
  2. 代替第二个模式中的 *。您的原始命令没有为 php 文件定义自动命令,而是为所有文件类型定义自动命令(如果文件类型之一是 php)。
  3. autocmd 分组和 banged :autocmd!s:它们可以防止重复的自动命令。

顺便说一句,你想通过在旧版本的文件上运行 php 来实现什么(它是 BufWritePre 事件,所以它发生在文件写入之前)?也许你应该用 :w !php -l <​​/code>、:w !php -l - 甚至 :w !php -l /dev 替换运行 php 的代码/stdin 取决于 php 接受的内容(我的系统上没有并且不想有)。

You can throw an uncaught exception in the hook and it will abort write: try

function s:RunPHP()
    execute '!php -l' shellescape(@%, 1)
    if v:shell_error | throw 'PHP failed' | endif
endfunction

function s:DefineBWPphpevent()
    augroup Run_php
        autocmd! BufWritePre <buffer> :call s:RunPHP()
    augroup END
endfunction

augroup Run_php
    autocmd!
    autocmd FileType  *  :autocmd! Run_php BufWritePre <buffer>
    autocmd FileType php :call s:DefineBWPphpevent()
augroup END

Also note

  1. shellescape: you should not use :!shell_command % as here vim does not do proper escaping and you will run into troubles if file name contains special symbols like spaces, quotes, dollar, newlines, ...
  2. <buffer> in place of * in second pattern. Your original command does not define autocommand for php files, it instead defines autocommand for all filetypes if one of the filetypes is php.
  3. autocmd grouping and banged :autocmd!s: they prevent duplicating autocommands.

By the way, what do you want to achieve by running php on old version of file (it is BufWritePre event, so it happens before file is written)? Maybe you should replace code that runs php with :w !php -l, :w !php -l - or even :w !php -l /dev/stdin depending on what php accepts (I do not have it on my system and do not want to have).

你的往事 2024-11-14 21:15:26

但我希望它在 php 命令失败时中止“写入”。

据我所知,VIM 不可能做到这一点。文档也没有说明这种功能,所以我很确定,如果不进行破解,这是无法完成的。问题是你想要实现什么目标?我有一个脚本,可以确保在提交版本控制之前(而不是保存文件时)在 PHP 中没有解析错误。这是一个简单的预提交挂钩,也许可以工作?

如果您确实对保存文件之前的 linting 感兴趣,您可以将“ZZ”和“:w”重写为您自己编写的命令,这反过来会对文件进行 lint(如果是 PHP 文件)并没有错误时保存。我不确定您是否可以重新映射 :w,但它可能会导致比它解决的问题更多的问题。

也许更简单的解决方案是在您自己选择的击键(例如 F5)上创建一个命令,并使其执行您想要的操作?

but I want it to abort the "write" if the php command fails.

As far as I know, this isn't possible with VIM. Documentation doesn't state this capability either, so I'm pretty sure that can't be done, without a hack. The question is though what you're trying to accomplish? I have a script that makes sure I have no parse errors in PHP before I commit to version control, instead of when I save the file. That's a simple pre-commit hook, perhaps that something that'll work?

If you're really interested in linting before you save the file, you might be able to rewrite "ZZ" and ":w" to a command you wrote yourself, that in turns lints the file (in case of a PHP file) and saves upon no errors. I'm not sure if you can remap the :w, and it might cause more issues than it solves though.

Perhaps a simpler solution would be to create a command on a keystroke you choose yourself (e.g. F5), and make that do what you want?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文