From b169a604900785685ea2779ae1ba2063e8f1f2bc Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 6 Jan 2021 12:48:45 +0100 Subject: [PATCH] [jOOQ/jOOQ#11194] Fixed edge cases --- .../java/org/jooq/codegen/GeneratorWriter.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java index 9e48e4fdca..54508bfc2a 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java @@ -267,12 +267,16 @@ public abstract class GeneratorWriter> { final int newlineStringLength = newlineString.length(); int lineLength = indentLength; + // [#11194] Edge case when the first word of a line is longer than the print margin + boolean whitespaceEncountered = false; + stringLoop: for (int i = 0; i < stringLength; i++) { if (peek(string, i, newlineString)) { sb.append(newlineString).append(indent); lineLength = indentLength; i += newlineStringLength - 1; + whitespaceEncountered = false; } // [#9728] TODO A more sophisticated way to handle Javadoc tags would be interesting. @@ -288,11 +292,15 @@ public abstract class GeneratorWriter> { for (int j = 0; (p = i + j) < stringLength; j++) { final boolean end = p + 1 >= stringLength; + final boolean whitespace = Character.isWhitespace(string.charAt(p)); + whitespaceEncountered |= whitespace; - if (j > 0 && Character.isWhitespace(string.charAt(p)) || end) { - if (printMarginForBlockComment > 0 && (lineLength += j) > printMarginForBlockComment) { + if (j > 0 && whitespace || end) { + lineLength += (end ? j + 1 : j); + + if (printMarginForBlockComment > 0 && lineLength > printMarginForBlockComment && (!end || whitespaceEncountered)) { sb.append(newlineString).append(indent); - lineLength = indentLength + j; + lineLength = indentLength + j - 1; i++; } @@ -532,6 +540,10 @@ public abstract class GeneratorWriter> { return clazzOrId == null ? Collections.emptyList() : clazzOrId; } + public String content() { + return sb.toString(); + } + @Override public String toString() { return "GenerationWriter [" + file + "]";