【问题标题】:How to autoindent code that looks fine on github, but horribly indented in the editor?如何自动缩进在 github 上看起来不错,但在编辑器中缩进可怕的代码?
【发布时间】:2019-08-05 15:24:38
【问题描述】:

我需要在 Sublime 和 PyCharm 中编辑一段看起来像这样的代码(大型源文件的一小部分):

我不确定作者使用的是哪种现代代码编辑器(以及要求在一行中混合制表符和空格的创新方法),但不知何故,他们的缩进在 github 上看起来很合理(所以我猜所有团队成员都在使用Atom?) - 我检查了,页面源代码仍然包含\t符号。

python -m py_compile 命令似乎对这段代码很满意,所以我尝试用8 spaces 替换标签,但看起来仍然很糟糕。任何用“固定数量”空格替换这些的尝试也不起作用,包括 5 个可能是数字或 &nbsp 浏览器应该使用的空格)。该文件没有 CRLF 结尾,我检查了。

我尝试使用内部使用 Python 2.7 AST 的formatter,它说缩进是错误的,所以还是没有运气。

我试图找出制表符数量和假定的空格数量背后的一些逻辑导致以下观察结果:

2 spaces + tab + 4 spaces => 12 spaces
sstssss ->
ssssssssssss (12 spaces) => t = 6 spaces
example
        for |<- these pipes should be one on top of the other
        |correct_t

sst ->
ssssssss (8 spaces) => t = 6 spaces
example
        |writer.add_scalar('eval/acc', acc, epoch)
    |if acc >= best_acc:

but

ssstsssss ->
sssssssssssss (13 spaces) => t = 5 spaces

         |best_acc = acc
             |model.save_networks('best'+str(epoch))

【问题讨论】:

    标签: tabs spaces


    【解决方案1】:

    哦,我想出了解决办法!我在 sublime 中将制表符宽度设置为 8 个空格,然后它按预期工作,即不是用 8 个空格替换每个 \t,而是将它们“制表”最多 8 个空格!

    在 linux 中,可以通过expand -t 8 &lt;filename&gt; 做同样的事情。

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多