@NotNull
public Runnable processFile(PsiFile file) {
- return new MyProcessor(file);
+ return new MyProcessor((GroovyFile)file, false);
+ }
+
+ public void removeUnusedImports(GroovyFile file) {
+ new MyProcessor(file, true).run();
}
public boolean supports(PsiFile file) {
private class MyProcessor implements Runnable {
private final GroovyFile myFile;
+ private final boolean myRemoveUnusedOnly;
- public MyProcessor(PsiFile file) {
- myFile = (GroovyFile) file;
+ private MyProcessor(GroovyFile file, boolean removeUnusedOnly) {
+ myFile = file;
+ myRemoveUnusedOnly = removeUnusedOnly;
}
public void run() {
});
- // remove ALL imports from file
- final GrImportStatement[] oldImports = myFile.getImportStatements();
+ final List<GrImportStatement> oldImports = new ArrayList<GrImportStatement>();
+ for (GrImportStatement statement : myFile.getImportStatements()) {
+ final GrCodeReferenceElement reference = statement.getImportReference();
+ if (reference != null && reference.getCanonicalText() != null) {
+ oldImports.add(statement);
+ }
+ }
+ if (myRemoveUnusedOnly) {
+ for (GrImportStatement oldImport : oldImports) {
+ if (!usedImports.contains(oldImport)) {
+ myFile.removeImport(oldImport);
+ }
+ }
+ return;
+ }
// Getting aliased imports
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myFile.getProject());
myFile.removeImport(myFile.addImport(factory.createImportStatementFromText("import xxxx"))); //to remove trailing whitespaces
for (GrImportStatement importStatement : oldImports) {
- final GrCodeReferenceElement reference = importStatement.getImportReference();
- if (reference == null || reference.getCanonicalText() == null) {
- continue;
- }
-
myFile.removeImport(importStatement);
}
}
package org.jetbrains.plugins.groovy.refactoring;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
import com.intellij.refactoring.RefactoringHelper;
import com.intellij.usageView.UsageInfo;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.*;
import com.intellij.util.containers.hash.HashSet;
+import org.jetbrains.plugins.groovy.lang.editor.GroovyImportOptimizer;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
-import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor;
-import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
-import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
-import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
-import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
-import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
import java.util.Set;
-import java.util.Collections;
/**
* @author Maxim.Medvedev
}
public void performOperation(Project project, Set<GroovyFile> files) {
+ final GroovyImportOptimizer optimizer = new GroovyImportOptimizer();
for (GroovyFile file : files) {
- removeUnusedImports(file);
+ optimizer.removeUnusedImports(file);
}
}
- public void removeUnusedImports(GroovyFile file) {
- final GrImportStatement[] imports = file.getImportStatements();
- final Set<GrImportStatement> unused = new HashSet<GrImportStatement>(imports.length);
- Collections.addAll(unused, imports);
- file.accept(new GroovyRecursiveElementVisitor() {
- public void visitCodeReferenceElement(GrCodeReferenceElement refElement) {
- visitRefElement(refElement);
- super.visitCodeReferenceElement(refElement);
- }
-
- public void visitReferenceExpression(GrReferenceExpression referenceExpression) {
- visitRefElement(referenceExpression);
- super.visitReferenceExpression(referenceExpression);
- }
-
- private void visitRefElement(GrReferenceElement refElement) {
- final GroovyResolveResult[] resolveResults = refElement.multiResolve(false);
- for (GroovyResolveResult resolveResult : resolveResults) {
- final GroovyPsiElement context = resolveResult.getCurrentFileResolveContext();
- final PsiElement element = resolveResult.getElement();
- if (element == null) return;
- if (context instanceof GrImportStatement) {
- final GrImportStatement importStatement = (GrImportStatement)context;
- unused.remove(importStatement);
- }
- }
- }
- });
- for (GrImportStatement importStatement : unused) {
- file.removeImport(importStatement);
- }
- }
}
assertEquals txt, txtFile.text
}
+ public void testPreserveUnknownImports() throws Exception {
+ def someClass = myFixture.addClass("public class SomeClass {}")
+
+ myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, """
+import foo.bar.Zoo
+SomeClass c = new SomeClass()
+""")
+ myFixture.renameElement(someClass, "NewClass")
+ myFixture.checkResult """
+import foo.bar.Zoo
+NewClass c = new NewClass()
+"""
+ }
+
public void doTest() throws Throwable {
final String testFile = getTestName(true).replace('$', '/') + ".test";
final List<String> list = TestUtils.readInput(TestUtils.getAbsoluteTestDataPath() + "groovy/refactoring/rename/" + testFile);