Java Code Examples for com.intellij.psi.util.PsiTreeUtil#findChildOfType()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#findChildOfType() . 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: InflateViewAnnotator.java    From idea-android-studio-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Nullable
public static InflateContainer matchInflate(PsiLocalVariable psiLocalVariable) {
    PsiType psiType = psiLocalVariable.getType();
    if(psiType instanceof PsiClassReferenceType) {
        PsiMethodCallExpression psiMethodCallExpression = PsiTreeUtil.findChildOfType(psiLocalVariable, PsiMethodCallExpression.class);
        if(psiMethodCallExpression != null) {
            PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();

            // @TODO: replace "inflate"; resolve method and check nethod calls
            if(psiMethod != null && psiMethod.getName().equals("inflate")) {
                PsiExpression[] expressions = psiMethodCallExpression.getArgumentList().getExpressions();
                if(expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
                    PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
                    if(xmlFile != null) {
                        return new InflateContainer(xmlFile, ((PsiLocalVariable) psiLocalVariable));
                    }
                }
            }
        }
    }

    return null;
}
 
Example 2
Source File: DuplicateLocalRouteInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void visitFile(PsiFile file) {
    // @TODO: detection of routing files in right way
    // routing.yml
    // comment.routing.yml
    // routing/foo.yml
    if(!YamlHelper.isRoutingFile(file)) {
        return;
    }

    YAMLDocument document = PsiTreeUtil.findChildOfType(file, YAMLDocument.class);
    if(document == null) {
        return;
    }

    YAMLValue topLevelValue = document.getTopLevelValue();
    if(topLevelValue != null) {
        YamlHelper.attachDuplicateKeyInspection(topLevelValue, holder);
    }
}
 
Example 3
Source File: TokenVocabResolver.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Tries to find a declaration named {@code ruleName} in the {@code tokenVocab} file if it exists.
 */
@Nullable
public static PsiElement resolveInTokenVocab(GrammarElementRefNode reference, String ruleName) {
	String tokenVocab = MyPsiUtils.findTokenVocabIfAny((ANTLRv4FileRoot) reference.getContainingFile());

	if (tokenVocab != null) {
		PsiFile tokenVocabFile = findRelativeFile(tokenVocab, reference.getContainingFile());

		if (tokenVocabFile != null) {
			GrammarSpecNode lexerGrammar = PsiTreeUtil.findChildOfType(tokenVocabFile, GrammarSpecNode.class);
			PsiElement node = MyPsiUtils.findSpecNode(lexerGrammar, ruleName);

			if (node instanceof LexerRuleSpecNode) {
				// fragments are not visible to the parser
				if (!((LexerRuleSpecNode) node).isFragment()) {
					return node;
				}
			}
			if (node instanceof TokenSpecNode) {
				return node;
			}
		}
	}

	return null;
}
 
Example 4
Source File: FunctorTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
public void testFunctorInstanciation() {
    PsiInnerModule module = (PsiInnerModule) first(moduleExpressions(parseCode("module Printing = Make({ let encode = encode_record; });")));

    assertNull(module.getBody());
    PsiFunctorCall call = PsiTreeUtil.findChildOfType(module, PsiFunctorCall.class);
    assertNotNull(call);
    assertEquals("Make({ let encode = encode_record; })", call.getText());
}
 
Example 5
Source File: LetParsingTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
public void testRecord() {
    PsiLet let = first(letExpressions(parseCode("let r = { one = 1; two = 2 }")));

    PsiLetBinding binding = first(PsiTreeUtil.findChildrenOfType(let, PsiLetBinding.class));
    assertNotNull(binding);
    PsiRecord record = PsiTreeUtil.findChildOfType(binding, PsiRecord.class);
    assertNotNull(record);
    Collection<PsiRecordField> fields = record.getFields();
    assertSize(2, fields);
    Iterator<PsiRecordField> itFields = fields.iterator();
    assertEquals("one = 1", itFields.next().getText());
    assertEquals("two = 2", itFields.next().getText());
}
 
Example 6
Source File: FunctorTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
public void testFunctorInstanciation() {
    PsiInnerModule module = (PsiInnerModule) first(moduleExpressions(parseCode("module Printing = Make (struct let encode = encode_record end)")));

    assertNull(module.getBody());
    PsiFunctorCall call = PsiTreeUtil.findChildOfType(module, PsiFunctorCall.class);
    assertNotNull(call);
    assertEquals("Make (struct let encode = encode_record end)", call.getText());
}
 
Example 7
Source File: LatteElementFactory.java    From intellij-latte with MIT License 5 votes vote down vote up
public static LattePhpClassUsage createClassRootUsage(Project project, String name) {
	final LatteFile file = createFileWithPhpMacro(project, "\\" + name);
	LattePhpClassUsage firstChild = PsiTreeUtil.findChildOfType(file, LattePhpClassUsage.class);
	if (firstChild != null) {
		try {
			return firstChild;

		} catch (NullPointerException e) {
			return null;
		}
	}
	return null;
}
 
Example 8
Source File: FunctionCallTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
public void testCall() {
    PsiLet e = first(letExpressions(parseCode("let _ = string_of_int(1)")));

    PsiFunctionCallParams callParams = PsiTreeUtil.findChildOfType(e.getBinding(), PsiFunctionCallParams.class);
    Collection<PsiElement> parameters = callParams.getParametersList();
    assertEquals(1, parameters.size());
}
 
Example 9
Source File: IfParsingTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
public void testBasicIfParsing() {
    PsiFile psiFile = parseCode("let _ = if x then ()");
    PsiIfStatement e = firstOfType(psiFile, PsiIfStatement.class);

    assertNotNull(e);
    assertNotNull(e.getBinaryCondition());
    PsiScopedExpr ifScope = PsiTreeUtil.findChildOfType(e, PsiScopedExpr.class);
    assertNotNull(ifScope);
    assertEquals("()", ifScope.getText());
}
 
Example 10
Source File: ElmPsiImplUtil.java    From elm-plugin with MIT License 4 votes vote down vote up
public static ElmUpperCasePath getModuleName(ElmImportClause module) {
    return PsiTreeUtil.findChildOfType(module, ElmUpperCasePath.class);
}
 
Example 11
Source File: XQueryElementFactory.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
public static XQueryVarName createVariableReference(Project project, String namespaceName,
                                                    String localVariableName) {
    final XQueryFile file = createFile(project, "$" + namespaceName + ":" + localVariableName);
    return PsiTreeUtil.findChildOfType(file, XQueryVarName.class);
}
 
Example 12
Source File: FunctionCallTest.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
public void testIssue120() {
    PsiLet e = first(letExpressions(parseCode("let _ = f(x == U.I, 1)")));

    PsiFunctionCallParams params = PsiTreeUtil.findChildOfType(e, PsiFunctionCallParams.class);
    assertSize(2, params.getParametersList());
}
 
Example 13
Source File: LocalOpenTest.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
public void testLocalList() {
    PsiElement expression = parseCode("ModA.ModB.[call()];");
    PsiLocalOpen o = PsiTreeUtil.findChildOfType(expression, PsiLocalOpen.class);
    assertEquals("[call()]", o.getText());
}
 
Example 14
Source File: JSGraphQLEndpointDocHighlightAnnotator.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {

	final IElementType elementType = element.getNode().getElementType();

       // highlight TO-DO items
       if(elementType == JSGraphQLEndpointDocTokenTypes.DOCTEXT) {
		final String elementText = element.getText().toLowerCase();
		if(isTodoToken(elementText)) {
			setTextAttributes(element, holder, CodeInsightColors.TODO_DEFAULT_ATTRIBUTES);
               holder.getCurrentAnnotationSession().putUserData(TODO_ELEMENT, element);
			return;
		} else {
               PsiElement prevSibling = element.getPrevSibling();
               while (prevSibling != null) {
                   if(prevSibling == holder.getCurrentAnnotationSession().getUserData(TODO_ELEMENT)) {
                       setTextAttributes(element, holder, CodeInsightColors.TODO_DEFAULT_ATTRIBUTES);
                       return;
                   }
                   prevSibling = prevSibling.getPrevSibling();
               }
           }
	}

	final PsiComment comment = PsiTreeUtil.getContextOfType(element, PsiComment.class);
	if (comment != null && JSGraphQLEndpointDocPsiUtil.isDocumentationComment(comment)) {
		final TextAttributesKey textAttributesKey = ATTRIBUTES.get(elementType);
		if (textAttributesKey != null) {
			setTextAttributes(element, holder, textAttributesKey);
		}
		// highlight invalid argument names after @param
		if(elementType == JSGraphQLEndpointDocTokenTypes.DOCVALUE) {
			final JSGraphQLEndpointFieldDefinition field = PsiTreeUtil.getNextSiblingOfType(comment, JSGraphQLEndpointFieldDefinition.class);
			if(field != null) {
				final JSGraphQLEndpointDocTag tag = PsiTreeUtil.getParentOfType(element, JSGraphQLEndpointDocTag.class);
				if(tag != null && tag.getDocName().getText().equals("@param")) {
					final String paramName = element.getText();
					final JSGraphQLEndpointInputValueDefinitions arguments = PsiTreeUtil.findChildOfType(field, JSGraphQLEndpointInputValueDefinitions.class);
					if(arguments == null) {
						// no arguments so invalid use of @param
						holder.createErrorAnnotation(element, "Invalid use of @param. The property has no arguments");
					} else {
						final JSGraphQLEndpointInputValueDefinition[] inputValues = PsiTreeUtil.getChildrenOfType(arguments, JSGraphQLEndpointInputValueDefinition.class);
						boolean found = false;
						if(inputValues != null) {
							for (JSGraphQLEndpointInputValueDefinition inputValue: inputValues) {
								if(inputValue.getInputValueDefinitionIdentifier().getText().equals(paramName)) {
									found = true;
									break;
								}
							}
						}
						if(!found) {
							holder.createErrorAnnotation(element, "@param name '" + element.getText() + "' doesn't match any of the field arguments");
						}

					}
				}
			}
		}
	}
}
 
Example 15
Source File: PsiClassField.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PsiElement getNameIdentifier() {
    return PsiTreeUtil.findChildOfType(this, PsiLowerSymbol.class);
}
 
Example 16
Source File: XQueryFile.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
public boolean isLibraryModule() {
    XQueryModuleDecl moduleDecl = PsiTreeUtil.findChildOfType(this, XQueryModuleDecl.class);
    return moduleDecl != null;
}
 
Example 17
Source File: XQueryFile.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
public XQueryQueryBody getQueryBody() {
    return PsiTreeUtil.findChildOfType(this, XQueryQueryBody.class);
}
 
Example 18
Source File: PsiTypeImpl.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PsiElement getNameIdentifier() {
    PsiTypeConstrName constr = findChildByClass(PsiTypeConstrName.class);
    return constr == null ? null : PsiTreeUtil.findChildOfType(constr, PsiLowerSymbol.class);
}
 
Example 19
Source File: XQueryFile.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
public XQueryDefaultFunctionNamespaceDecl getDefaultNamespaceFunctionDeclaration() {
    return PsiTreeUtil.findChildOfType(this, XQueryDefaultFunctionNamespaceDecl.class);
}
 
Example 20
Source File: BashPsiElementFactory.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
public static PsiElement createHeredocStartMarker(Project project, String name) {
    String data = String.format("cat << %s\n%s", name, name);
    return PsiTreeUtil.findChildOfType(createDummyBashFile(project, data), BashHereDocStartMarker.class);
}