Search in sources :

Example 1 with TextSearchScope

use of org.eclipse.search.core.text.TextSearchScope in project eclipse.platform.text by eclipse.

the class FileSearchTests method testDerivedFiles.

private void testDerivedFiles(TestResultCollector collector) throws Exception {
    StringBuilder buf = new StringBuilder();
    buf.append("hello\n");
    IFolder folder1 = ResourceHelper.createFolder(fProject.getFolder("folder1"));
    ResourceHelper.createFile(folder1, "file1", buf.toString());
    IFile file2 = ResourceHelper.createFile(folder1, "file2", buf.toString());
    file2.setDerived(true, null);
    IFolder folder2 = ResourceHelper.createFolder(folder1.getFolder("folder2"));
    folder2.setDerived(true, null);
    ResourceHelper.createFile(folder2, "file3", buf.toString());
    IFolder folder3 = ResourceHelper.createFolder(folder2.getFolder("folder3"));
    ResourceHelper.createFile(folder3, "file4", buf.toString());
    IFolder folder4 = ResourceHelper.createFolder(folder1.getFolder("folder4"));
    ResourceHelper.createFile(folder4, "file5", buf.toString());
    /**
     * folder1
     *     file1
     *     file2*
     *     folder2*
     *       file3
     *       folder3
     *         file4
     *     folder4
     *       file5
     */
    Pattern searchPattern = PatternConstructor.createPattern("hello", false, true);
    Pattern fileNamePattern = PatternConstructor.createPattern("*", false, false);
    TextSearchEngine engine = TextSearchEngine.create();
    {
        // visit all
        TextSearchScope scope = TextSearchScope.newSearchScope(new IResource[] { fProject }, fileNamePattern, true);
        collector.reset();
        engine.search(scope, collector, searchPattern, null);
        assertEquals(5, collector.getNumberOfResults());
    }
    {
        // visit non-derived
        TextSearchScope scope = TextSearchScope.newSearchScope(new IResource[] { fProject }, fileNamePattern, false);
        collector.reset();
        engine.search(scope, collector, searchPattern, null);
        assertEquals(2, collector.getNumberOfResults());
    }
    {
        // visit all in folder2
        TextSearchScope scope = TextSearchScope.newSearchScope(new IResource[] { folder2 }, fileNamePattern, true);
        collector.reset();
        engine.search(scope, collector, searchPattern, null);
        assertEquals(2, collector.getNumberOfResults());
    }
    {
        // visit non-derived in folder2
        TextSearchScope scope = TextSearchScope.newSearchScope(new IResource[] { folder2 }, fileNamePattern, false);
        collector.reset();
        engine.search(scope, collector, searchPattern, null);
        assertEquals(0, collector.getNumberOfResults());
    }
    {
        // visit all in folder3
        TextSearchScope scope = TextSearchScope.newSearchScope(new IResource[] { folder3 }, fileNamePattern, true);
        collector.reset();
        engine.search(scope, collector, searchPattern, null);
        assertEquals(1, collector.getNumberOfResults());
    }
    {
        // visit non-derived in folder3
        TextSearchScope scope = TextSearchScope.newSearchScope(new IResource[] { folder3 }, fileNamePattern, false);
        collector.reset();
        engine.search(scope, collector, searchPattern, null);
        assertEquals(0, collector.getNumberOfResults());
    }
}
Also used : Pattern(java.util.regex.Pattern) TextSearchScope(org.eclipse.search.core.text.TextSearchScope) FileTextSearchScope(org.eclipse.search.ui.text.FileTextSearchScope) IFile(org.eclipse.core.resources.IFile) TextSearchEngine(org.eclipse.search.core.text.TextSearchEngine) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

Pattern (java.util.regex.Pattern)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 TextSearchEngine (org.eclipse.search.core.text.TextSearchEngine)1 TextSearchScope (org.eclipse.search.core.text.TextSearchScope)1 FileTextSearchScope (org.eclipse.search.ui.text.FileTextSearchScope)1