From 1a691af5543b758a890fbb4ca98234b3de8a1b67 Mon Sep 17 00:00:00 2001 From: st1020 Date: Thu, 6 Nov 2025 22:03:31 +0800 Subject: [PATCH] feat: run string reflowing in IntelliJ plugin --- .../GoogleJavaFormatFormattingService.java | 2 ++ ...GoogleJavaFormatFormattingServiceTest.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java index 9d2d7a595..33bf70107 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java @@ -23,6 +23,7 @@ import com.google.googlejavaformat.java.FormatterException; import com.google.googlejavaformat.java.JavaFormatterOptions; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; +import com.google.googlejavaformat.java.StringWrapper; import com.intellij.formatting.service.AsyncDocumentFormattingService; import com.intellij.formatting.service.AsyncFormattingRequest; import com.intellij.ide.highlighter.JavaFileType; @@ -101,6 +102,7 @@ private GoogleJavaFormatFormattingTask(Formatter formatter, AsyncFormattingReque public void run() { try { String formattedText = formatter.formatSource(request.getDocumentText(), toRanges(request)); + formattedText = StringWrapper.wrap(formattedText, formatter); request.onTextReady(formattedText); } catch (FormatterException e) { request.onError( diff --git a/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java b/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java index fec086c68..cb9c4eeb4 100644 --- a/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java +++ b/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java @@ -23,6 +23,7 @@ import com.google.googlejavaformat.java.Formatter; import com.google.googlejavaformat.java.JavaFormatterOptions; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; +import com.google.googlejavaformat.java.StringWrapper; import com.intellij.formatting.service.AsyncFormattingRequest; import com.intellij.formatting.service.FormattingService; import com.intellij.formatting.service.FormattingServiceUtil; @@ -213,6 +214,25 @@ public void canChangeNonWhitespaceReformatsJavadoc() throws Exception { assertThat(delegatingFormatter.wasInvoked()).isTrue(); } + @Test + public void stringWrapper() throws Exception { + settings.setStyle(Style.GOOGLE); + PsiFile file = + createPsiFile( + "com/foo/FormatTest.java", + "class T {", + " String s = \"foo \" + \"Some Very Long Text, foo bar foo bar foo bar foo bar foo bar" + + " foo bar foo bar foo bar foo bar\";", + "}"); + String origText = file.getText(); + CodeStyleManager manager = CodeStyleManager.getInstance(file.getProject()); + WriteCommandAction.runWriteCommandAction( + file.getProject(), () -> manager.reformatText(file, 0, file.getTextLength())); + + assertThat(file.getText()).isEqualTo(StringWrapper.wrap(origText, new Formatter()) + "\n"); + assertThat(delegatingFormatter.wasInvoked()).isTrue(); + } + private PsiFile createPsiFile(String path, String... contents) throws IOException { VirtualFile virtualFile = fixture.getTempDirFixture().createFile(path, String.join("\n", contents));