【问题标题】:Can we set up Intelephense in VS Code as per WordPress coding Standard?我们可以按照 WordPress 编码标准在 VS Code 中设置 Intelephense 吗?
【发布时间】:2020-10-09 17:55:06
【问题描述】:

我在 VS Code 中使用 PHPCS 和 PHPCBF 进行 WordPress 开发。
(按照 https://github.com/tommcfarlin/phpcs-wpcs-vscode 中提到的说明)

虽然 PHPCBF 正在按照 WPCS 格式化代码,但我仍然觉得代码看起来很难看,尤其是当 PHP 文档中存在 Html 标签时。

当我使用 Intelephense 格式化 Php 代码时,代码看起来很舒服,包括正确缩进的 Html 标记。使用 Intelephense 的权衡是代码不再兼容 WPCS。有一个选项可以将 WordPress 作为存根包含在 Intelephense 中,但这仅用于包含 WordPress 特定功能,与格式无关。

使用 Intelephense 进行格式化时面临的一些问题是:

  1. 左括号后和右括号前没有空格,(phpcs)
  2. 文件注释前不能有空行(phpcs)
<?php
// this is a blank line inserted by Intelephense which is causing error no.2 provided in summary above
/**
 * This is a sample file
 *
 * @package something.
 * something something
 * something something
 */

get_header();
if (have_posts()) {
    while (have_posts()) {
// No space is inserted by Intelephense After opening and before Closing parenthesis which is causing error no.1 provided in summary above
        the_post(); ?>
        <h2>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a>
        </h2>
        <?php the_content(); ?>
        <hr>
<?php
    }
}
get_footer();
?>

【问题讨论】:

    标签: php wordpress visual-studio-code phpcs intelephense


    【解决方案1】:

    This issue 以“虽然此扩展不使用 phpcbf”的结论结束,所以我不确定您如何专门使用 Intelephense for VS Code 来做到这一点。

    但是,如果您正在寻找一种解决方案,在 VS Code 中使用 phpcbf 作为 php 文件的格式化程序,并且您的 phpcbf 已设置好并且您已安装所有 coding standards,则可以使用 phpcbf from Per Soderlind

    然后,如果您重新加载编辑器并尝试使用 Ctrl+Shift+I(或您的首选方法)格式化 php 文件,则会出现一个弹出窗口,您必须在其中选择首选格式化程序。

    那么你应该选择写着valeryanm.vscode-phpsab的那个

    或者如果没有出现弹出窗口,只需将以下内容添加到您的settings.json

    "[php]": {
            "editor.defaultFormatter": "valeryanm.vscode-phpsab"
        },
    

    现在,无论何时您尝试对其进行格式化,phpcbf 都会在整个文档上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多