What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 131 Stack Exchange communities.

Quite often I see code formatted like this in questions:

   foo
   bar
   hello

If the code consists of sufficiently long lines, it ends up looking like this:

                                                                 foobasdsdlfkasdlfksdfoobasdsdlfkasdlfksd

It'd be nice if there was a way (an icon to click or a magic keyboard incantation perhaps) to correct those two code blocks to:

foo
bar
hello

and

foobasdsdlfkasdlfksdfoobasdsdlfkasdlfksd

respectively.

Presumably this could work by looking at the lowest number of spaces x in the selected block, and then reducing the number of initial spaces on each line by x-4.

Obviously, there are a small minority of languages where spaces are meaningful that this might change the meaning of, but in most cases I think this would be useful.

I'm aware that I can cut and paste the code into my editor of choice to do this, but it'd be nice if there was some built-in way to do this.

share|improve this question
    
This is similar (though IMHO) different to meta.stackexchange.com/questions/8144/… –  Dominic Rodger Jan 14 '10 at 12:59

3 Answers 3

It's open source! You can add that feature yourself!

http://code.google.com/p/wmd-new/

share|improve this answer
2  
I wish I could, but I'm nowhere near smart enough to figure out what's going on in that JavaScript (github.com/cky/wmd/blob/master/wmd-base.js). –  Dominic Rodger Jan 14 '10 at 13:11
    
@Dominic: The doCode function is at github.com/derobins/wmd/blob/master/wmd.js line #2106 –  David Schmitt Feb 11 '10 at 9:19

You can use Ctrl+K repeatedly to delete all except the last N % 4 spaces.

share|improve this answer
    
Not in the version of WMD that Meta is currently using - is that the latest one? –  Dominic Rodger Feb 11 '10 at 9:23
    
@Dominic: It Works-For-Me[tm] on SO and here. I'm using Chrome. –  David Schmitt Feb 16 '10 at 11:58
    
Ah - I see what it does. Doesn't quite do what I want, because it doesn't end up as just 4 spaces - as you point out, so I'd still have to edit each line. –  Dominic Rodger Feb 16 '10 at 12:23
    
@Dominic: after looking at the WMD source, I came around to see that one to three spaces in front of the source are acceptable :-) –  David Schmitt Feb 18 '10 at 12:03

There's two big issues with code formatting that leads to side-scrollitis:

  1. excessive intendation
  2. excessively long lines

#1 can be solved by removing excessive indentation or by converting 8-space tabs to 4 spaces.
#2 can be solved by inserting more line breaks, e.g. turning

MyFunction(reallyreallylongarg1, reallyreallylongarg2, reallyreallylongarg3, reallyreallylongarg4, reallyreallylongarg5, reallyreallylongarg6);

to:

MyFunction(reallyreallylongarg1, reallyreallylongarg2,
    reallyreallylongarg3, reallyreallylongarg4,
    reallyreallylongarg5, reallyreallylongarg6);

#1 could possibly be done programmatically by the site, but #2 depends on the syntax of the language and is best done by a human who can decide the best reformatting based on context. But we've got lots of users with editing privileges; does this really need to be addressed programmatically?

tl;dr: This is what 2000+ reputation users are for! If you see badly formatted code, step in and clean it up! All hail the Junior Janitor!

share|improve this answer
2  
Yes there are lots of users with editing privileges (on SO I'm one of them), and I for one want to fix code that's hard to read - a good deal of which fits into #1, and is tedious to fix by hand. A programmatic fix would make it easier to be a good SO citizen, that's all I'm saying. –  Dominic Rodger Jan 14 '10 at 18:47

You must log in to answer this question.

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