9

Whenever I click the { } button, then paste my code in, I sometimes get just one line of code, but I never get my full code snippet enclosed in the proper format.

Is this because my code is indented? How do I fix this?

After pressing the {} button After pasting my code

7
  • 12
    It's not really a fix, but more a use suggestion: Paste your code first, highlight all of it, then click the {} button. It works a lot easier that way. That said, could you post an example block of code that this happens with?
    – Kendra
    Commented Feb 17, 2016 at 18:45
  • @Kendra yeah, done. Commented Feb 17, 2016 at 18:47
  • 1
    Or just use the Stack Snippet option instead...
    – Paulie_D
    Commented Feb 17, 2016 at 18:49
  • 1
    Yeah, I could. Just wanted to raise this because this looks like a bug. Commented Feb 17, 2016 at 18:54
  • It's not actually a bug, just unfortunate. There is - or was - a debate whether to implement a code-editor which should be opened when clicking the button and not having code to indent/dedent selected. Commented Feb 17, 2016 at 19:02
  • Yeah, none of the formatting options work like that. (I just tried it with the quote option.) It just doesn't make any formatting assumptions about any of the lines pasted after the one you you added the code formatting (or blockquote, bold, italics) to.
    – BSMP
    Commented Feb 17, 2016 at 19:08
  • 4

1 Answer 1

7

Not a bug, intended behavior. Formatting code IMO is beyond the scope of the StackOverflow editor. It allows you to perform automate basic Markdown functions. If you were writing Markdown by hand, you would have to indent the codeblock "manually" (in other words, using a text editor). Theoretically they could perform the indenting behavior you describe, but I believe indenting a block of code should be an explicit action performed by the user (CTRL+K). If the editor did it, it would be a case of Stop Helping Me! Don't get me started on tabs versus spaces, how buggy the implementation may be, and other subtle things that would discourage SE from implementing it

Now a Stack Snippet is not appropriate in all cases. As the name implies, it should only be used when you intend to have a runnable snippet. If the result of rendering your code is not important, don't use it. It'll just take up space.

5
  • 4
    Specifically, a Stack Snippet is only appropriate when using runnable HTML/CSS/Javascript snippets.
    – Kendra
    Commented Feb 17, 2016 at 19:12
  • 1
    The name implies nothing except a snippet. Runnable is a bonus IMO. If I had a question that would presentationally have a wall of code (required) then a Snippet is a reasonable option.
    – Paulie_D
    Commented Feb 17, 2016 at 19:14
  • 5
    @Paulie_D You must've missed the memo. There's a difference between "Code Sample" and "Stack Snippet" (and the terminology is ambiguous, but the intended usage has been made clear). Unfortunately this ambiguity has led to people abusing the function. While putting non-runnable HTML code in a Stack Snippet is harmless, it just adds noise. Commented Feb 17, 2016 at 19:33
  • 2
    Yeah, I'm getting awfully tired of seeing Stack Snippets used in C and C++ questions.
    – Cody Gray Mod
    Commented Feb 18, 2016 at 10:56
  • 1
    I hit the "code snippet button". I see "Insert code here". I paste. The code is outside of the code snippet. This seems unintentional.. to fix, just cause pastes into the [code snippet] text to be wrapped appropriately. I'm not asking for code styling or anything, just that the {} button behave as expected for pasting. Commented Feb 20, 2016 at 0:20

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