use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class FileReferenceCompletionImpl method getFileReferenceCompletionVariants.
@NotNull
@Override
public Object[] getFileReferenceCompletionVariants(final FileReference reference) {
final String s = reference.getText();
if (s != null && s.equals("/")) {
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
final CommonProcessors.CollectUniquesProcessor<PsiFileSystemItem> collector = new CommonProcessors.CollectUniquesProcessor<>();
final PsiElementProcessor<PsiFileSystemItem> processor = new PsiElementProcessor<PsiFileSystemItem>() {
@Override
public boolean execute(@NotNull PsiFileSystemItem fileSystemItem) {
return new FilteringProcessor<>(reference.getFileReferenceSet().getReferenceCompletionFilter(), collector).process(FileReference.getOriginalFile(fileSystemItem));
}
};
List<Object> additionalItems = ContainerUtil.newArrayList();
for (PsiFileSystemItem context : reference.getContexts()) {
for (final PsiElement child : context.getChildren()) {
if (child instanceof PsiFileSystemItem) {
processor.execute((PsiFileSystemItem) child);
}
}
if (context instanceof FileReferenceResolver) {
additionalItems.addAll(((FileReferenceResolver) context).getVariants(reference));
}
}
final FileType[] types = reference.getFileReferenceSet().getSuitableFileTypes();
final THashSet<PsiElement> set = new THashSet<>(collector.getResults(), VARIANTS_HASHING_STRATEGY);
final PsiElement[] candidates = PsiUtilCore.toPsiElementArray(set);
final Object[] variants = new Object[candidates.length + additionalItems.size()];
for (int i = 0; i < candidates.length; i++) {
PsiElement candidate = candidates[i];
Object item = reference.createLookupItem(candidate);
if (item == null) {
item = FileInfoManager.getFileLookupItem(candidate);
}
if (candidate instanceof PsiFile && item instanceof LookupElement && types.length > 0 && ArrayUtil.contains(((PsiFile) candidate).getFileType(), types)) {
item = PrioritizedLookupElement.withPriority((LookupElement) item, Double.MAX_VALUE);
}
variants[i] = item;
}
for (int i = 0; i < additionalItems.size(); i++) {
variants[i + candidates.length] = additionalItems.get(i);
}
if (!reference.getFileReferenceSet().isUrlEncoded()) {
return variants;
}
List<Object> encodedVariants = new ArrayList<>(variants.length + additionalItems.size());
for (int i = 0; i < candidates.length; i++) {
final PsiElement element = candidates[i];
if (element instanceof PsiNamedElement) {
final PsiNamedElement psiElement = (PsiNamedElement) element;
String name = psiElement.getName();
final String encoded = reference.encode(name, psiElement);
if (encoded == null)
continue;
if (!encoded.equals(name)) {
final Icon icon = psiElement.getIcon(Iconable.ICON_FLAG_READ_STATUS | Iconable.ICON_FLAG_VISIBILITY);
LookupElementBuilder item = FileInfoManager.getFileLookupItem(candidates[i], encoded, icon);
encodedVariants.add(item.withTailText(" (" + name + ")"));
} else {
encodedVariants.add(variants[i]);
}
}
}
encodedVariants.addAll(additionalItems);
return ArrayUtil.toObjectArray(encodedVariants);
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class PullAsAbstractUpFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
final PsiMethod method = (PsiMethod) startElement;
if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
return;
final PsiClass containingClass = method.getContainingClass();
LOG.assertTrue(containingClass != null);
PsiManager manager = containingClass.getManager();
if (containingClass instanceof PsiAnonymousClass) {
final PsiClassType baseClassType = ((PsiAnonymousClass) containingClass).getBaseClassType();
final PsiClass baseClass = baseClassType.resolve();
if (baseClass != null && manager.isInProject(baseClass)) {
pullUp(method, containingClass, baseClass);
}
} else {
final LinkedHashSet<PsiClass> classesToPullUp = new LinkedHashSet<>();
collectClassesToPullUp(manager, classesToPullUp, containingClass.getExtendsListTypes());
collectClassesToPullUp(manager, classesToPullUp, containingClass.getImplementsListTypes());
if (classesToPullUp.isEmpty()) {
//check visibility
new ExtractInterfaceHandler().invoke(project, new PsiElement[] { containingClass }, null);
} else if (classesToPullUp.size() == 1) {
pullUp(method, containingClass, classesToPullUp.iterator().next());
} else if (editor != null) {
NavigationUtil.getPsiElementPopup(classesToPullUp.toArray(new PsiClass[classesToPullUp.size()]), new PsiClassListCellRenderer(), "Choose super class", new PsiElementProcessor<PsiClass>() {
@Override
public boolean execute(@NotNull PsiClass aClass) {
pullUp(method, containingClass, aClass);
return false;
}
}, classesToPullUp.iterator().next()).showInBestPositionFor(editor);
}
}
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class ModifierFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
final PsiModifierList myModifierList = (PsiModifierList) startElement;
final PsiVariable variable = myVariable == null ? null : myVariable.getElement();
if (!FileModificationService.getInstance().preparePsiElementForWrite(myModifierList))
return;
if (variable != null && !FileModificationService.getInstance().preparePsiElementForWrite(variable))
return;
final List<PsiModifierList> modifierLists = new ArrayList<>();
final PsiFile containingFile = myModifierList.getContainingFile();
final PsiModifierList modifierList;
if (variable != null && variable.isValid()) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
variable.normalizeDeclaration();
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
modifierList = variable.getModifierList();
assert modifierList != null;
} else {
modifierList = myModifierList;
}
PsiElement owner = modifierList.getParent();
if (owner instanceof PsiMethod) {
PsiModifierList copy = (PsiModifierList) myModifierList.copy();
changeModifierList(copy);
final int accessLevel = PsiUtil.getAccessLevel(copy);
OverridingMethodsSearch.search((PsiMethod) owner, owner.getResolveScope(), true).forEach(new PsiElementProcessorAdapter<>(new PsiElementProcessor<PsiMethod>() {
@Override
public boolean execute(@NotNull PsiMethod inheritor) {
PsiModifierList list = inheritor.getModifierList();
if (inheritor.getManager().isInProject(inheritor) && PsiUtil.getAccessLevel(list) < accessLevel) {
modifierLists.add(list);
}
return true;
}
}));
}
if (!modifierLists.isEmpty()) {
if (Messages.showYesNoDialog(project, QuickFixBundle.message("change.inheritors.visibility.warning.text"), QuickFixBundle.message("change.inheritors.visibility.warning.title"), Messages.getQuestionIcon()) == Messages.YES) {
ApplicationManager.getApplication().runWriteAction(() -> {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(modifierLists)) {
return;
}
for (final PsiModifierList modifierList1 : modifierLists) {
changeModifierList(modifierList1);
}
});
}
}
ApplicationManager.getApplication().runWriteAction(() -> {
changeModifierList(modifierList);
if (myShouldHave && owner instanceof PsiMethod && PsiModifier.ABSTRACT.equals(myModifier)) {
final PsiMethod method = (PsiMethod) owner;
final PsiClass aClass = method.getContainingClass();
if (aClass != null && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
changeModifierList(aClass.getModifierList());
}
}
UndoUtil.markPsiFileForUndo(containingFile);
});
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-community by JetBrains.
the class IntroduceParameterHandler method introduceStrategy.
@VisibleForTesting
public boolean introduceStrategy(final Project project, final Editor editor, PsiFile file, final PsiElement[] elements) {
if (elements.length > 0) {
final AbstractInplaceIntroducer inplaceIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
if (inplaceIntroducer instanceof InplaceIntroduceParameterPopup) {
return false;
}
final PsiMethod containingMethod = Util.getContainingMethod(elements[0]);
if (containingMethod == null) {
return false;
}
final List<PsiMethod> enclosingMethods = getEnclosingMethods(containingMethod);
if (enclosingMethods.isEmpty()) {
return false;
}
final PsiElement[] elementsCopy;
if (!elements[0].isPhysical()) {
elementsCopy = elements;
} else {
final PsiFile copy = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
final TextRange range = new TextRange(elements[0].getTextRange().getStartOffset(), elements[elements.length - 1].getTextRange().getEndOffset());
final PsiExpression exprInRange = CodeInsightUtil.findExpressionInRange(copy, range.getStartOffset(), range.getEndOffset());
elementsCopy = exprInRange != null ? new PsiElement[] { exprInRange } : CodeInsightUtil.findStatementsInRange(copy, range.getStartOffset(), range.getEndOffset());
}
final PsiMethod containingMethodCopy = Util.getContainingMethod(elementsCopy[0]);
LOG.assertTrue(containingMethodCopy != null);
final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(containingMethodCopy);
final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy, enclosingMethodsInCopy.get(enclosingMethodsInCopy.size() - 1));
try {
if (!processor.prepare())
return false;
processor.showDialog();
//provide context for generated method to check exceptions compatibility
final PsiMethod emptyMethod = JavaPsiFacade.getElementFactory(project).createMethodFromText(processor.generateEmptyMethod("name").getText(), elements[0]);
final Collection<? extends PsiType> types = FunctionalInterfaceSuggester.suggestFunctionalInterfaces(emptyMethod);
if (types.isEmpty()) {
return false;
}
if (types.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
final PsiType next = types.iterator().next();
functionalInterfaceSelected(next, enclosingMethods, project, editor, processor, elements);
} else {
final Map<PsiClass, PsiType> classes = new LinkedHashMap<>();
for (PsiType type : types) {
classes.put(PsiUtil.resolveClassInType(type), type);
}
final PsiClass[] psiClasses = classes.keySet().toArray(new PsiClass[classes.size()]);
final String methodSignature = PsiFormatUtil.formatMethod(emptyMethod, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
final PsiType returnType = emptyMethod.getReturnType();
LOG.assertTrue(returnType != null);
final String title = "Choose Applicable Functional Interface: " + methodSignature + " -> " + returnType.getPresentableText();
NavigationUtil.getPsiElementPopup(psiClasses, new PsiClassListCellRenderer(), title, new PsiElementProcessor<PsiClass>() {
@Override
public boolean execute(@NotNull PsiClass psiClass) {
functionalInterfaceSelected(classes.get(psiClass), enclosingMethods, project, editor, processor, elements);
return true;
}
}).showInBestPositionFor(editor);
return true;
}
return true;
} catch (IncorrectOperationException | PrepareFailedException ignore) {
}
}
return false;
}
use of com.intellij.psi.search.PsiElementProcessor in project intellij-plugins by JetBrains.
the class AngularJSMessageFormatExpression method getExtensionType.
public AngularJSMessageFormatParser.ExtensionType getExtensionType() {
final Ref<PsiElement> ref = new Ref<>();
PsiTreeUtil.processElements(this, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
final boolean isExpressionName = element.getNode().getElementType() == AngularJSElementTypes.MESSAGE_FORMAT_EXPRESSION_NAME;
ref.set(element);
return !isExpressionName;
}
});
final PsiElement typeElement = getExtensionTypeElement();
if (typeElement == null)
return null;
try {
return AngularJSMessageFormatParser.ExtensionType.valueOf(typeElement.getText());
} catch (IllegalArgumentException e) {
return null;
}
}
Aggregations