[groovy] move import optimizer from EDT to background thread

This commit is contained in:
Daniil Ovchinnikov
2015-11-23 19:55:19 +03:00
parent 6ac1899321
commit 93483de651
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@ package org.jetbrains.plugins.groovy.editor;
import com.intellij.lang.ImportOptimizer;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.util.NotNullComputable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
@@ -64,7 +66,7 @@ public class GroovyImportOptimizer implements ImportOptimizer {
@Override
@NotNull
public Runnable processFile(PsiFile file) {
return new MyProcessor(file, false);
return new MyProcessor(file).compute();
}
@Override
@@ -72,20 +74,19 @@ public class GroovyImportOptimizer implements ImportOptimizer {
return file instanceof GroovyFile;
}
private class MyProcessor implements Runnable {
private static class MyProcessor implements NotNullComputable<Runnable> {
private final PsiFile myFile;
private final boolean myRemoveUnusedOnly;
private MyProcessor(PsiFile file, boolean removeUnusedOnly) {
private MyProcessor(PsiFile file) {
myFile = file;
myRemoveUnusedOnly = removeUnusedOnly;
}
@NotNull
@Override
public void run() {
if (!(myFile instanceof GroovyFile)) return;
public Runnable compute() {
if (!(myFile instanceof GroovyFile)) return EmptyRunnable.getInstance();
GroovyFile file = ((GroovyFile)myFile);
final GroovyFile file = ((GroovyFile)myFile);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
final Document document = documentManager.getDocument(file);
if (document != null) {
@@ -104,26 +105,18 @@ public class GroovyImportOptimizer implements ImportOptimizer {
implicitlyImportedClasses, innerClasses,
aliasImported, annotatedImports);
final List<GrImportStatement> oldImports = PsiUtil.getValidImportStatements(file);
if (myRemoveUnusedOnly) {
for (GrImportStatement oldImport : oldImports) {
if (!usedImports.contains(oldImport)) {
file.removeImport(oldImport);
}
}
return;
}
// Add new import statements
GrImportStatement[] newImports =
prepare(usedImports, simplyImportedClasses, staticallyImportedMembers, implicitlyImportedClasses, innerClasses, aliasImported,
annotatedImports, unresolvedOnDemandImports);
if (oldImports.isEmpty() && newImports.length == 0 && aliasImported.isEmpty()) {
return;
return EmptyRunnable.getInstance();
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(file.getProject());
GroovyFile tempFile = factory.createGroovyFile("", false, null);
final GroovyFile tempFile = factory.createGroovyFile("", false, null);
for (GrImportStatement newImport : newImports) {
tempFile.addImport(newImport);
@@ -134,17 +127,22 @@ public class GroovyImportOptimizer implements ImportOptimizer {
final int endOffset = oldImports.get(oldImports.size() - 1).getTextRange().getEndOffset();
String oldText = oldImports.isEmpty() ? "" : myFile.getText().substring(startOffset, endOffset);
if (tempFile.getText().trim().equals(oldText)) {
return;
return EmptyRunnable.getInstance();
}
}
for (GrImportStatement statement : tempFile.getImportStatements()) {
file.addImport(statement);
}
return new Runnable() {
@Override
public void run() {
for (GrImportStatement statement : tempFile.getImportStatements()) {
file.addImport(statement);
}
for (GrImportStatement importStatement : oldImports) {
file.removeImport(importStatement);
}
for (GrImportStatement importStatement : oldImports) {
file.removeImport(importStatement);
}
}
};
}
private GrImportStatement[] prepare(final Set<GrImportStatement> usedImports,