com.intellij.psi.PsiElement#getContext ( )源码实例Demo

下面列出了com.intellij.psi.PsiElement#getContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: idea-php-laravel-plugin   文件: PhpElementsUtil.java
@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);

}
 
源代码2 项目: idea-php-toolbox   文件: PhpElementsUtil.java
@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);

}
 
@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()]);
}
 
源代码4 项目: idea-php-laravel-plugin   文件: PhpElementsUtil.java
@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);
}
 
源代码5 项目: Thinkphp5-Plugin   文件: PsiElementUtil.java
@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);
}
 
@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);
}
 
源代码7 项目: Thinkphp5-Plugin   文件: PhpElementsUtil.java
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);

    }
 
@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;
}
 
@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];
}
 
@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;
}
 
源代码11 项目: idea-php-toolbox   文件: PhpElementsUtil.java
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);

    }
 
源代码12 项目: idea-php-laravel-plugin   文件: PhpElementsUtil.java
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);

    }
 
源代码13 项目: idea-php-toolbox   文件: PhpElementsUtil.java
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);

    }
 
源代码14 项目: idea-php-toolbox   文件: PhpElementsUtil.java
@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);
}
 
@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];
}
 
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;
}
 
@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);
}
 
@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;
}
 
private static boolean isStep(PsiElement insertedElement) {
    return insertedElement.getContext() instanceof SpecStep;
}
 
@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;
}