Fix handling of long words

Yang Zhang created

Change summary

util/text/text.go      | 11 +++++++++--
util/text/text_test.go |  8 ++++++++
2 files changed, 17 insertions(+), 2 deletions(-)

Detailed changes

util/text/text.go 🔗

@@ -89,9 +89,16 @@ func softwrapLine(line string, textWidth int) string {
 				width = 0
 			}
 		} else if wl > textWidth {
-			left, right := splitWord(chunks[len(chunks)-1], textWidth)
-			line2 += left + "\n"
+			// NOTE: By default, long words are splited to fill the remaining space.
+			// But if the long words is the first non-space word in the middle of the
+			// line, preceeding spaces shall not be counted in word spliting.
+			splitWidth := textWidth - width
+			if strings.HasSuffix(line2, "\n"+strings.Repeat(" ", width)) {
+				splitWidth += width
+			}
+			left, right := splitWord(chunks[len(chunks)-1], splitWidth)
 			chunks[len(chunks)-1] = right
+			line2 += left + "\n"
 			width = 0
 		} else {
 			line2 += "\n"

util/text/text_test.go 🔗

@@ -157,6 +157,14 @@ func TestWrapLeftPadded(t *testing.T) {
     蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`,
 			59, 4,
 		},
+		// Handle long unbreakable words in a full stentence
+		{
+			"OT: there are alternatives to maintainer-/user-set priority, e.g. \"[user pain](http://www.lostgarden.com/2008/05/improving-bug-triage-with-user-pain.html)\".",
+			`    OT: there are alternatives to maintainer-/user-set
+    priority, e.g. "[user pain](http://www.lostgarden.com/
+    2008/05/improving-bug-triage-with-user-pain.html)".`,
+			58, 4,
+		},
 	}
 
 	for i, tc := range cases {