21

I cannot recall how many times I have edited questions like this (look at the bottom part in red)..

Code block with hanging close bracket

Down there at the bottom is a } that should obviously have been included in the code block.

I wish code posters would check the preview of their question, notice the problem and fix it, or maybe look at the edits made to their question and take on board the changes. But if wishes were horses, no man would walk.

What I would like to see is a check for a line after the high-lit code that:

  • Is the next line that has characters
  • Is a closing bracket (in whatever the language of the major tag specifies as the code block close bracket)
  • Is the only character on the line (ignoring white-space).

And then on detecting such a condition, either:

  1. Prompt the user 'Did you forget to highlight the last line?', or better still..
  2. Automatically include that line.

Can we get something like this implemented?

Edit

As rene adroitly pointed out in a query for dangling close brackets - there are over 5000 instances of this since the beginning of the year. And that number is counting only the ones that have not already been fixed by editing!

  • 6
    Over 5000 posts since january that have this issue... – rene Mar 21 '15 at 12:29
  • @rene Thanks for the query! I take it that is not counting the ones that have already been fixed? – Andrew Thompson Mar 21 '15 at 12:33
  • 2
    That is correct. – rene Mar 21 '15 at 12:37
  • 3
    I suggest 3. blocking the submission. If the last line is unformatted, chances are the entire code block needs four extra spaces, nott just the last line. – John Dvorak Mar 21 '15 at 14:17
  • In addition to careless posters I suspect a big reason this goes unfixed is because the {} button is a toggle and not progressive. I'm constantly resorting to an external editor just to fix indentation. – candied_orange Apr 5 '15 at 15:26
  • @CandiedOrange " I'm constantly resorting to an external editor just to fix indentation." Umm.. OK. But this really isn't about the general indentation of the code and whether or not to fix that. It is about that final hanging } which the OP probably failed to select before clicking the {} button. If it is not acted on by the code button, it presumably would not be acted on by a 'progressive' pair of buttons. Your answer includes a great image, but perhaps it is better to move it to its own question (image + comment) rather than post it as an answer to this question. – Andrew Thompson Apr 12 '15 at 6:13
  • Very well, I've moved the progressive {} solution here: meta.stackexchange.com/a/253222/273454 – candied_orange Apr 12 '15 at 12:59
  • 1
    "Down there at the bottom" Look at the top too. This is my other favorite. See how the imports and class declaration are on the same indentation level as the class body? That's because whoever did the formatting just indented the top 5 lines. Example of what I mean: stackoverflow.com/review/suggested-edits/11440156. (Go to rendered output.) – Radiodef Apr 13 '16 at 0:09
  • @Radiodef Indeed. That was a sloppy edit. – Andrew Thompson Apr 13 '16 at 0:26
  • 1
    (Cont.) What the example in my previous comment indicates to me is there are people who have figured out that code is formatted by a 4-space indent but don't realize there's a button that does it. What we're seeing with the brace is really the same thing. People don't know how to format code. So I'm just thinking now that maybe it's not explained well enough. – Radiodef Apr 13 '16 at 0:29
  • 1
    @Radiodef I now have a copy/paste comment.. "Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the {} button at the top of the message posting/editing form." It would be nice to see a more official document established that we could point people to. – Andrew Thompson Apr 13 '16 at 0:31
3

I disagree that the hanging } is typically simply forgotten. If this were true the typical fix would be highlight that one line and press {}. It's not. Much more often all the code will need it's indenting fixed on many lines. Rarely it is enough to highlight it all and press {} once. Most typically lines need individual attention. This is what sends me reaching for an external editor.

I think prompting the user when a } is left alone on a line is a great idea.

However automatically adding spaces to the last line is a horrible idea. It would confuse users who didn't expect this change. It would conceal the bigger problem that now has to be fixed with both the user and this auto-correction both taken into account.

It would be nice if a } that is missing leading spaces caused all the code to get it's leading spaces. However, now you need an automatic to way identify all the code. That is not a simple problem.

  • "Much more often all the code will need it's indenting fixed on many lines." Do you have a search that backs that up? Seems much less common to me. – Andrew Thompson Apr 13 '16 at 0:27

You must log in to answer this question.

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