private void addImport(PsiElementFactory elementFactory, String fullyQualifiedName){ final PsiFile file = psiClass.getContainingFile(); if (!(file instanceof PsiJavaFile)) { return; } final PsiJavaFile javaFile = (PsiJavaFile)file; final PsiImportList importList = javaFile.getImportList(); if (importList == null) { return; } // Check if already imported for (PsiImportStatementBase is : importList.getAllImportStatements()) { String impQualifiedName = is.getImportReference().getQualifiedName(); if (fullyQualifiedName.equals(impQualifiedName)){ return; // Already imported so nothing neede } } // Not imported yet so add it importList.add(elementFactory.createImportStatementOnDemand(fullyQualifiedName)); }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) { if(before == null) { if(first == last && (first.getElementType() == JavaElementType.IMPORT_STATEMENT || first.getElementType() == JavaElementType.IMPORT_STATIC_STATEMENT)) { final PsiImportList list = (PsiImportList) SourceTreeToPsiMap.treeElementToPsi(this); final PsiImportStatementBase statement = (PsiImportStatementBase) SourceTreeToPsiMap.treeElementToPsi(first); final JavaPsiImplementationHelper instance = JavaPsiImplementationHelper.getInstance(list.getProject()); if(instance != null) { anchor = instance.getDefaultImportAnchor(list, statement); } before = Boolean.TRUE; } } return super.addInternal(first, last, anchor, before); }
@Override public PsiImportStatementBase createPsi(@NotNull final ASTNode node) { if (node instanceof ImportStaticStatementElement) { return new PsiImportStaticStatementImpl(node); } else { return new PsiImportStatementImpl(node); } }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before){ if (before == null){ if (first == last && (first.getElementType() == JavaElementType.IMPORT_STATEMENT || first.getElementType() == JavaElementType.IMPORT_STATIC_STATEMENT)){ final PsiImportList list = (PsiImportList)SourceTreeToPsiMap.treeElementToPsi(this); final PsiImportStatementBase statement = (PsiImportStatementBase)SourceTreeToPsiMap.treeElementToPsi(first); final JavaPsiImplementationHelper instance = JavaPsiImplementationHelper.getInstance(list.getProject()); if (instance != null) { anchor = instance.getDefaultImportAnchor(list, statement); } before = Boolean.TRUE; } } return super.addInternal(first, last, anchor, before); }
@Override public int compare(PsiImportStatementBase importStatementBase1, PsiImportStatementBase importStatementBase2) { final boolean onDemand = importStatementBase1.isOnDemand(); if (onDemand != importStatementBase2.isOnDemand()) { return onDemand ? -1 : 1; } // just sort on demand imports first, and sort the rest in reverse file order. return -PsiElementOrderComparator.getInstance().compare(importStatementBase1, importStatementBase2); }
@Override public PsiImportStatementBase createPsi(@NotNull final ASTNode node) { if(node instanceof ImportStaticStatementElement) { return new PsiImportStaticStatementImpl(node); } else { return new PsiImportStatementImpl(node); } }
private static void assertOrder(PsiJavaFile file, @NonNls String... expectedOrder) { PsiImportStatementBase[] statements = file.getImportList().getAllImportStatements(); assertEquals(expectedOrder.length, statements.length); for(int i = 0; i < statements.length; i++) { PsiImportStatementBase statement = statements[i]; String text = StringUtil.trimEnd(StringUtil.trimStart(statement.getText(), "import "), ";"); assertEquals(expectedOrder[i], text); } }
@Override public PsiImportStatementBase createPsi(@NotNull final PsiImportStatementStub stub) { return getPsiFactory(stub).createImportStatement(stub); }
@Override public ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement) { throw new UnsupportedOperationException("TODO"); }
private static void suggestChainedCalls(CompletionParameters parameters, CompletionResultSet result, PsiElement position) { PsiElement parent = position.getParent(); if(!(parent instanceof PsiJavaCodeReferenceElement) || parent.getParent() instanceof PsiImportStatementBase) { return; } PsiElement qualifier = ((PsiJavaCodeReferenceElement) parent).getQualifier(); if(!(qualifier instanceof PsiJavaCodeReferenceElement) || ((PsiJavaCodeReferenceElement) qualifier).isQualified()) { return; } PsiElement target = ((PsiJavaCodeReferenceElement) qualifier).resolve(); if(target != null && !(target instanceof PsiPackage)) { return; } PsiFile file = position.getContainingFile(); if(file instanceof PsiJavaCodeReferenceCodeFragment) { return; } String fullPrefix = parent.getText().substring(0, parameters.getOffset() - parent.getTextRange().getStartOffset()); CompletionResultSet qualifiedCollector = result.withPrefixMatcher(fullPrefix); ElementFilter filter = JavaCompletionContributor.getReferenceFilter(position); for(LookupElement base : suggestQualifierItems(parameters, (PsiJavaCodeReferenceElement) qualifier, filter)) { PsiType type = JavaCompletionUtil.getLookupElementType(base); if(type != null && !PsiType.VOID.equals(type)) { PsiReferenceExpression ref = ReferenceExpressionCompletionContributor.createMockReference(position, type, base); if(ref != null) { for(final LookupElement item : JavaSmartCompletionContributor.completeReference(position, ref, filter, true, true, parameters, result.getPrefixMatcher())) { qualifiedCollector.addElement(JavaCompletionUtil.highlightIfNeeded(null, new JavaChainLookupElement(base, item), item.getObject(), position)); } } } } }
public abstract ASTNode getDefaultImportAnchor(PsiImportList list, PsiImportStatementBase statement);