Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMickael Istria2016-10-10 17:50:17 +0000
committerMickael Istria2016-10-11 14:35:32 +0000
commitd929f218362bd560d9a6e43bf62820b06a1dcf79 (patch)
treea81618379fd6e5e957bed3887cff6314a805ed79 /org.eclipse.ui.genericeditor/src
parent659611f6d33c31e69fc91f4fb0ea5111bb2c74b7 (diff)
downloadeclipse.platform.text-d929f218362bd560d9a6e43bf62820b06a1dcf79.tar.gz
eclipse.platform.text-d929f218362bd560d9a6e43bf62820b06a1dcf79.tar.xz
eclipse.platform.text-d929f218362bd560d9a6e43bf62820b06a1dcf79.zip
Bug 496300 - Support contribution of multiple Hovers to Generic Editor
Multiple hovers are now supported. Contributors can control some sorting using the isBefore and isAfter extension attributes. Change-Id: I622fe6b5c541033ba6c3140e8ad7ceab759a57cb Signed-off-by: Mickael Istria <mistria@redhat.com>
Diffstat (limited to 'org.eclipse.ui.genericeditor/src')
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CompositeTextHover.java94
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ExtensionBasedTextViewerConfiguration.java10
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/OrderedExtensionComparator.java91
-rw-r--r--org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/TextHoverRegistry.java66
4 files changed, 248 insertions, 13 deletions
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CompositeTextHover.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CompositeTextHover.java
new file mode 100644
index 000000000..cefebe637
--- /dev/null
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/CompositeTextHover.java
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * - Mickael Istria (Red Hat Inc.)
+ *******************************************************************************/
+package org.eclipse.ui.internal.genericeditor;
+
+import java.util.List;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.jface.text.IInformationControlCreator;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextHover;
+import org.eclipse.jface.text.ITextHoverExtension;
+import org.eclipse.jface.text.ITextHoverExtension2;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.ui.internal.genericeditor.TextHoverRegistry.TextHoverExtension;
+
+/**
+ * A text hover that delegates its operations to children
+ * provided in constructor and returns the first interesting result.
+ *
+ * @since 1.0
+ */
+public class CompositeTextHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {
+
+ private List<TextHoverExtension> hoversToConsider;
+ private TextHoverExtension currentHover = null;
+
+ public CompositeTextHover(List<TextHoverExtension> hoversToConsider) {
+ Assert.isNotNull(hoversToConsider);
+ this.hoversToConsider = hoversToConsider;
+ }
+
+ @Override
+ public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
+ for (TextHoverExtension hover : this.hoversToConsider) {
+ ITextHover delegate = hover.getDelegate();
+ Object res = null;
+ if (delegate instanceof ITextHoverExtension2) {
+ res = ((ITextHoverExtension2)delegate).getHoverInfo2(textViewer, hoverRegion);
+ } else {
+ res = delegate.getHoverInfo(textViewer, hoverRegion);
+ }
+ if (res != null) {
+ currentHover = hover;
+ return res;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public IInformationControlCreator getHoverControlCreator() {
+ if (this.currentHover != null) {
+ ITextHover hover = this.currentHover.getDelegate();
+ if (hover instanceof ITextHoverExtension) {
+ return ((ITextHoverExtension)hover).getHoverControlCreator();
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
+ for (TextHoverExtension hover : this.hoversToConsider) {
+ ITextHover delegate = hover.getDelegate();
+ String res = delegate.getHoverInfo(textViewer, hoverRegion);
+ if (res != null) {
+ currentHover = hover;
+ return res;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
+ for (TextHoverExtension hover : this.hoversToConsider) {
+ ITextHover delegate = hover.getDelegate();
+ IRegion res = delegate.getHoverRegion(textViewer, offset);
+ if (res != null) {
+ return res;
+ }
+ }
+ return null;
+ }
+
+}
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 af4ecb553..10a89a3b3 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
@@ -11,7 +11,8 @@
package org.eclipse.ui.internal.genericeditor;
import java.util.Arrays;
-import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -56,7 +57,12 @@ public final class ExtensionBasedTextViewerConfiguration extends TextSourceViewe
private Set<IContentType> getContentTypes() {
if (this.contentTypes == null) {
- this.contentTypes = new HashSet<>(Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(editor.getEditorInput().getName())));
+ this.contentTypes = new LinkedHashSet<>();
+ this.contentTypes.addAll(Arrays.asList(Platform.getContentTypeManager().findContentTypesFor(editor.getEditorInput().getName())));
+ Iterator<IContentType> it = this.contentTypes.iterator();
+ while (it.hasNext()) {
+ this.contentTypes.add(it.next().getBaseType());
+ }
}
return this.contentTypes;
}
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/OrderedExtensionComparator.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/OrderedExtensionComparator.java
new file mode 100644
index 000000000..b390daa40
--- /dev/null
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/OrderedExtensionComparator.java
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Mickael Istria (Red Hat Inc.) - initial implementation
+ *******************************************************************************/
+package org.eclipse.ui.internal.genericeditor;
+
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.ui.internal.genericeditor.TextHoverRegistry.TextHoverExtension;
+
+/**
+ * A comparator that allows to sort elements according to their relative
+ * placement (isBefore and isAfter)
+ *
+ */
+class OrderedExtensionComparator implements Comparator<TextHoverExtension> {
+
+ private Map<String, TextHoverExtension> extensionsById;
+
+ public OrderedExtensionComparator(Collection<TextHoverExtension> extensions) {
+ Assert.isNotNull(extensions);
+ this.extensionsById = extensions.stream().collect(Collectors.toMap(TextHoverExtension::getId, Function.identity()));
+ }
+
+ @Override
+ public int compare(TextHoverExtension arg0, TextHoverExtension arg1) {
+ if (isDeclaredAsBefore(arg0, arg1) || isDeclaredAsAfter(arg1, arg0)) {
+ return -1;
+ }
+ if (isDeclaredAsAfter(arg0, arg1) || isDeclaredAsBefore(arg1, arg0)) {
+ return +1;
+ }
+ return arg0.toString().compareTo(arg1.toString());
+ }
+
+ private boolean isDeclaredAsBefore(TextHoverExtension arg0, TextHoverExtension arg1) {
+ String before0 = arg0.getIsBefore();
+ if (before0 == null) {
+ return false;
+ }
+ if ("*".equals(before0)) {
+ return true;
+ }
+ String id1 = arg1.getId();
+ if (id1 == null) {
+ return false;
+ }
+ if (before0.equals(id1)) {
+ return true;
+ }
+ String after1 = arg1.getIsAfter();
+ if (after1 == null) {
+ return false;
+ }
+ return isDeclaredAsAfter(arg0, this.extensionsById.get(after1));
+ }
+
+ private boolean isDeclaredAsAfter(TextHoverExtension arg0, TextHoverExtension arg1) {
+ String after0 = arg0.getIsAfter();
+ if (after0 == null) {
+ return false;
+ }
+ if ("*".equals(after0)) {
+ return true;
+ }
+ String id1 = arg1.getId();
+ if (id1 == null) {
+ return false;
+ }
+ if (after0.equals(id1)) {
+ return true;
+ }
+ String before1 = arg1.getIsBefore();
+ if (before1 == null) {
+ return false;
+ }
+ return isDeclaredAsAfter(arg0, this.extensionsById.get(before1));
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/TextHoverRegistry.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/TextHoverRegistry.java
index 88e4825f6..d6e83720e 100644
--- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/TextHoverRegistry.java
+++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/TextHoverRegistry.java
@@ -10,10 +10,16 @@
*******************************************************************************/
package org.eclipse.ui.internal.genericeditor;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.function.Function;
+import java.util.stream.Collectors;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
@@ -38,23 +44,32 @@ public final class TextHoverRegistry {
private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".hoverProviders"; //$NON-NLS-1$
- private Map<IConfigurationElement, TextHoverExtension> extensions = new HashMap<>();
+ private SortedSet<TextHoverExtension> extensions;
private boolean outOfSync = true;
- private static class TextHoverExtension {
+ static class TextHoverExtension {
private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$
private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
+ private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
+ private static final String IS_BEFORE_ATTRIBUTE = "isBefore"; //$NON-NLS-1$
+ private static final String IS_AFTER_ATTRIBUTE = "isAfter"; //$NON-NLS-1$
private IConfigurationElement extension;
private IContentType targetContentType;
private ITextHover delegate;
+ private String id;
+ private String isBefore;
+ private String isAfter;
public TextHoverExtension(IConfigurationElement extension) throws Exception {
this.extension = extension;
this.targetContentType = Platform.getContentTypeManager().getContentType(extension.getAttribute(CONTENT_TYPE_ATTRIBUTE));
+ this.id = extension.getAttribute(ID_ATTRIBUTE);
+ this.isBefore = extension.getAttribute(IS_BEFORE_ATTRIBUTE);
+ this.isAfter = extension.getAttribute(IS_AFTER_ATTRIBUTE);
}
- private ITextHover getDelegate() {
+ public ITextHover getDelegate() {
if (this.delegate == null) {
try {
this.delegate = (ITextHover) extension.createExecutableExtension(CLASS_ATTRIBUTE);
@@ -65,6 +80,24 @@ public final class TextHoverRegistry {
return delegate;
}
+ public String getId() {
+ if (this.id != null) {
+ return this.id;
+ }
+ return this.extension.getContributor().getName() + '@' + toString();
+ }
+
+ public String getIsAfter() {
+ return this.isAfter;
+ }
+
+ public String getIsBefore() {
+ return this.isBefore;
+ }
+
+ IConfigurationElement getConfigurationElement() {
+ return this.extension;
+ }
}
public TextHoverRegistry(IPreferenceStore preferenceStore) {
@@ -80,31 +113,42 @@ public final class TextHoverRegistry {
if (this.outOfSync) {
sync();
}
- // TODO rather that returning the 1st active hover, consider
- // supporting compound/aggregated hovers.
- for (TextHoverExtension ext : this.extensions.values()) {
+ List<TextHoverExtension> hoversToConsider = new ArrayList<>();
+ for (TextHoverExtension ext : this.extensions) {
if (contentTypes.contains(ext.targetContentType)) {
- return ext.getDelegate();
+ hoversToConsider.add(ext);
}
}
+ if (!hoversToConsider.isEmpty()) {
+ return new CompositeTextHover(hoversToConsider);
+ }
return null;
}
private void sync() {
- Set<IConfigurationElement> toRemoveExtensions = new HashSet<>(this.extensions.keySet());
+ Set<IConfigurationElement> toRemoveExtensions = new HashSet<>();
+ Map<IConfigurationElement, TextHoverExtension> ext = new HashMap<>();
+ if (this.extensions != null) {
+ ext = this.extensions.stream().collect(Collectors.toMap(TextHoverExtension::getConfigurationElement, Function.identity()));
+ toRemoveExtensions = ext.keySet();
+ }
for (IConfigurationElement extension : Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID)) {
toRemoveExtensions.remove(extension);
- if (!this.extensions.containsKey(extension)) {
+ if (!ext.containsKey(extension)) {
try {
- this.extensions.put(extension, new TextHoverExtension(extension));
+ ext.put(extension, new TextHoverExtension(extension));
} catch (Exception ex) {
GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex));
}
}
}
for (IConfigurationElement toRemove : toRemoveExtensions) {
- this.extensions.remove(toRemove);
+ ext.remove(toRemove);
}
+
+ OrderedExtensionComparator comparator = new OrderedExtensionComparator(ext.values());
+ this.extensions = new TreeSet<>(comparator);
+ this.extensions.addAll(ext.values());
this.outOfSync = false;
}

Back to the top