Java Code Examples for com.intellij.psi.util.PsiTreeUtil#getParentOfType()
The following examples show how to use
com.intellij.psi.util.PsiTreeUtil#getParentOfType() .
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: CS0027.java From consulo-csharp with Apache License 2.0 | 6 votes |
@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; }
Example 2
Source File: MatchNamespaceVisitor.java From consulo-csharp with Apache License 2.0 | 6 votes |
@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); }
Example 3
Source File: UtilityClassModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)); }
Example 4
Source File: EventSubscriberReferenceContributor.java From idea-php-shopware-plugin with MIT License | 6 votes |
@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]); }
Example 5
Source File: CSharpRefactoringUtil.java From consulo-csharp with Apache License 2.0 | 6 votes |
@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); }
Example 6
Source File: Specs2Utils.java From intellij with Apache License 2.0 | 5 votes |
@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; }
Example 7
Source File: ConvertAction.java From data-mediator with Apache License 2.0 | 5 votes |
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); }
Example 8
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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; }
Example 9
Source File: DataMediatorAction.java From data-mediator with Apache License 2.0 | 5 votes |
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); }
Example 10
Source File: HaxeClassModel.java From intellij-haxe with Apache License 2.0 | 5 votes |
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; }
Example 11
Source File: SynchronizedProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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(); }
Example 12
Source File: SplitIntoDeclarationAndAssignment.java From intellij-haxe with Apache License 2.0 | 5 votes |
@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()); }
Example 13
Source File: SoyElementIconProvider.java From bamboo-soy with Apache License 2.0 | 5 votes |
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; }
Example 14
Source File: XQueryDocumentationProviderTest.java From intellij-xquery with Apache License 2.0 | 5 votes |
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)); }
Example 15
Source File: LaravelLightCodeInsightFixtureTestCase.java From idea-php-laravel-plugin with MIT License | 5 votes |
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()); }
Example 16
Source File: YamlHelper.java From idea-php-symfony2-plugin with MIT License | 5 votes |
/** * 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; }
Example 17
Source File: PhpServiceArgumentIntention.java From idea-php-symfony2-plugin with MIT License | 5 votes |
@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()); }
Example 18
Source File: JavaCamelIdeaUtils.java From camel-idea-plugin with Apache License 2.0 | 4 votes |
@Override public boolean isPlaceForEndpointUri(PsiElement location) { PsiLiteralExpression expression = PsiTreeUtil.getParentOfType(location, PsiLiteralExpression.class, false); return expression != null && isInsideCamelRoute(expression, false); }
Example 19
Source File: CS1676.java From consulo-csharp with Apache License 2.0 | 4 votes |
@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; }
Example 20
Source File: CSharpUsageTypeProvider.java From consulo-csharp with Apache License 2.0 | 4 votes |
@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; }