Skip to content

Commit d86e930

Browse files
zegoogle-java-format Team
authored andcommitted
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 `<? extends TextRange>` to `<TextRange>` 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=#688 from ze:idea-1.12.0 53f216c PiperOrigin-RevId: 426491735
1 parent af804c6 commit d86e930

File tree

8 files changed

+87
-62
lines changed

8 files changed

+87
-62
lines changed

idea_plugin/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
.gradle
3+
gradle
4+
gradlew
5+
gradlew.bat

idea_plugin/build.gradle

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,35 @@
1515
*/
1616

1717
plugins {
18-
id "org.jetbrains.intellij" version "1.3.0"
18+
id "org.jetbrains.intellij" version "1.3.1"
1919
}
2020

2121
repositories {
2222
mavenCentral()
2323
}
2424

2525
ext {
26-
googleJavaFormatVersion = '1.13.0'
26+
googleJavaFormatVersion = "1.13.0"
2727
}
2828

29-
apply plugin: 'org.jetbrains.intellij'
30-
apply plugin: 'java'
29+
apply plugin: "org.jetbrains.intellij"
30+
apply plugin: "java"
31+
32+
sourceCompatibility = JavaVersion.VERSION_11
33+
targetCompatibility = JavaVersion.VERSION_11
3134

3235
intellij {
3336
pluginName = "google-java-format"
34-
version = "IC-213.5744.18-EAP-SNAPSHOT"
37+
plugins = ["java"]
38+
version = "221.3427-EAP-CANDIDATE-SNAPSHOT"
3539
}
3640

3741
patchPluginXml {
3842
pluginDescription = "Formats source code using the google-java-format tool. This version of " +
3943
"the plugin uses version ${googleJavaFormatVersion} of the tool."
40-
version = "${googleJavaFormatVersion}.0"
41-
sinceBuild = '201'
42-
untilBuild = ''
44+
version.set("${googleJavaFormatVersion}.0")
45+
sinceBuild = "203"
46+
untilBuild = ""
4347
}
4448

4549
publishPlugin {
@@ -48,8 +52,8 @@ publishPlugin {
4852

4953
sourceSets {
5054
main {
51-
java.srcDir 'src'
52-
resources.srcDir 'resources'
55+
java.srcDir "src"
56+
resources.srcDir "resources"
5357
}
5458
}
5559

idea_plugin/resources/META-INF/plugin.xml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
limitations under the License.
1515
-->
1616

17-
<idea-plugin url="https://github.com/google/google-java-format/tree/master/idea_plugin" require-restart="true">
17+
<idea-plugin url="https://github.com/google/google-java-format/tree/master/idea_plugin"
18+
require-restart="true">
1819
<id>google-java-format</id>
1920
<name>google-java-format</name>
2021
<vendor url="https://github.com/google/google-java-format">
@@ -24,7 +25,7 @@
2425
<!-- Mark it as available on all JetBrains IDEs. It's really only useful in
2526
IDEA and Android Studio, but there's no way to specify that for some
2627
reason. It won't crash PyCharm or anything, so whatever. -->
27-
<depends>com.intellij.modules.lang</depends>
28+
<depends>com.intellij.java</depends>
2829

2930
<change-notes><![CDATA[
3031
<dl>
@@ -58,17 +59,22 @@
5859
]]></change-notes>
5960

6061
<applicationListeners>
61-
<listener class="com.google.googlejavaformat.intellij.InitialConfigurationProjectManagerListener"
62-
topic="com.intellij.openapi.project.ProjectManagerListener"/>
62+
<listener
63+
class="com.google.googlejavaformat.intellij.InitialConfigurationProjectManagerListener"
64+
topic="com.intellij.openapi.project.ProjectManagerListener"/>
6365
<listener class="com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller"
64-
topic="com.intellij.openapi.project.ProjectManagerListener"/>
66+
topic="com.intellij.openapi.project.ProjectManagerListener"/>
6567
</applicationListeners>
6668

6769
<extensions defaultExtensionNs="com.intellij">
68-
<projectConfigurable instance="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable"
69-
id="google-java-format.settings"
70-
displayName="google-java-format Settings"/>
71-
<projectService serviceImplementation="com.google.googlejavaformat.intellij.GoogleJavaFormatSettings"/>
70+
<projectConfigurable
71+
instance="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable"
72+
id="google-java-format.settings"
73+
displayName="google-java-format Settings"/>
74+
<projectService
75+
serviceImplementation="com.google.googlejavaformat.intellij.GoogleJavaFormatSettings"/>
76+
<notificationGroup displayType="STICKY_BALLOON" id="Enable google-java-format"
77+
isLogByDefault="true"/>
7278
</extensions>
7379

7480
</idea-plugin>

idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
import com.intellij.util.ThrowableRunnable;
3535
import java.util.Collection;
3636
import org.checkerframework.checker.nullness.qual.Nullable;
37+
import org.jetbrains.annotations.NotNull;
3738

3839
/**
3940
* Decorates the {@link CodeStyleManager} abstract class by delegating to a concrete implementation
40-
* instance (likely IJ's default instance).
41+
* instance (likely IntelliJ's default instance).
4142
*/
4243
@SuppressWarnings("deprecation")
4344
class CodeStyleManagerDecorator extends CodeStyleManager
@@ -54,98 +55,102 @@ CodeStyleManager getDelegate() {
5455
}
5556

5657
@Override
57-
public Project getProject() {
58+
public @NotNull Project getProject() {
5859
return delegate.getProject();
5960
}
6061

6162
@Override
62-
public PsiElement reformat(PsiElement element) throws IncorrectOperationException {
63+
public @NotNull PsiElement reformat(@NotNull PsiElement element)
64+
throws IncorrectOperationException {
6365
return delegate.reformat(element);
6466
}
6567

6668
@Override
67-
public PsiElement reformat(PsiElement element, boolean canChangeWhiteSpacesOnly)
69+
public @NotNull PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly)
6870
throws IncorrectOperationException {
6971
return delegate.reformat(element, canChangeWhiteSpacesOnly);
7072
}
7173

7274
@Override
73-
public PsiElement reformatRange(PsiElement element, int startOffset, int endOffset)
75+
public PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset)
7476
throws IncorrectOperationException {
7577
return delegate.reformatRange(element, startOffset, endOffset);
7678
}
7779

7880
@Override
7981
public PsiElement reformatRange(
80-
PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly)
82+
@NotNull PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly)
8183
throws IncorrectOperationException {
8284
return delegate.reformatRange(element, startOffset, endOffset, canChangeWhiteSpacesOnly);
8385
}
8486

8587
@Override
86-
public void reformatText(PsiFile file, int startOffset, int endOffset)
88+
public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset)
8789
throws IncorrectOperationException {
8890
delegate.reformatText(file, startOffset, endOffset);
8991
}
9092

9193
@Override
92-
public void reformatText(PsiFile file, Collection<? extends TextRange> ranges)
94+
public void reformatText(@NotNull PsiFile file, @NotNull Collection<? extends TextRange> ranges)
9395
throws IncorrectOperationException {
9496
delegate.reformatText(file, ranges);
9597
}
9698

9799
@Override
98-
public void reformatTextWithContext(PsiFile psiFile, ChangedRangesInfo changedRangesInfo)
100+
public void reformatTextWithContext(
101+
@NotNull PsiFile psiFile, @NotNull ChangedRangesInfo changedRangesInfo)
99102
throws IncorrectOperationException {
100103
delegate.reformatTextWithContext(psiFile, changedRangesInfo);
101104
}
102105

103106
@Override
104-
public void reformatTextWithContext(PsiFile file, Collection<? extends TextRange> ranges)
107+
public void reformatTextWithContext(
108+
@NotNull PsiFile file, @NotNull Collection<? extends TextRange> ranges)
105109
throws IncorrectOperationException {
106110
delegate.reformatTextWithContext(file, ranges);
107111
}
108112

109113
@Override
110-
public void adjustLineIndent(PsiFile file, TextRange rangeToAdjust)
114+
public void adjustLineIndent(@NotNull PsiFile file, TextRange rangeToAdjust)
111115
throws IncorrectOperationException {
112116
delegate.adjustLineIndent(file, rangeToAdjust);
113117
}
114118

115119
@Override
116-
public int adjustLineIndent(PsiFile file, int offset) throws IncorrectOperationException {
120+
public int adjustLineIndent(@NotNull PsiFile file, int offset)
121+
throws IncorrectOperationException {
117122
return delegate.adjustLineIndent(file, offset);
118123
}
119124

120125
@Override
121-
public int adjustLineIndent(Document document, int offset) {
126+
public int adjustLineIndent(@NotNull Document document, int offset) {
122127
return delegate.adjustLineIndent(document, offset);
123128
}
124129

125-
public void scheduleIndentAdjustment(Document document, int offset) {
130+
public void scheduleIndentAdjustment(@NotNull Document document, int offset) {
126131
delegate.scheduleIndentAdjustment(document, offset);
127132
}
128133

129134
@Override
130-
public boolean isLineToBeIndented(PsiFile file, int offset) {
135+
public boolean isLineToBeIndented(@NotNull PsiFile file, int offset) {
131136
return delegate.isLineToBeIndented(file, offset);
132137
}
133138

134139
@Override
135140
@Nullable
136-
public String getLineIndent(PsiFile file, int offset) {
141+
public String getLineIndent(@NotNull PsiFile file, int offset) {
137142
return delegate.getLineIndent(file, offset);
138143
}
139144

140145
@Override
141146
@Nullable
142-
public String getLineIndent(PsiFile file, int offset, FormattingMode mode) {
147+
public String getLineIndent(@NotNull PsiFile file, int offset, FormattingMode mode) {
143148
return delegate.getLineIndent(file, offset, mode);
144149
}
145150

146151
@Override
147152
@Nullable
148-
public String getLineIndent(Document document, int offset) {
153+
public String getLineIndent(@NotNull Document document, int offset) {
149154
return delegate.getLineIndent(document, offset);
150155
}
151156

@@ -165,7 +170,7 @@ public Indent zeroIndent() {
165170
}
166171

167172
@Override
168-
public void reformatNewlyAddedElement(ASTNode block, ASTNode addedElement)
173+
public void reformatNewlyAddedElement(@NotNull ASTNode block, @NotNull ASTNode addedElement)
169174
throws IncorrectOperationException {
170175
delegate.reformatNewlyAddedElement(block, addedElement);
171176
}
@@ -192,22 +197,23 @@ public <T> T performActionWithFormatterDisabled(Computable<T> r) {
192197
}
193198

194199
@Override
195-
public int getSpacing(PsiFile file, int offset) {
200+
public int getSpacing(@NotNull PsiFile file, int offset) {
196201
return delegate.getSpacing(file, offset);
197202
}
198203

199204
@Override
200-
public int getMinLineFeeds(PsiFile file, int offset) {
205+
public int getMinLineFeeds(@NotNull PsiFile file, int offset) {
201206
return delegate.getMinLineFeeds(file, offset);
202207
}
203208

204209
@Override
205-
public void runWithDocCommentFormattingDisabled(PsiFile file, Runnable runnable) {
210+
public void runWithDocCommentFormattingDisabled(
211+
@NotNull PsiFile file, @NotNull Runnable runnable) {
206212
delegate.runWithDocCommentFormattingDisabled(file, runnable);
207213
}
208214

209215
@Override
210-
public DocCommentSettings getDocCommentSettings(PsiFile file) {
216+
public @NotNull DocCommentSettings getDocCommentSettings(@NotNull PsiFile file) {
211217
return delegate.getDocCommentSettings(file);
212218
}
213219

@@ -223,7 +229,8 @@ public FormattingMode getCurrentFormattingMode() {
223229
}
224230

225231
@Override
226-
public int adjustLineIndent(final Document document, final int offset, FormattingMode mode)
232+
public int adjustLineIndent(
233+
final @NotNull Document document, final int offset, FormattingMode mode)
227234
throws IncorrectOperationException {
228235
if (delegate instanceof FormattingModeAwareIndentAdjuster) {
229236
return ((FormattingModeAwareIndentAdjuster) delegate)
@@ -233,7 +240,7 @@ public int adjustLineIndent(final Document document, final int offset, Formattin
233240
}
234241

235242
@Override
236-
public void scheduleReformatWhenSettingsComputed(PsiFile file) {
243+
public void scheduleReformatWhenSettingsComputed(@NotNull PsiFile file) {
237244
delegate.scheduleReformatWhenSettingsComputed(file);
238245
}
239246
}

idea_plugin/src/com/google/googlejavaformat/intellij/FormatterUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ static Map<TextRange, String> getReplacements(
5050
}
5151

5252
private static Collection<Range<Integer>> toRanges(Collection<? extends TextRange> textRanges) {
53-
return textRanges
54-
.stream()
53+
return textRanges.stream()
5554
.map(textRange -> Range.closedOpen(textRange.getStartOffset(), textRange.getEndOffset()))
5655
.collect(Collectors.toList());
5756
}

0 commit comments

Comments
 (0)