Bug 544315 - DocumentProviderRegistry: also try IPath to get extension

Make getDocumentProvider(IEditorInput) try a little harder by trying
to figure out an IPath from the editor input and use the extension
of that path, if one can be determined.

Change-Id: I6426e0e9fa09b71bd472d5ca545d1333c17323ce
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
diff --git a/org.eclipse.ui.editors.tests/META-INF/MANIFEST.MF b/org.eclipse.ui.editors.tests/META-INF/MANIFEST.MF
index 4583413..110f4b4 100644
--- a/org.eclipse.ui.editors.tests/META-INF/MANIFEST.MF
+++ b/org.eclipse.ui.editors.tests/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %Plugin.name
 Bundle-SymbolicName: org.eclipse.ui.editors.tests;singleton:=true
-Bundle-Version: 3.11.200.qualifier
+Bundle-Version: 3.11.300.qualifier
 Bundle-Vendor: %Plugin.providerName
 Bundle-Localization: plugin
 Export-Package: org.eclipse.ui.editors.tests
diff --git a/org.eclipse.ui.editors.tests/build.properties b/org.eclipse.ui.editors.tests/build.properties
index 4dff298..f59a82b 100644
--- a/org.eclipse.ui.editors.tests/build.properties
+++ b/org.eclipse.ui.editors.tests/build.properties
@@ -16,7 +16,8 @@
                test.xml,\
                about.html,\
                .,\
-               META-INF/
+               META-INF/,\
+               plugin.xml
 
 src.includes = about.html
 
diff --git a/org.eclipse.ui.editors.tests/plugin.xml b/org.eclipse.ui.editors.tests/plugin.xml
new file mode 100644
index 0000000..f1c781f
--- /dev/null
+++ b/org.eclipse.ui.editors.tests/plugin.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2019 Thomas Wolf <thomas.wolf@paranor.ch>
+
+ This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License 2.0
+ which accompanies this distribution, and is available at
+ https://www.eclipse.org/legal/epl-2.0/
+
+ SPDX-License-Identifier: EPL-2.0
+-->
+<plugin>
+	<extension point="org.eclipse.ui.editors.documentProviders">
+		<provider id="org.eclipse.ui.editors.tests.documentProvider"
+			class="org.eclipse.ui.editors.tests.DocumentProviderRegistryTest$TestDocumentProvider"
+			extensions="testfile">
+		</provider>
+	</extension>
+</plugin>
\ No newline at end of file
diff --git a/org.eclipse.ui.editors.tests/pom.xml b/org.eclipse.ui.editors.tests/pom.xml
index 51fa4fa..5feb9da 100644
--- a/org.eclipse.ui.editors.tests/pom.xml
+++ b/org.eclipse.ui.editors.tests/pom.xml
@@ -19,7 +19,7 @@
   </parent>
   <groupId>org.eclipse.ui</groupId>
   <artifactId>org.eclipse.ui.editors.tests</artifactId>
-  <version>3.11.200-SNAPSHOT</version>
+  <version>3.11.300-SNAPSHOT</version>
   <packaging>eclipse-test-plugin</packaging>
   <properties>
   	<testSuite>${project.artifactId}</testSuite>
diff --git a/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/DocumentProviderRegistryTest.java b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/DocumentProviderRegistryTest.java
new file mode 100644
index 0000000..58f6157
--- /dev/null
+++ b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/DocumentProviderRegistryTest.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Thomas Wolf <thomas.wolf@paranor.ch>
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *******************************************************************************/
+package org.eclipse.ui.editors.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+
+import org.junit.After;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import org.eclipse.core.filesystem.EFS;
+import org.eclipse.core.filesystem.IFileStore;
+
+import org.eclipse.core.runtime.Path;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+
+import org.eclipse.core.filebuffers.tests.ResourceHelper;
+
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.ide.FileStoreEditorInput;
+import org.eclipse.ui.part.FileEditorInput;
+
+import org.eclipse.ui.texteditor.DocumentProviderRegistry;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+
+import org.eclipse.ui.editors.text.TextFileDocumentProvider;
+
+public class DocumentProviderRegistryTest {
+
+	@Rule
+	public TemporaryFolder tmp = new TemporaryFolder();
+
+	private IFile file;
+
+	@After
+	public void tearDown() throws Exception {
+		if (file != null) {
+			ResourceHelper.delete(file.getProject());
+		}
+		TestUtil.cleanUp();
+	}
+
+	@Test
+	public void testFindByExtensionInWorkspace() throws Exception {
+		IFolder folder = ResourceHelper.createFolder("DocumentProviderRegistryTestProject/test");
+		file = ResourceHelper.createFile(folder, "file.testfile", "");
+		assertTrue("File should exist: " + file.toString(), file.exists());
+		IEditorInput editorInput = new FileEditorInput(file);
+		IDocumentProvider provider = DocumentProviderRegistry.getDefault().getDocumentProvider(editorInput);
+		assertEquals("Unexpected document provider found : " + provider.getClass().getName(),
+				TestDocumentProvider.class, provider.getClass());
+	}
+
+	@Test
+	public void testFindByExtensionNonWorkspace() throws Exception {
+		File external = tmp.newFile("external.testfile");
+		IFileStore store = EFS.getLocalFileSystem().getStore(new Path(external.getCanonicalPath()));
+		IEditorInput editorInput = new FileStoreEditorInput(store);
+		IDocumentProvider provider = DocumentProviderRegistry.getDefault().getDocumentProvider(editorInput);
+		assertEquals("Unexpected document provider found : " + provider.getClass().getName(),
+				TestDocumentProvider.class, provider.getClass());
+	}
+
+	public static class TestDocumentProvider extends TextFileDocumentProvider {
+
+		// Nothing; class registered in plugin.xml so that we can test that we
+		// found the right one.
+
+	}
+}
diff --git a/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EditorsTestSuite.java b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EditorsTestSuite.java
index f5e3dc9..98f7917 100644
--- a/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EditorsTestSuite.java
+++ b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EditorsTestSuite.java
@@ -26,6 +26,7 @@
 @RunWith(Suite.class)
 @SuiteClasses({
 		ChainedPreferenceStoreTest.class,
+		DocumentProviderRegistryTest.class,
 		EncodingChangeTests.class,
 		GotoLineTest.class,
 		SegmentedModeTest.class,
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/texteditor/DocumentProviderRegistry.java b/org.eclipse.ui.editors/src/org/eclipse/ui/texteditor/DocumentProviderRegistry.java
index d4aa164..c08410c 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/texteditor/DocumentProviderRegistry.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/texteditor/DocumentProviderRegistry.java
@@ -25,10 +25,12 @@
 
 import org.osgi.framework.Bundle;
 
+import org.eclipse.core.runtime.Adapters;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.IExtensionPoint;
 import org.eclipse.core.runtime.ILog;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
@@ -36,6 +38,7 @@
 import org.eclipse.core.resources.IFile;
 
 import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IPathEditorInput;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.internal.editors.text.NLSUtility;
 
@@ -312,8 +315,17 @@
 		IDocumentProvider provider= null;
 
 		IFile file= editorInput.getAdapter(IFile.class);
-		if (file != null)
+		if (file != null) {
 			provider= getDocumentProvider(file.getFileExtension());
+		} else {
+			IPathEditorInput pathInput= Adapters.adapt(editorInput, IPathEditorInput.class);
+			if (pathInput != null) {
+				IPath path= pathInput.getPath();
+				if (path != null) {
+					provider= getDocumentProvider(path.getFileExtension());
+				}
+			}
+		}
 
 		if (provider == null) {
 			Set<IConfigurationElement> set= findInputTypeMapping(editorInput.getClass());