如何自动在github上看起来很好的代码,但在编辑器中可怕地缩进?

问题描述 投票:0回答:1

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

enter image description here

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

enter image description here

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个回答
0
投票

哦,我已经找到了解决方案!我将标签宽度设置为sublime中的8个空格然后它按预期工作,即不是用8个空格替换每个\t,而是“标记”它们最多8个空格!

在linux中,可以通过expand -t 8 <filename>做同样的事情。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.