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);
}
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;
}
};
}
Aggregations