public static boolean processElement(PsiScopeProcessor processor, PsiNamedElement namedElement) { if (namedElement == null) return true; NameHint nameHint = processor.getHint(NameHint.KEY); String name = nameHint == null ? null : nameHint.getName(ResolveState.initial()); if (name == null || name.equals(namedElement.getName())) { return processor.execute(namedElement, ResolveState.initial()); } return true; }
private void getVariantsFromQualifierType( @NotNull PsiType qualifierType, @NotNull Project project) { final ResolveState state = ResolveState.initial(); if (qualifierType instanceof PsiClassType) { PsiClassType.ClassResolveResult result = ((PsiClassType) qualifierType).resolveGenerics(); PsiClass qualifierClass = result.getElement(); if (qualifierClass != null) { qualifierClass.processDeclarations( myProcessor, state.put(PsiSubstitutor.KEY, result.getSubstitutor()), null, myRefExpr); } } else if (qualifierType instanceof PsiArrayType) { final GrTypeDefinition arrayClass = GroovyPsiManager.getInstance(project) .getArrayClass(((PsiArrayType) qualifierType).getComponentType()); if (arrayClass != null) { if (!arrayClass.processDeclarations(myProcessor, state, null, myRefExpr)) return; } } else if (qualifierType instanceof PsiIntersectionType) { for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) { getVariantsFromQualifierType(conjunct, project); } return; } ResolveUtil.processNonCodeMembers(qualifierType, myProcessor, myRefExpr, state); }
@Override public boolean processDeclarations( @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { // System.out.println ("XqyVarNameElementImpl.processDeclarations: " + toString() + "/" + // getName()); if (!(place instanceof XqyRefVarName)) { return true; } if (getText().equals(place.getText())) { // System.out.println (" candidate VarName: " + toString() + "/" + getText()); return processor.execute(this, ResolveState.initial()); } // TODO: Suppress vars with same name in wider scope if (state == XqyReferenceImpl.variantResolveState) { processor.execute(this, null); } return true; }
private JavaResolveResult advancedResolveInner(final PsiElement psiElement, final String qName) { final PsiManager manager = psiElement.getManager(); final GlobalSearchScope scope = getScope(); if (myIndex == myJavaClassReferenceSet.getReferences().length - 1) { final PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(qName, scope); if (aClass != null) { return new ClassCandidateInfo(aClass, PsiSubstitutor.EMPTY, false, psiElement); } else { if (!JavaClassReferenceProvider.ADVANCED_RESOLVE.getBooleanValue(getOptions())) { return JavaResolveResult.EMPTY; } } } PsiElement resolveResult = JavaPsiFacade.getInstance(manager.getProject()).findPackage(qName); if (resolveResult == null) { resolveResult = JavaPsiFacade.getInstance(manager.getProject()).findClass(qName, scope); } if (myInStaticImport && resolveResult == null) { resolveResult = resolveMember(qName, manager, getElement().getResolveScope()); } if (resolveResult == null) { PsiFile containingFile = psiElement.getContainingFile(); if (containingFile instanceof PsiJavaFile) { if (containingFile instanceof JspFile) { containingFile = containingFile.getViewProvider().getPsi(StdLanguages.JAVA); if (containingFile == null) return JavaResolveResult.EMPTY; } final ClassResolverProcessor processor = new ClassResolverProcessor(getCanonicalText(), psiElement); containingFile.processDeclarations(processor, ResolveState.initial(), null, psiElement); if (processor.getResult().length == 1) { final JavaResolveResult javaResolveResult = processor.getResult()[0]; if (javaResolveResult != JavaResolveResult.EMPTY && getOptions() != null) { final Boolean value = JavaClassReferenceProvider.RESOLVE_QUALIFIED_CLASS_NAME.getValue(getOptions()); final PsiClass psiClass = (PsiClass) javaResolveResult.getElement(); if (value != null && value.booleanValue() && psiClass != null) { final String qualifiedName = psiClass.getQualifiedName(); if (!qName.equals(qualifiedName)) { return JavaResolveResult.EMPTY; } } } return javaResolveResult; } } } return resolveResult != null ? new CandidateInfo(resolveResult, PsiSubstitutor.EMPTY, false, false, psiElement) : JavaResolveResult.EMPTY; }
@Override public boolean processResolveVariants(@NotNull GoScopeProcessor processor) { Collection<GoLabelDefinition> defs = getLabelDefinitions(); for (GoLabelDefinition def : defs) { if (!processor.execute(def, ResolveState.initial())) { return false; } } return true; }
@Nullable public static PsiElement resolveMixin(@NotNull PsiElement base, @NotNull String name) { CssResolveProcessor processor = CssPluginsFacade.getMixinProcessor(base, name); if (processor.executeInScope(base)) { processFile( base.getContainingFile(), processor, ResolveState.initial().put(PROCESSED_PATHS, new HashSet<String>())); } return processor.getResult(); }
public static boolean treeWalkUp(PsiElement place, PsiScopeProcessor processor) { PsiElement lastParent = null; PsiElement run = place; while (run != null) { if (!run.processDeclarations(processor, ResolveState.initial(), lastParent, place)) return false; lastParent = run; run = run.getContext(); // same as getParent } return true; }
public static void processClass( @NotNull GrCall call, PsiClassType type, @Nullable String argumentName, Map<String, ArgumentDescriptor> result) { if (argumentName == null) { ResolveUtil.processAllDeclarations( type, new MyPsiScopeProcessor(result, call), ResolveState.initial(), call); } else { ResolveUtil.processAllDeclarations( type, new MyPsiScopeProcessor(argumentName, true, result, call), ResolveState.initial(), call); ResolveUtil.processAllDeclarations( type, new MyPsiScopeProcessor(argumentName, false, result, call), ResolveState.initial(), call); } }
private void getVariantsFromQualifier(@NotNull GrExpression qualifier) { Project project = qualifier.getProject(); PsiType qualifierType = qualifier.getType(); final ResolveState state = ResolveState.initial(); if (qualifierType == null || qualifierType == PsiType.VOID) { if (qualifier instanceof GrReferenceExpression) { PsiElement resolved = ((GrReferenceExpression) qualifier).resolve(); if (resolved instanceof PsiPackage || resolved instanceof PsiVariable) { resolved.processDeclarations(myProcessor, state, null, myRefExpr); return; } } getVariantsFromQualifierType(TypesUtil.getJavaLangObject(qualifier), project); } else if (qualifierType instanceof PsiIntersectionType) { for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) { getVariantsFromQualifierType(conjunct, project); } } else if (qualifierType instanceof GrTraitType) { GrTypeDefinition definition = ((GrTraitType) qualifierType).getMockTypeDefinition(); if (definition != null) { PsiClassType classType = JavaPsiFacade.getElementFactory(project).createType(definition); getVariantsFromQualifierType(classType, project); } else { getVariantsFromQualifierType(((GrTraitType) qualifierType).getExprType(), project); for (PsiClassType traitType : ((GrTraitType) qualifierType).getTraitTypes()) { getVariantsFromQualifierType(traitType, project); } } } else { getVariantsFromQualifierType(qualifierType, project); if (qualifier instanceof GrReferenceExpression && !PsiUtil.isSuperReference(qualifier) && !PsiUtil.isInstanceThisRef(qualifier)) { PsiElement resolved = ((GrReferenceExpression) qualifier).resolve(); if (resolved instanceof PsiClass) { // //omitted .class GlobalSearchScope scope = myRefExpr.getResolveScope(); PsiClass javaLangClass = PsiUtil.getJavaLangClass(resolved, scope); if (javaLangClass != null) { PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; PsiTypeParameter[] typeParameters = javaLangClass.getTypeParameters(); if (typeParameters.length == 1) { substitutor = substitutor.put(typeParameters[0], qualifierType); } PsiType javaLangClassType = JavaPsiFacade.getElementFactory(myRefExpr.getProject()) .createType(javaLangClass, substitutor); ResolveUtil.processAllDeclarations(javaLangClassType, myProcessor, state, myRefExpr); } } } } }
public static boolean treeWalkUp( @Nullable PsiElement place, @NotNull PsiScopeProcessor processor) { PsiElement lastParent = null; PsiElement run = place; while (run != null) { if (place != run && !run.processDeclarations(processor, ResolveState.initial(), lastParent, place)) return false; lastParent = run; run = run.getParent(); } return true; }
public PsiElement resolve() { final String varName = getName(); if (varName == null) { return null; } BashVarProcessor processor = new BashVarProcessor(this, true); if (!BashPsiUtils.varResolveTreeWalkUp( processor, this, getContainingFile(), ResolveState.initial())) { return processor.getBestResult(false, this); } return null; }
private void getVariantsImpl() { GrExpression qualifier = myRefExpr.getQualifierExpression(); if (qualifier == null) { ResolveUtil.treeWalkUp(myRefExpr, myProcessor, true); for (PsiElement e = myRefExpr.getParent(); e != null; e = e.getParent()) { if (e instanceof GrClosableBlock) { ResolveState state = ResolveState.initial().put(ClassHint.RESOLVE_CONTEXT, e); for (ClosureMissingMethodContributor contributor : ClosureMissingMethodContributor.EP_NAME.getExtensions()) { contributor.processMembers((GrClosableBlock) e, myProcessor, myRefExpr, state); } } } GrExpression runtimeQualifier = PsiImplUtil.getRuntimeQualifier(myRefExpr); if (runtimeQualifier != null) { getVariantsFromQualifier(runtimeQualifier); } getBindings(); } else { if (myRefExpr.getDotTokenType() != GroovyTokenTypes.mSPREAD_DOT) { getVariantsFromQualifier(qualifier); if (qualifier instanceof GrReferenceExpression && ("class".equals(((GrReferenceExpression) qualifier).getReferenceName()) || PsiUtil.isThisReference(qualifier) && !PsiUtil.isInstanceThisRef(qualifier))) { processIfJavaLangClass(qualifier.getType()); } } else { getVariantsFromQualifierForSpreadOperator(qualifier); } } ResolveUtil.processCategoryMembers(myRefExpr, myProcessor, ResolveState.initial()); }
private PsiElement internalResolve() { final String referencedName = getReferencedName(); if (referencedName == null) { return null; } final BashFunctionProcessor processor = new BashFunctionProcessor(referencedName); boolean walkOn = PsiTreeUtil.treeWalkUp(processor, this, getContainingFile(), ResolveState.initial()); if (!walkOn) { return processor.hasResults() ? processor.getBestResult(true, this) : null; } return null; }
@Override public PsiElement resolve() { ErlangVarProcessor processor = new ErlangVarProcessor(myElement.getText(), myElement); ErlangListComprehension lc = PsiTreeUtil.getParentOfType(myElement, ErlangListComprehension.class); ErlangCompositeElement place = lc != null ? lc.getLcExprs() : myElement; ResolveUtil.treeWalkUp(place, processor); ErlangQVar result = processor.getResult(); if (result != null) return result; ErlangModule module = getModule(myElement.getContainingFile()); if (module == null) return null; module.processDeclarations(processor, ResolveState.initial(), module, module); return processor.getResult(); }
public boolean processNestedElements(PsiScopeProcessor processor) { final AntIntrospector introspector = AntDomExtender.getIntrospector(myAntClass); if (introspector != null) { String expectedName = ResolveUtil.getNameHint(processor); final PsiType stringType = getParameterList().getParameters()[1].getType(); final PsiType closureType = getParameterList().getParameters()[2].getType(); for (String name : Collections.list(introspector.getNestedElements())) { if (expectedName == null || expectedName.equals(name)) { final AntBuilderMethod method = new AntBuilderMethod( myPlace, name, closureType, introspector.getElementType(name), stringType); if (!processor.execute(method, ResolveState.initial())) return false; } } } return true; }
@org.jetbrains.annotations.NotNull public Object[] getVariants() { List<Object> variants = Lists.newArrayList(); BashFunctionVariantsProcessor processor = new BashFunctionVariantsProcessor(); PsiTreeUtil.treeWalkUp(processor, this, this.getContainingFile(), ResolveState.initial()); variants.addAll(processor.getFunctionDefs()); if (BashProjectSettings.storedSettings(getProject()).isAutocompleteBuiltinCommands()) { variants.addAll(LanguageBuiltins.commands); } if (BashProjectSettings.storedSettings(getProject()).isSupportBash4()) { variants.addAll(LanguageBuiltins.commands_v4); } return variants.toArray(); }
@NotNull @Override public Collection<StructureViewTreeElement> getChildrenBase() { final NavigatablePsiElement element = getElement(); final List<StructureViewTreeElement> result = new ArrayList<StructureViewTreeElement>(); if (element instanceof DartFile || element instanceof DartEmbeddedContent) { THashSet<DartComponentName> componentNames = new THashSet<DartComponentName>(); DartPsiCompositeElementImpl.processDeclarationsImpl( element, new ComponentNameScopeProcessor(componentNames), ResolveState.initial(), null); for (DartComponentName componentName : componentNames) { PsiElement parent = componentName.getParent(); if (parent instanceof DartComponent) { result.add(new DartStructureViewElement((DartComponent) parent)); } } } else if (element instanceof DartClass) { for (DartComponent subNamedComponent : DartResolveUtil.getNamedSubComponents((DartClass) element)) { result.add(new DartStructureViewElement(subNamedComponent)); } } Collections.sort( result, new Comparator<StructureViewTreeElement>() { @Override public int compare(StructureViewTreeElement o1, StructureViewTreeElement o2) { PsiElement element1, element2; if (o1 instanceof DartStructureViewElement && o2 instanceof DartStructureViewElement && (element1 = ((DartStructureViewElement) o1).getElement()) != null && (element2 = ((DartStructureViewElement) o2).getElement()) != null) { return element1.getTextRange().getStartOffset() - element2.getTextRange().getStartOffset(); } return 0; } }); return result; }
public boolean processDeclarations( @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { if (lastParent != null) { return true; } GrExpression lValue = getLValue(); if (lValue instanceof GrReferenceExpression) { String refName = processor instanceof ResolverProcessor ? ((ResolverProcessor) processor).getName() : null; if (isDeclarationAssignment((GrReferenceExpression) lValue, refName)) { if (!processor.execute(lValue, ResolveState.initial())) return false; } } return true; }
private void processMethods(final MethodResolverProcessor methodResolver) { GrReferenceResolveUtil.resolveImpl(methodResolver, this); if (methodResolver.hasApplicableCandidates()) { return; } // Search in ClosureMissingMethodContributor if (!isQualified() && getContext() instanceof GrMethodCall) { for (PsiElement e = this.getContext(); e != null; e = e.getContext()) { if (e instanceof GrClosableBlock) { ResolveState state = ResolveState.initial().put(ResolverProcessor.RESOLVE_CONTEXT, e); for (ClosureMissingMethodContributor contributor : ClosureMissingMethodContributor.EP_NAME.getExtensions()) { if (!contributor.processMembers((GrClosableBlock) e, methodResolver, this, state)) { return; } } } } } }
@NotNull private static MembersMap buildAllMaps(@NotNull PsiClass psiClass) { final List<Pair<PsiMember, PsiSubstitutor>> classes = new ArrayList<Pair<PsiMember, PsiSubstitutor>>(); final List<Pair<PsiMember, PsiSubstitutor>> fields = new ArrayList<Pair<PsiMember, PsiSubstitutor>>(); final List<Pair<PsiMember, PsiSubstitutor>> methods = new ArrayList<Pair<PsiMember, PsiSubstitutor>>(); FilterScopeProcessor<MethodCandidateInfo> processor = new FilterScopeProcessor<MethodCandidateInfo>( new OrFilter( ElementClassFilter.METHOD, ElementClassFilter.FIELD, ElementClassFilter.CLASS)) { @Override protected void add(PsiElement element, PsiSubstitutor substitutor) { if (element instanceof PsiMethod) { methods.add(Pair.create((PsiMember) element, substitutor)); } else if (element instanceof PsiField) { fields.add(Pair.create((PsiMember) element, substitutor)); } else if (element instanceof PsiClass) { classes.add(Pair.create((PsiMember) element, substitutor)); } } }; processDeclarationsInClassNotCached( psiClass, processor, ResolveState.initial(), null, null, psiClass, false, PsiUtil.getLanguageLevel(psiClass)); MembersMap result = new MembersMap(MemberType.class); result.put(MemberType.CLASS, generateMapByList(classes)); result.put(MemberType.METHOD, generateMapByList(methods)); result.put(MemberType.FIELD, generateMapByList(fields)); return result; }
@NotNull public Object[] getVariants() { List<Object> variants = Lists.newArrayList(); // collect the previously declared variables final BashVarVariantsProcessor processor = new BashVarVariantsProcessor(this); PsiTreeUtil.treeWalkUp(processor, this, this.getContainingFile(), ResolveState.initial()); variants.addAll(createPsiItems(processor.getVariables())); if (BashProjectSettings.storedSettings(getProject()).isAutocompleteBuiltinVars()) { variants.addAll(createItems(LanguageBuiltins.bashShellVars, BashIcons.BASH_VAR_ICON)); variants.addAll(createItems(LanguageBuiltins.bourneShellVars, BashIcons.BOURNE_VAR_ICON)); } if (BashProjectSettings.storedSettings(getProject()).isAutcompleteGlobalVars()) { variants.addAll( createItems( BashProjectSettings.storedSettings(getProject()).getGlobalVariables(), BashIcons.GLOBAL_VAR_ICON)); } return variants.toArray(); }
private void addReferencesFromLocalScopes() { VariableReferenceScopeProcessor processor = new VariableReferenceScopeProcessor(myElement); PsiTreeUtil.treeWalkUp(processor, myElement, null, ResolveState.initial()); if (processor.getResult() != null) matchingVariableNames.add(processor.getResult()); }
private static boolean processDeclarationsInClassNotCached( @NotNull PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable Set<PsiClass> visited, PsiElement last, @NotNull PsiElement place, boolean isRaw, @NotNull LanguageLevel languageLevel) { if (visited == null) visited = new THashSet<PsiClass>(); if (!visited.add(aClass)) return true; processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass); final ElementClassHint classHint = processor.getHint(ElementClassHint.KEY); final NameHint nameHint = processor.getHint(NameHint.KEY); if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.FIELD)) { if (nameHint != null) { final PsiField fieldByName = aClass.findFieldByName(nameHint.getName(state), false); if (fieldByName != null && !processor.execute(fieldByName, state)) return false; } else { final PsiField[] fields = aClass.getFields(); for (final PsiField field : fields) { if (!processor.execute(field, state)) return false; } } } PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory(); if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.METHOD)) { PsiSubstitutor baseSubstitutor = state.get(PsiSubstitutor.KEY); final PsiMethod[] methods = nameHint != null ? aClass.findMethodsByName(nameHint.getName(state), false) : aClass.getMethods(); for (final PsiMethod method : methods) { PsiSubstitutor finalSubstitutor = checkRaw(isRaw, factory, method, baseSubstitutor); ResolveState methodState = finalSubstitutor == baseSubstitutor ? state : state.put(PsiSubstitutor.KEY, finalSubstitutor); if (!processor.execute(method, methodState)) return false; } } if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) { if (last != null && last.getParent() == aClass) { // Parameters final PsiTypeParameterList list = aClass.getTypeParameterList(); if (list != null && !list.processDeclarations(processor, ResolveState.initial(), last, place)) return false; } if (!(last instanceof PsiReferenceList) && !(last instanceof PsiModifierList)) { // Inners if (nameHint != null) { final PsiClass inner = aClass.findInnerClassByName(nameHint.getName(state), false); if (inner != null) { if (!processor.execute(inner, state)) return false; } } else { final PsiClass[] inners = aClass.getInnerClasses(); for (final PsiClass inner : inners) { if (!processor.execute(inner, state)) return false; } } } } return last instanceof PsiReferenceList || processSuperTypes( aClass, processor, visited, last, place, state, isRaw, factory, languageLevel); }
/** * priority: inside class C: local variable, c.method, c.property, c.getter in other places: local * variable, c.method, c.getter, c.property */ @NotNull private GroovyResolveResult[] resolveMethodOrProperty( boolean allVariants, @Nullable GrExpression upToArgument, boolean genericsMatter) { final String name = getReferenceName(); if (name == null) return GroovyResolveResult.EMPTY_ARRAY; PropertyResolverProcessor propertyResolver = new PropertyResolverProcessor(name, this); GrReferenceResolveUtil.resolveImpl(propertyResolver, this); final GroovyResolveResult[] propertyCandidates = propertyResolver.getCandidates(); if (!allVariants) { // search for local variables for (GroovyResolveResult candidate : propertyCandidates) { if (candidate.getElement() instanceof GrVariable && !(candidate.getElement() instanceof GrField)) { return propertyResolver.getCandidates(); } } } final Pair<Boolean, GroovyResolveResult[]> shapeResults = resolveByShape(allVariants, upToArgument); if (!genericsMatter && !allVariants && shapeResults.first) { assertAllAreValid(shapeResults.second); return shapeResults.second; } MethodResolverProcessor methodResolver = null; if (genericsMatter) { methodResolver = createMethodProcessor(allVariants, name, false, upToArgument); for (GroovyResolveResult result : shapeResults.second) { final ResolveState state = ResolveState.initial() .put(PsiSubstitutor.KEY, result.getSubstitutor()) .put(ResolverProcessor.RESOLVE_CONTEXT, result.getCurrentFileResolveContext()) .put(SpreadState.SPREAD_STATE, result.getSpreadState()); methodResolver.execute(result.getElement(), state); } if (!allVariants && methodResolver.hasApplicableCandidates()) { return methodResolver.getCandidates(); } } // search for fields inside its class if (!allVariants) { for (GroovyResolveResult candidate : propertyCandidates) { final PsiElement element = candidate.getElement(); if (element instanceof GrField) { final PsiClass containingClass = ((PsiField) element).getContainingClass(); if (containingClass != null && PsiTreeUtil.isContextAncestor(containingClass, this, true)) return propertyCandidates; } } } List<GroovyResolveResult> allCandidates = new ArrayList<GroovyResolveResult>(); ContainerUtil.addAll(allCandidates, propertyCandidates); ContainerUtil.addAll( allCandidates, genericsMatter ? methodResolver.getCandidates() : shapeResults.second); // search for getters for (String getterName : GroovyPropertyUtils.suggestGettersName(name)) { AccessorResolverProcessor getterResolver = new AccessorResolverProcessor( getterName, name, this, true, genericsMatter, GrReferenceResolveUtil.getQualifierType(this), getTypeArguments()); GrReferenceResolveUtil.resolveImpl(getterResolver, this); final GroovyResolveResult[] candidates = getterResolver.getCandidates(); // can be only one candidate if (!allVariants && candidates.length == 1) { return candidates; } ContainerUtil.addAll(allCandidates, candidates); } if (allCandidates.size() > 0) { return allCandidates.toArray(new GroovyResolveResult[allCandidates.size()]); } return GroovyResolveResult.EMPTY_ARRAY; }