From d86e930de93f123994fba151a8d289b8035db87b Mon Sep 17 00:00:00 2001 From: Zakaria Elkatani Date: Fri, 4 Feb 2022 14:23:19 -0800 Subject: [PATCH] Make some best-practices changes to the plugin. Since the last plugin update was almost half a year ago, I thought it would be prudent to update it and resolve most of the issues regarding updating the plugin. It took around half an hour once I managed to correctly set up the module due to the gradle plugin version 1.2.1 + intellij version 2020.3 requiring java 11 to develop. All of the included annotation changes were requested by IntelliJ to add since the overrides added them as well. The change of `` to `` was to resolve the syntax errors introduced by a change removing the wildcard requirement. NotificationGroup was deprecated and moved into the plugin xml file as per the SDK docs The same was required for `StdFileTypes.JAVA` being deprecated with the fix being `JavaFileType.INSTANCE` + changing the depend module to add that fix. I had to change the project SDK to JDK 11 on my end to have the gradle build work. I also tested the plugin with 1.12 and 1.10 on some test files such as from #653 and #654. Would be good to have some double checking though because I've had issues trying to get the test file reformatted in #558 working. Probably because it's locked behind an experimental flag? Fixes #688 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/688 from ze:idea-1.12.0 53f216cd030a553c8200c75606168de0a8039e1a PiperOrigin-RevId: 426491735 --- idea_plugin/.gitignore | 5 ++ idea_plugin/build.gradle | 24 ++++---- idea_plugin/resources/META-INF/plugin.xml | 24 +++++--- .../intellij/CodeStyleManagerDecorator.java | 57 +++++++++++-------- .../intellij/FormatterUtil.java | 3 +- .../GoogleJavaFormatCodeStyleManager.java | 24 ++++---- .../intellij/GoogleJavaFormatSettings.java | 7 ++- ...alConfigurationProjectManagerListener.java | 5 +- 8 files changed, 87 insertions(+), 62 deletions(-) create mode 100644 idea_plugin/.gitignore diff --git a/idea_plugin/.gitignore b/idea_plugin/.gitignore new file mode 100644 index 000000000..16bc65a53 --- /dev/null +++ b/idea_plugin/.gitignore @@ -0,0 +1,5 @@ +build +.gradle +gradle +gradlew +gradlew.bat \ No newline at end of file diff --git a/idea_plugin/build.gradle b/idea_plugin/build.gradle index 36c12f13f..d9f769d47 100644 --- a/idea_plugin/build.gradle +++ b/idea_plugin/build.gradle @@ -15,7 +15,7 @@ */ plugins { - id "org.jetbrains.intellij" version "1.3.0" + id "org.jetbrains.intellij" version "1.3.1" } repositories { @@ -23,23 +23,27 @@ repositories { } ext { - googleJavaFormatVersion = '1.13.0' + googleJavaFormatVersion = "1.13.0" } -apply plugin: 'org.jetbrains.intellij' -apply plugin: 'java' +apply plugin: "org.jetbrains.intellij" +apply plugin: "java" + +sourceCompatibility = JavaVersion.VERSION_11 +targetCompatibility = JavaVersion.VERSION_11 intellij { pluginName = "google-java-format" - version = "IC-213.5744.18-EAP-SNAPSHOT" + plugins = ["java"] + version = "221.3427-EAP-CANDIDATE-SNAPSHOT" } patchPluginXml { pluginDescription = "Formats source code using the google-java-format tool. This version of " + "the plugin uses version ${googleJavaFormatVersion} of the tool." - version = "${googleJavaFormatVersion}.0" - sinceBuild = '201' - untilBuild = '' + version.set("${googleJavaFormatVersion}.0") + sinceBuild = "203" + untilBuild = "" } publishPlugin { @@ -48,8 +52,8 @@ publishPlugin { sourceSets { main { - java.srcDir 'src' - resources.srcDir 'resources' + java.srcDir "src" + resources.srcDir "resources" } } diff --git a/idea_plugin/resources/META-INF/plugin.xml b/idea_plugin/resources/META-INF/plugin.xml index 60705902a..f10cde5a3 100644 --- a/idea_plugin/resources/META-INF/plugin.xml +++ b/idea_plugin/resources/META-INF/plugin.xml @@ -14,7 +14,8 @@ limitations under the License. --> - + google-java-format google-java-format @@ -24,7 +25,7 @@ - com.intellij.modules.lang + com.intellij.java @@ -58,17 +59,22 @@ ]]> - + + topic="com.intellij.openapi.project.ProjectManagerListener"/> - - + + + diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java b/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java index fc335ad63..af5da957a 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java @@ -34,10 +34,11 @@ import com.intellij.util.ThrowableRunnable; import java.util.Collection; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; /** * Decorates the {@link CodeStyleManager} abstract class by delegating to a concrete implementation - * instance (likely IJ's default instance). + * instance (likely IntelliJ's default instance). */ @SuppressWarnings("deprecation") class CodeStyleManagerDecorator extends CodeStyleManager @@ -54,98 +55,102 @@ CodeStyleManager getDelegate() { } @Override - public Project getProject() { + public @NotNull Project getProject() { return delegate.getProject(); } @Override - public PsiElement reformat(PsiElement element) throws IncorrectOperationException { + public @NotNull PsiElement reformat(@NotNull PsiElement element) + throws IncorrectOperationException { return delegate.reformat(element); } @Override - public PsiElement reformat(PsiElement element, boolean canChangeWhiteSpacesOnly) + public @NotNull PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { return delegate.reformat(element, canChangeWhiteSpacesOnly); } @Override - public PsiElement reformatRange(PsiElement element, int startOffset, int endOffset) + public PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset) throws IncorrectOperationException { return delegate.reformatRange(element, startOffset, endOffset); } @Override public PsiElement reformatRange( - PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly) + @NotNull PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { return delegate.reformatRange(element, startOffset, endOffset, canChangeWhiteSpacesOnly); } @Override - public void reformatText(PsiFile file, int startOffset, int endOffset) + public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException { delegate.reformatText(file, startOffset, endOffset); } @Override - public void reformatText(PsiFile file, Collection ranges) + public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { delegate.reformatText(file, ranges); } @Override - public void reformatTextWithContext(PsiFile psiFile, ChangedRangesInfo changedRangesInfo) + public void reformatTextWithContext( + @NotNull PsiFile psiFile, @NotNull ChangedRangesInfo changedRangesInfo) throws IncorrectOperationException { delegate.reformatTextWithContext(psiFile, changedRangesInfo); } @Override - public void reformatTextWithContext(PsiFile file, Collection ranges) + public void reformatTextWithContext( + @NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { delegate.reformatTextWithContext(file, ranges); } @Override - public void adjustLineIndent(PsiFile file, TextRange rangeToAdjust) + public void adjustLineIndent(@NotNull PsiFile file, TextRange rangeToAdjust) throws IncorrectOperationException { delegate.adjustLineIndent(file, rangeToAdjust); } @Override - public int adjustLineIndent(PsiFile file, int offset) throws IncorrectOperationException { + public int adjustLineIndent(@NotNull PsiFile file, int offset) + throws IncorrectOperationException { return delegate.adjustLineIndent(file, offset); } @Override - public int adjustLineIndent(Document document, int offset) { + public int adjustLineIndent(@NotNull Document document, int offset) { return delegate.adjustLineIndent(document, offset); } - public void scheduleIndentAdjustment(Document document, int offset) { + public void scheduleIndentAdjustment(@NotNull Document document, int offset) { delegate.scheduleIndentAdjustment(document, offset); } @Override - public boolean isLineToBeIndented(PsiFile file, int offset) { + public boolean isLineToBeIndented(@NotNull PsiFile file, int offset) { return delegate.isLineToBeIndented(file, offset); } @Override @Nullable - public String getLineIndent(PsiFile file, int offset) { + public String getLineIndent(@NotNull PsiFile file, int offset) { return delegate.getLineIndent(file, offset); } @Override @Nullable - public String getLineIndent(PsiFile file, int offset, FormattingMode mode) { + public String getLineIndent(@NotNull PsiFile file, int offset, FormattingMode mode) { return delegate.getLineIndent(file, offset, mode); } @Override @Nullable - public String getLineIndent(Document document, int offset) { + public String getLineIndent(@NotNull Document document, int offset) { return delegate.getLineIndent(document, offset); } @@ -165,7 +170,7 @@ public Indent zeroIndent() { } @Override - public void reformatNewlyAddedElement(ASTNode block, ASTNode addedElement) + public void reformatNewlyAddedElement(@NotNull ASTNode block, @NotNull ASTNode addedElement) throws IncorrectOperationException { delegate.reformatNewlyAddedElement(block, addedElement); } @@ -192,22 +197,23 @@ public T performActionWithFormatterDisabled(Computable r) { } @Override - public int getSpacing(PsiFile file, int offset) { + public int getSpacing(@NotNull PsiFile file, int offset) { return delegate.getSpacing(file, offset); } @Override - public int getMinLineFeeds(PsiFile file, int offset) { + public int getMinLineFeeds(@NotNull PsiFile file, int offset) { return delegate.getMinLineFeeds(file, offset); } @Override - public void runWithDocCommentFormattingDisabled(PsiFile file, Runnable runnable) { + public void runWithDocCommentFormattingDisabled( + @NotNull PsiFile file, @NotNull Runnable runnable) { delegate.runWithDocCommentFormattingDisabled(file, runnable); } @Override - public DocCommentSettings getDocCommentSettings(PsiFile file) { + public @NotNull DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) { return delegate.getDocCommentSettings(file); } @@ -223,7 +229,8 @@ public FormattingMode getCurrentFormattingMode() { } @Override - public int adjustLineIndent(final Document document, final int offset, FormattingMode mode) + public int adjustLineIndent( + final @NotNull Document document, final int offset, FormattingMode mode) throws IncorrectOperationException { if (delegate instanceof FormattingModeAwareIndentAdjuster) { return ((FormattingModeAwareIndentAdjuster) delegate) @@ -233,7 +240,7 @@ public int adjustLineIndent(final Document document, final int offset, Formattin } @Override - public void scheduleReformatWhenSettingsComputed(PsiFile file) { + public void scheduleReformatWhenSettingsComputed(@NotNull PsiFile file) { delegate.scheduleReformatWhenSettingsComputed(file); } } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java b/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java index 9939bbade..a5e69c93d 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java @@ -50,8 +50,7 @@ static Map getReplacements( } private static Collection> toRanges(Collection textRanges) { - return textRanges - .stream() + return textRanges.stream() .map(textRange -> Range.closedOpen(textRange.getStartOffset(), textRange.getEndOffset())) .collect(Collectors.toList()); } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java index 550d0a93c..3d56743ee 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java @@ -22,10 +22,10 @@ import com.google.googlejavaformat.java.Formatter; import com.google.googlejavaformat.java.JavaFormatterOptions; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; +import com.intellij.ide.highlighter.JavaFileType; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; -import com.intellij.openapi.fileTypes.StdFileTypes; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; @@ -44,7 +44,7 @@ /** * A {@link CodeStyleManager} implementation which formats .java files with google-java-format. - * Formatting of all other types of files is delegated to IJ's default implementation. + * Formatting of all other types of files is delegated to IntelliJ's default implementation. */ class GoogleJavaFormatCodeStyleManager extends CodeStyleManagerDecorator { @@ -53,7 +53,7 @@ public GoogleJavaFormatCodeStyleManager(@NotNull CodeStyleManager original) { } @Override - public void reformatText(PsiFile file, int startOffset, int endOffset) + public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException { if (overrideFormatterForFile(file)) { formatInternal(file, ImmutableList.of(new TextRange(startOffset, endOffset))); @@ -63,7 +63,7 @@ public void reformatText(PsiFile file, int startOffset, int endOffset) } @Override - public void reformatText(PsiFile file, Collection ranges) + public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { if (overrideFormatterForFile(file)) { formatInternal(file, ranges); @@ -73,7 +73,7 @@ public void reformatText(PsiFile file, Collection ranges) } @Override - public void reformatTextWithContext(PsiFile file, ChangedRangesInfo info) + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull ChangedRangesInfo info) throws IncorrectOperationException { List ranges = new ArrayList<>(); if (info.insertedRanges != null) { @@ -84,7 +84,8 @@ public void reformatTextWithContext(PsiFile file, ChangedRangesInfo info) } @Override - public void reformatTextWithContext(PsiFile file, Collection ranges) { + public void reformatTextWithContext( + @NotNull PsiFile file, @NotNull Collection ranges) { if (overrideFormatterForFile(file)) { formatInternal(file, ranges); } else { @@ -94,7 +95,10 @@ public void reformatTextWithContext(PsiFile file, Collection ranges * Format the ranges of the given document. * *

Overriding methods will need to modify the document with the result of the external - * formatter (usually using {@link #performReplacements(Document, Map)}. + * formatter (usually using {@link #performReplacements(Document, Map)}). */ private void format(Document document, Collection ranges) { Style style = GoogleJavaFormatSettings.getInstance(getProject()).getStyle(); diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java index f6d9b5ff1..1e92a4bdd 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java @@ -23,6 +23,7 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; @State( name = "GoogleJavaFormatSettings", @@ -42,7 +43,7 @@ public State getState() { } @Override - public void loadState(State state) { + public void loadState(@NotNull State state) { this.state = state; } @@ -73,7 +74,7 @@ void setStyle(JavaFormatterOptions.Style style) { enum EnabledState { UNKNOWN, ENABLED, - DISABLED; + DISABLED } static class State { @@ -85,7 +86,7 @@ static class State { public void setEnabled(@Nullable String enabledStr) { if (enabledStr == null) { enabled = EnabledState.UNKNOWN; - } else if (Boolean.valueOf(enabledStr)) { + } else if (Boolean.parseBoolean(enabledStr)) { enabled = EnabledState.ENABLED; } else { enabled = EnabledState.DISABLED; diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java b/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java index da02310c7..1906347f7 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/InitialConfigurationProjectManagerListener.java @@ -17,8 +17,8 @@ package com.google.googlejavaformat.intellij; import com.intellij.notification.Notification; -import com.intellij.notification.NotificationDisplayType; import com.intellij.notification.NotificationGroup; +import com.intellij.notification.NotificationGroupManager; import com.intellij.notification.NotificationType; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManagerListener; @@ -28,11 +28,10 @@ final class InitialConfigurationProjectManagerListener implements ProjectManager private static final String NOTIFICATION_TITLE = "Enable google-java-format"; private static final NotificationGroup NOTIFICATION_GROUP = - new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true); + NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_TITLE); @Override public void projectOpened(@NotNull Project project) { - GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); if (settings.isUninitialized()) {