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

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#findChildOfType() . These examples are extracted from open source projects. 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
@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
/**
 * 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 3
@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 4
Source Project: reasonml-idea-plugin   File: LetParsingTest.java    License: 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 5
Source Project: reasonml-idea-plugin   File: FunctorTest.java    License: 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 6
Source Project: intellij-latte   File: LatteElementFactory.java    License: 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 7
Source Project: reasonml-idea-plugin   File: IfParsingTest.java    License: 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 8
Source Project: reasonml-idea-plugin   File: FunctionCallTest.java    License: 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 Project: reasonml-idea-plugin   File: FunctorTest.java    License: 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 10
Source Project: reasonml-idea-plugin   File: PsiClassField.java    License: MIT License 4 votes vote down vote up
@Nullable
@Override
public PsiElement getNameIdentifier() {
    return PsiTreeUtil.findChildOfType(this, PsiLowerSymbol.class);
}
 
Example 11
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);
}
 
Example 12
Source Project: intellij-xquery   File: XQueryFile.java    License: Apache License 2.0 4 votes vote down vote up
public XQueryDefaultFunctionNamespaceDecl getDefaultNamespaceFunctionDeclaration() {
    return PsiTreeUtil.findChildOfType(this, XQueryDefaultFunctionNamespaceDecl.class);
}
 
Example 13
Source Project: reasonml-idea-plugin   File: PsiTypeImpl.java    License: 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 14
Source Project: intellij-xquery   File: XQueryFile.java    License: Apache License 2.0 4 votes vote down vote up
public XQueryQueryBody getQueryBody() {
    return PsiTreeUtil.findChildOfType(this, XQueryQueryBody.class);
}
 
Example 15
Source Project: intellij-xquery   File: XQueryFile.java    License: Apache License 2.0 4 votes vote down vote up
public boolean isLibraryModule() {
    XQueryModuleDecl moduleDecl = PsiTreeUtil.findChildOfType(this, XQueryModuleDecl.class);
    return moduleDecl != null;
}
 
Example 16
Source Project: elm-plugin   File: ElmPsiImplUtil.java    License: MIT License 4 votes vote down vote up
public static ElmUpperCasePath getModuleName(ElmImportClause module) {
    return PsiTreeUtil.findChildOfType(module, ElmUpperCasePath.class);
}
 
Example 17
@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 18
Source Project: reasonml-idea-plugin   File: LocalOpenTest.java    License: 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 19
Source Project: reasonml-idea-plugin   File: FunctionCallTest.java    License: 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 20
public static XQueryVarName createVariableReference(Project project, String namespaceName,
                                                    String localVariableName) {
    final XQueryFile file = createFile(project, "$" + namespaceName + ":" + localVariableName);
    return PsiTreeUtil.findChildOfType(file, XQueryVarName.class);
}