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

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#getChildrenOfType() . 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
Source Project: JetBrains-NASM-Language   File: NASMUtil.java    License: MIT License 6 votes vote down vote up
static List<NASMIdentifier> findIdentifierReferencesByIdInProject(Project project, String targetIdentifierId) {
    List<NASMIdentifier> result = null;
    Collection<VirtualFile> virtualFiles =
            FileTypeIndex.getFiles(NASMFileType.INSTANCE, GlobalSearchScope.allScope(project));
    for (VirtualFile virtualFile : virtualFiles) {
        NASMFile simpleFile = (NASMFile) PsiManager.getInstance(project).findFile(virtualFile);
        if (simpleFile != null) {
            NASMIdentifier[] identifiers = PsiTreeUtil.getChildrenOfType(simpleFile, NASMIdentifier.class);
            if (identifiers != null) {
                for (NASMIdentifier identifier : identifiers) {
                    if (targetIdentifierId.equals(identifier.getId().getText())) {
                        if (result == null) {
                            result = new ArrayList<NASMIdentifier>();
                        }
                        result.add(identifier);
                    }
                }
            }
        }
    }
    return result != null ? result : Collections.<NASMIdentifier>emptyList();
}
 
Example 2
Source Project: buck   File: BuckIdentifierUtil.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns all known identifiers that match the given key name. */
public static List<BuckIdentifier> findIdentifiers(Project project, String key) {
  List<BuckIdentifier> result = new ArrayList<>();
  Collection<VirtualFile> virtualFiles =
      FileTypeIndex.getFiles(BuckFileType.INSTANCE, GlobalSearchScope.allScope(project));
  for (VirtualFile virtualFile : virtualFiles) {
    PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
    if (psiFile instanceof BuckFile) {
      BuckFile buckFile = (BuckFile) psiFile;
      BuckIdentifier[] identifiers =
          PsiTreeUtil.getChildrenOfType(buckFile, BuckIdentifier.class);
      for (BuckIdentifier identifier : identifiers) {
        if (identifier.getName().equals(key)) {
          result.add(identifier);
        }
      }
    }
  }
  return result;
}
 
Example 3
Source Project: buck   File: BuckIdentifierUtil.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns all known identifiers. */
public static List<BuckIdentifier> findIdentifiers(Project project) {
  List<BuckIdentifier> result = new ArrayList<>();
  Collection<VirtualFile> virtualFiles =
      FileTypeIndex.getFiles(BuckFileType.INSTANCE, GlobalSearchScope.allScope(project));
  for (VirtualFile virtualFile : virtualFiles) {
    PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
    if (psiFile instanceof BuckFile) {
      BuckFile buckFile = (BuckFile) psiFile;
      BuckIdentifier[] identifiers =
          PsiTreeUtil.getChildrenOfType(buckFile, BuckIdentifier.class);
      if (identifiers != null) {
        Collections.addAll(result, identifiers);
      }
    }
  }
  return result;
}
 
Example 4
Source Project: intellij-sdk-docs   File: SimpleUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static List<SimpleProperty> findProperties(Project project, String key) {
  List<SimpleProperty> result = new ArrayList<>();
  Collection<VirtualFile> virtualFiles =
        FileTypeIndex.getFiles(SimpleFileType.INSTANCE, GlobalSearchScope.allScope(project));
  for (VirtualFile virtualFile : virtualFiles) {
    SimpleFile simpleFile = (SimpleFile) PsiManager.getInstance(project).findFile(virtualFile);
    if (simpleFile != null) {
      SimpleProperty[] properties = PsiTreeUtil.getChildrenOfType(simpleFile, SimpleProperty.class);
      if (properties != null) {
        for (SimpleProperty property : properties) {
          if (key.equals(property.getKey())) {
             result.add(property);
          }
        }
      }
    }
  }
  return result;
}
 
Example 5
/**
 * Returns the import URL's in a Dart file.
 */
@NotNull
private static Stream<String> findImportUrls(@NotNull DartFile file) {
  final DartImportStatement[] imports = PsiTreeUtil.getChildrenOfType(file, DartImportStatement.class);
  if (imports == null) return Stream.empty();

  return Arrays.stream(imports).map(DartImportStatement::getUriString);
}
 
Example 6
@NotNull
protected final Object[] buildChildren(@NotNull final HierarchyNodeDescriptor descriptor) {

  final HaxeClass theHaxeClass = ((HaxeTypeHierarchyNodeDescriptor) descriptor).getHaxeClass();
  if (null == theHaxeClass) return ArrayUtil.EMPTY_OBJECT_ARRAY;

  if (theHaxeClass instanceof HaxeAnonymousType) return ArrayUtil.EMPTY_OBJECT_ARRAY;
  if (theHaxeClass.hasModifierProperty(HaxePsiModifier.FINAL_META)) return ArrayUtil.EMPTY_OBJECT_ARRAY;

  final PsiFile inClassPsiFile = theHaxeClass.getContainingFile();

  List<PsiClass> subTypeList = new ArrayList<PsiClass>(); // add the sub-types to this list, as they are found

  // ----
  // search current file for sub-types that extend/implement from this class/type
  //
  final HaxeClass[] allHaxeClassesInFile = PsiTreeUtil.getChildrenOfType(inClassPsiFile, HaxeClass.class);
  if (allHaxeClassesInFile != null) {
    for (HaxeClass aClassInFile : allHaxeClassesInFile) {
      if (isThisTypeASubTypeOfTheSuperType(aClassInFile, theHaxeClass)) {
        subTypeList.add(aClassInFile);
      }
    }
  }

  // if private class, scope ends there
  if (theHaxeClass.hasModifierProperty(HaxePsiModifier.PRIVATE)) { // XXX: how about @:allow occurrences?
    return typeListToObjArray(((HaxeTypeHierarchyNodeDescriptor) descriptor), subTypeList);
  }

  // Get the list of subtypes from the file-based indices.  Stub-based would
  // be faster, but we'll have to re-parent all of the PsiClass sub-classes.
  subTypeList.addAll(getSubTypes(theHaxeClass));

  return typeListToObjArray(((HaxeTypeHierarchyNodeDescriptor) descriptor), subTypeList);
}
 
Example 7
private List<FlutterSample> getSamplesFromDoc(String flutterPackagePath, Document document, String filePath) {
  final List<FlutterSample> samples = new ArrayList<>();

  // Find all candidate class definitions.
  final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
  assert (psiFile != null);

  final DartClass[] classes = PsiTreeUtil.getChildrenOfType(psiFile, DartClass.class);
  if (classes == null) {
    return Collections.emptyList();
  }

  // Get the dartdoc for the classes and use a regex to identify which ones have
  // "/// {@tool dartpad ...}".

  for (DartClass declaration : classes) {
    final String name = declaration.getName();
    if (name == null || name.startsWith("_")) {
      continue;
    }

    final List<String> dartdoc = DartDocumentUtils.getDartdocFor(document, declaration);
    if (containsDartdocFlutterSample(dartdoc)) {
      assert (declaration.getName() != null);

      String libraryName = filePath.substring(flutterPackagePath.length());
      final int index = libraryName.indexOf('/');
      if (index != -1) {
        libraryName = libraryName.substring(0, index);

        final FlutterSample sample = new FlutterSample(libraryName, declaration.getName());
        samples.add(sample);
      }
    }
  }

  return samples;
}
 
Example 8
/**
 * Returns the import URL's in a Dart file.
 */
private static @NotNull
Stream<String> findImportUrls(@NotNull DartFile file) {
  final DartImportStatement[] imports = PsiTreeUtil.getChildrenOfType(file, DartImportStatement.class);
  if (imports == null) return Stream.empty();

  return Arrays.stream(imports).map(DartImportStatement::getUriString);
}
 
Example 9
/**
 * @return the {@link SQFCommandArgument} instance that comes before {@link #getExprOperator()},
 * or null if doesn't exist (prefixArg? COMMAND postfixArg?)
 */
@Nullable
public SQFCommandArgument getPrefixArgument() {
	SQFCommandArgument[] args = PsiTreeUtil.getChildrenOfType(this, SQFCommandArgument.class);
	if (args != null && args.length > 0 && args[0].getTextOffset() < getExprOperator().getTextOffset()) {
		return args[0];
	}
	return null;
}
 
Example 10
/**
 * @return the {@link SQFCommandArgument} instance that comes after {@link #getExprOperator()},\
 * or null if doesn't exist  (prefixArg? COMMAND postfixArg?)
 */
@Nullable
public SQFCommandArgument getPostfixArgument() {
	SQFCommandArgument[] args = PsiTreeUtil.getChildrenOfType(this, SQFCommandArgument.class);
	if (args != null && args.length > 0) {
		int commandTextOffset = getExprOperator().getTextOffset();
		if (args[0].getTextOffset() > commandTextOffset) {
			return args[0];
		}
		if (args.length > 1) {
			return args[1];
		}
	}
	return null;
}
 
Example 11
@NotNull
@Override
public TerminatorScope getTerminatorScope() {
    // The terminator scope of an if statement is NONE if it only has one branch, otherwise it's the minimum
    // terminator scope of its two branches.
    GLSLStatement[] branches = PsiTreeUtil.getChildrenOfType(this, GLSLStatement.class);
    if (branches == null || branches.length < 2) return TerminatorScope.NONE;

    TerminatorScope minScope = TerminatorScope.SHADER;
    for (GLSLStatement statement : branches) {
        TerminatorScope childScope = statement.getTerminatorScope();
        minScope = minScope.compareTo(childScope) < 0 ? minScope : childScope;
    }
    return minScope;
}
 
Example 12
Source Project: netbeans-mmd-plugin   File: IdeaUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static List<PsiExtraFile> findPsiFileLinksForProjectScope(final Project project) {
  List<PsiExtraFile> result = new ArrayList<PsiExtraFile>();
  Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, MindMapFileType.INSTANCE,
      GlobalSearchScope.allScope(project));
  for (VirtualFile virtualFile : virtualFiles) {
    final MMDFile simpleFile = (MMDFile) PsiManager.getInstance(project).findFile(virtualFile);
    if (simpleFile != null) {
      final PsiExtraFile[] fileLinks = PsiTreeUtil.getChildrenOfType(simpleFile, PsiExtraFile.class);
      if (fileLinks != null) {
        Collections.addAll(result, fileLinks);
      }
    }
  }
  return result;
}
 
Example 13
private Set<String> getMethods(final PsiReferenceExpression methodExpr) {
    Set<String> methods = new HashSet<>();
    if (WindowObjects.getInstance().isIncludeMethods()) {
        PsiIdentifier[] identifiers =
                PsiTreeUtil.getChildrenOfType(methodExpr, PsiIdentifier.class);
        if (identifiers != null) {
            for (PsiIdentifier identifier : identifiers) {
                methods.add(identifier.getText());
            }
        }
    }
    return methods;
}
 
Example 14
@NotNull
public GLSLStatement[] getStatements() {
    GLSLStatement[] statements = PsiTreeUtil.getChildrenOfType(this, GLSLStatement.class);
    return (statements == null) ? GLSLStatement.NO_STATEMENTS : statements;
}
 
Example 15
private String getTypeForArrayAccess(PsiElement e) {

        ArrayAccessExpression arrayAccessExpression;
        Boolean internalResolve = false;

        if (e instanceof ArrayAccessExpression) {
            arrayAccessExpression = (ArrayAccessExpression)e;
        }
        else if (e instanceof NewExpression)
        {
            ClassReference[] classReferences = PsiTreeUtil.getChildrenOfType(e, ClassReference.class);
            if (classReferences == null || classReferences.length != 1) {
                return null;
            }

            ArrayAccessExpression[] arrayAccessExpressions = PsiTreeUtil.getChildrenOfType(classReferences[0], ArrayAccessExpression.class);
            if (arrayAccessExpressions == null || arrayAccessExpressions.length != 1) {
                return null;
            }

            arrayAccessExpression = arrayAccessExpressions[0];
            internalResolve = true;
        }
        else return null;

        Signature signature = getChildElementSignature(arrayAccessExpression);
        if (signature == null) {
            return null;
        }

        ArrayIndex arrayIndex = arrayAccessExpression.getIndex();
        if (arrayIndex == null) {
            return null;
        }

        String serviceName = getStringOrSignature(arrayIndex.getValue());
        if (serviceName == null) {
            return null;
        }

        return signature.toString() + '[' + (internalResolve ? "@" : "") + serviceName + ']';
    }
 
Example 16
Source Project: idea-php-symfony2-plugin   File: XmlHelper.java    License: MIT License 4 votes vote down vote up
public static Map<String, String> getFileParameterMap(XmlFile psiFile) {

        Map<String, String> services = new HashMap<>();

        if(!(psiFile.getFirstChild() instanceof XmlDocumentImpl)) {
            return services;
        }

        XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
        if(xmlTags == null) {
            return services;
        }

        for(XmlTag xmlTag: xmlTags) {
            if(xmlTag.getName().equals("container")) {
                for(XmlTag servicesTag: xmlTag.getSubTags()) {
                    if(servicesTag.getName().equals("parameters")) {
                        for(XmlTag parameterTag: servicesTag.getSubTags()) {

                            // <parameter key="fos_user.user_manager.class">FOS\UserBundle\Doctrine\UserManager</parameter>
                            // <parameter key="fos_rest.formats" type="collection">
                            //    <parameter key="json">false</parameter>
                            // </parameter>

                            if(parameterTag.getName().equals("parameter")) {
                                XmlAttribute keyAttr = parameterTag.getAttribute("key");
                                if(keyAttr != null) {
                                    String parameterName = keyAttr.getValue();
                                    if(parameterName != null && StringUtils.isNotBlank(parameterName)) {

                                        String parameterValue = null;

                                        String typeAttr = parameterTag.getAttributeValue("type");

                                        // get value of parameter if we have a text value
                                        if(!"collection".equals(typeAttr) && parameterTag.getSubTags().length == 0) {
                                            XmlTagValue attrClass = parameterTag.getValue();
                                            String myParameterValue = attrClass.getText();

                                            // dont index long values
                                            if(myParameterValue.length() < 150) {
                                                parameterValue = myParameterValue;
                                            }
                                        }

                                        services.put(parameterName.toLowerCase(), parameterValue);
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }

        return services;
    }
 
Example 17
@Nullable
default SQFExpression getRight() {
	SQFExpression[] arr = PsiTreeUtil.getChildrenOfType(this, SQFExpression.class);
	return (arr != null && arr.length > 1) ? arr[1] : null;
}
 
Example 18
@Nullable
default SQFExpression getRight() {
	SQFExpression[] arr = PsiTreeUtil.getChildrenOfType(this, SQFExpression.class);
	return (arr != null && arr.length > 1) ? arr[1] : null;
}
 
Example 19
@NotNull
public static LombokConfigProperty[] getLombokConfigProperties(@Nullable LombokConfigFile lombokConfigFile) {
  LombokConfigProperty[] result = PsiTreeUtil.getChildrenOfType(lombokConfigFile, LombokConfigProperty.class);
  return null == result ? EMPTY_LOMBOK_CONFIG_PROPERTIES : result;
}
 
Example 20
/**
 * Returns a map of module -> alias for each imported module.  If a module is imported but not qualified, alias
 * will be null.
 */
@NotNull
public static List<Import> parseImports(@NotNull final PsiFile file) {
    final boolean noImplicitPrelude = hasPragma(file, "NoImplicitPrelude");
    final Import prelude = Import.global("Prelude", false, null);
    boolean importedPrelude = false;
    HaskellImpdecl[] impdecls = PsiTreeUtil.getChildrenOfType(PsiTreeUtil.getChildOfType(file, HaskellBody.class), HaskellImpdecl.class);
    if (impdecls == null) {
        if (noImplicitPrelude) return Collections.emptyList();
        return Collections.singletonList(prelude);
    }
    List<Import> result = new ArrayList<Import>(impdecls.length);
    for (HaskellImpdecl impdecl : impdecls) {
        final List<HaskellQconid> qconids = impdecl.getQconidList();
        final int numQconids = qconids.size();
        if (numQconids == 0) { continue; }
        final HaskellQconid moduleQconid = qconids.get(0);
        final String module = moduleQconid.getText();
        final String alias = numQconids > 1 ? qconids.get(1).getText() : null;
        final boolean isQualified = impdecl.getQualified() != null;
        final boolean isHiding = impdecl.getHiding() != null;
        final String[] explicitNames;
        // Check if we have an empty import list.
        if (impdecl.getImpempty() != null) {
            explicitNames = ArrayUtils.EMPTY_STRING_ARRAY;
        // Otherwise, if we have a left paren, we have an import list.
        } else if (impdecl.getLparen() != null) {
            explicitNames = getTexts(collectNamedElementsInImporttList(impdecl.getImporttList()));
        // At this point, we must not have an import list at all.
        } else {
            explicitNames = null;
        }
        importedPrelude = importedPrelude || module.equals("Prelude");
        final Import anImport;
        if (isQualified) {
            if (alias == null) {
                anImport = Import.qualified(module, isHiding, explicitNames);
            } else {
                anImport = Import.qualifiedAs(module, alias, isHiding, explicitNames);
            }
        } else {
            if (alias == null) {
                anImport = Import.global(module, isHiding, explicitNames);
            } else {
                anImport = Import.globalAs(module, alias, isHiding, explicitNames);
            }
        }
        result.add(anImport);
    }
    if (!importedPrelude && !noImplicitPrelude) { result.add(prelude); }
    return result;
}