Ben Usman
Ben Usman

Reputation: 8387

How to autoindent code that looks fine on github, but horribly indented in the editor?

I need to edit a piece of code that looks like this both in Sublime and PyCharm (a small portion of a large source file):

enter image description here

I am not sure what kind of modern code editor (and innovative methodology calling for mixing tabs and spaces within a single line) authors were using, but somehow their indentation looks reasonably in on github (so I guess all team members were using Atom?) - I checked and the page source code still involves \t symbols.

enter image description here

The python -m py_compile command seems happy with this code as is, so I tried replacing tabs with 8 spaces, still looks horribly. Any attempt to replace these with 'fixed number' of spaces did not work either, including 5 spaces that is presumably the number or &nbsp browser is supposed to use). The file does not have CRLF endings, I checked.

I tried using a formatter that uses Python 2.7 AST internally, it says that indentation is wrong, so still no luck.

My attempt to find some logic behind the number of tabs and supposed number of spaces lead to following observations:

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))

Upvotes: 1

Views: 56

Answers (1)

Ben Usman
Ben Usman

Reputation: 8387

Oh, I have figured the solution! I set tab width to 8 spaces in sublime and then it works as expected, i.e. instead of replacing each \t with 8 spaces it 'tabs' them up to 8 spaces!

In linux one can do the same thing via expand -t 8 <filename>.

enter image description here

Upvotes: 1

Related Questions