Skip to content

Commit d00e3d5

Browse files
committed
Optimize no imports in an untrusted project without a balloon
Safe mode is a normal state, not a fault, yet the import optimizer logged a warning and showed a balloon each time it ran in an untrusted project, and "Optimize imports on the fly" runs it on every highlighting pass while the file has unused imports. It now returns a CollectingInfoRunnable that changes nothing: on the fly nothing shows, and an explicit Optimize Imports shows the reason in its hint instead of "No unused imports found".
1 parent 4dde8f4 commit d00e3d5

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎open-java-format-idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatImportOptimizer.java‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.intellij.formatting.service.FormattingNotificationService;
1818
import com.intellij.lang.ImportOptimizer;
19+
import com.intellij.lang.ImportOptimizer.CollectingInfoRunnable;
1920
import com.intellij.openapi.diagnostic.Logger;
2021
import com.intellij.openapi.editor.Document;
2122
import com.intellij.openapi.progress.ProcessCanceledException;
@@ -35,6 +36,18 @@
3536
final class PalantirJavaFormatImportOptimizer implements ImportOptimizer {
3637
private static final Logger logger = Logger.getInstance(PalantirJavaFormatImportOptimizer.class);
3738

39+
// Changes nothing. An explicit Optimize Imports shows the message in its hint, instead of "No unused imports
40+
// found"; on the fly nothing shows it.
41+
private static final CollectingInfoRunnable UNTRUSTED_PROJECT = new CollectingInfoRunnable() {
42+
@Override
43+
public void run() {}
44+
45+
@Override
46+
public String getUserNotificationInfo() {
47+
return Notifications.untrustedProjectMessage();
48+
}
49+
};
50+
3851
private final PalantirJavaFormatFormattingService formattingService;
3952

4053
PalantirJavaFormatImportOptimizer(PalantirJavaFormatFormattingService formattingService) {
@@ -58,10 +71,8 @@ public Runnable processFile(PsiFile file) {
5871
return EmptyRunnable.getInstance();
5972
}
6073
if (!PalantirJavaFormatFormattingService.isTrusted(project)) {
61-
logger.warn("open-java-format: skipping optimizing imports for untrusted project: " + project.getName());
62-
reportError(
63-
project, Notifications.GENERIC_ERROR_NOTIFICATION_GROUP, Notifications.untrustedProjectMessage());
64-
return EmptyRunnable.getInstance();
74+
// Safe mode is a normal state, and on the fly this runs on every highlighting pass: no warning, no balloon.
75+
return UNTRUSTED_PROJECT;
6576
}
6677
var formatter = formattingService.getFormatter(project);
6778
if (formatter.isEmpty()) {

‎open-java-format-idea-plugin/src/test/java/com/palantir/javaformat/intellij/PalantirJavaFormatImportOptimizerTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.intellij.formatting.service.AsyncFormattingRequest;
2525
import com.intellij.formatting.service.CoreFormattingService;
2626
import com.intellij.formatting.service.FormattingService;
27+
import com.intellij.ide.impl.TrustedProjects;
2728
import com.intellij.lang.ImportOptimizer;
2829
import com.intellij.openapi.command.WriteCommandAction;
2930
import com.intellij.openapi.util.TextRange;
@@ -166,6 +167,25 @@ void optimize_imports_is_the_ides_own_when_the_plugin_is_disabled() {
166167
assertThat(delegatingFormatter.optimizerInvoked).isFalse();
167168
}
168169

170+
@Test
171+
void optimize_imports_in_an_untrusted_project_changes_nothing_and_says_why_in_the_hint() {
172+
PsiFile file = createPsiFile(UNOPTIMIZED);
173+
TrustedProjects.setTrusted(fixture.getProject(), false);
174+
try {
175+
var processor = new OptimizeImportsProcessor(file.getProject(), file);
176+
processor.setCollectInfo(true);
177+
178+
run(processor);
179+
180+
assertThat(file.getText()).isEqualTo(UNOPTIMIZED);
181+
assertThat(requireNonNull(processor.getInfoCollector()).getOptimizeImportsNotification())
182+
.isEqualTo(Notifications.untrustedProjectMessage());
183+
} finally {
184+
// The light project outlives the test.
185+
TrustedProjects.setTrusted(fixture.getProject(), true);
186+
}
187+
}
188+
169189
private String commandLineOutput(String input) throws Exception {
170190
return delegatingFormatter
171191
.getFormatter(fixture.getProject())

0 commit comments

Comments
 (0)