Skip to content

Provide file contents when determining content type for search #3014 #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.eclipse.search.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.search.core;singleton:=true
Bundle-Version: 3.16.400.qualifier
Bundle-Version: 3.16.500.qualifier
Bundle-Activator: org.eclipse.search.internal.core.SearchCorePlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %providerName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,13 @@ public IStatus search(TextSearchScope scope, IProgressMonitor monitor) {
private final IContentType TEXT_TYPE = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);

private boolean hasBinaryContentType(IFile file) {
IContentType[] contentTypes = Platform.getContentTypeManager().findContentTypesFor(file.getName());
IContentType[] contentTypes;
try (java.io.InputStream contents = file.getContents()) {
contentTypes = Platform.getContentTypeManager().findContentTypesFor(contents, file.getName());
} catch (IOException | CoreException e) {
SearchCorePlugin.log(e);
contentTypes = Platform.getContentTypeManager().findContentTypesFor(file.getName());
}
for (IContentType contentType : contentTypes) {
if (contentType.isKindOf(TEXT_TYPE)) {
return false; // is text
Expand Down
2 changes: 1 addition & 1 deletion tests/org.eclipse.search.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.search.tests;singleton:=true
Bundle-Version: 3.11.700.qualifier
Bundle-Version: 3.11.800.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.search.core.tests;x-internal:=true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

Expand All @@ -26,7 +29,11 @@
import org.junit.ClassRule;
import org.junit.Test;

import org.eclipse.core.runtime.ContributorFactorySimple;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IContributor;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
Expand Down Expand Up @@ -547,6 +554,70 @@ private TestResult[] performSearch(TestResultCollector collector, String[] fileN

}

@Test
public void testBinaryContentTypeWithDescriberSerial() throws Exception {
testBinaryContentTypeWithDescriber(new SerialTestResultCollector());
}

@Test
public void testBinaryContentTypeWithDescriberParallel() throws Exception {
testBinaryContentTypeWithDescriber(new ParallelTestResultCollector());
}

private void testBinaryContentTypeWithDescriber(TestResultCollector collector) throws Exception {
IExtensionRegistry registry= Platform.getExtensionRegistry();

Field field= org.eclipse.core.internal.registry.ExtensionRegistry.class
.getDeclaredField("masterToken");
field.setAccessible(true);
Object masterToken= field.get(registry);

IContributor contributor= ContributorFactorySimple.createContributor(this);

try (java.io.InputStream is= new ByteArrayInputStream("""
<?xml version="1.0"?>
<plugin>
<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type
id="org.eclipse.search.tests.binaryFile"
name="Search Test Binary File"
priority="low">
<describer
class="org.eclipse.core.runtime.content.BinarySignatureDescriber"
plugin="org.eclipse.core.contenttype">
<!-- "binary" in ASCII encoding -->
<parameter name="signature" value="62 69 6E 61 72 79"/>
</describer>
</content-type>
<file-association
content-type="org.eclipse.search.tests.binaryFile"
file-patterns="[^.]+"/> <!-- no file extension -->
</extension>
</plugin>""".getBytes())) {
registry.addContribution(is, contributor, false, null, null, masterToken);
try (AutoCloseable c= () -> {
Arrays.stream(registry.getExtensions(contributor))
.forEach(extension -> registry.removeExtension(extension, masterToken));
}) {

IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1"));
IFile textfile= ResourceHelper.createFile(folder, "textfile", "text hello");
IFile binaryfile= ResourceHelper.createFile(folder, "binaryfile", "binary hello");

Pattern searchPattern= PatternConstructor.createPattern("hello", true, false);

FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { fProject }, (String[]) null, false);
TextSearchEngine.create().search(scope, collector, searchPattern, null);

TestResult[] results= collector.getResults();
assertEquals("Number of total results", 1, results.length);

assertMatches(results, 1, textfile, "text hello", "hello");
assertMatches(results, 0, binaryfile, "binary hello", "hello");
}
}
}



private void assertMatches(TestResult[] results, int expectedCount, IFile file, String fileContent, String string) {
Expand Down