From 1ba9040176a1bdaf3c47df85728ae4ab5b0d2a93 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Tue, 8 Aug 2017 16:33:41 +0200 Subject: Bug 520685 - [Generic Editor] Sort ext. by most specialized content-type Factorize extensions and registry polling (can be continued). This allows most registries to return first the extension that applies to the most specialized content-types. Change-Id: Icfd5d0c2e72cd579e9fe8c034420d88c628f39a6 Signed-off-by: Mickael Istria --- .../genericeditor/AutoEditStrategyRegistry.java | 49 ++++----------------- .../ContentAssistProcessorRegistry.java | 51 ++++------------------ .../ContentTypeSpecializationComparator.java | 39 +++++++++++++++++ .../GenericContentTypeRelatedExtension.java | 47 ++++++++++++++++++++ .../PresentationReconcilerRegistry.java | 45 ++++--------------- .../internal/genericeditor/ReconcilerRegistry.java | 46 ++++--------------- .../internal/genericeditor/TextHoverRegistry.java | 35 ++++----------- 7 files changed, 127 insertions(+), 185 deletions(-) create mode 100644 org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentTypeSpecializationComparator.java create mode 100644 org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java (limited to 'org.eclipse.ui.genericeditor') diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/AutoEditStrategyRegistry.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/AutoEditStrategyRegistry.java index 349f455d7ba..425415a953b 100644 --- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/AutoEditStrategyRegistry.java +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/AutoEditStrategyRegistry.java @@ -10,14 +10,13 @@ *******************************************************************************/ package org.eclipse.ui.internal.genericeditor; -import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; @@ -39,36 +38,9 @@ public class AutoEditStrategyRegistry { private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".autoEditStrategies"; //$NON-NLS-1$ - private Map extensions = new LinkedHashMap<>(); + private Map> extensions = new LinkedHashMap<>(); private boolean outOfSync = true; - static class AutoEditStrategyExtension { - private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$ - private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$ - - private IConfigurationElement extension; - private IContentType targetContentType; - - public AutoEditStrategyExtension(IConfigurationElement extension) throws Exception { - this.extension = extension; - this.targetContentType = Platform.getContentTypeManager() - .getContentType(extension.getAttribute(CONTENT_TYPE_ATTRIBUTE)); - } - - public IAutoEditStrategy createStrategy() { - try { - return (IAutoEditStrategy) extension.createExecutableExtension(CLASS_ATTRIBUTE); - } catch (CoreException e) { - GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e)); - return null; - } - } - - IConfigurationElement getConfigurationElement() { - return this.extension; - } - } - /** * Creates the registry and binds it to the extension point. */ @@ -96,16 +68,11 @@ public class AutoEditStrategyRegistry { if (this.outOfSync) { sync(); } - List res = new ArrayList<>(); - for (AutoEditStrategyExtension ext : this.extensions.values()) { - if (contentTypes.contains(ext.targetContentType)) { - IAutoEditStrategy strategy = ext.createStrategy(); - if (strategy != null) { - res.add(strategy); - } - } - } - return res; + return this.extensions.values().stream() + .filter(ext -> contentTypes.contains(ext.targetContentType)) + .sorted(new ContentTypeSpecializationComparator()) + .map(GenericContentTypeRelatedExtension::createDelegate) + .collect(Collectors.toList()); } private void sync() { @@ -115,7 +82,7 @@ public class AutoEditStrategyRegistry { toRemoveExtensions.remove(extension); if (!this.extensions.containsKey(extension)) { try { - this.extensions.put(extension, new AutoEditStrategyExtension(extension)); + this.extensions.put(extension, new GenericContentTypeRelatedExtension(extension)); } catch (Exception ex) { GenericEditorPlugin.getDefault().getLog() .log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex)); diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentAssistProcessorRegistry.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentAssistProcessorRegistry.java index 0da557bfef7..187de272595 100644 --- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentAssistProcessorRegistry.java +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentAssistProcessorRegistry.java @@ -10,15 +10,14 @@ *******************************************************************************/ 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.stream.Collectors; import org.eclipse.core.filebuffers.FileBuffers; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IRegistryChangeEvent; @@ -127,39 +126,7 @@ public class ContentAssistProcessorRegistry { } } - /** - * This class wraps and proxies an {@link IContentAssistProcessor} provided through extensions - * and loads it lazily when it can contribute to the editor, then delegates all operations to - * actual processor. - * When the contribution cannot contribute to the editor, this wrapper will return neutral values - * that don't affect editor behavior. - */ - private static class ContentAssistProcessorExtension { - private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$ - private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$ - - private IConfigurationElement extension; - private IContentType targetContentType; - - private ContentAssistProcessorExtension(IConfigurationElement element) throws Exception { - this.extension = element; - this.targetContentType = Platform.getContentTypeManager().getContentType(element.getAttribute(CONTENT_TYPE_ATTRIBUTE)); - } - - public ContentAssistProcessorDelegate createDelegate() { - try { - IContentAssistProcessor delegate = (IContentAssistProcessor) extension.createExecutableExtension(CLASS_ATTRIBUTE); - if (delegate != null) { - return new ContentAssistProcessorDelegate(delegate, targetContentType); - } - } catch (CoreException e) { - GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e)); - } - return null; - } - } - - private Map extensions = new HashMap<>(); + private Map> extensions = new HashMap<>(); private boolean outOfSync = true; /** @@ -185,13 +152,11 @@ public class ContentAssistProcessorRegistry { if (this.outOfSync) { sync(); } - List res = new ArrayList<>(); - for (ContentAssistProcessorExtension ext : this.extensions.values()) { - if (contentTypes.contains(ext.targetContentType)) { - res.add(ext.createDelegate()); - } - } - return res; + return this.extensions.values().stream() + .filter(ext -> contentTypes.contains(ext.targetContentType)) + .sorted(new ContentTypeSpecializationComparator()) + .map(GenericContentTypeRelatedExtension::createDelegate) + .collect(Collectors.toList()); } private void sync() { @@ -200,7 +165,7 @@ public class ContentAssistProcessorRegistry { toRemoveExtensions.remove(extension); if (!this.extensions.containsKey(extension)) { try { - this.extensions.put(extension, new ContentAssistProcessorExtension(extension)); + this.extensions.put(extension, new GenericContentTypeRelatedExtension(extension)); } catch (Exception ex) { GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex)); } diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentTypeSpecializationComparator.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentTypeSpecializationComparator.java new file mode 100644 index 00000000000..2a2d329434c --- /dev/null +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ContentTypeSpecializationComparator.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2017 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.Comparator; + +import org.eclipse.core.runtime.content.IContentType; + +/** + * Compares extension so that the ones with the most "specialized" content-types are returned first. + * + * @param + */ +public class ContentTypeSpecializationComparator implements Comparator> { + + @Override + public int compare(GenericContentTypeRelatedExtension o1, GenericContentTypeRelatedExtension o2) { + return depth(o2.targetContentType) - depth(o1.targetContentType); + } + + private static int depth(IContentType targetContentType) { + int res = 0; + IContentType current = targetContentType; + while (current != null) { + res++; + current = current.getBaseType(); + } + return res; + } + +} diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java new file mode 100644 index 00000000000..da7dc2be16c --- /dev/null +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2017 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 org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +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; + +/** + * This class wraps and proxies an instance of T provided through extensions + * and loads it lazily when it can contribute to the editor, then delegates all operations to + * actual instance. + * + * @param the actual type to proxy, typically the one defined on the extension point. + */ +public class GenericContentTypeRelatedExtension { + private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$ + private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$ + + public final IConfigurationElement extension; + public final IContentType targetContentType; + + public GenericContentTypeRelatedExtension(IConfigurationElement element) throws Exception { + this.extension = element; + this.targetContentType = Platform.getContentTypeManager().getContentType(element.getAttribute(CONTENT_TYPE_ATTRIBUTE)); + } + + public T createDelegate() { + try { + return (T) extension.createExecutableExtension(CLASS_ATTRIBUTE); + } catch (CoreException e) { + GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e)); + } + return null; + } +} diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/PresentationReconcilerRegistry.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/PresentationReconcilerRegistry.java index b1692332f9b..6826e4ead20 100644 --- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/PresentationReconcilerRegistry.java +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/PresentationReconcilerRegistry.java @@ -10,14 +10,13 @@ *******************************************************************************/ 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.stream.Collectors; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; @@ -38,33 +37,7 @@ public class PresentationReconcilerRegistry { private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".presentationReconcilers"; //$NON-NLS-1$ - /** - * This class wraps and proxies an {@link IPresentationReconcilier} provided through extensions - * and loads it lazily when it can contribute to the editor, then delegates all operations to - * actual reconcilier. - */ - private static class PresentationReconcilerExtension { - private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$ - private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$ - - private IConfigurationElement extension; - private IContentType targetContentType; - - private PresentationReconcilerExtension(IConfigurationElement element) throws Exception { - this.extension = element; - this.targetContentType = Platform.getContentTypeManager().getContentType(element.getAttribute(CONTENT_TYPE_ATTRIBUTE)); - } - - public IPresentationReconciler createDelegate() { - try { - return (IPresentationReconciler) extension.createExecutableExtension(CLASS_ATTRIBUTE); - } catch (CoreException e) { - GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e)); - } - return null; - } - } - private Map extensions = new HashMap<>(); + private Map> extensions = new HashMap<>(); private boolean outOfSync = true; /** @@ -90,13 +63,11 @@ public class PresentationReconcilerRegistry { if (this.outOfSync) { sync(); } - List res = new ArrayList<>(); - for (PresentationReconcilerExtension ext : this.extensions.values()) { - if (contentTypes.contains(ext.targetContentType)) { - res.add(ext.createDelegate()); - } - } - return res; + return this.extensions.values().stream() + .filter(ext -> contentTypes.contains(ext.targetContentType)) + .sorted(new ContentTypeSpecializationComparator()) + .map(GenericContentTypeRelatedExtension::createDelegate) + .collect(Collectors.toList()); } private void sync() { @@ -105,7 +76,7 @@ public class PresentationReconcilerRegistry { toRemoveExtensions.remove(extension); if (!this.extensions.containsKey(extension)) { try { - this.extensions.put(extension, new PresentationReconcilerExtension(extension)); + this.extensions.put(extension, new GenericContentTypeRelatedExtension(extension)); } catch (Exception ex) { GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex)); } diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ReconcilerRegistry.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ReconcilerRegistry.java index c942335a0dc..28d1f39f751 100644 --- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ReconcilerRegistry.java +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/ReconcilerRegistry.java @@ -10,14 +10,13 @@ *******************************************************************************/ 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.stream.Collectors; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; @@ -38,34 +37,7 @@ public class ReconcilerRegistry { private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".reconcilers"; //$NON-NLS-1$ - /** - * This class wraps and proxies an {@link IReconciler} provided through extensions - * and loads it lazily when it can contribute to the editor, then delegates all operations to - * actual reconcilier. - */ - private static class ReconcilerExtension { - private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$ - private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$ - - private IConfigurationElement extension; - private IContentType targetContentType; - - private ReconcilerExtension(IConfigurationElement element) throws Exception { - this.extension = element; - this.targetContentType = Platform.getContentTypeManager().getContentType(element.getAttribute(CONTENT_TYPE_ATTRIBUTE)); - } - - public IReconciler createDelegate() { - try { - return (IReconciler) extension.createExecutableExtension(CLASS_ATTRIBUTE); - } catch (CoreException e) { - GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e)); - } - return null; - } - - } - private Map extensions = new HashMap<>(); + private Map> extensions = new HashMap<>(); private boolean outOfSync = true; /** @@ -91,13 +63,11 @@ public class ReconcilerRegistry { if (this.outOfSync) { sync(); } - List res = new ArrayList<>(); - for (ReconcilerExtension ext : this.extensions.values()) { - if (contentTypes.contains(ext.targetContentType)) { - res.add(ext.createDelegate()); - } - } - return res; + return this.extensions.values().stream() + .filter(ext -> contentTypes.contains(ext.targetContentType)) + .sorted(new ContentTypeSpecializationComparator()) + .map(GenericContentTypeRelatedExtension::createDelegate) + .collect(Collectors.toList()); } private void sync() { @@ -106,7 +76,7 @@ public class ReconcilerRegistry { toRemoveExtensions.remove(extension); if (!this.extensions.containsKey(extension)) { try { - this.extensions.put(extension, new ReconcilerExtension(extension)); + this.extensions.put(extension, new GenericContentTypeRelatedExtension(extension)); } catch (Exception ex) { GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex)); } 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 0e572876762..374c066eccc 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016 Red Hat Inc. and others. + * Copyright (c) 2016-2017 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 @@ -10,7 +10,6 @@ *******************************************************************************/ package org.eclipse.ui.internal.genericeditor; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -21,7 +20,6 @@ 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; import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; @@ -47,36 +45,22 @@ public final class TextHoverRegistry { private SortedSet extensions; private boolean outOfSync = true; - static class TextHoverExtension { - private static final String CONTENT_TYPE_ATTRIBUTE = "contentType"; //$NON-NLS-1$ - private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$ + static class TextHoverExtension extends GenericContentTypeRelatedExtension { 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 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)); + super(extension); this.id = extension.getAttribute(ID_ATTRIBUTE); this.isBefore = extension.getAttribute(IS_BEFORE_ATTRIBUTE); this.isAfter = extension.getAttribute(IS_AFTER_ATTRIBUTE); } - public ITextHover createDelegate() { - try { - return (ITextHover) extension.createExecutableExtension(CLASS_ATTRIBUTE); - } catch (CoreException e) { - GenericEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e)); - } - return null; - } - public String getId() { if (this.id != null) { return this.id; @@ -110,14 +94,13 @@ public final class TextHoverRegistry { if (this.outOfSync) { sync(); } - List hoversToConsider = new ArrayList<>(); - for (TextHoverExtension ext : this.extensions) { - if (contentTypes.contains(ext.targetContentType)) { - hoversToConsider.add(ext); - } - } + List hoversToConsider = this.extensions.stream() + .filter(ext -> contentTypes.contains(ext.targetContentType)) + // don't sort in the stream as the initial structure is already sorted by isAfter/isBefore + .map(GenericContentTypeRelatedExtension::createDelegate) + .collect(Collectors.toList()); if (!hoversToConsider.isEmpty()) { - return new CompositeTextHover(hoversToConsider.stream().map(TextHoverExtension::createDelegate).collect(Collectors.toList())); + return new CompositeTextHover(hoversToConsider); } return null; } -- cgit v1.2.3