【问题标题】:Sublime Text 3 Reindent with proper array parenthesis in PHPSublime Text 3 在 PHP 中使用正确的数组括号重新缩进
【发布时间】:2014-03-19 14:49:13
【问题描述】:

我有一个类似that的问题

但上面链接中的答案对我没有帮助。我习惯在编码时重新缩进整个文件,我非常喜欢 Sublime Text。但是这个错误让我很生气。所以我需要用快捷方式重新缩进整个 PHP 文件,并且不要弄错缩进。

这就是我需要的:

它就是这样做的:

【问题讨论】:

  • 也许可以试试Sublime PHP Grammar 它有一些缩进错误修复和改进。它会做你需要的。它还有一个缩进测试套件,欢迎拉取请求和开放问题。

标签: sublimetext2 sublimetext sublimetext3


【解决方案1】:

作为@BullfrogBlues mentions in a comment,Sublime PHP Grammar 插件对此进行了修复。我不想要整个包,但幸运的是很容易从那里提取出数组缩进规则:

https://github.com/gerardroche/sublime-php-grammar/blob/master/Indentation%20Rules%20-%20Arrays.tmPreferences

只需将该文件保存到所有自定义 sn-ps 等所在的同一目录中(在 Mac 上为 ~/Library/Application Support/Sublime Text 3/Packages/User,不确定 Windows/Linux),瞧!

【讨论】:

    【解决方案2】:

    以下解决方案取自SublimeText Forum

    将此添加到您的键绑定中:

    { "keys": ["enter"], "command": "insert_snippet", "args": { "contents": "\n\t$0\n" }, "context":
        [
            { "key": "setting.auto_indent", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "selector", "operator": "equal", "operand": "punctuation.section.array.end.php", "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": "array\\($", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
        ]
    },
    

    【讨论】:

      【解决方案3】:

      我正在使用 Sublime Text 3,可以使用该页面上的说明解决此问题。我在此处复制相同的说明以供参考,我将尝试提供一些提示以找出它不适合您的原因。

      首先,我在我的键绑定设置中添加了这些行(首选项 -> 键绑定 - 用户):

      { "keys": ["enter"], "command": "run_macro_file", "args": {"file": "Packages/User/Add Line in Braces.sublime-macro"}, "context":
        [
          { "key": "setting.auto_indent", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
        ]
      }
      

      这里一定要注意,这个设置文件是一个JSON-Array,上面的代码一定要写在括号里:

      [
        // Copy above configuration here
      ]
      

      如果您的设置文件中已经有一些键绑定,则必须用逗号分隔它们:

      [
        {
          // Some existing keybindings
        },
        // Copy above configuration here
      ]
      

      然后你应该在你的用户文件夹中为 Sublime Text 3 创建一个宏文件。在哪里可以找到这个文件夹取决于你使用的操作系统。例如,在 Ubuntu 上是:

      ~/.config/sublime-text-3/Packages/User

      在这个文件夹下新建一个文件,并命名(注意大小写和空格):

      在 Braces.sublime-macro 中添加行

      在此文件中,复制以下脚本并保存:

      [
          {"command": "insert", "args": {"characters": "\n\n"} },
          {"command": "left_delete", "args": null},
          {"command": "move", "args": {"by": "lines", "forward": false} },
          {"command": "move_to", "args": {"to": "hardeol", "extend": false} },
          {"command": "reindent", "args": {"single_line": true} }
      ]
      

      这一定有效,它对我有用。

      【讨论】:

        猜你喜欢
        • 2015-05-02
        • 2013-10-17
        • 1970-01-01
        • 2014-01-05
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-26
        相关资源
        最近更新 更多