mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-01 13:11:39 +07:00
IDEADEV-366
This commit is contained in:
@@ -56,4 +56,6 @@ public abstract class CodeStyleManager {
|
||||
public abstract boolean checkIdentifierRole(String identifier, IdentifierRole role);
|
||||
|
||||
public abstract PsiElement qualifyClassReferences(PsiElement element);
|
||||
|
||||
public abstract void removeRedundantImports(PsiJavaFile file) throws IncorrectOperationException;
|
||||
}
|
||||
|
||||
@@ -166,6 +166,36 @@ public class CodeStyleManagerImpl extends CodeStyleManagerEx implements ProjectC
|
||||
return new ImportHelper(getSettings()).addImport(file, refClass);
|
||||
}
|
||||
|
||||
public void removeRedundantImports(PsiJavaFile file) throws IncorrectOperationException {
|
||||
final PsiImportStatementBase[] imports = file.getImportList().getAllImportStatements();
|
||||
if (imports.length == 0) return;
|
||||
|
||||
final Set<PsiImportStatementBase> redundants = new HashSet<PsiImportStatementBase>(Arrays.asList(imports));
|
||||
file.accept(new PsiRecursiveElementVisitor() {
|
||||
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
|
||||
if (!reference.isQualified()) {
|
||||
final ResolveResult resolveResult = reference.advancedResolve(false);
|
||||
final PsiElement resolveScope = resolveResult.getCurrentFileResolveScope();
|
||||
if (resolveScope instanceof PsiImportStatementBase)
|
||||
{
|
||||
//noinspection SuspiciousMethodCalls
|
||||
redundants.remove(resolveScope);
|
||||
}
|
||||
}
|
||||
super.visitReferenceElement(reference);
|
||||
}
|
||||
});
|
||||
|
||||
for (Iterator<PsiImportStatementBase> iterator = redundants.iterator(); iterator.hasNext();) {
|
||||
final PsiImportStatementBase importStatement = iterator.next();
|
||||
final PsiJavaCodeReferenceElement ref = importStatement.getImportReference();
|
||||
//Do not remove non-resolving refs
|
||||
if (ref == null || ref.resolve() == null) continue;
|
||||
|
||||
importStatement.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public int findEntryIndex(PsiImportStatementBase statement) {
|
||||
return new ImportHelper(getSettings()).findEntryIndex(statement);
|
||||
}
|
||||
@@ -504,7 +534,7 @@ public class CodeStyleManagerImpl extends CodeStyleManagerEx implements ProjectC
|
||||
};
|
||||
}
|
||||
|
||||
private void addNamesFromStatistics(Set names, VariableKind variableKind, String propertyName, PsiType type) {
|
||||
private void addNamesFromStatistics(Set<String> names, VariableKind variableKind, String propertyName, PsiType type) {
|
||||
String[] allNames = myStatisticsManager.getAllVariableNamesUsed(variableKind, propertyName, type);
|
||||
|
||||
int maxFrequency = 0;
|
||||
@@ -1079,10 +1109,10 @@ public class CodeStyleManagerImpl extends CodeStyleManagerEx implements ProjectC
|
||||
}
|
||||
}
|
||||
|
||||
Comparator comparator = new Comparator() {
|
||||
public int compare(Object o1, Object o2) {
|
||||
int count1 = myStatisticsManager.getVariableNameUseCount((String)o1, variableKind, propertyName, type);
|
||||
int count2 = myStatisticsManager.getVariableNameUseCount((String)o2, variableKind, propertyName, type);
|
||||
Comparator<String> comparator = new Comparator<String>() {
|
||||
public int compare(String s1, String s2) {
|
||||
int count1 = myStatisticsManager.getVariableNameUseCount(s1, variableKind, propertyName, type);
|
||||
int count2 = myStatisticsManager.getVariableNameUseCount(s2, variableKind, propertyName, type);
|
||||
return count2 - count1;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -14,9 +14,8 @@ import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.wm.WindowManager;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.refactoring.listeners.RefactoringListenerManager;
|
||||
import com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl;
|
||||
import com.intellij.refactoring.listeners.impl.RefactoringTransaction;
|
||||
@@ -28,6 +27,7 @@ import com.intellij.usageView.UsageViewDescriptor;
|
||||
import com.intellij.usageView.UsageViewUtil;
|
||||
import com.intellij.usages.*;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -300,7 +300,9 @@ public abstract class BaseRefactoringProcessor {
|
||||
RefactoringListenerManagerImpl listenerManager =
|
||||
(RefactoringListenerManagerImpl) RefactoringListenerManager.getInstance(myProject);
|
||||
myTransaction = listenerManager.startTransaction();
|
||||
Set<PsiJavaFile> touchedJavaFiles = getTouchedJavaFiles(_usages);
|
||||
performRefactoring(_usages);
|
||||
removeRedundantImports(touchedJavaFiles);
|
||||
myTransaction.commit();
|
||||
performPsiSpoilingRefactoring();
|
||||
}
|
||||
@@ -321,6 +323,35 @@ public abstract class BaseRefactoringProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private void removeRedundantImports(final Set<PsiJavaFile> javaFiles) {
|
||||
final CodeStyleManager styleManager = PsiManager.getInstance(myProject).getCodeStyleManager();
|
||||
for (Iterator<PsiJavaFile> iterator = javaFiles.iterator(); iterator.hasNext();) {
|
||||
try {
|
||||
final PsiJavaFile file = iterator.next();
|
||||
if (file.getVirtualFile() != null) {
|
||||
styleManager.removeRedundantImports(file);
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Set<PsiJavaFile> getTouchedJavaFiles(final UsageInfo[] usages) {
|
||||
Set<PsiJavaFile> javaFiles = new HashSet<PsiJavaFile>();
|
||||
for (int i = 0; i < usages.length; i++) {
|
||||
final PsiElement element = usages[i].getElement();
|
||||
if (element != null) {
|
||||
final PsiFile file = element.getContainingFile();
|
||||
if (file instanceof PsiJavaFile) {
|
||||
javaFiles.add((PsiJavaFile)file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return javaFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refactorings that spoil PSI (write something directly to documents etc.) should
|
||||
* do that in this method.<br>
|
||||
@@ -365,7 +396,9 @@ public abstract class BaseRefactoringProcessor {
|
||||
RefactoringListenerManagerImpl listenerManager =
|
||||
(RefactoringListenerManagerImpl) RefactoringListenerManager.getInstance(myProject);
|
||||
myTransaction = listenerManager.startTransaction();
|
||||
Set<PsiJavaFile> touchedJavaFiles = getTouchedJavaFiles(u[0]);
|
||||
performRefactoring(u[0]);
|
||||
removeRedundantImports(touchedJavaFiles);
|
||||
myTransaction.commit();
|
||||
performPsiSpoilingRefactoring();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package p1;
|
||||
|
||||
import java.lang.*;
|
||||
|
||||
class C {
|
||||
java.lang.AAA a = new java.lang.AAA();
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import static Test1.i;
|
||||
|
||||
class Test {
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package pack2;
|
||||
|
||||
import static pack1.A.staticMethod;
|
||||
import pack1.A;
|
||||
|
||||
class Usage {
|
||||
|
||||
Reference in New Issue
Block a user