Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 268639f0f3d4c8e3c2d3911afbaeeac1c0650e82 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Anton Leherbauer (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.eclipse.cdt.internal.ui.util.Messages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IContributor;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.dialogs.PreferencesUtil;

/**
 * A registry for all extensions to the
 * <code>org.eclipse.cdt.ui.completionProposalComputer</code>
 * extension point.
 *
 * @since 4.0
 */
public final class CompletionProposalComputerRegistry {

	private static final String EXTENSION_POINT = "completionProposalComputer"; //$NON-NLS-1$

	/** The singleton instance. */
	private static CompletionProposalComputerRegistry fgSingleton = null;

	/**
	 * Returns the default computer registry.
	 * <p>
	 * TODO keep this or add some other singleton, e.g. CUIPlugin?
	 * </p>
	 *
	 * @return the singleton instance
	 */
	public static synchronized CompletionProposalComputerRegistry getDefault() {
		if (fgSingleton == null) {
			fgSingleton = new CompletionProposalComputerRegistry();
		}

		return fgSingleton;
	}

	/**
	 * The sets of descriptors, grouped by partition type (key type:
	 * {@link String}, value type:
	 * {@linkplain List List&lt;CompletionProposalComputerDescriptor&gt;}).
	 */
	private final Map<String, List<CompletionProposalComputerDescriptor>> fDescriptorsByPartition = new HashMap<String, List<CompletionProposalComputerDescriptor>>();
	/**
	 * Unmodifiable versions of the sets stored in
	 * <code>fDescriptorsByPartition</code> (key type: {@link String},
	 * value type:
	 * {@linkplain List List&lt;CompletionProposalComputerDescriptor&gt;}).
	 */
	private final Map<String, List<CompletionProposalComputerDescriptor>> fPublicDescriptorsByPartition = new HashMap<String, List<CompletionProposalComputerDescriptor>>();
	/**
	 * All descriptors (element type:
	 * {@link CompletionProposalComputerDescriptor}).
	 */
	private final List<CompletionProposalComputerDescriptor> fDescriptors = new ArrayList<CompletionProposalComputerDescriptor>();
	/**
	 * Unmodifiable view of <code>fDescriptors</code>
	 */
	private final List<CompletionProposalComputerDescriptor> fPublicDescriptors = Collections
			.unmodifiableList(fDescriptors);

	private final List<CompletionProposalCategory> fCategories = new ArrayList<CompletionProposalCategory>();
	private final List<CompletionProposalCategory> fPublicCategories = Collections.unmodifiableList(fCategories);
	/**
	 * <code>true</code> if this registry has been loaded.
	 */
	private boolean fLoaded = false;

	/**
	 * Creates a new instance.
	 */
	public CompletionProposalComputerRegistry() {
	}

	/**
	 * Returns the list of {@link CompletionProposalComputerDescriptor}s describing all extensions
	 * to the <code>completionProposalComputer</code> extension point for the given partition
	 * type.
	 * <p>
	 * A valid partition is either one of the constants defined in
	 * {@link org.eclipse.cdt.ui.text.ICPartitions} or
	 * {@link org.eclipse.jface.text.IDocument#DEFAULT_CONTENT_TYPE}. An empty list is returned if
	 * there are no extensions for the given partition.
	 * </p>
	 * <p>
	 * The returned list is read-only and is sorted in the order that the extensions were read in.
	 * There are no duplicate elements in the returned list. The returned list may change if plug-ins
	 * are loaded or unloaded while the application is running or if an extension violates the API
	 * contract of {@link org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer}. When
	 * computing proposals, it is therefore imperative to copy the returned list before iterating
	 * over it.
	 * </p>
	 *
	 * @param partition
	 *        the partition type for which to retrieve the computer descriptors
	 * @return the list of extensions to the <code>completionProposalComputer</code> extension
	 *         point (element type: {@link CompletionProposalComputerDescriptor})
	 */
	List<CompletionProposalComputerDescriptor> getProposalComputerDescriptors(String partition) {
		ensureExtensionPointRead();
		List<CompletionProposalComputerDescriptor> result = fPublicDescriptorsByPartition.get(partition);
		if (result == null)
			return Collections.emptyList();
		return result;
	}

	/**
	 * Returns the list of {@link CompletionProposalComputerDescriptor}s describing all extensions
	 * to the <code>completionProposalComputer</code> extension point.
	 * <p>
	 * The returned list is read-only and is sorted in the order that the extensions were read in.
	 * There are no duplicate elements in the returned list. The returned list may change if plug-ins
	 * are loaded or unloaded while the application is running or if an extension violates the API
	 * contract of {@link org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer}. When
	 * computing proposals, it is therefore imperative to copy the returned list before iterating
	 * over it.
	 * </p>
	 *
	 * @return the list of extensions to the <code>completionProposalComputer</code> extension
	 *         point (element type: {@link CompletionProposalComputerDescriptor})
	 */
	List<CompletionProposalComputerDescriptor> getProposalComputerDescriptors() {
		ensureExtensionPointRead();
		return fPublicDescriptors;
	}

	/**
	 * Returns the list of proposal categories contributed to the
	 * <code>completionProposalComputer</code> extension point.
	 * <p>
	 * <p>
	 * The returned list is read-only and is sorted in the order that the extensions were read in.
	 * There are no duplicate elements in the returned list. The returned list may change if
	 * plug-ins are loaded or unloaded while the application is running.
	 * </p>
	 *
	 * @return list of proposal categories contributed to the
	 *         <code>completionProposalComputer</code> extension point (element type:
	 *         {@link CompletionProposalCategory})
	 */
	public List<CompletionProposalCategory> getProposalCategories() {
		ensureExtensionPointRead();
		return fPublicCategories;
	}

	/**
	 * Ensures that the extensions are read and stored in
	 * <code>fDescriptorsByPartition</code>.
	 */
	private void ensureExtensionPointRead() {
		boolean reload;
		synchronized (this) {
			reload = !fLoaded;
			fLoaded = true;
		}
		if (reload)
			reload();
	}

	/**
	 * Reloads the extensions to the extension point.
	 * <p>
	 * This method can be called more than once in order to reload from
	 * a changed extension registry.
	 * </p>
	 */
	public void reload() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		List<IConfigurationElement> elements = new ArrayList<IConfigurationElement>(
				Arrays.asList(registry.getConfigurationElementsFor(CUIPlugin.getPluginId(), EXTENSION_POINT)));

		Map<String, List<CompletionProposalComputerDescriptor>> map = new HashMap<String, List<CompletionProposalComputerDescriptor>>();
		List<CompletionProposalComputerDescriptor> all = new ArrayList<CompletionProposalComputerDescriptor>();

		List<CompletionProposalCategory> categories = getCategories(elements);
		for (IConfigurationElement element : elements) {
			try {
				CompletionProposalComputerDescriptor desc = new CompletionProposalComputerDescriptor(element, this,
						categories);
				Set<String> partitions = desc.getPartitions();
				for (Object element2 : partitions) {
					String partition = (String) element2;
					List<CompletionProposalComputerDescriptor> list = map.get(partition);
					if (list == null) {
						list = new ArrayList<CompletionProposalComputerDescriptor>();
						map.put(partition, list);
					}
					list.add(desc);
				}
				all.add(desc);

			} catch (CoreException x) {
				/*
				 * Element is not valid any longer as the contributing plug-in was unloaded or for
				 * some other reason. Do not include the extension in the list and inform the user
				 * about it.
				 */
				Object[] args = { element.toString() };
				String message = Messages
						.format(ContentAssistMessages.CompletionProposalComputerRegistry_invalid_message, args);
				IStatus status = new Status(IStatus.WARNING, CUIPlugin.getPluginId(), IStatus.OK, message, x);
				informUser(status);
			}
		}

		synchronized (this) {
			fCategories.clear();
			fCategories.addAll(categories);

			Set<String> partitions = map.keySet();
			fDescriptorsByPartition.keySet().retainAll(partitions);
			fPublicDescriptorsByPartition.keySet().retainAll(partitions);
			for (String partition : partitions) {
				List<CompletionProposalComputerDescriptor> old = fDescriptorsByPartition.get(partition);
				List<CompletionProposalComputerDescriptor> current = map.get(partition);
				if (old != null) {
					old.clear();
					old.addAll(current);
				} else {
					fDescriptorsByPartition.put(partition, current);
					fPublicDescriptorsByPartition.put(partition, Collections.unmodifiableList(current));
				}
			}

			fDescriptors.clear();
			fDescriptors.addAll(all);
		}
	}

	private List<CompletionProposalCategory> getCategories(List<IConfigurationElement> elements) {
		IPreferenceStore store = CUIPlugin.getDefault().getPreferenceStore();
		String preference = store.getString(PreferenceConstants.CODEASSIST_EXCLUDED_CATEGORIES);
		Set<String> disabled = new HashSet<String>();
		StringTokenizer tok = new StringTokenizer(preference, "\0"); //$NON-NLS-1$
		while (tok.hasMoreTokens())
			disabled.add(tok.nextToken());
		Map<String, Integer> ordered = new HashMap<String, Integer>();
		preference = store.getString(PreferenceConstants.CODEASSIST_CATEGORY_ORDER);
		tok = new StringTokenizer(preference, "\0"); //$NON-NLS-1$
		while (tok.hasMoreTokens()) {
			StringTokenizer inner = new StringTokenizer(tok.nextToken(), ":"); //$NON-NLS-1$
			String id = inner.nextToken();
			int rank = Integer.parseInt(inner.nextToken());
			ordered.put(id, Integer.valueOf(rank));
		}

		List<CompletionProposalCategory> categories = new ArrayList<CompletionProposalCategory>();
		for (Iterator<IConfigurationElement> iter = elements.iterator(); iter.hasNext();) {
			IConfigurationElement element = iter.next();
			try {
				if (element.getName().equals("proposalCategory")) { //$NON-NLS-1$
					iter.remove(); // remove from list to leave only computers

					CompletionProposalCategory category = new CompletionProposalCategory(element, this);
					categories.add(category);
					category.setIncluded(!disabled.contains(category.getId()));
					Integer rank = ordered.get(category.getId());
					if (rank != null) {
						int r = rank.intValue();
						boolean separate = r < 0xffff;
						category.setSeparateCommand(separate);
						category.setSortOrder(r);
					}
				}
			} catch (CoreException x) {
				/*
				 * Element is not valid any longer as the contributing plug-in was unloaded or for
				 * some other reason. Do not include the extension in the list and inform the user
				 * about it.
				 */
				Object[] args = { element.toString() };
				String message = Messages
						.format(ContentAssistMessages.CompletionProposalComputerRegistry_invalid_message, args);
				IStatus status = new Status(IStatus.WARNING, CUIPlugin.getPluginId(), IStatus.OK, message, x);
				informUser(status);
			}
		}
		return categories;
	}

	/**
	 * Log the status and inform the user about a misbehaving extension.
	 *
	 * @param descriptor the descriptor of the misbehaving extension
	 * @param status a status object that will be logged
	 */
	void informUser(CompletionProposalComputerDescriptor descriptor, IStatus status) {
		CUIPlugin.log(status);
		String title = ContentAssistMessages.CompletionProposalComputerRegistry_error_dialog_title;
		CompletionProposalCategory category = descriptor.getCategory();
		IContributor culprit = descriptor.getContributor();
		Set<String> affectedPlugins = getAffectedContributors(category, culprit);

		final String avoidHint;
		final String culpritName = culprit == null ? null : culprit.getName();
		if (affectedPlugins.isEmpty()) {
			if (CUIPlugin.PLUGIN_ID.equals(culpritName)) {
				// don't warn about internal computers
				return;
			}
			avoidHint = Messages.format(ContentAssistMessages.CompletionProposalComputerRegistry_messageAvoidanceHint,
					new Object[] { culpritName, category.getDisplayName() });
		} else {
			avoidHint = Messages.format(
					ContentAssistMessages.CompletionProposalComputerRegistry_messageAvoidanceHintWithWarning,
					new Object[] { culpritName, category.getDisplayName(), toString(affectedPlugins) });
		}
		String message = status.getMessage();
		// inlined from MessageDialog.openError
		MessageDialog dialog = new MessageDialog(CUIPlugin.getActiveWorkbenchShell(), title, null /* default image */,
				message, MessageDialog.ERROR, new String[] { IDialogConstants.OK_LABEL }, 0) {
			@Override
			protected Control createCustomArea(Composite parent) {
				Link link = new Link(parent, SWT.NONE);
				link.setText(avoidHint);
				link.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						PreferencesUtil
								.createPreferenceDialogOn(getShell(),
										"org.eclipse.cdt.ui.preferences.CodeAssistPreferenceAdvanced", null, null) //$NON-NLS-1$
								.open();
					}
				});
				GridData gridData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
				gridData.widthHint = this.getMinimumMessageWidth();
				link.setLayoutData(gridData);
				return link;
			}
		};
		dialog.open();
	}

	/**
	 * Returns the names of contributors affected by disabling a category.
	 *
	 * @param category the category that would be disabled
	 * @param culprit the culprit plug-in, which is not included in the returned list
	 * @return the names of the contributors other than <code>culprit</code> that contribute to <code>category</code> (element type: {@link String})
	 */
	private Set<String> getAffectedContributors(CompletionProposalCategory category, IContributor culprit) {
		Set<String> affectedPlugins = new HashSet<String>();
		for (CompletionProposalComputerDescriptor desc : getProposalComputerDescriptors()) {
			CompletionProposalCategory cat = desc.getCategory();
			if (cat.equals(category)) {
				IContributor contributor = desc.getContributor();
				if (contributor != null && !culprit.equals(contributor))
					affectedPlugins.add(contributor.getName());
			}
		}
		return affectedPlugins;
	}

	private Object toString(Collection<String> collection) {
		// strip brackets off AbstractCollection.toString()
		String string = collection.toString();
		return string.substring(1, string.length() - 1);
	}

	private void informUser(IStatus status) {
		CUIPlugin.log(status);
		String title = ContentAssistMessages.CompletionProposalComputerRegistry_error_dialog_title;
		String message = status.getMessage();
		MessageDialog.openError(CUIPlugin.getActiveWorkbenchShell(), title, message);
	}
}

Back to the top