Search in sources :

Example 56 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class ScopeTreeViewPanel method getSelectedPsiElements.

@NotNull
private PsiElement[] getSelectedPsiElements() {
    final TreePath[] treePaths = myTree.getSelectionPaths();
    if (treePaths != null) {
        Set<PsiElement> result = new HashSet<>();
        for (TreePath path : treePaths) {
            final Object component = path.getLastPathComponent();
            if (component instanceof PackageDependenciesNode) {
                PackageDependenciesNode node = (PackageDependenciesNode) component;
                final PsiElement psiElement = node.getPsiElement();
                if (psiElement != null && psiElement.isValid()) {
                    result.add(psiElement);
                }
            }
        }
        return PsiUtilCore.toPsiElementArray(result);
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : TreePath(javax.swing.tree.TreePath) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class SafeDeleteHandler method invoke.

public static void invoke(final Project project, PsiElement[] elements, @Nullable Module module, boolean checkDelegates, @Nullable final Runnable successRunnable) {
    for (PsiElement element : elements) {
        if (!SafeDeleteProcessor.validElement(element)) {
            return;
        }
    }
    final PsiElement[] temptoDelete = PsiTreeUtil.filterAncestors(elements);
    Set<PsiElement> elementsSet = new HashSet<>(Arrays.asList(temptoDelete));
    Set<PsiElement> fullElementsSet = new LinkedHashSet<>();
    if (checkDelegates) {
        for (PsiElement element : temptoDelete) {
            boolean found = false;
            for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
                if (delegate.handlesElement(element)) {
                    found = true;
                    Collection<? extends PsiElement> addElements = delegate instanceof SafeDeleteProcessorDelegateBase ? ((SafeDeleteProcessorDelegateBase) delegate).getElementsToSearch(element, module, elementsSet) : delegate.getElementsToSearch(element, elementsSet);
                    if (addElements == null)
                        return;
                    fullElementsSet.addAll(addElements);
                    break;
                }
            }
            if (!found) {
                fullElementsSet.add(element);
            }
        }
    } else {
        ContainerUtil.addAll(fullElementsSet, temptoDelete);
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, fullElementsSet, true))
        return;
    final PsiElement[] elementsToDelete = PsiUtilCore.toPsiElementArray(fullElementsSet);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        RefactoringSettings settings = RefactoringSettings.getInstance();
        SafeDeleteProcessor.createInstance(project, null, elementsToDelete, settings.SAFE_DELETE_SEARCH_IN_COMMENTS, settings.SAFE_DELETE_SEARCH_IN_NON_JAVA, true).run();
        if (successRunnable != null)
            successRunnable.run();
    } else {
        final SafeDeleteDialog.Callback callback = new SafeDeleteDialog.Callback() {

            @Override
            public void run(final SafeDeleteDialog dialog) {
                SafeDeleteProcessor.createInstance(project, () -> {
                    if (successRunnable != null) {
                        successRunnable.run();
                    }
                    dialog.close(DialogWrapper.CANCEL_EXIT_CODE);
                }, elementsToDelete, dialog.isSearchInComments(), dialog.isSearchForTextOccurences(), true).run();
            }
        };
        SafeDeleteDialog dialog = new SafeDeleteDialog(project, elementsToDelete, callback);
        dialog.show();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RefactoringSettings(com.intellij.refactoring.RefactoringSettings) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 58 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class AbstractTreeUi method getChildrenFor.

//todo [kirillk] temporary consistency check
private Object[] getChildrenFor(final Object element) {
    final Ref<Object[]> passOne = new Ref<>();
    try (LockToken ignored = acquireLock()) {
        execute(new TreeRunnable("AbstractTreeUi.getChildrenFor") {

            @Override
            public void perform() {
                passOne.set(getTreeStructure().getChildElements(element));
            }
        });
    } catch (IndexNotReadyException e) {
        warnOnIndexNotReady(e);
        return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
    if (!Registry.is("ide.tree.checkStructure"))
        return passOne.get();
    final Object[] passTwo = getTreeStructure().getChildElements(element);
    final HashSet<Object> two = new HashSet<>(Arrays.asList(passTwo));
    if (passOne.get().length != passTwo.length) {
        LOG.error("AbstractTreeStructure.getChildren() must either provide same objects or new objects but with correct hashCode() and equals() methods. Wrong parent element=" + element);
    } else {
        for (Object eachInOne : passOne.get()) {
            if (!two.contains(eachInOne)) {
                LOG.error("AbstractTreeStructure.getChildren() must either provide same objects or new objects but with correct hashCode() and equals() methods. Wrong parent element=" + element);
                break;
            }
        }
    }
    return passOne.get();
}
Also used : LockToken(com.intellij.util.concurrency.LockToken) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet)

Example 59 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class SearchResults method updateExcluded.

private void updateExcluded() {
    Set<RangeMarker> invalid = new HashSet<>();
    for (RangeMarker marker : myExcluded) {
        if (!marker.isValid()) {
            invalid.add(marker);
            marker.dispose();
        }
    }
    myExcluded.removeAll(invalid);
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) HashSet(com.intellij.util.containers.HashSet)

Example 60 with HashSet

use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.

the class GenericInlineHandler method invoke.

public static boolean invoke(final PsiElement element, @Nullable Editor editor, final InlineHandler languageSpecific) {
    final PsiReference invocationReference = editor != null ? TargetElementUtil.findReference(editor) : null;
    final InlineHandler.Settings settings = languageSpecific.prepareInlineElement(element, editor, invocationReference != null);
    if (settings == null || settings == InlineHandler.Settings.CANNOT_INLINE_SETTINGS) {
        return settings != null;
    }
    final Collection<? extends PsiReference> allReferences;
    if (settings.isOnlyOneReferenceToInline()) {
        allReferences = Collections.singleton(invocationReference);
    } else {
        final Ref<Collection<? extends PsiReference>> usagesRef = new Ref<>();
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> usagesRef.set(ReferencesSearch.search(element).findAll()), "Find Usages", false, element.getProject());
        allReferences = usagesRef.get();
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final Map<Language, InlineHandler.Inliner> inliners = initializeInliners(element, settings, allReferences);
    for (PsiReference reference : allReferences) {
        collectConflicts(reference, element, inliners, conflicts);
    }
    final Project project = element.getProject();
    if (!conflicts.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
        } else {
            final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts);
            if (!conflictsDialog.showAndGet()) {
                return true;
            }
        }
    }
    HashSet<PsiElement> elements = new HashSet<>();
    for (PsiReference reference : allReferences) {
        PsiElement refElement = reference.getElement();
        if (refElement != null) {
            elements.add(refElement);
        }
    }
    if (!settings.isOnlyOneReferenceToInline()) {
        elements.add(element);
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, elements, true)) {
        return true;
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        final String subj = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : "element";
        CommandProcessor.getInstance().executeCommand(project, () -> {
            final PsiReference[] references = sortDepthFirstRightLeftOrder(allReferences);
            final UsageInfo[] usages = new UsageInfo[references.length];
            for (int i = 0; i < references.length; i++) {
                usages[i] = new UsageInfo(references[i]);
            }
            for (UsageInfo usage : usages) {
                inlineReference(usage, element, inliners);
            }
            if (!settings.isOnlyOneReferenceToInline()) {
                languageSpecific.removeDefinition(element, settings);
            }
        }, RefactoringBundle.message("inline.command", StringUtil.notNullize(subj, "<nameless>")), null);
    });
    return true;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiReference(com.intellij.psi.PsiReference) MultiMap(com.intellij.util.containers.MultiMap) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) Language(com.intellij.lang.Language) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) InlineHandler(com.intellij.lang.refactoring.InlineHandler) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Aggregations

HashSet (com.intellij.util.containers.HashSet)162 NotNull (org.jetbrains.annotations.NotNull)50 VirtualFile (com.intellij.openapi.vfs.VirtualFile)31 File (java.io.File)22 PsiElement (com.intellij.psi.PsiElement)20 Module (com.intellij.openapi.module.Module)19 ArrayList (java.util.ArrayList)18 Project (com.intellij.openapi.project.Project)17 THashSet (gnu.trove.THashSet)15 Nullable (org.jetbrains.annotations.Nullable)14 HashMap (com.intellij.util.containers.HashMap)13 IOException (java.io.IOException)13 PsiFile (com.intellij.psi.PsiFile)12 UsageInfo (com.intellij.usageView.UsageInfo)12 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 MultiMap (com.intellij.util.containers.MultiMap)11 Pair (com.intellij.openapi.util.Pair)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)6 Document (com.intellij.openapi.editor.Document)5