Skip to content

Commit

Permalink
Use optionally computed content type from CompareConfiguration
Browse files Browse the repository at this point in the history
Detected content type id could be retrieved now from the
CompareConfiguration with the CONTENT_TYPE key (see
eclipse-platform/eclipse.platform#1294).

Use this value (if available) as fallbackContentTypes in
ExtensionBasedTextViewerConfiguration.

The allows GenericEditorMergeViewer to know for which content type
was it actually created and so use that in cases where the content type
can't be derived from the input (like data from git revisions
that don't have regular files or buffers associated to the viewer).

Fixes eclipse-platform#1747
  • Loading branch information
iloveeclipse committed Apr 9, 2024
1 parent 41e1e3f commit d1fbc3c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bundles/org.eclipse.ui.genericeditor/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Require-Bundle: org.eclipse.ui.workbench.texteditor;bundle-version="3.10.0",
org.eclipse.ui.ide;bundle-version="3.12.0",
org.eclipse.core.resources;bundle-version="3.11.0",
org.eclipse.core.expressions;bundle-version="3.6.0",
org.eclipse.compare;resolution:=optional
org.eclipse.compare;bundle-version="3.11.0";resolution:=optional
Export-Package: org.eclipse.ui.internal.genericeditor;x-internal:=true,
org.eclipse.ui.internal.genericeditor.hover;x-internal:=true,
org.eclipse.ui.internal.genericeditor.markers;x-internal:=true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*******************************************************************************/
package org.eclipse.ui.internal.genericeditor;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -138,7 +141,27 @@ public Set<IContentType> getContentTypes(IDocument documentParam) {
}
}
}
return this.resolvedContentTypes.isEmpty() ? fallbackContentTypes : resolvedContentTypes;
// Try to guess content type based on the content itself, if we don't
// have neither a resource, nor file name and no fallback contentTypes
if (documentParam != null && this.resolvedContentTypes.isEmpty() && this.fallbackContentTypes.isEmpty()) {
String content = documentParam.get();
if (content != null && !content.isBlank()) {
try (InputStream is = new ByteArrayInputStream(content.getBytes())) {
IContentType type = Platform.getContentTypeManager().findContentTypeFor(is, null);
while (type != null) {
this.resolvedContentTypes.add(type);
type = type.getBaseType();
}
} catch (IOException e) {
// silently ignore
}
}
}

if (this.resolvedContentTypes.isEmpty()) {
this.resolvedContentTypes.addAll(this.fallbackContentTypes);
}
return this.resolvedContentTypes;
}

private static ITextFileBuffer getCurrentBuffer(IDocument document) {
Expand Down Expand Up @@ -317,12 +340,17 @@ public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer)
editor, getContentTypes(sourceViewer.getDocument()));

InformationPresenter presenter = new InformationPresenter(new CompositeInformationControlCreator(hovers));
// By default the InformationPresented is set to take the focus when visible,
// which makes the Browser to overtake all the focus/mouse etc. control over the
// By default the InformationPresented is set to take the focus when
// visible,
// which makes the Browser to overtake all the focus/mouse etc. control
// over the
// 'org.eclipse.jface.text.information.InformationPresenter.Closer`.
// As we want to make t possible to close the information presenter by clicking
// outside of the information control or resizing the editor etc. - we need to
// disable such focus overtake by calling `takesFocusWhenVisible(false)` on the
// As we want to make t possible to close the information presenter by
// clicking
// outside of the information control or resizing the editor etc. - we
// need to
// disable such focus overtake by calling `takesFocusWhenVisible(false)`
// on the
// presenter.
//
presenter.takesFocusWhenVisible(false);
Expand Down Expand Up @@ -414,8 +442,8 @@ protected IInformationControl doCreateInformationControl(Shell parent) {
}

/**
* Set content-types that will be considered is no content-type can be deduced
* from the document (eg document is not backed by a FileBuffer)
* Set content-types that will be considered is no content-type can be
* deduced from the document (eg document is not backed by a FileBuffer)
*/
public void setFallbackContentTypes(Set<IContentType> contentTypes) {
this.fallbackContentTypes = (contentTypes == null ? Set.of() : contentTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
Expand All @@ -40,6 +41,16 @@ public class GenericEditorMergeViewer extends TextMergeViewer {

public GenericEditorMergeViewer(Composite parent, CompareConfiguration configuration) {
super(parent, configuration);
if (configuration != null) {
Object id = configuration.getProperty(CompareConfiguration.CONTENT_TYPE);
if (id instanceof String contentTypeId) {
IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
while (contentType != null) {
fallbackContentTypes.add(contentType);
contentType = contentType.getBaseType();
}
}
}
}

@Override
Expand All @@ -48,8 +59,10 @@ protected SourceViewer createSourceViewer(Composite parent, int textOrientation)
res.addTextInputListener(new ITextInputListener() {
@Override
public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
fallbackContentTypes
.addAll(new ExtensionBasedTextViewerConfiguration(null, null).getContentTypes(newInput));
if (fallbackContentTypes.isEmpty()) {
fallbackContentTypes
.addAll(new ExtensionBasedTextViewerConfiguration(null, null).getContentTypes(newInput));
}
configureTextViewer(res);
}

Expand Down

0 comments on commit d1fbc3c

Please sign in to comment.