Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0a311599c14998cfa71efb9a632d66391bc6f02c (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
/**
 *  Copyright (c) 2017 Angelo ZERR.
 *
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide extension point for CodeMining - Bug 528419
 */
package org.eclipse.ui.internal.texteditor.codemining;

import java.util.ArrayList;
import java.util.List;

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.jface.text.codemining.ICodeMiningProvider;
import org.eclipse.jface.text.source.ISourceViewer;

import org.eclipse.ui.internal.texteditor.TextEditorPlugin;

import org.eclipse.ui.texteditor.ITextEditor;

/**
 * A codemining providers registry used to access the
 * {@link CodeMiningProviderDescriptor}s that describe the codemining provider
 * extensions.
 *
 * @see CodeMiningProviderDescriptor
 * @since 3.10
 */
public class CodeMiningProviderRegistry {

	/**
	 * Extension id of spelling engine extension point. (value
	 * <code>"codeMiningProviders"</code>).
	 */
	public static final String CODEMINING_PROVIDERS_EXTENSION_POINT = "codeMiningProviders"; //$NON-NLS-1$

	/** All descriptors */
	private CodeMiningProviderDescriptor[] fDescriptors;

	/** <code>true</code> iff the extensions have been loaded at least once */
	private boolean fLoaded = false;

	/**
	 * Returns all descriptors.
	 *
	 * @return all descriptors
	 */
	private CodeMiningProviderDescriptor[] getDescriptors() {
		ensureExtensionsLoaded();
		return fDescriptors;
	}

	/**
	 * Reads all extensions.
	 * <p>
	 * This method can be called more than once in order to reload from a changed
	 * extension registry.
	 * </p>
	 */
	public synchronized void reloadExtensions() {
		List<CodeMiningProviderDescriptor> descriptors = new ArrayList<>();
		IConfigurationElement[] elements = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(TextEditorPlugin.PLUGIN_ID, CODEMINING_PROVIDERS_EXTENSION_POINT);
		for (IConfigurationElement element : elements) {
			try {
				CodeMiningProviderDescriptor descriptor = new CodeMiningProviderDescriptor(element);
				descriptors.add(descriptor);
			} catch (CoreException e) {
				TextEditorPlugin.getDefault().getLog()
						.log(new Status(IStatus.ERROR, element.getNamespaceIdentifier(), e.getMessage()));
			}
		}
		fDescriptors = descriptors.toArray(new CodeMiningProviderDescriptor[descriptors.size()]);
		fLoaded = true;
	}

	/**
	 * Ensures the extensions have been loaded at least once.
	 */
	private void ensureExtensionsLoaded() {
		if (!fLoaded)
			reloadExtensions();
	}

	/**
	 * Returns the codemining providers for the given viewer and editor and null
	 * otherwise.
	 *
	 * @param viewer
	 *            the viewer
	 * @param editor
	 *            the editor
	 * @return the codemining providers for the given viewer and editor and null
	 *         otherwise.
	 */
	public ICodeMiningProvider[] getProviders(ISourceViewer viewer, ITextEditor editor) {
		List<ICodeMiningProvider> providers = new ArrayList<>();
		for (CodeMiningProviderDescriptor descriptor : getDescriptors()) {
			if (descriptor.matches(viewer, editor)) {
				ICodeMiningProvider provider = descriptor.createCodeMiningProvider(editor);
				if (provider != null) {
					providers.add(provider);
				}
			}
		}
		return !providers.isEmpty() ? providers.toArray(new ICodeMiningProvider[providers.size()]) : null;
	}
}

Back to the top