Search in sources :

Example 16 with HashSet

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

the class GrChangeSignatureUsageProcessor method processPrimaryMethodInner.

private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
    if (changeInfo.isNameChanged()) {
        String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
        if (newName != null && !newName.equals(method.getName())) {
            method.setName(changeInfo.getNewName());
        }
    }
    final GrModifierList modifierList = method.getModifierList();
    if (changeInfo.isVisibilityChanged()) {
        modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
    }
    PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;
    final PsiMethod context = changeInfo.getMethod();
    GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
    if (changeInfo.isReturnTypeChanged()) {
        CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
        if (newReturnType == null) {
            if (oldReturnTypeElement != null) {
                oldReturnTypeElement.delete();
                if (modifierList.getModifiers().length == 0) {
                    modifierList.setModifierProperty(GrModifier.DEF, true);
                }
            }
        } else {
            PsiType type = newReturnType.getType(context, method.getManager());
            GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(substitutor.substitute(type)));
            if (oldReturnTypeElement == null) {
                modifierList.setModifierProperty(GrModifier.DEF, false);
            }
        }
    }
    JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
    final GrParameterList parameterList = method.getParameterList();
    GrParameter[] oldParameters = parameterList.getParameters();
    final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null;
    Set<GrParameter> toRemove = new HashSet<>(oldParameters.length);
    ContainerUtil.addAll(toRemove, oldParameters);
    GrParameter anchor = null;
    final GrDocComment docComment = method.getDocComment();
    final GrDocTag[] tags = docComment == null ? null : docComment.getTags();
    int newParamIndex = 0;
    for (JavaParameterInfo newParameter : newParameters) {
        //if old parameter name differs from base method parameter name we don't change it
        final String newName;
        final int oldIndex = newParameter.getOldIndex();
        if (oldIndex >= 0 && oldBaseParams != null) {
            final String oldName = oldParameters[oldIndex].getName();
            if (oldName.equals(oldBaseParams[oldIndex].getName())) {
                newName = newParameter.getName();
            } else {
                newName = oldName;
            }
        } else {
            newName = newParameter.getName();
        }
        final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;
        if (docComment != null && oldParameter != null) {
            final String oldName = oldParameter.getName();
            for (GrDocTag tag : tags) {
                if ("@param".equals(tag.getName())) {
                    final GrDocParameterReference parameterReference = tag.getDocParameterReference();
                    if (parameterReference != null && oldName.equals(parameterReference.getText())) {
                        parameterReference.handleElementRename(newName);
                    }
                }
            }
        }
        GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName);
        if (oldParameter != null) {
            grParameter.getModifierList().replace(oldParameter.getModifierList());
        }
        if ("def".equals(newParameter.getTypeText())) {
            grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
        } else if (StringUtil.isEmpty(newParameter.getTypeText())) {
            grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
        }
        anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
        if (newParamIndex < oldParameters.length) {
            GrParameter oldParam = oldParameters[newParamIndex];
            PsiElement prev = oldParam.getPrevSibling();
            if (prev instanceof PsiWhiteSpace) {
                parameterList.addBefore(prev, anchor);
            }
        }
        newParamIndex++;
    }
    for (GrParameter oldParameter : toRemove) {
        oldParameter.delete();
    }
    JavaCodeStyleManager.getInstance(parameterList.getProject()).shortenClassReferences(parameterList);
    CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);
    if (changeInfo.isExceptionSetOrOrderChanged()) {
        final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
        PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
        for (int i = 0; i < infos.length; i++) {
            ThrownExceptionInfo info = infos[i];
            exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
        }
        PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
        thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
        JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
        CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
    }
    return true;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) GrDocParameterReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocParameterReference) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)

Example 17 with HashSet

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

the class RepositoryLibrarySynchronizer method collectLibraries.

private static Collection<Library> collectLibraries(@NotNull final Project project, @NotNull final Predicate<Library> predicate) {
    final HashSet<Library> result = new HashSet<>();
    ApplicationManager.getApplication().runReadAction(() -> {
        for (final Module module : ModuleManager.getInstance(project).getModules()) {
            OrderEnumerator.orderEntries(module).withoutSdk().forEachLibrary(library -> {
                if (predicate.apply(library)) {
                    result.add(library);
                }
                return true;
            });
        }
        for (Library library : ProjectLibraryTable.getInstance(project).getLibraries()) {
            if (predicate.apply(library)) {
                result.add(library);
            }
        }
    });
    return result;
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) HashSet(com.intellij.util.containers.HashSet)

Example 18 with HashSet

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

the class ModuleContextProvider method getModules.

public static Module[] getModules(@Nullable PsiFile context) {
    if (context == null)
        return Module.EMPTY_ARRAY;
    final Set<Module> modules = new HashSet<>();
    for (ModuleContextProvider moduleContextProvider : Extensions.getExtensions(EP_NAME)) {
        ContainerUtil.addAllNotNull(modules, moduleContextProvider.getContextModules(context));
    }
    Module module = ModuleUtilCore.findModuleForPsiElement(context);
    if (module != null)
        modules.add(module);
    return modules.toArray(new Module[modules.size()]);
}
Also used : Module(com.intellij.openapi.module.Module) HashSet(com.intellij.util.containers.HashSet)

Example 19 with HashSet

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

the class PyStarImportElementImpl method iterateNames.

@NotNull
public Iterable<PyElement> iterateNames() {
    if (getParent() instanceof PyFromImportStatement) {
        PyFromImportStatement fromImportStatement = (PyFromImportStatement) getParent();
        final List<PsiElement> importedFiles = fromImportStatement.resolveImportSourceCandidates();
        ChainIterable<PyElement> chain = new ChainIterable<>();
        for (PsiElement importedFile : new HashSet<>(importedFiles)) {
            // resolver gives lots of duplicates
            final PsiElement source = PyUtil.turnDirIntoInit(importedFile);
            if (source instanceof PyFile) {
                final PyFile sourceFile = (PyFile) source;
                chain.add(filterStarImportableNames(sourceFile.iterateNames(), sourceFile));
            }
        }
        return chain;
    }
    return Collections.emptyList();
}
Also used : ChainIterable(com.jetbrains.python.toolbox.ChainIterable) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with HashSet

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

the class ClassRenderer method buildChildren.

@Override
public void buildChildren(final Value value, final ChildrenBuilder builder, final EvaluationContext evaluationContext) {
    DebuggerManagerThreadImpl.assertIsManagerThread();
    final ValueDescriptorImpl parentDescriptor = (ValueDescriptorImpl) builder.getParentDescriptor();
    final NodeManager nodeManager = builder.getNodeManager();
    final NodeDescriptorFactory nodeDescriptorFactory = builder.getDescriptorManager();
    List<DebuggerTreeNode> children = new ArrayList<>();
    if (value instanceof ObjectReference) {
        final ObjectReference objRef = (ObjectReference) value;
        final ReferenceType refType = objRef.referenceType();
        // default ObjectReference processing
        List<Field> fields = refType.allFields();
        if (!fields.isEmpty()) {
            Set<String> names = new HashSet<>();
            for (Field field : fields) {
                if (shouldDisplay(evaluationContext, objRef, field)) {
                    FieldDescriptor fieldDescriptor = createFieldDescriptor(parentDescriptor, nodeDescriptorFactory, objRef, field, evaluationContext);
                    String name = fieldDescriptor.getName();
                    if (names.contains(name)) {
                        fieldDescriptor.putUserData(FieldDescriptor.SHOW_DECLARING_TYPE, Boolean.TRUE);
                    } else {
                        names.add(name);
                    }
                    children.add(nodeManager.createNode(fieldDescriptor, evaluationContext));
                }
            }
            if (children.isEmpty()) {
                children.add(nodeManager.createMessageNode(DebuggerBundle.message("message.node.class.no.fields.to.display")));
            } else if (XDebuggerSettingsManager.getInstance().getDataViewSettings().isSortValues()) {
                children.sort(NodeManagerImpl.getNodeComparator());
            }
        } else {
            children.add(nodeManager.createMessageNode(MessageDescriptor.CLASS_HAS_NO_FIELDS.getLabel()));
        }
    }
    builder.setChildren(children);
}
Also used : ArrayList(java.util.ArrayList) ValueDescriptorImpl(com.intellij.debugger.ui.impl.watch.ValueDescriptorImpl) 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