Java Code Examples for com.intellij.psi.search.FilenameIndex#getAllFilesByExt()

The following examples show how to use com.intellij.psi.search.FilenameIndex#getAllFilesByExt() . 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: NutzInjectToPropsReferenceContributor.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
    PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
    String value = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null;
    String key = NutzInjectConfUtil.getKey(value);
    if (NutzInjectConfUtil.isInjectConf(literalExpression) && key != null) {
        Project project = element.getProject();
        final Collection<VirtualFile> propertiesFiles = FilenameIndex.getAllFilesByExt(project, "properties", getSearchScope(project, element));
        final List<PsiElement> properties = NutzInjectConfUtil.findPropertiesPsiElement(project, propertiesFiles, key);
        List<PsiReference> psiReferences = new ArrayList<>();
        properties.forEach(psiElement -> psiReferences.add(new PsiJavaInjectReference(element, psiElement)));
        return psiReferences.toArray(new PsiReference[0]);
    } else {
        return PsiReference.EMPTY_ARRAY;
    }
}
 
Example 2
Source File: DrupalUtil.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
@NotNull
public static Set<String> getModuleNames(@NotNull Project project) {
    Set<String> allFilesByExt = new HashSet<>();

    for (VirtualFile virtualFile : FilenameIndex.getAllFilesByExt(project, "yml")) {
        if(!virtualFile.getName().endsWith(".info.yml")) {
            continue;
        }

        allFilesByExt.add(StringUtils.stripEnd(virtualFile.getName(), ".info.yml"));
    }

    allFilesByExt.addAll(FilenameIndex.getAllFilesByExt(project, "module").stream()
        .map(virtualFile -> StringUtils.stripEnd(virtualFile.getName(), ".module"))
        .collect(Collectors.toList()));

    return allFilesByExt;
}
 
Example 3
Source File: AndroidUtils.java    From idea-android-studio-plugin with GNU General Public License v2.0 6 votes vote down vote up
public static List<PsiFile> getLayoutFiles(Project project) {

        List<PsiFile> psiFileList = new ArrayList<PsiFile>();

        for (VirtualFile virtualFile : FilenameIndex.getAllFilesByExt(project, "xml")) {
            VirtualFile parent = virtualFile.getParent();
            if (parent != null && "layout".equals(parent.getName())) {
                String relative = VfsUtil.getRelativePath(virtualFile, project.getBaseDir(), '/');
                if (relative != null) {
                    PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
                    if (psiFile != null) {
                        psiFileList.add(psiFile);
                    }
                }
            }
        }

        return psiFileList;
    }
 
Example 4
Source File: DojoModuleFileResolver.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
/**
 * gets a list of all files that are part of your dojo project sources.
 * only retrieves javascript files
 *
 * @param project
 * @return
 */
public Collection<VirtualFile> getAllDojoProjectSourceFiles(Project project)
{
    VirtualFile[] sourceDirectories = SourcesLocator.getProjectSourceDirectories(project, true);
    List<VirtualFile> files = new ArrayList<VirtualFile>();
    for(VirtualFile file : sourceDirectories)
    {
        files.add(file);
    }

    Set<VirtualFile> psiFiles = new HashSet<VirtualFile>();
    Collection<VirtualFile> results = FilenameIndex.getAllFilesByExt(project, "js", GlobalSearchScope.projectScope(project));
    for(VirtualFile result : results)
    {
        for(VirtualFile directory : sourceDirectories)
        {
            if(VfsUtilCore.isAncestor(directory, result, false))
            {
                psiFiles.add(result);
                continue;
            }
        }
    }

    return results;
}
 
Example 5
Source File: NutzLineUtil.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
/**
 * 取得文件
 *
 * @param bindingElement
 * @return
 */
public static List<VirtualFile> findTemplteFileList(PsiElement bindingElement) {
    JavaNutzTemplateVO nutzTemplate = getTemplateFilePathAndName(bindingElement);
    List<VirtualFile> fileList = new ArrayList<>();
    String[] exts = nutzTemplate.getFileExtension().split(";");
    for (String ext : exts) {
        if (ext.trim() != "") {
            //优化定位至当前Module 跨模块资源无法取得还是返回
            //GlobalSearchScope globalSearchScope = ((ModuleWithDependenciesScope) bindingElement.getResolveScope()).getModule().getModuleScope();
            Collection<VirtualFile> tempVirtualFiles = FilenameIndex.getAllFilesByExt(bindingElement.getProject(), ext.replaceAll("\\.", ""), GlobalSearchScope.projectScope(bindingElement.getProject()));
            if (tempVirtualFiles != null && tempVirtualFiles.size() > 0) {
                String tempNutzFileName = nutzTemplate.getTemplatePath();
                //兼容绝对路径模式
                if (tempNutzFileName.endsWith(ext) && tempNutzFileName.indexOf("/") > -1) {
                    tempNutzFileName = tempNutzFileName.substring(0, tempNutzFileName.length() - ext.length());
                }
                //去除前缀
                tempNutzFileName = tempNutzFileName.substring(nutzTemplate.getName().length());
                //补上文件后缀
                tempNutzFileName = tempNutzFileName.replace(".", "/") + ext;
                String finalTempNutzFileName = tempNutzFileName;
                tempVirtualFiles.stream().filter(virtualFile -> virtualFile.getCanonicalPath().endsWith(finalTempNutzFileName))
                        .forEach(virtualFile -> fileList.add(virtualFile));
            }
        }
    }
    return fileList;
}
 
Example 6
Source File: HtmlTemplateLineUtil.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
/**
 * 取得文件
 *
 * @param bindingElement
 * @return
 */
public static List<VirtualFile> findTemplteFileList(PsiElement bindingElement) {
    String path = getTemplateFilePathAndName(bindingElement);
    Collection<VirtualFile> virtualFiles = FilenameIndex.getAllFilesByExt(bindingElement.getProject(), getFileExtension(path).replaceAll("\\.", ""), GlobalSearchScope.projectScope(bindingElement.getProject()));
    List<VirtualFile> fileList = new ArrayList<>();
    virtualFiles.stream().filter(virtualFile -> virtualFile.getCanonicalPath().endsWith(path))
            .forEach(virtualFile -> fileList.add(virtualFile));
    return fileList;
}
 
Example 7
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
public static List<VirtualFile> findTemplteFileList(PsiElement psiAnnotationParamList) {
    Object value = getTemplteFileName(psiAnnotationParamList);
    ArrayList<VirtualFile> arrayList = new ArrayList<>();
    String xmlPath = psiAnnotationParamList.getContainingFile().getParent().getVirtualFile().getPath() + "/" + value;
    Collection<VirtualFile> virtualFiles = FilenameIndex.getAllFilesByExt(psiAnnotationParamList.getProject(), "xml", GlobalSearchScope.projectScope(psiAnnotationParamList.getProject()));
    Optional<VirtualFile> optional = virtualFiles.stream().filter(virtualFile -> virtualFile.getPath().equals(xmlPath)).findFirst();
    if (optional.isPresent()) {
        arrayList.add(optional.get());
    }
    return arrayList;
}
 
Example 8
Source File: NutzInjectConfFoldingBuilder.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean b) {
    Project project = root.getProject();
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<VirtualFile> propertiesFiles = FilenameIndex.getAllFilesByExt(project, "properties", GlobalSearchScope.projectScope(project));
    Collection<PsiLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, PsiLiteralExpression.class);
    for (final PsiLiteralExpression literalExpression : literalExpressions) {
        if (!NutzInjectConfUtil.isInjectConf(literalExpression)) {
            continue;
        }
        String value = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null;
        String key = NutzInjectConfUtil.getKey(value);
        if (key != null) {
            final List<String> properties = NutzInjectConfUtil.findProperties(project, propertiesFiles, key);
            final TextRange textRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
            String data;
            if (properties.size() == 1) {
                data = properties.get(0);
            } else if (properties.size() > 1) {
                data = properties.get(0) + "[该键值存在多个配置文件中!]";
            } else {
                data = "[" + key + "]不存在,使用时可能产生异常,请检查!";
            }
            descriptors.add(new NutzLocalizationFoldingDescriptor(literalExpression.getNode(), textRange, data));
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
 
Example 9
Source File: NutzLocalizationFoldingBuilder.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    Project project = root.getProject();
    String localizationPackage = NutzLocalUtil.getLocalizationPackage(project);
    if (null == localizationPackage) {
        return FoldingDescriptor.EMPTY;
    }
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<VirtualFile> propertiesFiles = FilenameIndex.getAllFilesByExt(project, "properties", GlobalSearchScope.projectScope(project));
    Collection<PsiLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, PsiLiteralExpression.class);
    for (final PsiLiteralExpression literalExpression : literalExpressions) {
        if (!NutzLocalUtil.isLocal(literalExpression)) {
            continue;
        }
        String key = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null;
        if (key != null) {
            final List<String> properties = NutzLocalUtil.findProperties(project, propertiesFiles, localizationPackage, key);
            TextRange textRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
            String value;
            if (properties.size() == 1) {
                value = properties.get(0);
            } else if (properties.size() > 1) {
                value = properties.get(0) + "[该键值存在多个配置文件中!]";
            } else {
                value = "国际化信息中不存在[" + key + "],使用时可能产生异常,请检查!";
            }
            descriptors.add(new NutzLocalizationFoldingDescriptor(literalExpression.getNode(), textRange, value));
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
 
Example 10
Source File: Java2XmlReferenceContributor.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
    PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
    Object literalExpressionValue = literalExpression.getValue();
    String value = literalExpressionValue instanceof String ? (String) literalExpressionValue : null;
    if (Objects.nonNull(value) && !value.contains(" ")) {
        List<String> fieldStrings = new ArrayList<>();
        PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
        if (SqlsXmlUtil.hasExecuteService(psiClass)) {
            //如果是父类实现了这个service暂时不支持跳转
            fieldStrings.addAll(SqlsXmlUtil.addExecuteService(element.getProject()));
        }
        PsiField[] fields = psiClass.getFields();
        List<PsiField> extendsClassFields = SqlsXmlUtil.getExtendsClassFields(psiClass);
        extendsClassFields.addAll(Arrays.asList(fields));
        for (PsiField field : extendsClassFields) {
            if (SqlsXmlUtil.isSqlTplField(field)) {
                fieldStrings.add(field.getName() + ".");
            }
        }
        if (CollectionUtils.isNotEmpty(fieldStrings)) {
            boolean a = SqlsXmlUtil.isInjectXml(literalExpression, fieldStrings);
            if (a) {
                Project project = element.getProject();
                //查询文件名,减少匹配范围
                String xmlFileName = SqlsXmlUtil.findXmlFileName(psiClass);
                Collection<VirtualFile> virtualFiles = FilenameIndex.getVirtualFilesByName(project, xmlFileName, SqlsXmlUtil.getSearchScope(project, element));
                if (CollectionUtils.isEmpty(virtualFiles)) {
                    //扩大查找范围
                    virtualFiles = FilenameIndex.getAllFilesByExt(project, "xml", SqlsXmlUtil.getSearchScope(project, element));
                }
                final List<PsiElement> elements = SqlsXmlUtil.findXmlPsiElement(project, virtualFiles, value);
                return elements.stream().map(psiElement -> new PsiJavaInjectReference(element, psiElement)).toArray(PsiReference[]::new);
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
 
Example 11
Source File: ImagesProjectNode.java    From intellij-sdk-docs with Apache License 2.0 5 votes vote down vote up
private void addAllByExt(Project project, String ext) {
  final Set<VirtualFile> imagesFiles = getImagesFiles(project);
  final VirtualFile projectDir = ProjectUtil.guessProjectDir(project);
  for (VirtualFile file : FilenameIndex.getAllFilesByExt(project, ext)) {
    while (file != null && !file.equals(projectDir)) {
      imagesFiles.add(file);
      file = file.getParent();
    }
  }
}
 
Example 12
Source File: StepUtil.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
private static VirtualFile[] findConceptFiles(Module module) {
    Collection<VirtualFile> conceptFiles = FilenameIndex.getAllFilesByExt(module.getProject(), Constants.CONCEPT_EXTENSION);
    return conceptFiles.toArray(new VirtualFile[conceptFiles.size()]);
}