Java Code Examples for com.intellij.psi.ResolveResult#getElement()

The following examples show how to use com.intellij.psi.ResolveResult#getElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Symfony2InterfacesUtil.java    From Thinkphp5-Plugin with MIT License 6 votes vote down vote up
/**
 * 根据方法引用获取方法
 * Single resolve doesnt work if we have non unique class names in project context,
 * so try a multiResolve and use first matched method
 */
@Nullable
public static Method getMultiResolvedMethod(PsiReference psiReference) {
    if (psiReference == null) return null;
    // class be unique in normal case, so try this first
    PsiElement resolvedReference = psiReference.resolve();
    if (resolvedReference instanceof Method) {
        return (Method) resolvedReference;
    }

    // try multiResolve if class exists twice in project
    if (psiReference instanceof PsiPolyVariantReference) {
        for (ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) {
            PsiElement element = resolveResult.getElement();
            if (element instanceof Method) {
                return (Method) element;
            }
        }
    }

    return null;
}
 
Example 2
Source File: Symfony2InterfacesUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
/**
 * Single resolve doesnt work if we have non unique class names in project context,
 * so try a multiResolve and use first matched method
 */
@Nullable
public static Method getMultiResolvedMethod(PsiReference psiReference) {

    // class be unique in normal case, so try this first
    PsiElement resolvedReference = psiReference.resolve();
    if (resolvedReference instanceof Method) {
        return (Method) resolvedReference;
    }

    // try multiResolve if class exists twice in project
    if(psiReference instanceof PsiPolyVariantReference) {
        for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) {
            PsiElement element = resolveResult.getElement();
            if(element instanceof Method) {
                return (Method) element;
            }
        }
    }

    return null;
}
 
Example 3
Source File: LattePhpUtil.java    From intellij-latte with MIT License 6 votes vote down vote up
public static boolean isReferenceTo(@NotNull PhpClass originalClass, @NotNull ResolveResult[] results, @NotNull PsiElement element, @NotNull String name) {
    for (ResolveResult result : results) {
        if (!(result.getElement() instanceof BaseLattePhpElement)) {
            continue;
        }

        if (!name.equals(((BaseLattePhpElement) result.getElement()).getPhpElementName())) {
            continue;
        }

        Collection<PhpClass> phpClasses = ((BaseLattePhpElement) result.getElement()).getPhpType().getPhpClasses(element.getProject());
        if (phpClasses.size() == 0) {
            continue;
        }

        for (PhpClass phpClass : phpClasses) {
            if (isReferenceFor(originalClass, phpClass)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: CS1614.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
private static CSharpTypeDeclaration hasElementWithName(ResolveResult[] resolveResults, String ref)
{
	for(ResolveResult resolveResult : resolveResults)
	{
		if(!resolveResult.isValidResult())
		{
			continue;
		}
		PsiElement resolveResultElement = resolveResult.getElement();
		if(resolveResultElement instanceof CSharpTypeDeclaration && Comparing.equal(((CSharpTypeDeclaration) resolveResultElement).getName(),
				ref))
		{
			return (CSharpTypeDeclaration) resolveResultElement;
		}
	}
	return null;
}
 
Example 5
Source File: StaticVsInstanceComparator.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
private int getWeightByTypeArguments(ResolveResult o1)
{
	PsiElement element = o1.getElement();
	if(element instanceof DotNetGenericParameterListOwner)
	{
		int genericParametersCount = ((DotNetGenericParameterListOwner) element).getGenericParametersCount();
		int weight = genericParametersCount * 10;
		if(genericParametersCount == myTypeArgumentsSize)
		{
			weight += 1000;
		}
		return weight;
	}
	return 0;
}
 
Example 6
Source File: CSharpReferenceExpressionImplUtil.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
private static boolean isReferenceTo(@Nonnull ResolveResult resolveResult, PsiElement element)
{
	PsiElement psiElement = resolveResult.getElement();
	if(element instanceof DotNetNamespaceAsElement && psiElement instanceof DotNetNamespaceAsElement)
	{
		if(Comparing.equal(((DotNetNamespaceAsElement) psiElement).getPresentableQName(), ((DotNetNamespaceAsElement) element).getPresentableQName()))
		{
			return true;
		}
	}

	if(element.getManager().areElementsEquivalent(element, psiElement))
	{
		return true;
	}
	return false;
}
 
Example 7
Source File: Symfony2InterfacesUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 * Single resolve doesnt work if we have non unique class names in project context,
 * so try a multiResolve
 */
@Nullable
public static Method[] getMultiResolvedMethod(PsiReference psiReference) {

    // class be unique in normal case, so try this first
    PsiElement resolvedReference = psiReference.resolve();
    if (resolvedReference instanceof Method) {
        return new Method[] { (Method) resolvedReference };
    }

    // try multiResolve if class exists twice in project
    if(psiReference instanceof PsiPolyVariantReference) {
        Collection<Method> methods = new HashSet<>();
        for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) {
            PsiElement element = resolveResult.getElement();
            if(element instanceof Method) {
                methods.add((Method) element);
            }
        }

        if(methods.size() > 0) {
            return methods.toArray(new Method[methods.size()]);
        }

    }

    return null;
}
 
Example 8
Source File: XQueryParameterInfoHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
private List<XQueryFunctionDecl> extractFunctionDeclarations(ResolveResult[] resolveResults) {
    List<XQueryFunctionDecl> functionDeclarations = new ArrayList<XQueryFunctionDecl>();
    for (ResolveResult result : resolveResults) {
        PsiElement element = result.getElement();
        if (element instanceof XQueryFunctionName) {
            functionDeclarations.add((XQueryFunctionDecl) element.getParent());
        }
    }
    return functionDeclarations;
}
 
Example 9
Source File: CSharpGenericParameterInfoHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
private static DotNetGenericParameterListOwner findGenericParameterOwner(ParameterInfoContext context)
{
	final PsiElement at = context.getFile().findElementAt(context.getEditor().getCaretModel().getOffset());
	if(at == null)
	{
		return null;
	}

	DotNetTypeList typeList = PsiTreeUtil.getParentOfType(at, DotNetTypeList.class);
	if(typeList == null)
	{
		return null;
	}

	PsiElement parent = typeList.getParent();
	if(!(parent instanceof CSharpReferenceExpression))
	{
		return null;
	}

	int argumentsSize = DotNetPsiCountUtil.countChildrenOfType(typeList.getNode(), CSharpTokens.COMMA) + 1;

	CSharpReferenceExpression referenceExpression = (CSharpReferenceExpression) parent;
	ResolveResult[] resolveResults = referenceExpression.multiResolve(true);
	for(ResolveResult resolveResult : resolveResults)
	{
		PsiElement element = resolveResult.getElement();
		if(element instanceof DotNetGenericParameterListOwner)
		{
			int genericParametersCount = ((DotNetGenericParameterListOwner) element).getGenericParametersCount();
			if(genericParametersCount == argumentsSize)
			{
				return (DotNetGenericParameterListOwner) element;
			}
		}
	}

	return null;
}
 
Example 10
Source File: CSharpExpressionEvaluator.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredReadAction
public void visitIndexAccessExpression(CSharpIndexAccessExpressionImpl expression)
{
	PsiElement parent = expression.getParent();
	if(parent instanceof CSharpAssignmentExpressionImpl && ((CSharpAssignmentExpressionImpl) parent).getCallArguments()[0] == expression)
	{
		expressionIsNotSupported();
	}

	ResolveResult resolveResult = CSharpResolveUtil.findFirstValidResult(expression.multiResolve(false));
	if(resolveResult == null || !(resolveResult instanceof MethodResolveResult) || !(resolveResult.getElement() instanceof CSharpIndexMethodDeclaration))
	{
		cantEvaluateExpression();
	}

	CSharpIndexMethodDeclaration indexMethodDeclaration = (CSharpIndexMethodDeclaration) resolveResult.getElement();

	expression.getQualifier().accept(this);

	pushNArguments((MethodResolveResult) resolveResult);

	DotNetTypeRef[] parameterTypeRefs = indexMethodDeclaration.getParameterTypeRefs();
	List<DotNetTypeDeclaration> parameterTypes = new ArrayList<DotNetTypeDeclaration>();
	for(DotNetTypeRef parameterTypeRef : parameterTypeRefs)
	{
		PsiElement element = parameterTypeRef.resolve().getElement();
		if(!(element instanceof CSharpTypeDeclaration))
		{
			throw new UnsupportedOperationException("parameter type is not type");
		}
		parameterTypes.add((DotNetTypeDeclaration) element);
	}

	myEvaluators.add(new IndexMethodEvaluator(indexMethodDeclaration, parameterTypes));
}
 
Example 11
Source File: CSharpReferenceExpressionImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
public static DotNetTypeRef toTypeRef(@Nonnull ResolveResult resolveResult)
{
	PsiElement element = resolveResult.getElement();
	DotNetGenericExtractor extractor = DotNetGenericExtractor.EMPTY;
	if(resolveResult instanceof CSharpResolveResultWithExtractor)
	{
		extractor = ((CSharpResolveResultWithExtractor) resolveResult).getExtractor();
	}
	return toTypeRef(element, extractor);
}
 
Example 12
Source File: CSharpReferenceExpressionImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
public static ResolveResult[] multiResolveImpl(ResolveToKind kind, final CSharpCallArgumentListOwner callArgumentListOwner, final CSharpQualifiedNonReference element, boolean resolveFromParent)
{
	ResolveResult[] resolveResults = buildSelectorAndMultiResolve(kind, callArgumentListOwner, element, resolveFromParent);
	if(element instanceof CSharpReferenceExpression)
	{
		int typeArgumentListSize = getTypeArgumentListSize(element);
		if(typeArgumentListSize > 0)
		{
			DotNetTypeRef[] typeArgumentListRefs = ((CSharpReferenceExpression) element).getTypeArgumentListRefs();

			for(int i = 0; i < resolveResults.length; i++)
			{
				ResolveResult resolveResult = resolveResults[i];

				PsiElement resolveResultElement = resolveResult.getElement();
				if(resolveResultElement instanceof CSharpTypeDeclaration)
				{
					Map<DotNetGenericParameter, DotNetTypeRef> map = new HashMap<DotNetGenericParameter, DotNetTypeRef>();
					DotNetGenericParameter[] genericParameters = ((CSharpTypeDeclaration) resolveResultElement).getGenericParameters();
					for(int j = 0; j < typeArgumentListRefs.length; j++)
					{
						DotNetTypeRef typeArgumentListRef = typeArgumentListRefs[j];
						DotNetGenericParameter genericParameter = ArrayUtil2.safeGet(genericParameters, j);
						if(genericParameter == null)
						{
							continue;
						}
						map.put(genericParameter, typeArgumentListRef);
					}
					resolveResults[i] = CSharpResolveResultWithExtractor.withExtractor(resolveResult, CSharpGenericExtractor.create(map));
				}
			}
		}
	}
	return resolveResults;
}
 
Example 13
Source File: AsPsiElementProcessor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(ResolveResult resolveResult)
{
	PsiElement element = resolveResult.getElement();
	myElements.add(element);
	return true;
}
 
Example 14
Source File: StaticVsInstanceComparator.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
private static boolean isNameEqual(ResolveResult o1, ResolveResult o2)
{
	PsiElement e1 = o1.getElement();
	PsiElement e2 = o2.getElement();
	if(e1 instanceof PsiNamedElement && e2 instanceof PsiNamedElement)
	{
		return Comparing.equal(((PsiNamedElement) e1).getName(), ((PsiNamedElement) e2).getName());
	}
	return false;
}
 
Example 15
Source File: TypeLikeComparator.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public int getWeight(ResolveResult resolveResult)
{
	PsiElement element = resolveResult.getElement();

	if(element instanceof DotNetVariable)
	{
		return 200000;
	}

	if(element instanceof CSharpElementGroup)
	{
		return 100000;
	}

	if(element instanceof CSharpTypeDefStatement)
	{
		return 50100;
	}

	if(element instanceof DotNetGenericParameterListOwner)
	{
		if(((DotNetGenericParameterListOwner) element).getGenericParametersCount() == myGenericCount)
		{
			return 50000;
		}
		return -((DotNetGenericParameterListOwner) element).getGenericParametersCount() * 100;
	}

	if(element instanceof DotNetNamespaceAsElement)
	{
		return 0;
	}

	return 10;
}
 
Example 16
Source File: Symfony2InterfacesUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 * Single resolve doesnt work if we have non unique class names in project context,
 * so try a multiResolve
 */
@Nullable
public static Method[] getMultiResolvedMethod(PsiReference psiReference) {

    // class be unique in normal case, so try this first
    PsiElement resolvedReference = psiReference.resolve();
    if (resolvedReference instanceof Method) {
        return new Method[] { (Method) resolvedReference };
    }

    // try multiResolve if class exists twice in project
    if(psiReference instanceof PsiPolyVariantReference) {
        Collection<Method> methods = new HashSet<>();
        for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) {
            PsiElement element = resolveResult.getElement();
            if(element instanceof Method) {
                methods.add((Method) element);
            }
        }

        if(methods.size() > 0) {
            return methods.toArray(new Method[methods.size()]);
        }

    }

    return null;
}
 
Example 17
Source File: CSharpResolveUtil.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nullable
public static PsiElement findFirstValidElement(ResolveResult[] resolveResults)
{
	ResolveResult firstValidResult = findFirstValidResult(resolveResults);
	return firstValidResult == null ? null : firstValidResult.getElement();
}
 
Example 18
Source File: CSharpExpressionEvaluator.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredReadAction
public void visitMethodCallExpression(CSharpMethodCallExpressionImpl expression)
{
	DotNetExpression callExpression = expression.getCallExpression();

	ResolveResult resolveResult = CSharpResolveUtil.findFirstValidResult(expression.multiResolve(false));
	if(resolveResult == null || !(resolveResult instanceof MethodResolveResult) || !(resolveResult.getElement() instanceof CSharpMethodDeclaration))
	{
		cantEvaluateExpression();
	}

	CSharpMethodDeclaration methodDeclaration = (CSharpMethodDeclaration) resolveResult.getElement();

	if(callExpression instanceof CSharpReferenceExpression)
	{
		CSharpTypeDeclaration typeDeclaration = null;

		PsiElement qualifier = ((CSharpReferenceExpression) callExpression).getQualifier();
		if(qualifier != null)
		{
			qualifier.accept(this);
		}
		else
		{
			if(methodDeclaration.hasModifier(DotNetModifier.STATIC))
			{
				typeDeclaration = (CSharpTypeDeclaration) methodDeclaration.getParent();
				myEvaluators.add(StaticObjectEvaluator.INSTANCE);
			}
			else
			{
				myEvaluators.add(ThisObjectEvaluator.INSTANCE);
			}
		}

		String referenceName = ((CSharpReferenceExpression) callExpression).getReferenceName();
		if(referenceName == null)
		{
			cantEvaluateExpression();
		}

		pushNArguments((MethodResolveResult) resolveResult);

		pushMethodEvaluator(expression, methodDeclaration, typeDeclaration, referenceName);
	}
}
 
Example 19
Source File: StaticVsInstanceComparator.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
private int getWeightByContext(ResolveResult resolveResult)
{
	final PsiElement element = resolveResult.getElement();
	if(element == null)
	{
		return -100;
	}

	// type alias have max priority
	if(element instanceof CSharpTypeDefStatement)
	{
		return Integer.MAX_VALUE;
	}

	if(myParent != null)
	{
		CSharpContextUtil.ContextType parentContext = CSharpContextUtil.ContextType.ANY;
		if(element instanceof CSharpTypeDeclaration)
		{
			parentContext = CSharpContextUtil.ContextType.STATIC;
		}
		else if(element instanceof DotNetVariable)
		{
			parentContext = CSharpContextUtil.ContextType.INSTANCE;
		}

		DotNetTypeDeclaration forceTarget = resolveTargetElement(element, myParent);
		if(forceTarget == null)
		{
			return parentContext == CSharpContextUtil.ContextType.INSTANCE ? 10 : 5;
		}

		ResolveResult[] resolveResults = RecursionManager.doPreventingRecursion(myParent, false, () -> myParent.tryResolveFromQualifier(forceTarget));
		if(resolveResults == null || resolveResults.length == 0)
		{
			return parentContext == CSharpContextUtil.ContextType.INSTANCE ? 10 : 5;
		}

		for(ResolveResult result : resolveResults)
		{
			PsiElement element1 = result.getElement();
			if(element1 == null)
			{
				continue;
			}

			CSharpContextUtil.ContextType contextForResolved = CSharpContextUtil.getContextForResolved(element1);

			if(parentContext != CSharpContextUtil.ContextType.ANY)
			{
				switch(parentContext)
				{
					case INSTANCE:
						if(contextForResolved.isAllowInstance())
						{
							return 5000;
						}
						break;
					case STATIC:
						if(contextForResolved == CSharpContextUtil.ContextType.STATIC)
						{
							return 5000;
						}
						break;
				}
			}
		}
	}
	else
	{
		// if expression is single - types and namespaces are in the end of queue
		if(element instanceof CSharpTypeDeclaration || element instanceof DotNetNamespaceAsElement)
		{
			return -1;
		}
	}
	return 0;
}
 
Example 20
Source File: ExpectedTypeVisitor.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredReadAction
public void visitAssignmentExpression(CSharpAssignmentExpressionImpl parent)
{
	DotNetExpression[] expressions = parent.getParameterExpressions();
	// <caret> = test;
	if(expressions.length == 1)
	{
		return;
	}
	if(expressions[0] == myCurrentElement)
	{
		DotNetExpression rightExpression = expressions[1];
		DotNetTypeRef typeRef = rightExpression.toTypeRef(true);
		myExpectedTypeInfos.add(new ExpectedTypeInfo(typeRef, null));
	}
	else
	{
		CSharpOperatorReferenceImpl operatorElement = parent.getOperatorElement();

		ResolveResult[] resolveResults = operatorElement.multiResolve(true);

		for(ResolveResult resolveResult : resolveResults)
		{
			PsiElement element = resolveResult.getElement();
			// stub variant
			if(operatorElement == element)
			{
				PsiElement typeProvider = null;
				DotNetExpression expression = expressions[0];
				if(expression instanceof CSharpReferenceExpression)
				{
					typeProvider = ((CSharpReferenceExpression) expression).resolve();
				}
				myExpectedTypeInfos.add(new ExpectedTypeInfo(expression.toTypeRef(false), typeProvider));
			}
			else if(element instanceof CSharpMethodDeclaration)
			{
				if(((CSharpMethodDeclaration) element).isOperator())
				{
					DotNetParameter[] parameters = ((CSharpMethodDeclaration) element).getParameters();
					DotNetParameter parameter = ArrayUtil2.safeGet(parameters, 1);
					if(parameter == null)
					{
						return;
					}
					myExpectedTypeInfos.add(new ExpectedTypeInfo(parameter.toTypeRef(true), element));
				}
			}
		}
	}
}