71

It is common when writing Haskell questions to write language extensions beginning with - as code. The editor seems to think this is a hyphenated word that it can split between two lines, even though that's not really reasonable with this code. Adding spaces inside or outside the code environment doesn't adjust the alignment in this case.

I think the editor should treat all "words" (contiguous non-whitespace characters) in code as atomic. The editor should treat hyphens in code just like any other character.

enter image description here

9
  • 5
    This question isn't about super-long strings, it's about wrapping at hypens in code. The point is, if I remove the -, the editor doesn't write XDataKinds as X on one line and DataKinds on the other.
    – crockeea
    Commented Mar 20, 2016 at 15:44
  • 4
    Would be nice if inline code spans just never wrapped at all. But I don't know if that would cause multiline code blocks incorrectly wrapped in backticks to break the layout...
    – BoltClock Mod
    Commented Mar 20, 2016 at 16:34
  • 8
    @BoltClock that would be an incentive to not (ab)use backticks.
    – Braiam
    Commented Mar 20, 2016 at 19:24
  • 3
    Perhaps treating leading hyphens as nonbreaking, and allowing breaks at embedded hyphens, would be a good compromise? Commented Mar 21, 2016 at 10:44
  • 1
    What's wrong with placing a hard break in yourself? (in the example above add newline before -XDataKinds).
    – user692942
    Commented Mar 21, 2016 at 10:47
  • 3
    @Braiam: Sure it would be an incentive not to start abusing them, but my point is that already existing abuses would break pages left and right as a result of this change (since it's a CSS change, not a Markdown postprocessing change), and users not in the know will inevitably get confused until the affected posts are fixed.
    – BoltClock Mod
    Commented Mar 21, 2016 at 18:25
  • @BoltClock I doubt it... since SO stores the HTML on the database, and this change needs (?) a new class.
    – Braiam
    Commented Mar 22, 2016 at 2:54
  • @Braiam: Why would it need a new class? It's literally just a white-space: nowrap on code elements (if we're talking about my suggestion).
    – BoltClock Mod
    Commented Mar 22, 2016 at 4:04
  • @BoltClock if memory doesn't fails me, the html of every post is already generated and stored within the database. If this go forwards, generating the same html adding a class and modifying the css to target that class...
    – Braiam
    Commented Mar 23, 2016 at 13:26

3 Answers 3

21

I'd agree that wrapping at dashes probably doesn't make sense in code, but wrapping in general is desirable, for example:

If you want to store a constant (such as π) in Java, you should declare it public static final float MY_CONSTANT_NAME.

The type for an iterator over a const vector​<my_item_type> in C++ is vector​<my_item_type>​::​const_iterator, but these days you'd be better off using auto than typing all that. (note that I added zero-width spaces to explicitly allow line wrapping here)

Unfortunately, those requirements make what you're asking for non-trivial to implement (see https://stackoverflow.com/a/8755071/1180785).


So what can you do right now?

The unicode character Non-Breaking-Hyphen (\u2011) prevents this behaviour (in fact this is its reason for being). It's not copy+paste friendly since it wouldn't work as code, but for just showing a token name in a description (as seen in your example) it would be fine:

Here's a long line which will (hopefully, and depending on your device) -wraparoundtothenextlineinthisblock (using standard dash -)

Here's a long line which will (hopefully, and depending on your device) ‑wraparoundtothenextlineinthisblock (using \u2011 )

4
  • 11
    Eerily close to the minus conundrum, so, while I agree it is typographically correct (in the sense that a regular hyphen indicates "breaking here is okay" and this one does not), one question: what if you copy and paste this into source code?
    – Jongware
    Commented Mar 20, 2016 at 21:25
  • 2
    I kind of feel that your first example should have been a code block anyway. It seems a bit much for inline code.
    – canon
    Commented Mar 20, 2016 at 22:35
  • 3
    "Depending on your device" indeed. In Chrome, the first line wraps after the hyphen and second wraps before it, as expected. In Firefox, both lines wrap before the hyphen--in fact, it seems to have a policy of never wrapping before the fifth character, which seems like a nice compromise to me. In Edge, the first line wraps before the hyphen, and the second wraps before "device)". o_0
    – Alan Moore
    Commented Mar 22, 2016 at 3:56
  • @RadLexus well at least fixing it is not as much of a nightmare as it would be to fix code that has been mimic'd Commented Mar 22, 2016 at 11:43
9

I would rather see inline code not wrap at all when possible. If an inline code block doesn't fit on the current line, it should be moved to the start of the next line.

That said, this is still problematic because inline code could still be too long for one line. For a well formatted post, these should be in a separate code block; then they would be scrollable. But there's no way to enforce that. So what can we do?

I considered suggesting that it should split only on whitespace. That would certainly help, but there's no reason that such a long code block even has to contain whitespace. What would we do in those cases? Any kind of splitting scheme based on the characters involved would run into this same problem: what do we do when an inline code block does not contain a character qualified for splitting?

There's also the notion that we should just make these code blocks scrollable when they pass the end of the line, but this seems to largely defeat the entire purpose of having inline code blocks. Inline code blocks are supposed to be short blocks that you want to flow with the rest of your text. Making an inline block scrollable would be jarring and ugly at best.

We shouldn't try to work around this with weird unicode characters, either. This would make it impossible to copy/paste portions of the code. Note that this isn't the same as "blindly copying and running code off the internet." You might copy/paste a single argument like in the example, for instance. If you make it a "non-breaking hyphen," my bet is that bash or your command would choke on it.

So it seems that we're always going to have weird cases where this can happen. I honestly don't think we can do any better than

  1. If an inline code block preceded by non-code text is too long for the current line, move it to the start of the next one.
  2. If it's already at the start of the line and it's still too long, split it as we already do. This will be ugly and may split in places you don't want, but that's what you get for abusing inline code blocks with that kind of length.

In the mean time, a work around would be to insert a line break (<br>) prior to the code block. This is very annoying, though. Substantial changes to your answer could make the manual line break cause a split to the line in a very weird place, essentially making your attempts to make your answer look nice backfire.

(I've expanded my answer to clarify. In retrospect, I think my original second paragraph seemed too dismissive of the issue. I understand this is an issue, but trying to find a good solution is fiddly at best.)

3
  • I've deleted a pile of comments that were meta even for meta! Comments about comments about reasons about blah about nothing that was this post!
    – Marc Gravell Mod
    Commented Mar 23, 2016 at 13:44
  • @MarcGravell I think this post was over-moderated. Many comments were deleted that were relevant to the discussion, and I even [had to lobby] to get some of mine reinstated, which you might be able to see in the history. In particular, I see that my objection to Lankymarts comment is gone, and with it went my objections to @Konamimans answer below. Some of the comments did get out of hand, but I think too much was cut.
    – crockeea
    Commented Jun 13, 2016 at 12:29
  • @Eric Marc deleted the comment chain on this particular answer at my request via custom flag. I did so when a user commented to the effect that none of the comments really added anything to the discussion, and I believed they were correct. (Many of the ones deleted were my own.)
    – jpmc26
    Commented Jun 13, 2016 at 20:38
0

While the suggestion has merit, we are not going to do implement it for now, as it would be too little gain for the amount of work it would take to validate the solution and fix the many edge cases that are likely to arise.

As a workaround you can use the non-breaking hyphen as suggested by Dave, or try to write your code snippet as a full code block instead (although arguably, that's usually not easy without breaking the post layout).

1
  • 2
    I'm disappointed in your decision, as the non-breaking hyphen is a complete non-solution. But thanks for letting me know that this post hadn't been forgotten.
    – crockeea
    Commented Jun 13, 2016 at 12:23

You must log in to answer this question.

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