Java Code Examples for com.intellij.psi.PsiElement#isEquivalentTo()

The following examples show how to use com.intellij.psi.PsiElement#isEquivalentTo() . 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: RecursiveCallCollector.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Override
public void collect(PsiElement psiElement, @Nonnull Consumer<LineMarkerInfo> consumer)
{
	if(psiElement.getNode().getElementType() == CSharpTokens.IDENTIFIER && psiElement.getParent() instanceof CSharpReferenceExpression &&
			psiElement.getParent().getParent() instanceof CSharpMethodCallExpressionImpl)
	{
		PsiElement resolvedElement = ((CSharpReferenceExpression) psiElement.getParent()).resolve();
		if(resolvedElement instanceof CSharpMethodDeclaration)
		{
			CSharpMethodDeclaration methodDeclaration = PsiTreeUtil.getParentOfType(psiElement, CSharpMethodDeclaration.class);
			if(resolvedElement.isEquivalentTo(methodDeclaration))
			{
				LineMarkerInfo<PsiElement> lineMarkerInfo = new LineMarkerInfo<PsiElement>(psiElement, psiElement.getTextRange(), AllIcons.Gutter.RecursiveMethod, Pass.LINE_MARKERS,
						FunctionUtil.constant("Recursive call"), null, GutterIconRenderer.Alignment.CENTER);
				consumer.consume(lineMarkerInfo);
			}
		}
	}
}
 
Example 2
Source File: CSharpNavBarExtension.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public PsiElement getParent(@Nonnull PsiElement psiElement)
{
	if(psiElement instanceof DotNetNamedElement)
	{
		PsiFile containingFile = psiElement.getContainingFile();
		if(containingFile instanceof CSharpFile)
		{
			DotNetNamedElement element = CSharpPsiUtilImpl.findSingleElement((CSharpFile) containingFile);
			if(psiElement.isEquivalentTo(element))
			{
				return containingFile.getParent();
			}
		}
	}
	return super.getParent(psiElement);
}
 
Example 3
Source File: CSharpRecursiveGuardWeigher.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Integer weigh(@Nonnull LookupElement element)
{
	PsiElement psiElement = element.getPsiElement();
	if(psiElement == null)
	{
		return 0;
	}

	for(PsiElement e : myElementSet)
	{
		if(psiElement.isEquivalentTo(e))
		{
			return Integer.MIN_VALUE;
		}
	}
	return 0;
}
 
Example 4
Source File: CSharpLightElement.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEquivalentTo(@Nullable PsiElement another)
{
	PsiElement ori1 = another == null ? null : another.getOriginalElement();
	PsiElement ori2 = getOriginalElement();

	if(ori1 != null && ori1.isEquivalentTo(ori2))
	{
		return true;
	}

	return super.isEquivalentTo(another);
}
 
Example 5
Source File: CSharpTypeDeclarationImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
private static boolean isInheritOrSelf0(DotNetTypeDeclaration typeDeclaration, String... vmQNames)
{
	if(ArrayUtil.contains(typeDeclaration.getVmQName(), vmQNames))
	{
		return true;
	}

	DotNetTypeRef[] anExtends = typeDeclaration.getExtendTypeRefs();
	if(anExtends.length > 0)
	{
		for(DotNetTypeRef dotNetType : anExtends)
		{
			PsiElement psiElement = dotNetType.resolve().getElement();
			if(psiElement instanceof DotNetTypeDeclaration)
			{
				if(psiElement.isEquivalentTo(typeDeclaration))
				{
					return false;
				}

				if(ArrayUtil.contains(((DotNetTypeDeclaration) psiElement).getVmQName(), vmQNames))
				{
					return true;
				}

				if(isInheritOrSelf0((DotNetTypeDeclaration) psiElement, vmQNames))
				{
					return true;
				}
			}
		}
	}
	return false;
}
 
Example 6
Source File: MsilToNativeElementTransformer.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiElement findElementByOriginal(@Nonnull PsiElement wrappedElement, @Nonnull PsiElement originalTarget)
{
	PsiElement originalElement = wrappedElement.getOriginalElement();

	if(originalElement.isEquivalentTo(originalTarget))
	{
		return wrappedElement;
	}

	if(wrappedElement instanceof MsilMethodAsCSharpMethodDeclaration)
	{
		MsilClassEntry delegate = ((MsilMethodAsCSharpMethodDeclaration) wrappedElement).getDelegate();
		if(delegate != null && delegate.isEquivalentTo(originalTarget))
		{
			return wrappedElement;
		}
	}

	if(wrappedElement instanceof DotNetMemberOwner)
	{
		DotNetNamedElement[] members = ((DotNetMemberOwner) wrappedElement).getMembers();
		for(DotNetNamedElement member : members)
		{
			PsiElement elementByOriginal = findElementByOriginal(member, originalTarget);
			if(elementByOriginal != null)
			{
				return elementByOriginal;
			}
		}
	}
	return null;
}
 
Example 7
Source File: CS0236.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public HighlightInfoFactory checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull CSharpReferenceExpression element)
{
	if(element.kind() == CSharpReferenceExpression.ResolveToKind.FIELD_OR_PROPERTY)
	{
		return null;
	}

	CSharpFieldDeclaration fieldDeclaration = PsiTreeUtil.getParentOfType(element, CSharpFieldDeclaration.class);
	if(fieldDeclaration == null)
	{
		return null;
	}

	DotNetExpression initializer = fieldDeclaration.getInitializer();
	if(initializer == null || !initializer.getTextRange().contains(element.getTextRange()))
	{
		return null;
	}

	PsiElement parent = fieldDeclaration.getParent();
	if(!(parent instanceof CSharpTypeDeclaration))
	{
		return null;
	}

	PsiElement target = element.resolve();
	if(PsiTreeUtil.getParentOfType(element, DotNetType.class) != null || target instanceof CSharpTypeDeclaration)
	{
		return null;
	}

	if(target instanceof DotNetModifierListOwner && !((DotNetModifierListOwner) target).hasModifier(CSharpModifier.STATIC) && parent.isEquivalentTo(target.getParent()))
	{
		return newBuilder(element, formatElement(target));
	}
	return null;
}
 
Example 8
Source File: ComponentNameScopeProcessor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public boolean execute(@Nonnull PsiElement element, ResolveState state)
{
	if(element.isEquivalentTo(myToSkip))
	{
		return true;
	}
	if(element instanceof PsiNamedElement)
	{
		myResult.add((PsiNamedElement) element);
	}
	return true;
}
 
Example 9
Source File: CamelRouteLineMarkerProvider.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element,
                                        Collection<? super RelatedItemLineMarkerInfo> result) {
    //TODO: remove this when IdeaUtils.isFromJavaMethodCall will be fixed
    if (isJavaTokenLiteralExpression(element)
        || isXmlTokenLiteralExpression(element)
        || isCamelRouteStartIdentifierExpression(element)) {
        boolean showIcon = getCamelPreferenceService().isShowCamelIconInGutter();
        boolean camelPresent = ServiceManager.getService(element.getProject(), CamelService.class).isCamelPresent();

        if (!showIcon || !camelPresent) {
            return;
        }

        boolean validCamelFile = isCamelFile(element);
        if (!validCamelFile) {
            return;
        }

        //skip the PsiLiteralExpression that are not the first operand of PsiPolyadicExpression to avoid having multiple gutter icons
        // on the same PsiPolyadicExpression
        if (element instanceof PsiLiteralExpression) {
            if (isPartOfPolyadicExpression((PsiLiteralExpression) element)) {
                if (!element.isEquivalentTo(getFirstExpressionFromPolyadicExpression((PsiLiteralExpression) element))) {
                    return;
                }
            }
        }

        Icon icon = getCamelPreferenceService().getCamelIcon();

        if (getCamelIdeaUtils().isCamelRouteStartExpression(element)) {

            // evaluate the targets lazy
            NotNullLazyValue<Collection<? extends PsiElement>> targets = new NotNullLazyValue<Collection<? extends PsiElement>>() {
                @NotNull
                @Override
                protected Collection<PsiElement> compute() {
                    List<PsiElement> routeDestinationForPsiElement = findRouteDestinationForPsiElement(element);
                    // Add identifier references as navigation target
                    resolvedIdentifier(element)
                        .map(PsiElement::getNavigationElement)
                        .ifPresent(routeDestinationForPsiElement::add);
                    return routeDestinationForPsiElement;
                }
            };

            NavigationGutterIconBuilder<PsiElement> builder =
                NavigationGutterIconBuilder.create(icon)
                    .setTargets(targets)
                    .setTooltipText("Camel route")
                    .setPopupTitle("Navigate to " + findRouteFromElement(element))
                    .setAlignment(GutterIconRenderer.Alignment.RIGHT)
                    .setCellRenderer(new GutterPsiElementListCellRenderer());
            result.add(builder.createLineMarkerInfo(element));
        }
    }
}
 
Example 10
Source File: PimpleCompletionContributor.java    From silex-idea-plugin with MIT License 4 votes vote down vote up
public void addCompletions(@NotNull CompletionParameters parameters,
                           ProcessingContext context,
                           @NotNull CompletionResultSet resultSet) {

    PsiElement stringLiteralExpression = parameters.getPosition().getParent();
    Project project = stringLiteralExpression.getProject();

    if(!ProjectComponent.isEnabled(project)) {
        return;
    }

    if (!(stringLiteralExpression instanceof StringLiteralExpression)) {
        return;
    }

    PsiElement arrayKeyElement = stringLiteralExpression.getParent();
    PsiElement element = arrayKeyElement.getParent();

     if (element instanceof ArrayHashElement) {

         if (!arrayKeyElement.isEquivalentTo(element.getFirstChild())) {
             return;
         }

        element = element.getParent();
    }

    if (!(element instanceof ArrayCreationExpression)) {
        return;
    }

    PsiElement parameterList = element.getParent();
    if (!(parameterList instanceof ParameterList)) {
        return;
    }

    PsiElement[] params = ((ParameterList) parameterList).getParameters();
    if (!(params.length > 1 && params[1].isEquivalentTo(element))) {
        return;
    }

    PsiElement methodReference = parameterList.getParent();
    if (!(methodReference instanceof MethodReference)) {
        return;
    }

    String methodReferenceName = ((MethodReference) methodReference).getName();
    if ((methodReferenceName == null) || !(methodReferenceName.equals("register"))) {
        return;
    }

    Container container = Utils.findContainerForMethodReference((MethodReference) methodReference);
    if (container == null) {
        return;
    }

    for (Parameter parameter : container.getParameters().values()) {
        resultSet.addElement(new ParameterLookupElement(parameter));
    }

    resultSet.stopHere();
}
 
Example 11
Source File: Utils.java    From silex-idea-plugin with MIT License 4 votes vote down vote up
private static Container findContainerForPimpleArrayAccess(ArrayAccessExpression arrayAccessElement, Boolean onlyParentContainers) {

        PsiElement children;
        PsiElement element = arrayAccessElement;
        while ((children = PsiTreeUtil.getChildOfType(element, ArrayAccessExpression.class)) != null) {
            element = children;
        }

        // check if var is pimple container
        Signature signature = new Signature();

        PsiElement signatureElement = PsiTreeUtil.getChildOfAnyType(element, Variable.class, FieldReference.class);
        if (signatureElement == null) {
            return null;
        }

        if (signatureElement instanceof Variable) {
            signature.set(((Variable) signatureElement).getSignature());
        }

        if (signatureElement instanceof FieldReference) {
            signature.set(((FieldReference) signatureElement).getSignature());
        }

        PhpIndex phpIndex = PhpIndex.getInstance(arrayAccessElement.getProject());

        ArrayList<String> parameters = new ArrayList<String>();
        if (!findPimpleContainer(phpIndex, signature.base, parameters)) {
            return null;
        }

        Container container = ContainerResolver.get(arrayAccessElement.getProject());

        // find proper base container from signature
        for (String parameter : parameters) {
            container = container.getContainers().get(getResolvedParameter(phpIndex, parameter));
            if (container == null)
                return null;
        }

        PsiElement lastElement = onlyParentContainers ? arrayAccessElement : arrayAccessElement.getParent();

        // find proper container
        while (!element.isEquivalentTo(lastElement) ) {

            ArrayIndex arrayIndex = ((ArrayAccessExpression)element).getIndex();
            if (arrayIndex == null) {
                return null;
            }

            PsiElement arrayIndexElement = arrayIndex.getValue();
            if (arrayIndexElement == null) {
                return null;
            }

            String containerName;

            if (arrayIndexElement instanceof StringLiteralExpression) {
                containerName = ((StringLiteralExpression) arrayIndexElement).getContents();
            }
            else if (arrayIndexElement instanceof MemberReference) {
                containerName = getResolvedParameter(phpIndex, ((MemberReference) arrayIndexElement).getSignature());
            }
            else return null;

            container = container.getContainers().get(containerName);
            if (container == null) {
                return null;
            }

            element = element.getParent();
        }

        return container;

    }
 
Example 12
Source File: ShaderReference.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredReadAction
public boolean isReferenceTo(PsiElement element)
{
	return element.isEquivalentTo(resolve());
}
 
Example 13
Source File: IdempotenceChecker.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static String checkPsiEquivalence(@Nonnull PsiElement existing, @Nonnull PsiElement fresh) {
  if (!existing.equals(fresh) && !existing.isEquivalentTo(fresh) && !fresh.isEquivalentTo(existing) && (seemsToBeResolveTarget(existing) || seemsToBeResolveTarget(fresh))) {
    return reportProblem(existing, fresh);
  }
  return null;
}
 
Example 14
Source File: PomTargetPsiElementImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEquivalentTo(PsiElement another) {
  return equals(another) ||
         (another != null && myTarget instanceof PsiTarget && another.isEquivalentTo(((PsiTarget)myTarget).getNavigationElement()));
}