44

It would be great to add some "tools" to code blocks. For example:

  • Select all
  • Copy to clipboard
  • Expand
  • Collapse

code tools

I also think sometimes a code block is just too big and it would be nice to display it "collapsed" in a div with just 150px height. For example, you post a SSCCE so that people can copy and paste it. There is no need to display a 600px height div because you just want people to copy the code, not to read it. But, thanks to the mentioned tools, people can still expand the block if they want.

9
  • I think the copy-to-clipboard feature has been suggested before and rejected :)
    – Jack
    Commented Feb 26, 2013 at 12:41
  • 1
    This feature would be crazy useful for jQuery/html questions where you quite often want to copy the parts of a post and create a jsfiddle to try out/demo some answers.
    – Jamiec
    Commented Feb 26, 2013 at 12:45
  • 5
    Yes, the copy button has been discussed before, but seems it is not rejected: meta.stackexchange.com/questions/32625/…. Don't know about the other tools Commented Feb 26, 2013 at 12:49
  • I don't see that a copy button would be useful; my left hand sits easily over Ctrl-C and its friends. But something to quickly expand a code block to skip the scroll bar, then shrink it to just a few lines so I can see the text underneath, would be quite handy.
    – ghoti
    Commented Feb 26, 2013 at 13:03
  • 1
    The copy to clipboard is not so easy, and is probably bring more bugs/problems than the use of it. The select all can do the work.
    – Aristos
    Commented Feb 26, 2013 at 13:55
  • 1
    @Rory I've voted to close that as a duplicate of this, since this question seems to better articulate the request.
    – yoozer8
    Commented Feb 26, 2013 at 21:25
  • 1
    this collapse feature will become very usefull and not that difficult to make: jsfiddle.net/xCpBH/1/embedded/result,html,css,js
    – Wouter J
    Commented Mar 1, 2013 at 11:55
  • I've been frustrated with this too, built a solution for myself and turned it into a Chrome extension to share. Hope you'll find it useful. See my answer below. Commented Jan 11, 2016 at 10:49

2 Answers 2

1

What about something along the lines of how LaTeX-Community.org allow users to open code in writeLaTeX (an online collaborative LaTeX editor/compiler)? For details and examples of how it works, see the announcement:

http://latex-community.org/forum/viewtopic.php?f=37&t=22340

This type of integration has been proposed for TeX SX, and with the recent integration of CircuitLabs into Electronics SX, it seems like there are some precedents (and models) for how this could work.

(I originally posted this as an answer in this thread, but feels it might fit better here)

2
  • 1
    I should add that I'm one of the developers at writeLaTeX, so happy to discuss how we could work on an integration along these lines for SX Commented Mar 7, 2013 at 0:17
  • That's a great feature, but on SO we have a lot of different languages so it won't be easy. A "copy all" button is much easier. Commented Mar 7, 2013 at 8:53
0

This is better solved by an add-on script like this one: Select Code Block Buttons Script.

That app currently only supports 'select all', but could easily be expanded to include the other features you requested.

That said, here's why I think this shouldn't be implemented for everyone:

  • Including too much code
    The ideal SO post is a good explanation of the question/answer with a minimal code snippet to illustrate it. Adding extra support for huge walls of text suggests to users that lots of code should be included. Of course, in the real world it's sometimes necessary to include significant amounts of code, but we don't want to encourage that.
  • Cut and paste instead of learning
    Ideally people come away from SO with a new understanding of whatever they had a question about, but there's definitely a risk that they just come away with some code that solves their problem but that they don't really understand. There are already repositories for useful snippets of code out on the web, we don't want to inadvertently turn SO into one of those.

For all that, I think that the strongest argument in favor of such a feature is better enabling SSCCEs in questions. Lots of modern software doesn't lend itself to a SSCCE because even 'Hello World' requires multiple files in a particular folder hierarchy. Anything that makes it easier for answerers to quickly test questioners' code to try and help is good. However, if you're going to be firing up a small project to check out a problem, you really want an online emulator like jsfiddle or a public repo/bundle with everything already organized correctly instead of having to cut and paste a bunch of text, even if it is easy to select.

3
  • 1
    encourage? I don't think reducing the max-height of code blocks will encourage posting significant amounts of code. It is just a CSS modification so that questions/answers that have a lot of code don't take all the screen. In my opinion SSCCE's are not expected to be read (you can read them in Eclipse for example) so they should be a little bit hidden. But if you still want to read them in the page, a "expand" button won't hurt. Commented Mar 7, 2013 at 8:50
  • @TheBronx Collapse/expand buttons probably wouldn't encourage huge code dumps as much as select all/copy, but if the code isn't actually meant to be read on SO then the best solution would still seem to be including it in zip file or similar that can get it into Eclipse in the least painful way possible.
    – blahdiblah
    Commented Mar 7, 2013 at 17:24
  • uploading a zip is too much. I'm thinking of questions where you have to post a complete Java class for example, or even two. Or add a XML file. Showing the code in the question is ok as it limits the problem to a few files/classes and allows readers to imagine the "magnitude" of the problem while a zip file is more opaque. But showing the code inside big divs is not smart, it is hard to see the whole question or copy the code. Commented Mar 8, 2013 at 10:46

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