我一直在使用自定义快捷方式在这篇文章中提到的 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/