[java] non-mandatory import list

This commit is contained in:
Roman Shevchenko
2016-06-29 21:02:18 +03:00
parent f22dd502a3
commit ebb84bbd3d
5 changed files with 32 additions and 25 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -167,8 +167,7 @@ public class JavaElementSignatureProvider extends AbstractElementSignatureProvid
@Nullable StringBuilder processingInfoStorage)
{
if (type.equals("imports")) {
if (!(file instanceof PsiJavaFile)) return null;
return ((PsiJavaFile)file).getImportList();
return file instanceof PsiJavaFile ? ((PsiJavaFile)file).getImportList() : null;
}
else if (type.equals("method")) {
String name = tokenizer.nextToken();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -66,6 +66,9 @@ public class ImportHelper{
}
public PsiImportList prepareOptimizeImportsResult(@NotNull final PsiJavaFile file) {
PsiImportList oldList = file.getImportList();
if (oldList == null) return null;
// Java parser works in a way that comments may be included to the import list, e.g.:
// import a;
// /* comment */
@@ -98,8 +101,8 @@ public class ImportHelper{
codeStyleManager.reformat(dummyFile);
PsiImportList newImportList = dummyFile.getImportList();
assert newImportList != null : dummyFile.getText();
PsiImportList result = (PsiImportList)newImportList.copy();
PsiImportList oldList = file.getImportList();
if (oldList.isReplaceEquivalent(result)) return null;
if (!nonImports.isEmpty()) {
PsiElement firstPrevious = newImportList.getPrevSibling();
@@ -431,6 +434,7 @@ public class ImportHelper{
try {
PsiImportList importList = file.getImportList();
assert importList != null : file;
PsiImportStatement statement = useOnDemand ? factory.createImportStatementOnDemand(packageName) : factory.createImportStatement(refClass);
importList.add(statement);
if (useOnDemand) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -87,7 +87,7 @@ public abstract class JavaCodeStyleManager {
* file if an Optimize Imports operation was performed on it.
*
* @param file the file to calculate the import list for.
* @return the calculated import list.
* @return the calculated import list, or {@code null} when the file has no import list.
*/
public abstract PsiImportList prepareOptimizeImportsResult(@NotNull PsiJavaFile file);
@@ -126,28 +126,27 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
}
@Override
@NotNull
public PsiImportList getImportList() {
StubElement<?> stub = getStub();
if (stub != null) {
PsiImportList[] nodes = stub.getChildrenByType(JavaStubElementTypes.IMPORT_LIST, PsiImportList.ARRAY_FACTORY);
if (nodes.length != 1) {
reportStubAstMismatch(stub + "; " + stub.getChildrenStubs(), getStubTree(), PsiDocumentManager.getInstance(getProject()).getCachedDocument(this));
}
return nodes[0];
if (nodes.length == 1) return nodes[0];
if (nodes.length == 0) return null;
reportStubAstMismatch(stub + "; " + stub.getChildrenStubs(), getStubTree(), PsiDocumentManager.getInstance(getProject()).getCachedDocument(this));
}
ASTNode node = calcTreeElement().findChildByType(JavaElementType.IMPORT_LIST);
assert node != null : getFileType() + ", " + getName();
return SourceTreeToPsiMap.treeToPsiNotNull(node);
return (PsiImportList)SourceTreeToPsiMap.treeElementToPsi(node);
}
@Override
@NotNull
public PsiElement[] getOnDemandImports(boolean includeImplicit, boolean checkIncludes) {
PsiImportList importList = getImportList();
if (importList == null) return EMPTY_ARRAY;
List<PsiElement> array = new ArrayList<PsiElement>();
PsiImportList importList = getImportList();
PsiImportStatement[] statements = importList.getImportStatements();
for (PsiImportStatement statement : statements) {
if (statement.isOnDemand()) {
@@ -174,8 +173,10 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
@Override
@NotNull
public PsiClass[] getSingleClassImports(boolean checkIncludes) {
List<PsiClass> array = new ArrayList<PsiClass>();
PsiImportList importList = getImportList();
if (importList == null) return PsiClass.EMPTY_ARRAY;
List<PsiClass> array = new ArrayList<PsiClass>();
PsiImportStatement[] statements = importList.getImportStatements();
for (PsiImportStatement statement : statements) {
if (!statement.isOnDemand()) {
@@ -191,12 +192,14 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
@Override
public PsiJavaCodeReferenceElement findImportReferenceTo(PsiClass aClass) {
PsiImportList importList = getImportList();
PsiImportStatement[] statements = importList.getImportStatements();
for (PsiImportStatement statement : statements) {
if (!statement.isOnDemand()) {
PsiElement ref = statement.resolve();
if (ref != null && getManager().areElementsEquivalent(ref, aClass)) {
return statement.getImportReference();
if (importList != null) {
PsiImportStatement[] statements = importList.getImportStatements();
for (PsiImportStatement statement : statements) {
if (!statement.isOnDemand()) {
PsiElement ref = statement.resolve();
if (ref != null && getManager().areElementsEquivalent(ref, aClass)) {
return statement.getImportReference();
}
}
}
}
@@ -316,7 +319,7 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
if (!processor.execute(aClass, state)) return false;
}
final PsiImportStatement[] importStatements = importList.getImportStatements();
final PsiImportStatement[] importStatements = importList != null ? importList.getImportStatements() : PsiImportStatement.EMPTY_ARRAY;
// single-type processing
for (PsiImportStatement statement : importStatements) {
@@ -91,6 +91,7 @@ public class PsiReferenceExpressionImpl extends PsiReferenceExpressionBase imple
boolean doImportStatic;
if (containingFile instanceof PsiJavaFile) {
importList = ((PsiJavaFile)containingFile).getImportList();
assert importList != null : containingFile;
PsiImportStatementBase singleImportStatement = importList.findSingleImportStatement(staticName);
doImportStatic = singleImportStatement == null;
if (singleImportStatement instanceof PsiImportStaticStatement) {
@@ -116,8 +117,8 @@ public class PsiReferenceExpressionImpl extends PsiReferenceExpressionBase imple
return this;
}
public static void bindToElementViaStaticImport(final PsiClass qualifierClass, final String staticName, final PsiImportList importList)
throws IncorrectOperationException {
public static void bindToElementViaStaticImport(PsiClass qualifierClass, String staticName, PsiImportList importList) throws IncorrectOperationException {
assert importList != null;
final String qualifiedName = qualifierClass.getQualifiedName();
final List<PsiJavaCodeReferenceElement> refs = getImportsFromClass(importList, qualifiedName);
if (refs.size() < JavaCodeStyleSettingsFacade.getInstance(qualifierClass.getProject()).getNamesCountToUseImportOnDemand()) {