From d4b3f0dd7dafcb863141570f2ac4cb2a9cee0a7b Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 11 Nov 2024 08:22:56 -0800 Subject: [PATCH] Fix an off-by-one bug with column limit handling for text blocks PiperOrigin-RevId: 695348215 --- .../googlejavaformat/java/StringWrapper.java | 3 ++- .../java/testdata/B361077825.input | 15 +++++++++++++++ .../java/testdata/B361077825.output | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index 81ea800da..ab4093a11 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -205,7 +205,8 @@ private void indentTextBlocks( initialLines.get(1).stripTrailing().length() - lines.get(0).stripTrailing().length(); String prefix = - (deindent == 0 || lines.stream().anyMatch(x -> x.length() + startColumn > columnLimit)) + (deindent == 0 + || lines.stream().anyMatch(x -> x.length() + startColumn - 1 > columnLimit)) ? "" : " ".repeat(startColumn - 1); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.input new file mode 100644 index 000000000..c93942a5d --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.input @@ -0,0 +1,15 @@ +class T { + String a = + """ + # No implicit input file, because they can only be created outside a symbolic macro, + """; + + String b = + """ + # No implicit input file, because they can only be created outside a symbolic macro, + """; + String c = + """ + # No implicit input file, because they can only be created outside a symbolic macro, + """; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output new file mode 100644 index 000000000..b407bfe26 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output @@ -0,0 +1,15 @@ +class T { + String a = + """ + # No implicit input file, because they can only be created outside a symbolic macro, + """; + + String b = + """ + # No implicit input file, because they can only be created outside a symbolic macro, + """; + String c = +""" + # No implicit input file, because they can only be created outside a symbolic macro, +"""; +}