diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index c4e9d610b..1ae292f25 100644 --- a/idea_plugin/build.gradle.kts +++ b/idea_plugin/build.gradle.kts @@ -18,7 +18,7 @@ import org.jetbrains.intellij.platform.gradle.TestFrameworkType // https://github.com/JetBrains/intellij-platform-gradle-plugin/releases plugins { - id("org.jetbrains.intellij.platform") version "2.20.0" + id("org.jetbrains.intellij.platform") version "2.20.1" // See https://plugins.jetbrains.com/docs/intellij/using-kotlin.html#bundled-stdlib-versions kotlin("jvm") version "2.4.21" } @@ -31,7 +31,7 @@ repositories { // https://github.com/google/google-java-format/releases val googleJavaFormatVersion = "2.0.0" -val pluginPatchVersion = "1" +val pluginPatchVersion = "2" java { toolchain { languageVersion = JavaLanguageVersion.of(21) } 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 442cfa079..a534f9248 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 @@ -50,7 +50,8 @@ protected FormattingTask createFormattingTask(AsyncFormattingRequest request) { } Style style = GoogleJavaFormatSettings.getInstance(project).getStyle(); - Formatter formatter = createFormatter(style, request.canChangeWhitespaceOnly()); + Formatter formatter = + createFormatter(style, request.canChangeWhitespaceOnly(), isWholeFile(request)); return new GoogleJavaFormatFormattingTask(formatter, request); } @@ -64,14 +65,27 @@ protected String getName() { return "google-java-format"; } - private static Formatter createFormatter(Style style, boolean canChangeWhiteSpaceOnly) { + private static Formatter createFormatter( + Style style, boolean canChangeWhiteSpaceOnly, boolean isWholeFile) { JavaFormatterOptions.Builder optBuilder = JavaFormatterOptions.builder().style(style); if (canChangeWhiteSpaceOnly) { optBuilder.formatJavadoc(false).reorderModifiers(false).cleanup(false); + } else if (!isWholeFile) { + // Disable cleanups during partial formatting so google-java-format does not re-indent + // unselected statements across the rest of the file. + optBuilder.cleanup(false); } return new Formatter(optBuilder.build()); } + private static boolean isWholeFile(AsyncFormattingRequest request) { + List ranges = request.getFormattingRanges(); + return ranges.size() == 1 + && ranges.get(0).getStartOffset() == 0 + // using greater than or equal because ranges are sometimes passed inaccurately + && ranges.get(0).getEndOffset() >= request.getDocumentText().length(); + } + @Override public @NotNull Set getFeatures() { return Set.of(Feature.FORMAT_FRAGMENTS, Feature.OPTIMIZE_IMPORTS); @@ -120,14 +134,6 @@ private static Collection> toRanges(AsyncFormattingRequest reques .collect(ImmutableList.toImmutableList()); } - private static boolean isWholeFile(AsyncFormattingRequest request) { - List ranges = request.getFormattingRanges(); - return ranges.size() == 1 - && ranges.get(0).getStartOffset() == 0 - // using greater than or equal because ranges are sometimes passed inaccurately - && ranges.get(0).getEndOffset() >= request.getDocumentText().length(); - } - @Override public boolean isRunUnderProgress() { return true; diff --git a/idea_plugin/src/main/resources/META-INF/plugin.xml b/idea_plugin/src/main/resources/META-INF/plugin.xml index 8efab4b59..539572464 100644 --- a/idea_plugin/src/main/resources/META-INF/plugin.xml +++ b/idea_plugin/src/main/resources/META-INF/plugin.xml @@ -35,6 +35,8 @@ ]]> +
2.0.0.2
+
Fix partial/selection formatting re-indenting unselected lines in the rest of the file.
2.0.0.1
Add max-line-length configuration support.
Fix NPEs and other errors related to a null style.
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 4a90079e7..98fb753e1 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 @@ -241,6 +241,54 @@ public void canChangeNonWhitespaceReformatsJavadoc() throws Exception { assertThat(delegatingFormatter.wasInvoked()).isTrue(); } + @Test + public void partialFormatDoesNotReindentUnselectedCode() throws Exception { + settings.setStyle(Style.GOOGLE); + PsiFile file = + createPsiFile( + "com/foo/FormatTest.java", + "package com.foo;", + "public class FormatTest {", + " int a = 1;", + " int b=2+3;", + " int c = 4;", + "}"); + int start = file.getText().indexOf("int b=2+3;"); + int end = start + "int b=2+3;".length(); + CodeStyleManager manager = CodeStyleManager.getInstance(file.getProject()); + WriteCommandAction.runWriteCommandAction( + file.getProject(), () -> manager.reformatText(file, start, end)); + + assertThat(file.getText()) + .isEqualTo( + String.join( + "\n", + "package com.foo;", + "public class FormatTest {", + " int a = 1;", + " int b = 2 + 3;", + " int c = 4;", + "}")); + assertThat(delegatingFormatter.wasInvoked()).isTrue(); + } + + @Test + public void wholeFileFormatRunsCleanups() throws Exception { + settings.setStyle(Style.GOOGLE); + PsiFile file = + createPsiFile( + "com/foo/FormatTest.java", + "package com.foo;", + "@Deprecated()", + "public class FormatTest {}"); + CodeStyleManager manager = CodeStyleManager.getInstance(file.getProject()); + WriteCommandAction.runWriteCommandAction( + file.getProject(), () -> manager.reformatText(file, 0, file.getTextLength())); + + assertThat(file.getText()).contains("@Deprecated\n"); + assertThat(delegatingFormatter.wasInvoked()).isTrue(); + } + private PsiFile createPsiFile(String path, String... contents) throws IOException { VirtualFile virtualFile = fixture.getTempDirFixture().createFile(path, String.join("\n", contents));