Bug 576425 - Support locations outside a local file system

Support locations outside a local file system by calling
buffer.getContentType() if the location is null (always the case for
remote resources), otherwise a NPE is thrown.

Signed-off-by: Rubén Porras Campo <pcr@avaloq.com>
Change-Id: I2a89e0f35cbdff72b478ce20ecd419496457c45e
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
index bf4fa76..d99ff00 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
@@ -825,16 +825,29 @@
 
 	@NonNull
 	public static List<IContentType> getDocumentContentTypes(@NonNull IDocument document) {
-		String fileName = getFileName(document);
-		if (fileName == null) {
-			return Collections.emptyList();
-		}
 		List<IContentType> contentTypes = new ArrayList<>();
-		try (InputStream contents = new DocumentInputStream(document)) {
-			contentTypes
-					.addAll(Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(contents, fileName)));
-		} catch (IOException e) {
-			LanguageServerPlugin.logError(e);
+
+		ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
+		if (buffer != null) {
+			try {
+				// may be a more specific content-type, relying on some content-type factory and actual content (not just name)
+				IContentType contentType = buffer.getContentType();
+				if (contentType != null) {
+					contentTypes.add(contentType);
+				}
+			} catch (CoreException e) {
+				LanguageServerPlugin.logError("Exception occurred while fetching the content type from the buffer", e); //$NON-NLS-1$;
+			}
+		}
+
+		String fileName = getFileName(document);
+		if (fileName != null) {
+			try (InputStream contents = new DocumentInputStream(document)) {
+				contentTypes
+						.addAll(Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(contents, fileName)));
+			} catch (IOException e) {
+				LanguageServerPlugin.logError(e);
+			}
 		}
 		return contentTypes;
 	}
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java
index 7b8029d..287ab31 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java
@@ -28,8 +28,6 @@
 
 import org.eclipse.core.expressions.ExpressionConverter;
 import org.eclipse.core.expressions.IEvaluationContext;
-import org.eclipse.core.filebuffers.FileBuffers;
-import org.eclipse.core.filebuffers.ITextFileBuffer;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
@@ -403,16 +401,13 @@
 	}
 
 	public boolean canUseLanguageServer(@NonNull IDocument document) {
-		ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
-		if (buffer == null) {
+		List<IContentType> contentTypes = LSPEclipseUtils.getDocumentContentTypes(document);
+
+		if (contentTypes.isEmpty()) {
 			return false;
 		}
-		String name = buffer.getLocation().lastSegment();
-		if (name == null) {
-			return false;
-		}
-		return !getAvailableLSFor(
-				Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(name))).isEmpty();
+
+		return !getAvailableLSFor(contentTypes).isEmpty();
 	}
 
 	public boolean canUseLanguageServer(@NonNull IFile file) {