Search in sources :

Example 51 with GlobalSearchScope

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

the class MavenGroovyPomScriptType method doPatchResolveScope.

public GlobalSearchScope doPatchResolveScope(@NotNull GroovyFile file, @NotNull GlobalSearchScope baseScope) {
    final Module module = ModuleUtilCore.findModuleForPsiElement(file);
    if (module == null) {
        return baseScope;
    }
    Project project = module.getProject();
    GlobalSearchScope result = baseScope;
    CachedValuesManager cachedValuesManager = CachedValuesManager.getManager(file.getProject());
    Boolean hasGroovyModuleLib = cachedValuesManager.getCachedValue(file.getProject(), () -> CachedValueProvider.Result.createSingleDependency(hasModuleWithGroovyLibrary(project), ProjectRootManagerEx.getInstanceEx(project)));
    if (hasGroovyModuleLib) {
        final Collection<VirtualFile> files = additionalScopeFiles();
        result = result.uniteWith(new NonClasspathDirectoriesScope(files));
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonClasspathDirectoriesScope(com.intellij.psi.search.NonClasspathDirectoriesScope) Project(com.intellij.openapi.project.Project) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module)

Example 52 with GlobalSearchScope

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

the class GrChageSignatureUsageSearcher method findSimpleUsagesWithoutParameters.

private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod overridingMethod : overridingMethods) {
        if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
            result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
        }
    }
    boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
    if (needToChangeCalls) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference ref : refs) {
            PsiElement element = ref.getElement();
            if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
                continue;
            boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
            if (PsiUtil.isMethodUsage(element)) {
                result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
            } else if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(ref.getElement()));
            } else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
                DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
                result.add(implicitUsageInfo);
            } else if (element instanceof PsiClass) {
                LOG.assertTrue(method.isConstructor());
                final PsiClass psiClass = (PsiClass) element;
                if (psiClass instanceof GrAnonymousClassDefinition) {
                    result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
                    continue;
                }
                /*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
          if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
                                                 ((JavaChangeInfoImpl)myChangeInfo).propagateParametersMethods)) {
            continue;
          }
          if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
                                                 ((JavaChangeInfoImpl)myChangeInfo).propagateExceptionsMethods)) {
            continue;
          }*/
                result.add(new NoConstructorClassUsageInfo(psiClass));
            } else if (ref instanceof PsiCallReference) {
                result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
            } else {
                result.add(new MoveRenameUsageInfo(element, ref, method));
            }
        }
    } else if (myChangeInfo.isParameterTypesChanged()) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference reference : refs) {
            final PsiElement element = reference.getElement();
            if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(reference));
            }
        }
    }
    // Conflicts
    if (method instanceof GrMethod) {
        detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
    }
    for (final PsiMethod overridingMethod : overridingMethods) {
        if (overridingMethod instanceof GrMethod) {
            detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
        }
    }
    return overridingMethods;
}
Also used : GrDocTagValueToken(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTagValueToken) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Example 53 with GlobalSearchScope

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

the class DuplicateStringLiteralInspection method getCandidateFiles.

@NotNull
private Set<PsiFile> getCandidateFiles(String stringToFind, Project project) {
    final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
    final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
    final List<String> words = StringUtil.getWordsInStringLongestFirst(stringToFind);
    if (words.isEmpty())
        return Collections.emptySet();
    Set<PsiFile> resultFiles = null;
    for (String word : words) {
        if (word.length() < MIN_STRING_LENGTH) {
            continue;
        }
        ProgressManager.checkCanceled();
        final Set<PsiFile> files = new THashSet<>();
        Processor<PsiFile> processor = Processors.cancelableCollectProcessor(files);
        searchHelper.processAllFilesWithWordInLiterals(word, scope, processor);
        if (resultFiles == null) {
            resultFiles = files;
        } else {
            resultFiles.retainAll(files);
        }
        if (resultFiles.isEmpty())
            return Collections.emptySet();
    }
    return resultFiles != null ? resultFiles : Collections.emptySet();
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with GlobalSearchScope

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

the class I18nInspection method isArgOfJUnitAssertion.

private static boolean isArgOfJUnitAssertion(PsiExpression expression) {
    final PsiElement parent = expression.getParent();
    if (!(parent instanceof PsiExpressionList)) {
        return false;
    }
    final PsiElement grandparent = parent.getParent();
    if (!(grandparent instanceof PsiMethodCallExpression)) {
        return false;
    }
    final PsiMethodCallExpression call = (PsiMethodCallExpression) grandparent;
    final PsiReferenceExpression methodExpression = call.getMethodExpression();
    @NonNls final String methodName = methodExpression.getReferenceName();
    if (methodName == null) {
        return false;
    }
    if (!methodName.startsWith("assert") && !methodName.equals("fail")) {
        return false;
    }
    final PsiMethod method = call.resolveMethod();
    if (method == null) {
        return false;
    }
    final PsiClass containingClass = method.getContainingClass();
    if (containingClass == null) {
        return false;
    }
    final Project project = expression.getProject();
    final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
    final PsiClass junitAssert = JavaPsiFacade.getInstance(project).findClass("junit.framework.Assert", scope);
    return junitAssert != null && !containingClass.isInheritor(junitAssert, true);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 55 with GlobalSearchScope

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

the class FormSourceCodeGenerator method generateSetupCodeForComponent.

private void generateSetupCodeForComponent(final LwComponent component, final HashMap<LwComponent, String> component2TempVariable, final TObjectIntHashMap<String> class2variableIndex, final HashMap<String, LwComponent> id2component, final Module module, final PsiClass aClass) throws CodeGenerationException {
    id2component.put(component.getId(), component);
    GlobalSearchScope globalSearchScope = module.getModuleWithDependenciesAndLibrariesScope(false);
    final LwContainer parent = component.getParent();
    final String variable = getVariable(component, component2TempVariable, class2variableIndex, aClass);
    final String componentClass = component instanceof LwNestedForm ? getNestedFormClass(module, (LwNestedForm) component) : getComponentLayoutGenerator(component.getParent()).mapComponentClass(component.getComponentClassName());
    if (component.isCustomCreate() && component.getBinding() == null) {
        throw new CodeGenerationException(component.getId(), UIDesignerBundle.message("error.custom.create.no.binding"));
    }
    if (!component.isCustomCreate()) {
        final String binding = component.getBinding();
        if (binding != null) {
            myBuffer.append(binding);
        } else {
            myBuffer.append("final ");
            myBuffer.append(componentClass);
            myBuffer.append(" ");
            myBuffer.append(variable);
        }
        myBuffer.append('=');
        startConstructor(componentClass);
        // will finish the line
        endConstructor();
    }
    if (component instanceof LwContainer) {
        final LwContainer container = (LwContainer) component;
        if (!container.isCustomCreate() || container.getComponentCount() > 0) {
            getComponentLayoutGenerator(container).generateContainerLayout(container, this, variable);
        }
    }
    // introspected properties
    final LwIntrospectedProperty[] introspectedProperties = component.getAssignedIntrospectedProperties();
    // see SCR #35990
    Arrays.sort(introspectedProperties, (p1, p2) -> p1.getName().compareTo(p2.getName()));
    for (final LwIntrospectedProperty property : introspectedProperties) {
        if (property instanceof LwIntroComponentProperty) {
            // component properties are processed in second pass
            continue;
        }
        Object value = component.getPropertyValue(property);
        //noinspection HardCodedStringLiteral
        final boolean isTextWithMnemonicProperty = "text".equals(property.getName()) && (isAssignableFrom(AbstractButton.class.getName(), componentClass, globalSearchScope) || isAssignableFrom(JLabel.class.getName(), componentClass, globalSearchScope));
        // handle resource bundles
        if (property instanceof LwRbIntroStringProperty) {
            final StringDescriptor descriptor = (StringDescriptor) value;
            if (descriptor.getValue() == null) {
                if (isTextWithMnemonicProperty) {
                    if (isAssignableFrom(AbstractButton.class.getName(), componentClass, globalSearchScope)) {
                        myNeedLoadButtonText = true;
                        startMethodCall("this", AsmCodeGenerator.LOAD_BUTTON_TEXT_METHOD);
                        pushVar(variable);
                        push(descriptor);
                        endMethod();
                    } else {
                        myNeedLoadLabelText = true;
                        startMethodCall("this", AsmCodeGenerator.LOAD_LABEL_TEXT_METHOD);
                        pushVar(variable);
                        push(descriptor);
                        endMethod();
                    }
                } else {
                    startMethodCall(variable, property.getWriteMethodName());
                    push(descriptor);
                    endMethod();
                }
                continue;
            } else {
                value = descriptor.getValue();
            }
        } else if (property instanceof LwIntroListModelProperty) {
            generateListModelProperty(property, class2variableIndex, aClass, value, variable);
            continue;
        }
        SupportCode.TextWithMnemonic textWithMnemonic = null;
        if (isTextWithMnemonicProperty) {
            textWithMnemonic = SupportCode.parseText((String) value);
            value = textWithMnemonic.myText;
        }
        final String propertyClass = property.getPropertyClassName();
        if (propertyClass.equals(Color.class.getName())) {
            ColorDescriptor descriptor = (ColorDescriptor) value;
            if (!descriptor.isColorSet())
                continue;
        }
        startMethodCall(variable, property.getWriteMethodName());
        if (propertyClass.equals(Dimension.class.getName())) {
            newDimension((Dimension) value);
        } else if (propertyClass.equals(Integer.class.getName())) {
            push(((Integer) value).intValue());
        } else if (propertyClass.equals(Double.class.getName())) {
            push(((Double) value).doubleValue());
        } else if (propertyClass.equals(Float.class.getName())) {
            push(((Float) value).floatValue());
        } else if (propertyClass.equals(Long.class.getName())) {
            push(((Long) value).longValue());
        } else if (propertyClass.equals(Short.class.getName())) {
            push(((Short) value).shortValue());
        } else if (propertyClass.equals(Byte.class.getName())) {
            push(((Byte) value).byteValue());
        } else if (propertyClass.equals(Character.class.getName())) {
            push(((Character) value).charValue());
        } else if (propertyClass.equals(Boolean.class.getName())) {
            push(((Boolean) value).booleanValue());
        } else if (propertyClass.equals(Rectangle.class.getName())) {
            newRectangle((Rectangle) value);
        } else if (propertyClass.equals(Insets.class.getName())) {
            newInsets((Insets) value);
        } else if (propertyClass.equals(String.class.getName())) {
            push((String) value);
        } else if (propertyClass.equals(Color.class.getName())) {
            pushColor((ColorDescriptor) value);
        } else if (propertyClass.equals(Font.class.getName())) {
            pushFont(variable, (FontDescriptor) value, property.getReadMethodName());
        } else if (propertyClass.equals(Icon.class.getName())) {
            pushIcon((IconDescriptor) value);
        } else if (property instanceof LwIntroEnumProperty) {
            pushVar(propertyClass.replace('$', '.') + "." + value.toString());
        } else {
            throw new RuntimeException("unexpected property class: " + propertyClass);
        }
        endMethod();
        if (!isTextWithMnemonicProperty) {
            continue;
        }
        if (textWithMnemonic.myMnemonicIndex == -1) {
            continue;
        }
        if (isAssignableFrom(AbstractButton.class.getName(), componentClass, globalSearchScope)) {
            generateSetMnemonic(variable, textWithMnemonic, module, "setMnemonic", AbstractButton.class);
        } else if (isAssignableFrom(JLabel.class.getName(), componentClass, globalSearchScope)) {
            generateSetMnemonic(variable, textWithMnemonic, module, "setDisplayedMnemonic", JLabel.class);
        }
    }
    generateClientProperties(component, variable);
    // add component to parent
    if (!(component.getParent() instanceof LwRootContainer)) {
        final String parentVariable = getVariable(parent, component2TempVariable, class2variableIndex, aClass);
        String componentVar = variable;
        if (component instanceof LwNestedForm) {
            componentVar = variable + "." + AsmCodeGenerator.GET_ROOT_COMPONENT_METHOD_NAME + "()";
        }
        getComponentLayoutGenerator(component.getParent()).generateComponentLayout(component, this, componentVar, parentVariable);
    }
    if (component instanceof LwContainer) {
        final LwContainer container = (LwContainer) component;
        generateBorder(container, variable);
        for (int i = 0; i < container.getComponentCount(); i++) {
            generateSetupCodeForComponent((LwComponent) container.getComponent(i), component2TempVariable, class2variableIndex, id2component, module, aClass);
        }
    }
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SupportCode(com.intellij.uiDesigner.core.SupportCode)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15