52

It would be nice to be able to put emphasis on specific bits of a fragment or of a quote (bold can help for a quote but I find that it makes things less readable sometimes).

What do you think?

Update: On http://www.pastebin.com/, you can highlight particular lines by prefixing each line with @@.

See also:

3
  • 3
    Really nice idea. I think it may tempt n00bs to abuse it when posting huge chunks of code ("Hey, I highlighted the relevant part!!!"), but that's easily dealt with.
    – Jon Seigel
    Commented Jun 20, 2010 at 16:45
  • 4
    @Jon As long as they format their code, I'd accept to deal with that :) Commented Jun 20, 2010 at 16:57
  • 2
    Very nice indeed, but please not with that unreadable color-combination :) Commented Jun 21, 2010 at 0:23

3 Answers 3

25

I like the idea.

For highlighting a whole line, one could extend the markdown syntax and define that an asterisk before the four spaces required to highlight a code block highlights that line:

    for ($i = 0; $i <= 1; $i++)
     {  
*      print "$i lines!";  // this line gets highlighted
     }

this would work without having to work out a character or character sequence that turns on highlighting within a block.

3
  • 1
    First suggestion I've seen that would work. No opposed. Commented Jun 20, 2010 at 18:34
  • 2
    Not that i think its very important, but could we mark the first line that way too the way SOs markdown works? Commented Jun 23, 2010 at 20:49
  • @Georg no, but arguably the first line is easy to point out in the text body.
    – Pekka
    Commented Nov 3, 2011 at 19:06
4

What about just breaking up the code block?


You need to use the print function:

function foobar()
{
  for ($i = 0; $i <= 1; $i++)
  { 
    print "$i lines!";
  }
}
5
  • I'd probably leave out the explanatory text though. The break in the code block would be enough to notice. Writing the explanation that way is harder to read, especially in a larger block of code. You could still leave the explanatory native comment within the block though. Commented Nov 3, 2011 at 20:49
  • @Jeff: I like it. See my revised answer Commented Nov 3, 2011 at 20:56
  • I'd leave out the horizontal lines though. You can get away with leaving those out. I'll edit your answer if you don't mind just to show what I mean. Rollback or improve if you wish. Commented Nov 3, 2011 at 20:58
  • @JeffMercado Even better, thanks! I couldn't easily figure out how to do that, which is why I was using the <hr>s Commented Nov 3, 2011 at 20:59
  • 1
    It's a work-around. The only thing it highlights is that you can't highlight a line in a code block
    – Bohemian
    Commented Feb 14, 2014 at 23:37
1

Meh.

IMHO, if a code block is so large that a simple comment like this:

blah.blah.blah(arg, arg, arg);
class.class.method(arg, arg, arg + 1, weird_arg, blah);  // <-- something strange happens here
blah.blah.blah(arg, arg, arg);

...doesn't stand out on its own, then chances are the code block is including too much and the example should be pared down a bit.

7
  • Well, using comments is what I currently do but 1. you sometimes needs the whole context of a piece of code, 2. comments might not get spotted and 3. I find it limiting, especially when dealing with XML blocks. Not to mention that comments doesn't work with quotes. Commented Jun 20, 2010 at 17:03
  • @Ether I disagree. It is often necessary to post a full block of code to make a diagnosis, and being able to highlight the line the compiler/interpreter chokes at would be a good thing.
    – Pekka
    Commented Jun 20, 2010 at 17:41
  • 4
    @Down: Pare it down: if you can't pare it down you're only guessing about where the trouble is in any case. Commented Jun 20, 2010 at 18:37
  • 3
    @dmckee I disagree, especially newbies who get a "error x on line 188" often need to show a whole block to make sense, but it would be great to know where line 188 is in all that, which is where the highlighting comes in. But +1 for teaching me a new english word.
    – Pekka
    Commented Jun 20, 2010 at 20:53
  • 3
    @Down: Maybe it's the teacher in me, but I see that as an opportunity to teach the new programmers that vital debugging skill of isolating the offending code. Commented Jun 20, 2010 at 23:18
  • 1
    @dmckee: As long as SO is open to beginners there will always be people who can't identify the relevant parts properly. Even non-beginners may have that problem - e.g. plain COM programming carries so much visual noise that its hard to trim or reduce to a few central lines. gtk+ using C is also quite line-hungry. Commented Jun 21, 2010 at 0:27
  • It's not about the size of the block, it's about highlighting that part that matters.
    – Bohemian
    Commented Feb 14, 2014 at 23:38

You must log in to answer this question.

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