Search in sources :

Example 1 with PsiImportList

use of com.intellij.psi.PsiImportList in project intellij-community by JetBrains.

the class ImportListElement method addInternal.

@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);
}
Also used : JavaPsiImplementationHelper(com.intellij.psi.impl.JavaPsiImplementationHelper) PsiImportStatementBase(com.intellij.psi.PsiImportStatementBase) PsiImportList(com.intellij.psi.PsiImportList)

Example 2 with PsiImportList

use of com.intellij.psi.PsiImportList in project intellij-community by JetBrains.

the class JavaImportOptimizer method processFile.

@Override
@NotNull
public Runnable processFile(final PsiFile file) {
    if (!(file instanceof PsiJavaFile)) {
        return EmptyRunnable.getInstance();
    }
    Project project = file.getProject();
    final PsiImportList newImportList = JavaCodeStyleManager.getInstance(project).prepareOptimizeImportsResult((PsiJavaFile) file);
    if (newImportList == null)
        return EmptyRunnable.getInstance();
    return new CollectingInfoRunnable() {

        private int myImportListLengthDiff;

        @Override
        public void run() {
            try {
                final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
                final Document document = manager.getDocument(file);
                if (document != null) {
                    manager.commitDocument(document);
                }
                final PsiImportList oldImportList = ((PsiJavaFile) file).getImportList();
                assert oldImportList != null;
                int importsBefore = oldImportList.getAllImportStatements().length;
                oldImportList.replace(newImportList);
                myImportListLengthDiff = importsBefore - newImportList.getAllImportStatements().length;
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }

        @Nullable
        @Override
        public String getUserNotificationInfo() {
            if (myImportListLengthDiff > 0) {
                return "removed " + myImportListLengthDiff + " import" + (myImportListLengthDiff > 1 ? "s" : "");
            }
            return null;
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) PsiJavaFile(com.intellij.psi.PsiJavaFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) PsiImportList(com.intellij.psi.PsiImportList) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiImportList (com.intellij.psi.PsiImportList)2 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiImportStatementBase (com.intellij.psi.PsiImportStatementBase)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1 JavaPsiImplementationHelper (com.intellij.psi.impl.JavaPsiImplementationHelper)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1