Sublime 文本编辑器中的 PHP 语法检查 [英] PHP Syntax Check in Sublime Text Editor

查看:55
本文介绍了Sublime 文本编辑器中的 PHP 语法检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Gedit中,我可以在当前文档上添加一个php -l"的外部工具,如果我安装了PHP命令行,它将对文档进行语法检查.有没有办法用 Sublime Text Editor 做到这一点?(请注意,我有一台 Mac 并且安装了 PHP CLI.)

In Gedit, I can add an External Tool of "php -l" on the current document, and if I have PHP command line installed, it will syntax check the document. Is there a way to do this with Sublime Text Editor? (Note, I have a Mac and it has PHP CLI installed.)

我想我必须将代码片段粘贴到 Sublime 插件中,对吗?

I imagine I'll have to paste a code snippet into Sublime plugin, right?

推荐答案

您所指的操作称为linting",并且有许多 Sublime 插件可以对 PHP 文件进行 lint.正如 Len_D 提到PHP Syntax Checker 是其中之一,但我实际上推荐 SublimeLinter 用于 Sublime Text 2.(ST3 有一个不同版本的 SublimeLinter,但它不向后兼容,并且与 ST2 版本的架构完全不同,后者不再受到官方支持.)

The action you are referring to is called "linting" and there are a number of plugins for Sublime that lint PHP files. As mentioned by Len_D, PHP Syntax Checker is one, but I'd actually recommend SublimeLinter for Sublime Text 2 instead. (There is a different version of SublimeLinter for ST3, but it's not backwards-compatible, and has a completely different architecture than the ST2 version, which is no longer officially supported.)

要安装,首先安装包控制(如果您还没有安装),然后重新启动 Sublime.使用 CtrlShiftP 打开命令面板,然后输入 pci 调出Package C控制:I安装包.按 Enter,然后输入 sublimelinter,然后再次按 Enter 进行安装.安装完成后,再次重启 ST2 祝你好运.要配置,首先打开 Sublime Text 2 ->首选项 ->包设置 ->SublimeLinter ->Settings-Default 并复制其全部内容.然后,从同一子菜单中打开 Settings-User 并将内容粘贴到其中.您现在可以关闭 Settings-Default.要正确突出显示语法(以说明注释的位置),请选择 JavaScript ->JSON 来自 Sublime 窗口右下方的选项列表.

To install, first install Package Control if you haven't already, then restart Sublime. Open the Command Palette with CtrlShiftP and type pci to bring up Package Control: Install Package. Hit Enter, then type in sublimelinter, then hit Enter again to install. After installation is complete, restart ST2 again for good luck. To configure, first open Sublime Text 2 -> Preferences -> Package Settings -> SublimeLinter -> Settings-Default and copy its entire contents. Then, open Settings-User from the same sub-menu and paste the contents into it. You can now close Settings-Default. For proper syntax highlighting (to tell where the comments are), select JavaScript -> JSON from the option list in the lower right of the Sublime window.

向下滚动到 "sublimelinter_executable_map" 字典中的第 36 行,并在开始的 { 和结束的 } 大括号之间添加一个空行.通过打开终端并输入 which php,找到系统上 php 可执行文件的完整路径.复制路径并在您刚刚创建的空白行中为 "php" 添加一个条目.例如,如果路径是 /opt/local/bin/php,则整个部分应如下所示:

Scroll down to line 36 in the "sublimelinter_executable_map" dict and add a blank line between the opening { and closing } braces. Find the full path to the php executable on your system by opening Terminal and typing which php. Copy the path and add an entry for "php" on the blank line you just made. For example, if the path is /opt/local/bin/php, the full section should look like this:

"sublimelinter_executable_map":
{
    "php": "/opt/local/bin/php"
},

向下滚动其余 "sublimelinter_*" 选项并修改它们以适合您的偏好.第 108 行之后的选项很可能与您无关,因为它们处理 JavaScript、CSS、Python 等的 linter.但是,如果您想使用 SublimeLinter,请随时通读它们对于其他语言.完成后,保存文件,您应该已经设置好了.SublimeLinter 将根据 "sublimelinter"(第 13 行)和 "sublimelinter_delay" 设置(第 67 行)显示其消息(将值增加到增加停止打字和出现 linter 消息之间的延迟).如果您不想要这种实时"linting,请将 "sublimelinter" 设置为 load-savesave-onlyfalse,取决于您的喜好.我个人觉得实时 linting 相当烦人...

Scroll down through the rest of the "sublimelinter_*" options and modify them to suit your preferences. The options after line 108 are most likely irrelevant for you, as they deal with linters for JavaScript, CSS, Python, etc. However, feel free to read through them in case you'd like to use SublimeLinter for other languages. Once you're done, save the file and you should be all set. SublimeLinter will display its messages according to the "sublimelinter" (line 13) and "sublimelinter_delay" settings (line 67) (increase the value to increase the delay between stopping typing and linter messages appearing). If you don't want this "live" linting, set "sublimelinter" to load-save, save-only, or false, depending on your preferences. I personally find live linting to be rather annoying...

仅此而已.README 中提供了完整的文档.请记住,如果/当您升级到 Sublime Text 3(顺便说一下,我强烈推荐),您需要安装和配置 SublimeLinter3,即将插件完全重写为更加模块化的架构.因此,基本的 SublimeLinter 包必须由特定于语言的 linter 插件补充,例如 SublimeLinter-php.请确保您阅读了完整文档(是的,有很多,但值得)让一切顺利进行.

And that's about it. Full documentation is available in the README. Please keep in mind that if/when you upgrade to Sublime Text 3 (which I highly recommend, by the way), you'll need to install and configure SublimeLinter3, which is a complete re-write of the plugin into a more modular architecture. As such, the base SublimeLinter package must be supplemented by a language-specific linter plugin like SublimeLinter-php. Please ensure you read the full documentation (yes, there's a lot, but it's worth it) to get everything running smoothly.

祝你好运!

这篇关于Sublime 文本编辑器中的 PHP 语法检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆