下面列出了com.intellij.psi.util.PsiTreeUtil#findChildOfType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
/**
* 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;
}
@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);
}
}
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());
}
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());
}
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;
}
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());
}
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());
}
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());
}
@Nullable
@Override
public PsiElement getNameIdentifier() {
return PsiTreeUtil.findChildOfType(this, PsiLowerSymbol.class);
}
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);
}
public XQueryDefaultFunctionNamespaceDecl getDefaultNamespaceFunctionDeclaration() {
return PsiTreeUtil.findChildOfType(this, XQueryDefaultFunctionNamespaceDecl.class);
}
@Nullable
@Override
public PsiElement getNameIdentifier() {
PsiTypeConstrName constr = findChildByClass(PsiTypeConstrName.class);
return constr == null ? null : PsiTreeUtil.findChildOfType(constr, PsiLowerSymbol.class);
}
public XQueryQueryBody getQueryBody() {
return PsiTreeUtil.findChildOfType(this, XQueryQueryBody.class);
}
public boolean isLibraryModule() {
XQueryModuleDecl moduleDecl = PsiTreeUtil.findChildOfType(this, XQueryModuleDecl.class);
return moduleDecl != null;
}
public static ElmUpperCasePath getModuleName(ElmImportClause module) {
return PsiTreeUtil.findChildOfType(module, ElmUpperCasePath.class);
}
@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");
}
}
}
}
}
}
}
public void testLocalList() {
PsiElement expression = parseCode("ModA.ModB.[call()];");
PsiLocalOpen o = PsiTreeUtil.findChildOfType(expression, PsiLocalOpen.class);
assertEquals("[call()]", o.getText());
}
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());
}
public static XQueryVarName createVariableReference(Project project, String namespaceName,
String localVariableName) {
final XQueryFile file = createFile(project, "$" + namespaceName + ":" + localVariableName);
return PsiTreeUtil.findChildOfType(file, XQueryVarName.class);
}