public static List<AsciiDocSection> findProperties(Project project, String key) { List<AsciiDocSection> result = null; Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, AsciiDocFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { AsciiDocFile asciiDocFile = (AsciiDocFile)PsiManager.getInstance(project).findFile(virtualFile); if (asciiDocFile != null) { ArrayList<AsciiDocSection> properties = new ArrayList<>(); new PsiFilter(AsciiDocSection.class).createVisitor(properties).visitFile(asciiDocFile); for (AsciiDocSection property : properties) { if (key.equals(property.getTitle())) { if (result == null) { result = new ArrayList<>(); } result.add(property); } } } } return result != null ? result : Collections.emptyList(); }
public static <T extends PsiElement> Map<T, FileStatus> getElementsChanged(PsiElement file, PsiElement oldFile, final PsiFilter<T> filter) { final HashMap<T, FileStatus> result = new HashMap<T, FileStatus>(); final List<T> oldElements = new ArrayList<T>(); final List<T> elements = new ArrayList<T>(); if (file == null) { oldFile.accept(filter.createVisitor(oldElements)); calculateStatuses(elements, oldElements, result, filter); return result; } final Project project = file.getProject(); file.accept(filter.createVisitor(elements)); final VirtualFile vf = file.getContainingFile().getVirtualFile(); FileStatus status = vf == null ? null : FileStatusManager.getInstance(project).getStatus(vf); if (status == null && oldFile == null) { status = FileStatus.ADDED; } if (status == FileStatus.ADDED || status == FileStatus.DELETED || status == FileStatus.DELETED_FROM_FS || status == FileStatus.UNKNOWN) { for (T element : elements) { result.put(element, status); } return result; } if (oldFile == null) return result; oldFile.accept(filter.createVisitor(oldElements)); calculateStatuses(elements, oldElements, result, filter); return result; }
public static <T extends PsiElement> Map<T, FileStatus> getElementsChanged(PsiFile file, final PsiFilter<T> filter) { final Project project = file.getProject(); final VirtualFile vf = file.getVirtualFile(); if (vf == null) return Collections.emptyMap(); final String oldText = getUnmodifiedDocument(vf, project); //TODO: make loop for different languages //TODO: for ( PsiFile f : file.getViewProvider().getAllFiles() ) //TODO: for some languages (eg XML) isEquivalentTo works ugly. Think about pluggable matchers for different languages/elements final PsiFile oldFile = oldText == null ? null : PsiFileFactory.getInstance(project).createFileFromText(oldText, file); return getElementsChanged(file, oldFile, filter); }
public static List<AsciiDocSection> findProperties(Project project) { List<AsciiDocSection> result = new ArrayList<>(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, AsciiDocFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { AsciiDocFile simpleFile = (AsciiDocFile)PsiManager.getInstance(project).findFile(virtualFile); if (simpleFile != null) { new PsiFilter(AsciiDocSection.class).createVisitor(result).visitFile(simpleFile); } } return result; }
public void testMethods() throws Exception { doTest(new PsiFilter<PsiMethod>(PsiMethod.class)); }
public void testFields() throws Exception { doTest(new PsiFilter<PsiField>(PsiField.class)); }
private <T extends PsiElement> void doTest(PsiFilter<T> filter) throws IOException { final Map<T,FileStatus> map = PsiChangeTracker.getElementsChanged(myChangedFile, myOriginalFile, filter); final Map<String, String> changes = convert(map); final Map<String, String> expected = getExpectedResults(); assert expected.equals(changes) : "Changes are not equal \n Expected:\n" + expected + "\nFound:\n" + changes; }