Line break should not happen after/before math if there is no whitespace

I don’t understand your questions. Look at the screenshot: it’s quite clear. The ) is on a line on its own. It should not be.

To reproduce it, type some math preceded by a space and followed by non-whitespace characters, e.g. (word $x$). Now keep adding characters to the beginning of the line to make the line longer, until it wraps. You will see the ) jumping onto a new line on its own, as in my screenshot.