Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRubén Porras Campo2021-10-21 11:29:09 +0000
committerMickael Istria2021-10-21 11:48:37 +0000
commite3323e3a0771f559fabb8df5360286b0f9cdd726 (patch)
treef4e53eaf366a1b15d9e075060c76147a436a29a2
parent386cf8f19faf660f69ba35498f2bf0a5368014fa (diff)
downloadeclipse.platform.text-e3323e3a0771f559fabb8df5360286b0f9cdd726.tar.gz
eclipse.platform.text-e3323e3a0771f559fabb8df5360286b0f9cdd726.tar.xz
eclipse.platform.text-e3323e3a0771f559fabb8df5360286b0f9cdd726.zip
Support locations outside a local file system by calling buffer.getContentType(). For remote resources, the location of the buffer is null, thus the retrieval of its last segment throws a NPE. The code is improved to avoid the NPE. Signed-off-by: Rubén Porras Campo <pcr@avaloq.com> Change-Id: Id704542fe231f9517a47d9fdb6c076ed77bfa77d Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/186762 Tested-by: Platform Bot <platform-bot@eclipse.org> Reviewed-by: Mickael Istria <mistria@redhat.com>
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java57
1 files changed, 39 insertions, 18 deletions
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java
index 1d77bf787bb..0f1f2128350 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java
@@ -20,7 +20,6 @@ package org.eclipse.ui.internal.genericeditor;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@@ -30,8 +29,12 @@ import java.util.Set;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.DefaultInformationControl;
@@ -85,36 +88,54 @@ public final class ExtensionBasedTextViewerConfiguration extends TextSourceViewe
Set<IContentType> getContentTypes(ITextViewer viewer) {
if (this.contentTypes == null) {
this.contentTypes = new LinkedHashSet<>();
- String fileName = null;
- fileName = getCurrentFileName(viewer);
- if (fileName == null) {
- return Collections.emptySet();
+ ITextFileBuffer buffer = getCurrentBuffer(viewer);
+ if (buffer != null) {
+ try {
+ IContentType contentType = buffer.getContentType();
+ if (contentType != null) {
+ this.contentTypes.add(contentType);
+ }
+ } catch (CoreException ex) {
+ GenericEditorPlugin.getDefault().getLog()
+ .log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex));
+ }
}
- Queue<IContentType> types = new LinkedList<>(
- Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(fileName)));
- while (!types.isEmpty()) {
- IContentType type = types.poll();
- this.contentTypes.add(type);
- IContentType parent = type.getBaseType();
- if (parent != null) {
- types.add(parent);
+ String fileName = getCurrentFileName(viewer);
+ if (fileName != null) {
+ Queue<IContentType> types = new LinkedList<>(
+ Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(fileName)));
+ while (!types.isEmpty()) {
+ IContentType type = types.poll();
+ this.contentTypes.add(type);
+ IContentType parent = type.getBaseType();
+ if (parent != null) {
+ types.add(parent);
+ }
}
}
}
return this.contentTypes;
}
+ private static ITextFileBuffer getCurrentBuffer(ITextViewer viewer) {
+ IDocument viewerDocument = viewer.getDocument();
+ if (viewerDocument != null) {
+ return FileBuffers.getTextFileBufferManager().getTextFileBuffer(viewerDocument);
+ }
+ return null;
+ }
+
private String getCurrentFileName(ITextViewer viewer) {
String fileName = null;
if (this.editor != null) {
fileName = editor.getEditorInput().getName();
}
if (fileName == null) {
- IDocument viewerDocument = viewer.getDocument();
- if (viewerDocument != null) {
- ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(viewerDocument);
- if (buffer != null) {
- fileName = buffer.getLocation().lastSegment();
+ ITextFileBuffer buffer = getCurrentBuffer(viewer);
+ if (buffer != null) {
+ IPath path = buffer.getLocation();
+ if (path != null) {
+ fileName = path.lastSegment();
}
}
}

Back to the top