Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions idea_plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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<TextRange> 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<Feature> getFeatures() {
return Set.of(Feature.FORMAT_FRAGMENTS, Feature.OPTIMIZE_IMPORTS);
Expand Down Expand Up @@ -120,14 +134,6 @@ private static Collection<Range<Integer>> toRanges(AsyncFormattingRequest reques
.collect(ImmutableList.toImmutableList());
}

private static boolean isWholeFile(AsyncFormattingRequest request) {
List<TextRange> 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;
Expand Down
2 changes: 2 additions & 0 deletions idea_plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
]]></description>
<change-notes><![CDATA[
<dl>
<dt>2.0.0.2</dt>
<dd>Fix partial/selection formatting re-indenting unselected lines in the rest of the file.</dd>
<dt>2.0.0.1</dt>
<dd>Add max-line-length configuration support.</dd>
<dd>Fix NPEs and other errors related to a null style.</dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading