Java Code Examples for com.intellij.psi.search.FileTypeIndex#getFiles()

The following examples show how to use com.intellij.psi.search.FileTypeIndex#getFiles() . 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: RemoveUnusedParameterFix.java    From bamboo-soy with Apache License 2.0 6 votes vote down vote up
private static List<SoyParamSpecificationIdentifier> getReferencingParamSpecificationIdentifiers(
    SoyParamDefinitionIdentifier paramDefinitionIdentifier) {
  Project project = paramDefinitionIdentifier.getProject();
  final ImmutableList.Builder<SoyParamSpecificationIdentifier> result = ImmutableList.builder();
  Collection<VirtualFile> virtualFiles =
      FileTypeIndex.getFiles(SoyFileType.INSTANCE, GlobalSearchScope.allScope(project));
  for (VirtualFile virtualFile : virtualFiles) {
    SoyFile soyFile = (SoyFile) PsiManager.getInstance(project).findFile(virtualFile);
    if (soyFile != null) {
      soyFile.accept(new SoyRecursiveElementVisitor() {
        @Override
        public void visitParamSpecificationIdentifier(
            @NotNull SoyParamSpecificationIdentifier identifier) {
          super.visitParamSpecificationIdentifier(identifier);
          PsiReference reference = identifier.getReference();
          if (reference != null && paramDefinitionIdentifier.equals(reference.resolve())) {
            result.add(identifier);
          }
        }
      });
    }
  }
  return result.build();
}
 
Example 2
Source File: BuckIdentifierUtil.java    From buck with 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 File: JSGraphQLEndpointCompletionContributor.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
private boolean completeImportFile(@NotNull CompletionResultSet result, PsiFile file, PsiElement parent) {
	if ((parent instanceof JSGraphQLEndpointQuotedString || parent instanceof JSGraphQLEndpointString) && PsiTreeUtil.getParentOfType(parent, JSGraphQLEndpointImportFileReference.class) != null) {

		final Project project = file.getProject();
		final VirtualFile entryFile = JSGraphQLConfigurationProvider.getService(project).getEndpointEntryFile(file);
		final GlobalSearchScope scope = JSGraphQLEndpointPsiUtil.getImportScopeFromEntryFile(project, entryFile, file);
		final Collection<VirtualFile> files = FileTypeIndex.getFiles(JSGraphQLEndpointFileType.INSTANCE, scope);
		for (VirtualFile virtualFile : files) {
			if(virtualFile.equals(entryFile)) {
				// entry file should never be imported
				continue;
			}
			final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
			if (psiFile != null) {
				if(psiFile.equals(file)) {
					// don't suggest the current file
					continue;
				}
				String name = JSGraphQLEndpointImportUtil.getImportName(project, psiFile);
				result.addElement(LookupElementBuilder.create(name).withIcon(psiFile.getIcon(0)));
			}
		}
		return true;
	}
	return false;
}
 
Example 4
Source File: ArmaPluginUtil.java    From arma-intellij-plugin with MIT License 6 votes vote down vote up
/**
 * Gets the config files (either a description.ext or multiple, at least 1, config.cpp (case sensitivity doesn't matter)).
 * If no description.ext file or config.cpp files could be found, this will return an empty list. This will also
 * return and empty list if a module for the given PsiElement couldn't be found.
 * <p>
 * If a description.ext file is found, this method will return a singleton list of the description.ext file,regardless
 * if there are config.cpp files. If there is no description.ext files, this will return all config.cpp files found
 *
 * @param module a PsiElement used to determine what module the root config file is located in
 * @return a list of VirtualFile instances, or an empty list
 */
@NotNull
public static List<VirtualFile> getConfigVirtualFiles(@NotNull Module module) {
	Collection<VirtualFile> files = FileTypeIndex.getFiles(HeaderFileType.INSTANCE, module.getModuleContentScope());
	List<VirtualFile> configs = new ArrayList<>();
	for (VirtualFile virtFile : files) {
		if (virtFile.getName().equalsIgnoreCase("description.ext")) {
			return Collections.singletonList(virtFile);
		}
		if (virtFile.getName().equalsIgnoreCase("config.cpp")) {
			configs.add(virtFile);
		}

	}
	return configs;
}
 
Example 5
Source File: BuckIdentifierUtil.java    From buck with 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 6
Source File: MuleConfigUtils.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
@NotNull
private static List<XmlTag> getGlobalElementsInScope(Project project, GlobalSearchScope searchScope) {
    final List<XmlTag> result = new ArrayList<>();
    final Collection<VirtualFile> files = FileTypeIndex.getFiles(StdFileTypes.XML, searchScope);
    final DomManager manager = DomManager.getDomManager(project);
    for (VirtualFile file : files) {
        final PsiFile xmlFile = PsiManager.getInstance(project).findFile(file);
        if (isMuleFile(xmlFile)) {
            final DomFileElement<Mule> fileElement = manager.getFileElement((XmlFile) xmlFile, Mule.class);
            if (fileElement != null) {
                final Mule rootElement = fileElement.getRootElement();
                final XmlTag[] subTags = rootElement.getXmlTag().getSubTags();
                for (XmlTag subTag : subTags) {
                    if (isGlobalElement(subTag)) {
                        result.add(subTag);
                    }
                }
            }
        }
    }
    return result;
}
 
Example 7
Source File: MuleConfigUtils.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
@NotNull
private static List<DomElement> getFlowsInScope(Project project, GlobalSearchScope searchScope) {
    final List<DomElement> result = new ArrayList<>();
    final Collection<VirtualFile> files = FileTypeIndex.getFiles(StdFileTypes.XML, searchScope);
    final DomManager manager = DomManager.getDomManager(project);
    for (VirtualFile file : files) {
        final PsiFile xmlFile = PsiManager.getInstance(project).findFile(file);
        if (isMuleFile(xmlFile)) {
            final DomFileElement<Mule> fileElement = manager.getFileElement((XmlFile) xmlFile, Mule.class);
            if (fileElement != null) {
                final Mule rootElement = fileElement.getRootElement();
                result.addAll(rootElement.getFlows());
                result.addAll(rootElement.getSubFlows());
            }
        }
    }
    return result;
}
 
Example 8
Source File: MuleConfigUtils.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
@Nullable
private static XmlTag findFlowInScope(Project project, String flowName, GlobalSearchScope searchScope) {
    final Collection<VirtualFile> files = FileTypeIndex.getFiles(StdFileTypes.XML, searchScope);
    for (VirtualFile file : files) {
        XmlTag flow = findFlowInFile(project, flowName, file);
        if (flow != null) {
            return flow;
        }
    }
    return null;
}
 
Example 9
Source File: GlobalConfigsTreeStructure.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
@Override
        protected SimpleNode[] buildChildren() {
            List<SimpleNode> myConfigNodes = new ArrayList<>();

            final DomManager manager = DomManager.getDomManager(myProject);

            final Collection<VirtualFile> files = FileTypeIndex.getFiles(StdFileTypes.XML, GlobalSearchScope.projectScope(myProject));

            for (VirtualFile file : files) {

                final PsiFile xmlFile = PsiManager.getInstance(myProject).findFile(file);

                if (xmlFile != null) {
//                    PsiDirectory directory = xmlFile.getParent();
//                    Module module = ModuleUtilCore.findModuleForPsiElement((PsiElement) (directory == null ? xmlFile : directory));

                    if (MuleConfigUtils.isMuleFile(xmlFile)) {
                        final DomFileElement<Mule> fileElement = manager.getFileElement((XmlFile) xmlFile, Mule.class);

                        if (fileElement != null) {
                            final Mule rootElement = fileElement.getRootElement();
                            XmlTag[] subTags = rootElement.getXmlTag().getSubTags();

                            for (XmlTag nextTag : subTags) {
                                MuleElementType muleElementType = MuleConfigUtils.getMuleElementTypeFromXmlElement(nextTag);

                                if (muleElementType != null && //This is a global config file and it has at least one connector
                                        (MuleElementType.CONFIG.equals(muleElementType) || (MuleElementType.TRANSPORT_CONNECTOR.equals(muleElementType)))) {
                                    MuleConfigNode nextConfigNode = new MuleConfigNode(this, xmlFile);
                                    myConfigNodes.add(nextConfigNode);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return myConfigNodes.toArray(new SimpleNode[]{});
        }
 
Example 10
Source File: ArmaPluginUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * @param module {@link Module} instance to get a stringtable.xml file for
 * @return a {@link VirtualFile} that maps to a Stringtable.xml file (name case sensitivity doesn't matter),
 * or null if couldn't be found
 */
@Nullable
public static VirtualFile getStringTableXmlFile(@NotNull Module module) {
	Collection<VirtualFile> files = FileTypeIndex.getFiles(XmlFileType.INSTANCE, module.getModuleContentScope());
	for (VirtualFile virtFile : files) {
		if (virtFile.getName().equalsIgnoreCase("stringtable.xml")) {
			return virtFile;
		}
	}
	return null;
}
 
Example 11
Source File: SQFReferenceContributor.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Adds all {@link SQFCommand} instances in the current module that is equal to findCommand into a list and returns it
 *
 * @param project     project
 * @param findCommand the command
 * @return list
 */
@NotNull
public static List<SQFCommand> findAllCommandInstances(@NotNull Project project, @NotNull SQFCommand findCommand) {
	List<SQFCommand> result = new ArrayList<>();
	Module m = ModuleUtil.findModuleForPsiElement(findCommand);
	if (m == null) {
		return result;
	}
	GlobalSearchScope searchScope = m.getModuleContentScope();
	Collection<VirtualFile> files = FileTypeIndex.getFiles(SQFFileType.INSTANCE, searchScope);
	for (VirtualFile virtualFile : files) {
		PsiFile file = PsiManager.getInstance(project).findFile(virtualFile);
		if (!(file instanceof SQFFile)) {
			continue;
		}
		SQFFile sqfFile = (SQFFile) file;
		PsiUtil.traverseBreadthFirstSearch(sqfFile.getNode(), astNode -> {
			PsiElement nodeAsElement = astNode.getPsi();
			if (nodeAsElement instanceof SQFCommand) {
				SQFCommand command = (SQFCommand) nodeAsElement;
				if (command.commandNameEquals(findCommand.getCommandName())) {
					result.add(command);
				}
			}
			return false;
		});
	}
	return result;
}
 
Example 12
Source File: SQFReferenceContributor.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Adds all {@link SQFVariable}s in the current module that is equal to findVar into a list and returns it
 * <p>
 * If findVar is a local variable, the list returned will be empty.
 *
 * @param project project
 * @param findVar variable
 * @return list
 */
@NotNull
public static List<SQFVariable> findGlobalVariables(@NotNull Project project, @NotNull SQFVariable findVar) {
	List<SQFVariable> result = new ArrayList<>();
	if (findVar.isLocal()) {
		return result;
	}
	Module m = ModuleUtil.findModuleForPsiElement(findVar);
	if (m == null) {
		return result;
	}
	GlobalSearchScope searchScope = m.getModuleContentScope();
	Collection<VirtualFile> files = FileTypeIndex.getFiles(SQFFileType.INSTANCE, searchScope);
	for (VirtualFile virtualFile : files) {
		PsiFile file = PsiManager.getInstance(project).findFile(virtualFile);
		if (!(file instanceof SQFFile)) {
			continue;
		}
		SQFFile sqfFile = (SQFFile) file;
		PsiUtil.traverseBreadthFirstSearch(sqfFile.getNode(), astNode -> {
			PsiElement nodeAsElement = astNode.getPsi();
			if (nodeAsElement instanceof SQFVariable) {
				SQFVariable var = (SQFVariable) nodeAsElement;
				if (var.isLocal()) {
					return false;
				}
				if (SQFVariableName.nameEquals(var.getVarName(), findVar.getVarName())) {
					result.add(var);
				}
			}
			return false;
		});
	}
	return result;
}
 
Example 13
Source File: ArmaPluginUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * @param module {@link Module} instance to get a stringtable.xml file for
 * @return a {@link VirtualFile} that maps to a Stringtable.xml file (name case sensitivity doesn't matter),
 * or null if couldn't be found
 */
@Nullable
public static VirtualFile getStringTableXmlFile(@NotNull Module module) {
	Collection<VirtualFile> files = FileTypeIndex.getFiles(XmlFileType.INSTANCE, module.getModuleContentScope());
	for (VirtualFile virtFile : files) {
		if (virtFile.getName().equalsIgnoreCase("stringtable.xml")) {
			return virtFile;
		}
	}
	return null;
}
 
Example 14
Source File: TreeFileChooserDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public String[] getNames(final boolean checkBoxState) {
  final String[] fileNames;
  if (myFileType != null && myProject != null) {
    GlobalSearchScope scope = myShowLibraryContents ? GlobalSearchScope.allScope(myProject) : GlobalSearchScope.projectScope(myProject);
    Collection<VirtualFile> virtualFiles = FileTypeIndex.getFiles(myFileType, scope);
    fileNames = ContainerUtil.map2Array(virtualFiles, String.class, new Function<VirtualFile, String>() {
      @Override
      public String fun(VirtualFile file) {
        return file.getName();
      }
    });
  }
  else {
    fileNames = FilenameIndex.getAllFilenames(myProject);
  }
  final Set<String> array = new THashSet<String>();
  for (String fileName : fileNames) {
    if (!array.contains(fileName)) {
      array.add(fileName);
    }
  }

  final String[] result = ArrayUtil.toStringArray(array);
  Arrays.sort(result);
  return result;
}
 
Example 15
Source File: SQFReferenceContributor.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Adds all {@link SQFVariable}s in the current module that is equal to findVar into a list and returns it
 * <p>
 * If findVar is a local variable, the list returned will be empty.
 *
 * @param project project
 * @param findVar variable
 * @return list
 */
@NotNull
public static List<SQFVariable> findGlobalVariables(@NotNull Project project, @NotNull SQFVariable findVar) {
	List<SQFVariable> result = new ArrayList<>();
	if (findVar.isLocal()) {
		return result;
	}
	Module m = ModuleUtil.findModuleForPsiElement(findVar);
	if (m == null) {
		return result;
	}
	GlobalSearchScope searchScope = m.getModuleContentScope();
	Collection<VirtualFile> files = FileTypeIndex.getFiles(SQFFileType.INSTANCE, searchScope);
	for (VirtualFile virtualFile : files) {
		PsiFile file = PsiManager.getInstance(project).findFile(virtualFile);
		if (!(file instanceof SQFFile)) {
			continue;
		}
		SQFFile sqfFile = (SQFFile) file;
		PsiUtil.traverseBreadthFirstSearch(sqfFile.getNode(), astNode -> {
			PsiElement nodeAsElement = astNode.getPsi();
			if (nodeAsElement instanceof SQFVariable) {
				SQFVariable var = (SQFVariable) nodeAsElement;
				if (var.isLocal()) {
					return false;
				}
				if (SQFVariableName.nameEquals(var.getVarName(), findVar.getVarName())) {
					result.add(var);
				}
			}
			return false;
		});
	}
	return result;
}
 
Example 16
Source File: FileManager.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
public static List<PsiFile> getAllJavaFiles(Module module) {
    Collection<VirtualFile> javaVirtualFiles = FileTypeIndex.getFiles(JavaFileType.INSTANCE, moduleScope(module));
    List<PsiFile> javaFiles = new ArrayList<>();

    for (VirtualFile javaVFile : javaVirtualFiles) {
        PsiFile file = PsiManager.getInstance(module.getProject()).findFile(javaVFile);
        if (file != null && PsiTreeUtil.findChildrenOfType(file, PsiClass.class).size() > 0) {
            javaFiles.add(file);
        }
    }
    Collections.sort(javaFiles, (o1, o2) -> FileManager.getJavaFileName(o1).compareToIgnoreCase(FileManager.getJavaFileName(o2)));
    return javaFiles;
}
 
Example 17
Source File: LatteIndexUtil.java    From intellij-latte with MIT License 4 votes vote down vote up
private static void reparseFiles(@NotNull Project project) {
    Collection<VirtualFile> virtualFiles = FileTypeIndex.getFiles(LatteFileType.INSTANCE, GlobalSearchScope.allScope(project));
    FileContentUtilCore.reparseFiles(virtualFiles);
    //FileContentUtil.reparseFiles(project, virtualFiles, false);
}
 
Example 18
Source File: FileManager.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
public static List<VirtualFile> getAllSpecFiles(Project project) {
    Collection<VirtualFile> virtualFiles = FileTypeIndex.getFiles(SpecFileType.INSTANCE, GlobalSearchScope.projectScope(project));
    return new ArrayList<>(virtualFiles);
}
 
Example 19
Source File: FileManager.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
public static List<VirtualFile> getConceptFiles(Project project) {
    Collection<VirtualFile> virtualFiles = FileTypeIndex.getFiles(ConceptFileType.INSTANCE, GlobalSearchScope.projectScope(project));
    return new ArrayList<>(virtualFiles);
}
 
Example 20
Source File: MuleElementDefinitionService.java    From mule-intellij-plugins with Apache License 2.0 4 votes vote down vote up
@NotNull
    private List<MuleModuleDefinition> getModuleDefinitions(Project project, GlobalSearchScope searchScope) {
        final List<MuleModuleDefinition> result = new ArrayList<>();
        final Collection<VirtualFile> files = FileTypeIndex.getFiles(XmlFileType.INSTANCE, searchScope);
        for (VirtualFile file : files) {
            final PsiFile xmlFile = PsiManager.getInstance(project).findFile(file);
            if (xmlFile != null && isMuleSchema(xmlFile)) {
//                System.out.println("xmlFile = " + xmlFile.getName());
                final PsiElement[] children = xmlFile.getChildren();
                final XmlTag rootTag = ((XmlDocument) children[0]).getRootTag();
                if (rootTag != null) {
                    final String namespace = getNamespace(rootTag);
                    final String name = ArrayUtil.getLastElement(namespace.split("/"));
//                    System.out.println("namespace = " + namespace);
//                    System.out.println("name = " + name);
                    final XmlTag[] elements = rootTag.findSubTags("element", MuleSchemaUtils.HTTP_WWW_W3_ORG_2001_XMLSCHEMA);
                    final List<MuleElementDefinition> definitions = new ArrayList<>();
                    for (XmlTag element : elements) {
                        final String elementName = element.getAttributeValue("name");
//                        System.out.println("name = " + elementName);
                        final MuleElementType muleElementType = MuleSchemaUtils.getMuleElementTypeFromElement(element);
                        if (muleElementType != null) {
                            String description = "";
                            final XmlTag annotation = ArrayUtil.getFirstElement(element.findSubTags("annotation", MuleSchemaUtils.HTTP_WWW_W3_ORG_2001_XMLSCHEMA));
                            if (annotation != null) {
                                final XmlTag documentation = ArrayUtil.getFirstElement(annotation.findSubTags("documentation", MuleSchemaUtils.HTTP_WWW_W3_ORG_2001_XMLSCHEMA));
                                if (documentation != null) {
                                    description = documentation.getValue().getText();
                                }
                            }
                            definitions.add(new MuleElementDefinition(element, elementName, description, muleElementType));
//                            System.out.println("muleElementType = " + muleElementType);
//                            System.out.println("description = " + description);
                        }
                    }
                    result.add(new MuleModuleDefinition(name, StringUtil.capitalize(name), namespace, xmlFile, definitions));
                }
            }
        }
        return result;
    }