Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c024e1ecf840476fd34a9492af33c74e60f83c84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
 * Copyright (c) 2016-2017 Red Hat Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * - Mickael Istria (Red Hat Inc.)
 *******************************************************************************/
package org.eclipse.ui.internal.genericeditor.hover;

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.IConfigurationElement;
import org.eclipse.core.runtime.IRegistryChangeEvent;
import org.eclipse.core.runtime.IRegistryChangeListener;
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.ITextHover;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.internal.genericeditor.GenericContentTypeRelatedExtension;
import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Text hover registry that manages the detectors
 * contributed by the <code>org.eclipse.ui.workbench.texteditor.hoverProvider</code> extension point for
 * targets contributed by the <code>org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets</code> extension point.
 *
 * @since 1.0
 */
public final class TextHoverRegistry {

	private static final String EXTENSION_POINT_ID = GenericEditorPlugin.BUNDLE_ID + ".hoverProviders"; //$NON-NLS-1$

	private SortedSet<TextHoverExtension> extensions;
	private boolean outOfSync = true;

	static class TextHoverExtension extends GenericContentTypeRelatedExtension<ITextHover> {
		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 String id;
		private String isBefore;
		private String isAfter;

		public TextHoverExtension(IConfigurationElement extension) throws Exception {
			super(extension);
			this.id = extension.getAttribute(ID_ATTRIBUTE);
			this.isBefore = extension.getAttribute(IS_BEFORE_ATTRIBUTE);
			this.isAfter = extension.getAttribute(IS_AFTER_ATTRIBUTE);
		}

		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) {
		Platform.getExtensionRegistry().addRegistryChangeListener(new IRegistryChangeListener() {
			@Override
			public void registryChanged(IRegistryChangeEvent event) {
				outOfSync = true;
			}
		}, EXTENSION_POINT_ID);
	}

	public List<ITextHover> getAvailableHovers(ISourceViewer sourceViewer, ITextEditor editor, Set<IContentType> contentTypes) {
		if (this.outOfSync) {
			sync();
		}
		return this.extensions.stream()
				.filter(ext -> contentTypes.contains(ext.targetContentType))
				.filter(ext -> ext.matches(sourceViewer, editor))
				// don't sort in the stream as the initial structure is already sorted by isAfter/isBefore
				.map(GenericContentTypeRelatedExtension<ITextHover>::createDelegate)
				.collect(Collectors.toList());
	}

	private void sync() {
		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 (!ext.containsKey(extension)) {
				try {
					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) {
			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