Skip to content

Commit

Permalink
Restored old inspection threading behaviour due to #466; added a time…
Browse files Browse the repository at this point in the history
…out on the background thread for sanity
  • Loading branch information
jshiell committed Jan 2, 2020
1 parent 3df58b6 commit 6438c8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static org.infernus.idea.checkstyle.CheckStyleBundle.message;
import static org.infernus.idea.checkstyle.util.Async.asyncResultOf;
import static org.infernus.idea.checkstyle.util.Notifications.showException;
import static org.infernus.idea.checkstyle.util.Notifications.showWarning;

public class CheckStyleInspection extends LocalInspectionTool {

private static final Logger LOG = Logger.getInstance(CheckStyleInspection.class);
private static final List<Problem> NO_PROBLEMS_FOUND = Collections.emptyList();
private static final long FIVE_SECONDS = 5000L;

private final CheckStyleInspectionPanel configPanel = new CheckStyleInspectionPanel();

Expand All @@ -59,7 +61,7 @@ public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile,
@NotNull final InspectionManager manager,
final boolean isOnTheFly) {
final Module module = moduleOf(psiFile);
return asProblemDescriptors(inspectFile(psiFile, module, manager), manager);
return asProblemDescriptors(asyncResultOf(() -> inspectFile(psiFile, module, manager), NO_PROBLEMS_FOUND, FIVE_SECONDS), manager);
}

@Nullable
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/org/infernus/idea/checkstyle/util/Async.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
package org.infernus.idea.checkstyle.util;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public final class Async {
private static final Logger LOG = Logger.getInstance(Async.class);

private static final int FIFTY_MS = 50;

private Async() {
}

@Nullable
public static <T> T asyncResultOf(@NotNull final Callable<T> callable,
@Nullable final T defaultValue,
final long timeoutInMs) {
try {
return whenFinished(executeOnPooledThread(callable), timeoutInMs).get();

} catch (Exception e) {
return defaultValue;
}
}

public static <T> Future<T> executeOnPooledThread(final Callable<T> callable) {
return ApplicationManager.getApplication().executeOnPooledThread(callable);
}

public static <T> Future<T> whenFinished(final Future<T> future) {
public static <T> Future<T> whenFinished(final Future<T> future,
final long timeoutInMs) {
long elapsedTime = 0;
while (!future.isDone() && !future.isCancelled()) {
ProgressManager.checkCanceled();
waitFor(FIFTY_MS);
elapsedTime += waitFor(FIFTY_MS);

if (timeoutInMs >= 0 && elapsedTime >= timeoutInMs) {
LOG.debug("Async task exhausted timeout of " + timeoutInMs + "ms, cancelling.");
future.cancel(true);
throw new ProcessCanceledException();
}
}
return future;
}

private static void waitFor(final int millis) {
private static long waitFor(final int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
return millis;
}
}

0 comments on commit 6438c8d

Please sign in to comment.