折叠失败:vim找不到任何折叠


9

我正在尝试折叠一些PHP代码,并且在文章结尾处包含了一些我想折叠的代码的简化版本。

我尝试了以下vim命令,但仅以下ex命令有效。non-ex命令不起作用,并在状态行中产生一条红色的白色消息:我找不到任何折叠。

:help folding
:help fold-commands
:help foldmethod

:set foldmethod=syntax

zi - toggle folding

zj - move to top of next fold
zk - move to bottom of previous fold

za - toggle current fold open and closed
zo - open current fold
zc - close current fold

zA - toggle all current folds at the current cursor position
zO - open all current folds at the current cursor position
zC - close all current folds at the current cursor position

如何获取vim来查找折叠(例如,我想在{}之间或在(和之间折叠代码)

这是一些示例代码(只是为了证明它在语法上是正确的,因此vim命令应该可以工作):

function getTree() {

  return array(
    "node1" => array(
      "node11" => array(
        "node111" => "leaf111",
        "node112" => "leaf112",
        "node113" => "leaf113",
      ),
          "node12" => array(
        "node121" => "leaf121",
        "node122" => "leaf122",
        "node123" => "leaf123",
      ),
      "node13" => array(
        "node131" => "leaf131",
        "node132" => "leaf132",
        "node133" => "leaf133",
      ),
    ),
    "node2" => array(
      "node21" => array(
        "node211" => "leaf211",
        "node212" => "leaf212",
        "node213" => "leaf213",
      ),
          "node22" => array(
        "node221" => "leaf221",
        "node222" => "leaf222",
        "node223" => "leaf223",
      ),
      "node23" => array(
        "node231" => "leaf231",
        "node232" => "leaf232",
        "node233" => "leaf233",
      ),
    ),
    "node3" => array(
      "node31" => array(
        "node311" => "leaf311",
        "node312" => "leaf312",
        "node313" => "leaf313",
      ),
          "node32" => array(
        "node321" => "leaf321",
        "node322" => "leaf322",
        "node323" => "leaf323",
      ),
      "node33" => array(
        "node331" => "leaf331",
        "node332" => "leaf332",
        "node333" => "leaf333",
      ),
    ),
  );

}

Answers:


9

Vim没有内置PHP语法折叠功能。但是,如果所有代码都正确缩进(如您的示例所示),则可以使用其他折叠方法:

:set foldmethod=indent

5

phpfolding.vim提供了这一点。这样做的好处:set foldmethod=indent是它“更智能”,因为它查看的是实际的PHP语法,而不仅仅是缩进。从自述文件:

  • 它记住折页设置。如果添加功能并再次执行脚本,打开的折叠将不会关闭。
  • 不会与注释块或字符串文字中的方括号相混淆。
  • 用其PhpDoc注释折叠类属性。
  • 将所有类别的属性折叠为一折。
  • 折叠原始标记样式也会折叠。
  • 折叠后的“ **”后缀表示PhpDoc位于内部(可配置)。
  • 折叠后的“ **#@ +”后缀表示PhpDocBlock位于内部(可配置)。
  • 可以将折叠后的空行配置为包含在折叠中。
  • 支持嵌套折叠(函数内部的函数等)
  • 折叠私有,公共,受保护的类变量+多行参数。
  • 现在,默认情况下,禁用类是一个选项。

通过下载.vim脚本并将其放入来安装它~/.vim/ftplugin/php/(或使用插件管理器,如果使用的话)。它将自动折叠,您可以使用禁用它let g:DisableAutoPHPFolding = 1


我会尝试这个插件。我确信它提供的额外功能将很有用,因为有时在编写代码时会出现语法错误。谢谢。
约翰·桑德森
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.