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

The following examples show how to use com.intellij.psi.PsiElement#getContext() . 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: PhpGoToDeclarationHandler.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Editor editor) {

    if(!DrupalProjectComponent.isEnabled(psiElement)) {
        return new PsiElement[0];
    }

    List<PsiElement> psiElementList = new ArrayList<>();

    PsiElement context = psiElement.getContext();
    if(context instanceof StringLiteralExpression && DrupalPattern.isAfterArrayKey(psiElement, "route_name")) {
        PsiElement routeTarget = RouteHelper.getRouteNameTarget(psiElement.getProject(), ((StringLiteralExpression) context).getContents());
        if(routeTarget != null) {
            psiElementList.add(routeTarget);
        }
    }

    return psiElementList.toArray(new PsiElement[psiElementList.size()]);
}
 
Example 2
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 6 votes vote down vote up
@Nullable
public static MethodReferenceBag getMethodParameterReferenceBag(PsiElement psiElement, int wantIndex) {

    PsiElement variableContext = psiElement.getContext();
    if(!(variableContext instanceof ParameterList)) {
        return null;
    }

    ParameterList parameterList = (ParameterList) variableContext;
    if (!(parameterList.getContext() instanceof MethodReference)) {
        return null;
    }

    ParameterBag currentIndex = getCurrentParameterIndex(psiElement);
    if(currentIndex == null) {
        return null;
    }

    if(wantIndex >= 0 && currentIndex.getIndex() != wantIndex) {
        return null;
    }

    return new MethodReferenceBag(parameterList, (MethodReference) parameterList.getContext(), currentIndex);

}
 
Example 3
Source File: PhpElementsUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@Nullable
public static MethodReferenceBag getMethodParameterReferenceBag(PsiElement psiElement, int wantIndex) {

    PsiElement variableContext = psiElement.getContext();
    if(!(variableContext instanceof ParameterList)) {
        return null;
    }

    ParameterList parameterList = (ParameterList) variableContext;
    if (!(parameterList.getContext() instanceof MethodReference)) {
        return null;
    }

    ParameterBag currentIndex = getCurrentParameterIndex(psiElement);
    if(currentIndex == null) {
        return null;
    }

    if(wantIndex >= 0 && currentIndex.getIndex() != wantIndex) {
        return null;
    }

    return new MethodReferenceBag(parameterList, (MethodReference) parameterList.getContext(), currentIndex);

}
 
Example 4
Source File: ArrayKeyValueSignatureRegistrarMatcher.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
private ArrayCreationExpression findArrayCreationExpression(@NotNull StringLiteralExpression psiElement, @NotNull String key) {

    // value inside array
    // $menu->addChild(array(
    //   'route' => 'foo',
    // ));
    if(PhpPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).accepts(psiElement.getContext())) {
        PsiElement arrayValue = psiElement.getContext();
        if(arrayValue != null) {
            PsiElement arrayHashElement = arrayValue.getContext();
            if(arrayHashElement instanceof ArrayHashElement) {
                PhpPsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
                if(arrayKey instanceof StringLiteralExpression && ((StringLiteralExpression) arrayKey).getContents().equals(key)) {
                    PsiElement arrayCreationExpression = arrayHashElement.getContext();
                    if(arrayCreationExpression instanceof ArrayCreationExpression) {
                        if(!(arrayCreationExpression.getParent() instanceof ParameterList)) {
                            return null;
                        }

                        return (ArrayCreationExpression) arrayCreationExpression;
                    }
                }
            }
        }
    }

    return null;
}
 
Example 5
Source File: PhpStringLiteralExpressionReference.java    From yiistorm with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {

    ParameterList parameterList = (ParameterList) psiElement.getContext();

    if (parameterList == null || !(parameterList.getContext() instanceof MethodReference)) {
        return new PsiReference[0];
    }
    MethodReference method = (MethodReference) parameterList.getContext();
    // System.err.println(referenceClass);
    return this.getPsiReferenceBase(psiElement);

    // return new PsiReference[0];
}
 
Example 6
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
public static ParameterBag getCurrentParameterIndex(PsiElement psiElement) {

    if (!(psiElement.getContext() instanceof ParameterList)) {
        return null;
    }

    ParameterList parameterList = (ParameterList) psiElement.getContext();
    if (!(parameterList.getContext() instanceof ParameterListOwner)) {
        return null;
    }

    return getCurrentParameterIndex(parameterList.getParameters(), psiElement);
}
 
Example 7
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
public static boolean isFunctionReference(PsiElement psiElement, int wantIndex, String... funcName) {

        if(funcName.length == 0) {
            return false;
        }

        PsiElement variableContext = psiElement.getContext();
        if(!(variableContext instanceof ParameterList)) {
            return false;
        }

        ParameterList parameterList = (ParameterList) variableContext;
        PsiElement context = parameterList.getContext();
        if (!(context instanceof FunctionReference)) {
            return false;
        }

        FunctionReference methodReference = (FunctionReference) context;
        String name = methodReference.getName();

        if(name == null || !Arrays.asList(funcName).contains(name)) {
            return false;
        }

        ParameterBag currentIndex = getCurrentParameterIndex(psiElement);
        if(currentIndex == null) {
            return false;
        }

        return !(wantIndex >= 0 && currentIndex.getIndex() != wantIndex);

    }
 
Example 8
Source File: PhpElementsUtil.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
public static boolean isFunctionReference(PsiElement psiElement, int wantIndex, String... funcName) {

        PsiElement variableContext = psiElement.getContext();
        if(!(variableContext instanceof ParameterList)) {
            return false;
        }

        ParameterList parameterList = (ParameterList) variableContext;
        PsiElement context = parameterList.getContext();
        if (!(context instanceof FunctionReference)) {
            return false;
        }

        FunctionReference methodReference = (FunctionReference) context;
        String name = methodReference.getName();

        if(name == null || !Arrays.asList(funcName).contains(name)) {
            return false;
        }

        ParameterBag currentIndex = getCurrentParameterIndex(psiElement);
        if(currentIndex == null) {
            return false;
        }

        return !(wantIndex >= 0 && currentIndex.getIndex() != wantIndex);

    }
 
Example 9
Source File: UtilityClassProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean validateOnRightType(PsiClass psiClass, ProblemBuilder builder) {
  if (checkWrongType(psiClass)) {
    builder.addError("@UtilityClass is only supported on a class (can't be an interface, enum, or annotation).");
    return false;
  }
  PsiElement context = psiClass.getContext();
  if (context == null) {
    return false;
  }
  if (!(context instanceof PsiFile)) {
    PsiElement contextUp = context;
    while (true) {
      if (contextUp instanceof PsiClass) {
        PsiClass psiClassUp = (PsiClass) contextUp;
        if (psiClassUp.getContext() instanceof PsiFile) {
          return true;
        }
        boolean isStatic = isStatic(psiClassUp.getModifierList());
        if (isStatic || checkWrongType(psiClassUp)) {
          contextUp = contextUp.getContext();
        } else {
          builder.addError("@UtilityClass automatically makes the class static, however, this class cannot be made static.");
          return false;
        }
      } else {
        builder.addError("@UtilityClass cannot be placed on a method local or anonymous inner class, or any class nested in such a class.");
        return false;
      }
    }
  }
  return true;
}
 
Example 10
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
public static boolean isFunctionReference(PsiElement psiElement, int wantIndex, String... funcName) {

        if(funcName.length == 0) {
            return false;
        }

        PsiElement variableContext = psiElement.getContext();
        if(!(variableContext instanceof ParameterList)) {
            return false;
        }

        ParameterList parameterList = (ParameterList) variableContext;
        PsiElement context = parameterList.getContext();
        if (!(context instanceof FunctionReference)) {
            return false;
        }

        FunctionReference methodReference = (FunctionReference) context;
        String name = methodReference.getName();

        if(name == null || !Arrays.asList(funcName).contains(name)) {
            return false;
        }

        ParameterBag currentIndex = getCurrentParameterIndex(psiElement);
        if(currentIndex == null) {
            return false;
        }

        return !(wantIndex >= 0 && currentIndex.getIndex() != wantIndex);

    }
 
Example 11
Source File: WeexReferenceProvider.java    From weex-language-support with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {

    if (!WeexFileUtil.isOnWeexFile(psiElement)) {
        return new PsiReference[0];
    }

    String text = psiElement.getText().replaceAll("\"+", "");
    if (Pattern.compile("\\{\\{.*\\}\\}").matcher(text).matches() && text.length() > 4) {
        String valueType = "var";
        if (psiElement.getParent() != null && psiElement.getParent().getParent() != null) {
            PsiElement tag = psiElement.getParent().getParent();
            String attr = null;
            if (psiElement.getContext() != null) {
                attr = ((XmlAttribute) psiElement.getContext()).getName();
            }
            if (attr != null && tag instanceof XmlTag) {
                String tagName = ((XmlTag) tag).getName();
                WeexTag weexTag = DirectiveLint.getWeexTag(tagName);
                if (weexTag != null && weexTag.getAttribute(attr) != null) {
                    valueType = weexTag.getAttribute(attr).valueType;
                }
            }
        }
        return new PsiReference[]{new MustacheVarReference(psiElement, valueType.toLowerCase())};
    }
    return new PsiReference[0];
}
 
Example 12
Source File: OttoLineMarkerProvider.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@Override public boolean shouldShow(Usage usage) {
  PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();
  if (element instanceof PsiJavaCodeReferenceElement) {
    if ((element = element.getContext()) instanceof PsiTypeElement) {
      if ((element = element.getContext()) instanceof PsiParameter) {
        if ((element = element.getContext()) instanceof PsiParameterList) {
          if ((element = element.getContext()) instanceof PsiMethod) {
            return SubscriberMetadata.isAnnotatedWithSubscriber((PsiMethod) element);
          }
        }
      }
    }
  }
  return false;
}
 
Example 13
Source File: PhpElementsUtil.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@Nullable
public static ParameterBag getCurrentParameterIndex(PsiElement psiElement) {

    if (!(psiElement.getContext() instanceof ParameterList)) {
        return null;
    }

    ParameterList parameterList = (ParameterList) psiElement.getContext();
    if (!(parameterList.getContext() instanceof ParameterListOwner)) {
        return null;
    }

    return getCurrentParameterIndex(parameterList.getParameters(), psiElement);
}
 
Example 14
Source File: PhpElementsUtil.java    From Thinkphp5-Plugin with MIT License 5 votes vote down vote up
public static boolean isFunctionReference(PsiElement psiElement, int wantIndex, String... funcName) {

        PsiElement variableContext = psiElement.getContext();
        if(!(variableContext instanceof ParameterList)) {
            return false;
        }

        ParameterList parameterList = (ParameterList) variableContext;
        PsiElement context = parameterList.getContext();
        if (!(context instanceof FunctionReference)) {
            return false;
        }

        FunctionReference methodReference = (FunctionReference) context;
        String name = methodReference.getName();

        if(name == null || !Arrays.asList(funcName).contains(name)) {
            return false;
        }

        ParameterBag currentIndex = getCurrentParameterIndex(psiElement);
        if(currentIndex == null) {
            return false;
        }

        return !(wantIndex >= 0 && currentIndex.getIndex() != wantIndex);

    }
 
Example 15
Source File: PsiElementUtils.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static ParameterBag getCurrentParameterIndex(PsiElement psiElement) {

    if (!(psiElement.getContext() instanceof ParameterList)) {
        return null;
    }

    ParameterList parameterList = (ParameterList) psiElement.getContext();
    if (!(parameterList.getContext() instanceof ParameterListOwner)) {
        return null;
    }

    return getCurrentParameterIndex(parameterList.getParameters(), psiElement);
}
 
Example 16
Source File: PsiElementUtil.java    From Thinkphp5-Plugin with MIT License 5 votes vote down vote up
@Nullable
public static ParameterBag getCurrentParameterIndex(PsiElement psiElement) {

    if (!(psiElement.getContext() instanceof ParameterList)) {
        return null;
    }

    ParameterList parameterList = (ParameterList) psiElement.getContext();
    if (!(parameterList.getContext() instanceof ParameterListOwner)) {
        return null;
    }

    return getCurrentParameterIndex(parameterList.getParameters(), psiElement);
}
 
Example 17
Source File: PhpParameterStringCompletionConfidence.java    From idea-php-laravel-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {

    if(!(psiFile instanceof PhpFile)) {
        return ThreeState.UNSURE;
    }

    Project project = contextElement.getProject();
    if(!LaravelProjectComponent.isEnabled(project) || !LaravelSettings.getInstance(project).useAutoPopup) {
        return ThreeState.UNSURE;
    }

    PsiElement context = contextElement.getContext();
    if(!(context instanceof StringLiteralExpression)) {
        return ThreeState.UNSURE;
    }

    // $test == "";
    if(context.getParent() instanceof BinaryExpression) {
        return ThreeState.NO;
    }

    // $this->container->get("");
    PsiElement stringContext = context.getContext();
    if(stringContext instanceof ParameterList) {
        return ThreeState.NO;
    }

    // $this->method(... array('foo'); array('bar' => 'foo') ...);
    ArrayCreationExpression arrayCreationExpression = PhpElementsUtil.getCompletableArrayCreationElement(context);
    if(arrayCreationExpression != null && arrayCreationExpression.getContext() instanceof ParameterList) {
        return ThreeState.NO;
    }

    // $array['value']
    if(PlatformPatterns.psiElement().withSuperParent(2, ArrayIndex.class).accepts(contextElement)) {
        return ThreeState.NO;
    }

    return ThreeState.UNSURE;
}
 
Example 18
Source File: StepCompletionContributor.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
private static boolean isStep(PsiElement insertedElement) {
    return insertedElement.getContext() instanceof SpecStep;
}
 
Example 19
Source File: PhpAnnotationCompletionConfidence.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {

    if(!(psiFile instanceof PhpFile)) {
        return ThreeState.UNSURE;
    }

    PsiElement context = contextElement.getContext();
    if(context instanceof StringLiteralExpression) {

        // foo="<|>"
        if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocString).accepts(context)) {
            return ThreeState.NO;
        }

    } else if(context instanceof PhpDocComment) {

        // * <|>
        if(PhpPatterns.psiElement().afterLeafSkipping(
            PhpPatterns.psiElement(PsiWhiteSpace.class),
            PhpPatterns.psiElement(PhpDocTokenTypes.DOC_LEADING_ASTERISK)
        ).accepts(contextElement)) {
            return ThreeState.NO;
        }

    } else if(context instanceof PhpPsiElementImpl) {

        // @Foo(<|>)
        if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList).accepts(context)) {
            return ThreeState.NO;
        }

        // @<|>
        if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocTag).accepts(context)) {
            return ThreeState.NO;
        }

    }

    return ThreeState.UNSURE;
}
 
Example 20
Source File: LazySubscriberReferenceProvider.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement psiElement, int i, Editor editor) {
    if(psiElement == null || !ShopwareProjectComponent.isValidForProject(psiElement)) {
        return new PsiElement[0];
    }

    PsiElement context = psiElement.getContext();
    if(!(context instanceof StringLiteralExpression)) {
        return new PsiElement[0];
    }

    String hookNameContent = null;

    ArrayCreationExpression arrayCreationExpression = PhpElementsUtil.getCompletableArrayCreationElement(context);
    if(arrayCreationExpression != null) {

        PsiElement returnStatement = arrayCreationExpression.getParent();
        if(returnStatement instanceof PhpReturn) {
            Method method = PsiTreeUtil.getParentOfType(returnStatement, Method.class);
            if(method != null) {
                if("getSubscribedEvents".equals(method.getName())) {
                    PhpClass phpClass = method.getContainingClass();
                    if(phpClass != null && PhpElementsUtil.isInstanceOf(phpClass, "\\Enlight\\Event\\SubscriberInterface")) {
                        hookNameContent = ((StringLiteralExpression) context).getContents();
                    }
                }
            }
        }

    } else  {

        MethodMatcher.MethodMatchParameter match = new MethodMatcher.StringParameterMatcher(context, 0)
            .withSignature("\\Shopware_Components_Plugin_Bootstrap", "subscribeEvent")
            .match();

        if(match == null) {
            return new PsiElement[0];
        }

        hookNameContent = ((StringLiteralExpression) context).getContents();
    }

    if(hookNameContent == null) {
        return new PsiElement[0];
    }

    return getHookTargets(psiElement.getProject(), hookNameContent);
}