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

The following examples show how to use com.intellij.psi.search.FilenameIndex#getVirtualFilesByName() . 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: 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 2
Source File: GraphQLRelayModernEnableStartupActivity.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public void runActivity(@NotNull Project project) {
    final GraphQLSettings settings = GraphQLSettings.getSettings(project);
    if (settings.isEnableRelayModernFrameworkSupport()) {
        // already enabled Relay Modern
        return;
    }
    try {
        final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
        for (VirtualFile virtualFile : FilenameIndex.getVirtualFilesByName(project, "package.json", scope)) {
            if (!virtualFile.isDirectory() && virtualFile.isInLocalFileSystem()) {
                try (InputStream inputStream = virtualFile.getInputStream()) {
                    final String packageJson = IOUtils.toString(inputStream, virtualFile.getCharset());
                    if (packageJson.contains("\"react-relay\"") || packageJson.contains("\"relay-compiler\"")) {
                        final Notification enableRelayModern = new Notification("GraphQL", "Relay Modern project detected", "<a href=\"enable\">Enable Relay Modern</a> GraphQL tooling", NotificationType.INFORMATION, (notification, event) -> {
                            settings.setEnableRelayModernFrameworkSupport(true);
                            ApplicationManager.getApplication().saveSettings();
                            notification.expire();
                            DaemonCodeAnalyzer.getInstance(project).restart();
                            EditorNotifications.getInstance(project).updateAllNotifications();
                        });
                        enableRelayModern.setImportant(true);
                        Notifications.Bus.notify(enableRelayModern);
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Unable to detect Relay Modern", e);
    }
}
 
Example 3
Source File: OSSPantsIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Find VirtualFile in project by filename.
 */
@NotNull
protected VirtualFile searchForVirtualFileInProject(String filename) {
  Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(myProject, filename, GlobalSearchScope.allScope(myProject));
  assertEquals(String.format("%s not found.", filename), 1, files.size());
  return files.iterator().next();
}
 
Example 4
Source File: OSSPantsIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Find VirtualFile in project by filename.
 */
@NotNull
protected VirtualFile firstMatchingVirtualFileInProject(String filename) {
  Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(myProject, filename, GlobalSearchScope.allScope(myProject));
  assertTrue(String.format("Filename %s not found in project", filename), files.size() > 0);
  return files.iterator().next();
}
 
Example 5
Source File: JumpFromRemoteFileToLocalAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static Collection<VirtualFile> findLocalFiles(Project project, String url, String fileName) {
  for (LocalFileFinder finder : LocalFileFinder.EP_NAME.getExtensions()) {
    final VirtualFile file = finder.findLocalFile(url, project);
    if (file != null) {
      return Collections.singletonList(file);
    }
  }

  return FilenameIndex.getVirtualFilesByName(project, fileName, GlobalSearchScope.allScope(project));
}
 
Example 6
Source File: UniqueVFilePathBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static UniqueNameBuilder<VirtualFile> filesWithTheSameName(String fileName, Project project,
                                                                   boolean skipNonOpenedFiles,
                                                                   GlobalSearchScope scope) {
  Collection<VirtualFile> filesWithSameName = skipNonOpenedFiles ? Collections.emptySet() :
                                              FilenameIndex.getVirtualFilesByName(project, fileName,
                                                                                  scope);
  THashSet<VirtualFile> setOfFilesWithTheSameName = new THashSet<>(filesWithSameName);
  // add open files out of project scope
  for(VirtualFile openFile: FileEditorManager.getInstance(project).getOpenFiles()) {
    if (openFile.getName().equals(fileName)) {
      setOfFilesWithTheSameName.add(openFile);
    }
  }
  if (!skipNonOpenedFiles) {
    for (VirtualFile recentlyEditedFile : EditorHistoryManager.getInstance(project).getFiles()) {
      if (recentlyEditedFile.getName().equals(fileName)) {
        setOfFilesWithTheSameName.add(recentlyEditedFile);
      }
    }
  }

  filesWithSameName = setOfFilesWithTheSameName;

  if (filesWithSameName.size() > 1) {
    String path = project.getBasePath();
    path = path == null ? "" : FileUtil.toSystemIndependentName(path);
    UniqueNameBuilder<VirtualFile> builder = new UniqueNameBuilder<>(path, File.separator, 25);
    for (VirtualFile virtualFile: filesWithSameName) {
      builder.addPath(virtualFile, virtualFile.getPath());
    }
    return builder;
  }
  return null;
}
 
Example 7
Source File: ORProjectManager.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
public static Set<VirtualFile> findFilesInProject(@NotNull String filename, @NotNull Project project) {
    GlobalSearchScope scope = GlobalSearchScope.allScope(project);
    Collection<VirtualFile> virtualFilesByName = FilenameIndex.getVirtualFilesByName(project, filename, scope);
    return new HashSet<>(virtualFilesByName);
}
 
Example 8
Source File: PsiPatchBaseDirectoryDetector.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<VirtualFile> findFiles(final String fileName) {
  return FilenameIndex.getVirtualFilesByName(myProject, fileName, GlobalSearchScope.projectScope(myProject));
}
 
Example 9
Source File: GradleUtil.java    From freeline with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * 查找所有的build.gradle文件
 *
 * @param project
 * @return
 */
public static Collection<VirtualFile> getAllGradleFile(Project project) {
    Collection<VirtualFile> collection = FilenameIndex.getVirtualFilesByName(project,
            GradleConstants.DEFAULT_SCRIPT_NAME, GlobalSearchScope.allScope(project));
    return collection == null ? Collections.EMPTY_LIST : collection;
}