php - PHP 文件的 sublime 缩进不正确

标签 php editor sublimetext2 sublimetext3 indentation

我一直在使用自定义快捷方式在这篇文章中提到的 Sublime Text 中缩进 Indent multiple lines in Sublime Text

但是如果有奇数个空格就不行了。就像我的制表符大小设置为 2 个空格,当任何行的开头有 3 个空格时,它不会缩进剩余的代码。

例如:

<?php

function test_indent() {
  if (condition) {
   echo "here";
  }
   else {
    echo "else";
   }
}

当我使用上面帖子中指定的自定义快捷方式缩进时,它是:

{ "keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false} }

对我来说,结果如下:

function test_indent() {
  if (condition) {
   echo "here";
 }
 else {
  echo "else";
}
}

我需要做什么才能使其正确缩进?

最佳答案

在尝试这个过程时,我发现如果您首先完全unindent 选择,那么reindent 将按预期工作。这似乎是 reindent 命令中的一个错误,当缩进不是所设置的“制表符大小”的倍数时会发生。

因此,我们可以通过更改您的快捷方式以完全取消缩进选择然后再次重新缩进来解决此错误。

Tools 菜单中,选择 Developer -> New Plugin...

选择所有内容并将其替换为以下内容:

import sublime
import sublime_plugin
import re

class UnindentAndReindentCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        while any([sel for sel in self.view.sel() if re.search('^[ \t]+', self.view.substr(sel.cover(self.view.line(sel.begin()))), re.MULTILINE)]):
            self.view.run_command('unindent')
        self.view.run_command('reindent', { 'single_line': False })

将其保存在 ST 建议的位置,如 fix_reindent.py - 文件扩展名很重要,但基本名称不重要。

然后,更改您的键绑定(bind)以使用我们刚刚创建的新 unindent_and_reindent 命令:

{ "keys": ["ctrl+shift+f"], "command": "unindent_and_reindent" }

现在它会正确地重新缩进您的代码。

关于php - PHP 文件的 sublime 缩进不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42510367/

相关文章:

node.js - 如何使用 Sublime Text 运行 Node 应用程序

c - Sublime Text 2 : Having trouble making an OpenGL sublime-build

php - MySQL - 带有变量的 SELECT 查询不会执行

java - 如何在 swt 树编辑器中更改标签的颜色

javascript - 如何在绘制圆圈的循环结束时为圆圈设置动画 p5.js

eclipse - 如何在eclipse IDE中查看Java的字节码?

css - twitter bootstrap 版本 3.0 rc1 NameError : . make-column is undefined

php - Laravel 部署……有标准的方式吗?

php - 上传图片时在 null 上调用成员函数 storeAs()

php - 使用 LIKE 检索行并传递一些 id。