-9

When you copy your code and want to post it on SO, it's usually indented relative to outer brackets/tags. When you post it and click the Code Sample, it leaves it like it is and you have to trim the spaces yourself.

Personally I don't find it as that big of a deal when I post. But there are a lot of posts by users, usually newbies that can't handle it and as a result keep it indented.

What I mean, code like:

   {
       printf("hello world");
   }

Should automatically transform to:

{
    printf("hello world");
}
8
  • 1
    Select the code and press the code block button { } on the editor it will remove indents with successive presses until it reaches the edge then indents by four.
    – user692942
    Commented Mar 16, 2016 at 11:15
  • 2
    Whitespace isn't necessarily meaningless. Removing it can change the meaning of the code in some languages. Sound easier to just have the user format the code rather than risk changing code through an automated system Commented Mar 16, 2016 at 11:16
  • @psubsee2003 Is there any purpose in any language to start all of your lines of codes with spaces?
    – Alex Weitz
    Commented Mar 16, 2016 at 11:17
  • 1
    @Alex in indentation based languages (such as Python), yes... Commented Mar 16, 2016 at 11:25
  • 2
    @JonClements Indentation only matters when it's relative to a statement or a method. It doesn't make any sense to post code to SO where all of your lines are indented.
    – Alex Weitz
    Commented Mar 16, 2016 at 11:29
  • And then you split the code into multiple small blocks interleaved with commentary, so leaving the indentation completely as-is is critical. Commented Mar 16, 2016 at 11:46
  • I personally remove the whitespace before the first bracket and then the formatting works fine after that. Commented Mar 16, 2016 at 11:55
  • 1
    @AlexWeitz Another valid use case is ASCII art/diagrams. Commented Mar 16, 2016 at 12:14

1 Answer 1

3

You can already do this with the built in editor tools.

Selecting the code and pressing the { } button will (if already indented) remove indents with each successive press until the left edge is reached. Then pressing again will result in the correct four space code block indent.


Update: Does actually work the problem is the example is an awkward one (three spaces instead of four spaces)

It appears that if the indentation is in four space increments the { } will fix the indents for you, but odd sizes like three maintain the white-space between indents.

If we have (indented by four spaces)

    {
        printf("hello world");
    }

Select block and press { } once

{
    printf("hello world");
}

press { } again

    {
        printf("hello world");
    }

If we have a odd number of spaces used for indentation this happens and the { } doesn't remove the odd white-space it just keeps it and indents accordingly.

If we have (indented by three spaces)

       {
            printf("hello world");
       }

Select block and press { } once

   {
        printf("hello world");
   }

press { } again

       {
            printf("hello world");
       }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .