下面列出了com.intellij.psi.util.PsiTreeUtil#getParentOfType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
@RequiredReadAction
public void visitNamespaceDeclaration(CSharpNamespaceDeclaration declaration)
{
CSharpNamespaceDeclaration top = PsiTreeUtil.getParentOfType(declaration, CSharpNamespaceDeclaration.class);
if(top != null)
{
return;
}
DotNetReferenceExpression namespaceReference = declaration.getNamespaceReference();
if(namespaceReference == null)
{
return;
}
myRootNamespaces.add(declaration);
}
@RequiredReadAction
@Nullable
@Override
public HighlightInfoFactory checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull CSharpReferenceExpression element)
{
if(element.kind() == CSharpReferenceExpression.ResolveToKind.THIS)
{
CSharpFieldDeclaration declaration = PsiTreeUtil.getParentOfType(element, CSharpFieldDeclaration.class);
if(declaration == null)
{
return null;
}
return newBuilder(element, "this");
}
return null;
}
@Nullable
public static DotNetExpression getSelectedExpression(@Nonnull final Project project,
@Nonnull PsiFile file,
@Nonnull final PsiElement element1,
@Nonnull final PsiElement element2)
{
PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
if(parent == null)
{
return null;
}
if(parent instanceof DotNetExpression)
{
return (DotNetExpression) parent;
}
return PsiTreeUtil.getParentOfType(parent, DotNetExpression.class);
}
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
final List<ResolveResult> results = new ArrayList<>();
PhpClass phpClass = PsiTreeUtil.getParentOfType(getElement(), PhpClass.class);
if(phpClass == null) {
return results.toArray(new ResolveResult[0]);
}
for(Method method: phpClass.getMethods()) {
if(method.getModifier().isPublic() && !method.getName().startsWith("_") && method.getName().equals(this.valueName)) {
results.add(new PsiElementResolveResult(method));
}
}
return results.toArray(new ResolveResult[0]);
}
public void testUtilityClassModifiersInnerClass() {
PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
PsiClass innerClass = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiClass.class);
assertNotNull(innerClass);
assertNotNull(innerClass.getModifierList());
PsiElement parent = innerClass.getParent();
assertNotNull(parent);
assertTrue(parent instanceof PsiClass);
PsiClass parentClass = (PsiClass) parent;
assertNotNull(parentClass.getModifierList());
assertTrue("@UtilityClass should make parent class final", ((PsiClass) innerClass.getParent()).getModifierList().hasModifierProperty(PsiModifier.FINAL));
assertTrue("@UtilityClass should make inner class static", innerClass.getModifierList().hasModifierProperty(PsiModifier.STATIC));
}
@NotNull
private Set<String> getServicesInScope(@NotNull PsiElement psiElement) {
PhpClass phpClass = PsiTreeUtil.getParentOfType(psiElement, PhpClass.class);
return phpClass == null
? Collections.emptySet()
: ContainerCollectionResolver.ServiceCollector.create(psiElement.getProject()).convertClassNameToServices(phpClass.getFQN());
}
/**
* foo:
* bar:
* |
*
* Will return [foo, bar]
*
* todo: YAMLUtil.getFullKey is useless because its not possible to prefix self item value and needs array value
* @param psiElement any PsiElement inside a key value
*/
public static List<String> getParentArrayKeys(PsiElement psiElement) {
List<String> keys = new ArrayList<>();
YAMLKeyValue yamlKeyValue = PsiTreeUtil.getParentOfType(psiElement, YAMLKeyValue.class);
if(yamlKeyValue != null) {
getParentArrayKeys(yamlKeyValue, keys);
}
return keys;
}
public void assertPhpReferenceSignatureEquals(LanguageFileType languageFileType, String configureByText, String typeSignature) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
psiElement = PsiTreeUtil.getParentOfType(psiElement, PhpReference.class);
if (!(psiElement instanceof PhpReference)) {
fail("Element is not PhpReference.");
}
assertEquals(typeSignature, ((PhpReference) psiElement).getSignature());
}
private void doTestGenerateDoc(@NotNull String expected, @NotNull String text,
Class<? extends PsiElement> documentationSourceClass,
FileData... otherFiles) throws Exception {
for (FileData otherFile : otherFiles) {
myFixture.configureByText(otherFile.name, otherFile.contents);
}
myFixture.configureByText(SOURCE_FILE_NAME, text);
final int caretPosition = myFixture.getEditor().getCaretModel().getOffset();
PsiElement elementAtCaret = myFixture.getFile().findElementAt(caretPosition);
PsiElement element = PsiTreeUtil.getParentOfType(elementAtCaret, documentationSourceClass);
assertEquals(expected, documentationProvider.generateDoc(element, null));
}
private IElementType maybeGetVariableDefinitionElementType(
@NotNull PsiElement element, IElementType elementType) {
PsiElement parent = PsiTreeUtil.getParentOfType(element, AtElementSingle.class);
if (parent != null) {
elementType =
parent.getNode() == null
? SoyTypes.VARIABLE_DEFINITION_IDENTIFIER
: parent.getNode().getElementType();
}
return elementType;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElement elementAt = file.findElementAt(editor.getCaretModel().getOffset());
HaxeLocalVarDeclaration localVarDeclaration = PsiTreeUtil.getParentOfType(elementAt, HaxeLocalVarDeclaration.class);
if (localVarDeclaration == null) return;
String name = localVarDeclaration.getComponentName().getName();
HaxeTypeTag typeTag = localVarDeclaration.getTypeTag();
HaxeVarInit varInit = localVarDeclaration.getVarInit();
String text = "var " + name;
if (typeTag != null) {
text += " " + typeTag.getText();
}
text += ";";
HaxeFieldDeclaration varDeclaration = HaxeElementGenerator.createVarDeclaration(project, text);
text = name + varInit.getText();
varDeclaration.getNode().addLeaf(HaxeTokenTypes.OSEMI, "\n", null);
PsiElement statementFromText = HaxeElementGenerator.createStatementFromText(project, text);
statementFromText.getNode().addLeaf(HaxeTokenTypes.OSEMI, ";", null);
localVarDeclaration.getParent().addBefore(varDeclaration, localVarDeclaration);
PsiElement replace = localVarDeclaration.replace(statementFromText);
final TextRange range = replace.getTextRange();
final PsiFile baseFile = file.getViewProvider().getPsi(file.getViewProvider().getBaseLanguage());
CodeStyleManager.getInstance(project).reformatText(baseFile, range.getStartOffset(), range.getEndOffset());
}
@NotNull
@Override
public Collection<LombokProblem> verifyAnnotation(@NotNull PsiAnnotation psiAnnotation) {
final ProblemNewBuilder problemNewBuilder = new ProblemNewBuilder(2);
PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiAnnotation, PsiMethod.class);
if (null != psiMethod) {
if (psiMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
problemNewBuilder.addError("'@Synchronized' is legal only on concrete methods.",
PsiQuickFixFactory.createModifierListFix(psiMethod, PsiModifier.ABSTRACT, false, false)
);
}
final String lockFieldName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "value");
if (StringUtil.isNotEmpty(lockFieldName)) {
final PsiClass containingClass = psiMethod.getContainingClass();
if (null != containingClass) {
final PsiField lockField = containingClass.findFieldByName(lockFieldName, true);
if (null != lockField) {
if (!lockField.hasModifierProperty(PsiModifier.FINAL)) {
problemNewBuilder.addWarning(String.format("Synchronization on a non-final field %s.", lockFieldName),
PsiQuickFixFactory.createModifierListFix(lockField, PsiModifier.FINAL, true, false));
}
} else {
final PsiClassType javaLangObjectType = PsiType.getJavaLangObject(containingClass.getManager(), containingClass.getResolveScope());
problemNewBuilder.addError(String.format("The field %s does not exist.", lockFieldName),
PsiQuickFixFactory.createNewFieldFix(containingClass, lockFieldName, javaLangObjectType, "new Object()", PsiModifier.PRIVATE, PsiModifier.FINAL));
}
}
}
} else {
problemNewBuilder.addError("'@Synchronized' is legal only on methods.");
}
return problemNewBuilder.getProblems();
}
public static HaxeClassModel fromElement(PsiElement element) {
if (element == null) return null;
HaxeClass haxeClass = element instanceof HaxeClass
? (HaxeClass) element
: PsiTreeUtil.getParentOfType(element, HaxeClass.class);
if (haxeClass != null) {
return new HaxeClassModel(haxeClass);
}
return null;
}
private PsiClass getPsiClassFromContext(AnActionEvent e) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if (psiFile == null || editor == null) {
return null;
}
int offset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(offset);
return PsiTreeUtil.getParentOfType(element, PsiClass.class);
}
@NotNull
private PsiModifierList getFieldModifierListAtCaret() {
PsiFile file = loadToPsiFile(getTestName(false) + ".java");
PsiField field = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiField.class);
assertNotNull(field);
PsiModifierList modifierList = field.getModifierList();
assertNotNull(modifierList);
return modifierList;
}
private PsiClass getPsiClassFromContext(AnActionEvent e) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
// psiFile.getViewProvider().getVirtualFile()
if (psiFile == null || editor == null) {
return null;
}
int offset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(offset);
return PsiTreeUtil.getParentOfType(element, PsiClass.class);
}
@Nullable
private static ScInfixExpr getContainingInfixExpr(
PsiElement element, Predicate<PsiElement> predicate) {
while (element != null && !predicate.test(element)) {
element = PsiTreeUtil.getParentOfType(element, ScInfixExpr.class);
}
return (ScInfixExpr) element;
}
@Nullable
@Override
@RequiredReadAction
public UsageType getUsageType(PsiElement element)
{
if(element instanceof CSharpReferenceExpression)
{
PsiElement resolvedElement = ((CSharpReferenceExpression) element).resolve();
if(resolvedElement == null)
{
return null;
}
CSharpReferenceExpression.ResolveToKind kind = ((CSharpReferenceExpression) element).kind();
switch(kind)
{
case METHOD:
return METHOD_CALL;
case CONSTRUCTOR:
if(element.getParent() instanceof DotNetAttribute)
{
return ATTRIBUTE;
}
return UsageType.CLASS_NEW_OPERATOR;
case TYPE_LIKE:
DotNetType type = PsiTreeUtil.getParentOfType(element, DotNetType.class);
if(type == null)
{
return null;
}
PsiElement parent = type.getParent();
if(parent instanceof CSharpLocalVariable)
{
return UsageType.CLASS_LOCAL_VAR_DECLARATION;
}
else if(parent instanceof CSharpFieldDeclaration)
{
return UsageType.CLASS_FIELD_DECLARATION;
}
else if(parent instanceof DotNetParameter)
{
return UsageType.CLASS_METHOD_PARAMETER_DECLARATION;
}
else if(parent instanceof CSharpSimpleLikeMethodAsElement)
{
return UsageType.CLASS_METHOD_RETURN_TYPE;
}
else if(parent instanceof CSharpTypeCastExpressionImpl)
{
return UsageType.CLASS_CAST_TO;
}
else if(parent instanceof CSharpAsExpressionImpl)
{
return CLASS_IN_AS;
}
else if(parent instanceof CSharpIsExpressionImpl)
{
return CLASS_IN_IS;
}
else if(parent instanceof CSharpTypeOfExpressionImpl)
{
return TYPE_OF_EXPRESSION;
}
break;
case ANY_MEMBER:
if(resolvedElement instanceof CSharpMethodDeclaration && !((CSharpMethodDeclaration) resolvedElement).isDelegate())
{
return AS_METHOD_REF;
}
break;
}
}
return null;
}
@Override
public boolean isPlaceForEndpointUri(PsiElement location) {
PsiLiteralExpression expression = PsiTreeUtil.getParentOfType(location, PsiLiteralExpression.class, false);
return expression != null
&& isInsideCamelRoute(expression, false);
}
@RequiredReadAction
@Nullable
@Override
public HighlightInfoFactory checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull CSharpLambdaParameter element)
{
CSharpLambdaExpressionImpl lambdaExpression = PsiTreeUtil.getParentOfType(element, CSharpLambdaExpressionImpl.class);
if(lambdaExpression == null)
{
return null;
}
CSharpLambdaResolveResult lambdaResolveResult = CSharpLambdaExpressionImplUtil.resolveLeftLambdaTypeRef(lambdaExpression);
if(lambdaResolveResult == null)
{
return null;
}
CSharpMethodDeclaration target = lambdaResolveResult.getTarget();
if(target == null)
{
return null;
}
DotNetParameter[] parameters = target.getParameters();
DotNetParameter realParameter = ArrayUtil2.safeGet(parameters, element.getIndex());
if(realParameter == null)
{
return null;
}
for(CSharpModifier modifier : ourModifiers)
{
if(realParameter.hasModifier(modifier) && !element.hasModifier(modifier))
{
return newBuilder(element, String.valueOf(element.getIndex() + 1), modifier.getPresentableText()).addQuickFix(new AddModifierFix(modifier, element));
}
}
return null;
}