Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fd6eb6efa9fd733e59e41cf53dab959e12bd7e17 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*******************************************************************************
 * Copyright (c) 2000, 2015 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *     Anton Leherbauer (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.ui;

import java.util.HashSet;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ElementChangedEvent;
import org.eclipse.cdt.core.model.IArchive;
import org.eclipse.cdt.core.model.IArchiveContainer;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.IBinaryContainer;
import org.eclipse.cdt.core.model.ICContainer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IElementChangedListener;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.model.ArchiveContainer;
import org.eclipse.cdt.internal.core.model.BinaryContainer;
import org.eclipse.cdt.internal.ui.BaseCElementContentProvider;
import org.eclipse.cdt.internal.ui.actions.SelectionConverter;
import org.eclipse.cdt.internal.ui.text.CWordFinder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.information.IInformationProvider;
import org.eclipse.jface.text.information.IInformationProviderExtension;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * A content provider for C elements.
 * <p>
 * The following C element hierarchy is surfaced by this content provider:
 * <p>
 * <pre>
C model (<code>ICModel</code>)<br>
   C project (<code>ICProject</code>)<br>
      Virtual binaries  container(<code>IBinaryContainery</code>)
      Virtual archives  container(<code>IArchiveContainery</code>)
      Source root (<code>ISourceRoot</code>)<br>
          C Container(folders) (<code>ICContainer</code>)<br>
          Translation unit (<code>ITranslationUnit</code>)<br>
          Binary file (<code>IBinary</code>)<br>
          Archive file (<code>IArchive</code>)<br>
      Non C Resource file (<code>Object</code>)<br>

 * </pre>
 */
public class CElementContentProvider extends BaseCElementContentProvider
		implements IElementChangedListener, IInformationProvider, IInformationProviderExtension {
	/** Editor. */
	protected ITextEditor fEditor;
	protected StructuredViewer fViewer;
	protected Object fInput;

	/** Remember what refreshes we already have pending so we don't post them again. */
	protected HashSet<IRefreshable> pendingRefreshes = new HashSet<IRefreshable>();

	/**
	 * Creates a new content provider for C elements.
	 */
	public CElementContentProvider() {
		// Empty.
	}

	/**
	 * Creates a new content provider for C elements.
	 * @param editor Editor.
	 */
	public CElementContentProvider(ITextEditor editor) {
		fEditor = editor;
	}

	/**
	 * Creates a new content provider for C elements.
	 */
	public CElementContentProvider(boolean provideMembers, boolean provideWorkingCopy) {
		super(provideMembers, provideWorkingCopy);
	}

	@Override
	public void dispose() {
		super.dispose();
		CoreModel.getDefault().removeElementChangedListener(this);
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		super.inputChanged(viewer, oldInput, newInput);

		fViewer = (StructuredViewer) viewer;

		if (oldInput == null && newInput != null) {
			CoreModel.getDefault().addElementChangedListener(this);
		} else if (oldInput != null && newInput == null) {
			CoreModel.getDefault().removeElementChangedListener(this);
		}
		fInput = newInput;
	}

	@Override
	public void elementChanged(final ElementChangedEvent event) {
		try {
			processDelta(event.getDelta());
		} catch (CModelException e) {
			CUIPlugin.log(e);
			e.printStackTrace();
		}
	}

	protected boolean isPathEntryChange(ICElementDelta delta) {
		int flags = delta.getFlags();
		return (delta.getKind() == ICElementDelta.CHANGED && ((flags & ICElementDelta.F_BINARY_PARSER_CHANGED) != 0
				|| (flags & ICElementDelta.F_ADDED_PATHENTRY_LIBRARY) != 0
				|| (flags & ICElementDelta.F_ADDED_PATHENTRY_SOURCE) != 0
				|| (flags & ICElementDelta.F_REMOVED_PATHENTRY_LIBRARY) != 0
				|| (flags & ICElementDelta.F_PATHENTRY_REORDER) != 0
				|| (flags & ICElementDelta.F_REMOVED_PATHENTRY_SOURCE) != 0
				|| (flags & ICElementDelta.F_CHANGED_PATHENTRY_INCLUDE) != 0));
	}

	/**
	 * Processes a delta recursively. When more than two children are affected the
	 * tree is fully refreshed starting at this node. The delta is processed in the
	 * current thread but the viewer updates are posted to the UI thread.
	 */
	protected void processDelta(ICElementDelta delta) throws CModelException {
		int kind = delta.getKind();
		int flags = delta.getFlags();
		ICElement element = delta.getElement();

		//System.out.println("Processing " + element);

		// handle open and closing of a project
		if (((flags & ICElementDelta.F_CLOSED) != 0) || ((flags & ICElementDelta.F_OPENED) != 0)) {
			postRefresh(element);
			return;
		}

		// We do not care about changes in Working copies
		// well, we do see bug 147694
		if (element instanceof ITranslationUnit) {
			ITranslationUnit unit = (ITranslationUnit) element;
			if (unit.isWorkingCopy()) {
				if (!getProvideWorkingCopy() || kind == ICElementDelta.REMOVED || kind == ICElementDelta.ADDED) {
					return;
				}
			}
			if (!getProvideMembers() && kind == ICElementDelta.CHANGED) {
				return;
			}
		}

		if (kind == ICElementDelta.REMOVED) {
			postRemove(element);
			updateContainer(element);
			return;
		}

		if (kind == ICElementDelta.ADDED) {
			Object parent = internalGetParent(element);
			postAdd(parent, element);
			updateContainer(element);
		}

		if (isPathEntryChange(delta)) {
			postRefresh(element.getCProject());
			return;
		}

		if (kind == ICElementDelta.CHANGED) {
			// Binary/Archive changes is done differently since they
			// are at two places, they are in the {Binary,Archive}Container
			// and in the Tree hierarchy
			if (updateContainer(element)) {
				Object parent = getParent(element);
				postRefresh(parent);
				return;
			} else if (element instanceof ITranslationUnit) {
				postRefresh(element);
				return;
			} else if (element instanceof ICContainer) {
				// if element itself has changed, not its children
				if ((flags & ~(ICElementDelta.F_CHILDREN | ICElementDelta.F_FINE_GRAINED)) != 0) {
					postRefresh(element);
				}
			} else if (element instanceof ArchiveContainer || element instanceof BinaryContainer) {
				postContainerRefresh((IParent) element, element.getCProject());
			}

		}

		if (processResourceDeltas(delta.getResourceDeltas(), element))
			return;

		ICElementDelta[] affectedChildren = delta.getAffectedChildren();
		for (ICElementDelta element2 : affectedChildren) {
			processDelta(element2);
		}
	}

	/**
	 * Processes resource deltas.
	 *
	 * @return true if the parent got refreshed
	 */
	private boolean processResourceDeltas(IResourceDelta[] deltas, Object parent) {
		if (deltas == null)
			return false;

		if (deltas.length > 1 && !(parent instanceof ICModel)) {
			// more than one child changed, refresh from here downwards
			// but not if the parent is ICModel
			// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=202085
			postRefresh(parent);
			return true;
		}

		for (IResourceDelta delta : deltas) {
			if (processResourceDelta(delta, parent))
				return true;
		}

		return false;
	}

	/**
	 * Processes a resource delta.
	 *
	 * @return true if the parent got refreshed
	 */
	private boolean processResourceDelta(IResourceDelta delta, Object parent) {
		int status = delta.getKind();
		IResource resource = delta.getResource();
		// filter out changes affecting the output folder
		if (resource == null) {
			return false;
		}

		// this could be optimized by handling all the added children in the parent
		if ((status & IResourceDelta.REMOVED) != 0) {
			postRemove(resource);
		}
		if ((status & IResourceDelta.ADDED) != 0) {
			postAdd(parent, resource);
		}

		int flags = delta.getFlags();
		// open/close state change of a project
		if ((flags & IResourceDelta.OPEN) != 0) {
			postProjectStateChanged(parent);
			return true;
		}

		processResourceDeltas(delta.getAffectedChildren(), resource);
		return false;
	}

	private boolean updateContainer(ICElement cfile) throws CModelException {
		IParent container = null;
		ICProject cproject = null;
		if (cfile instanceof IBinary) {
			IBinary bin = (IBinary) cfile;
			if (bin.showInBinaryContainer()) {
				cproject = bin.getCProject();
				container = cproject.getBinaryContainer();
			}
		} else if (cfile instanceof IArchive) {
			cproject = cfile.getCProject();
			container = cproject.getArchiveContainer();
		}
		if (container != null) {
			postContainerRefresh(container, cproject);
			return true;
		}
		return false;
	}

	// Tree refresh system
	// We keep track of what we're going to refresh and avoid posting multiple refresh
	// messages for the same elements. This avoids major performance issues where
	// we update tree views hundreds or thousands of times.
	protected interface IRefreshable {
		public void refresh();
	}

	protected final class RefreshContainer implements IRefreshable {
		private IParent container;
		private Object project;

		public RefreshContainer(IParent container, Object project) {
			this.container = container;
			this.project = project;
		}

		@Override
		public void refresh() {
			if (container instanceof IBinaryContainer || container instanceof IArchiveContainer) {
				// Always refresh the project to properly show/hide container
				fViewer.refresh(project);
			} else if (container.hasChildren()) {
				if (fViewer.testFindItem(container) != null) {
					fViewer.refresh(container);
				} else {
					fViewer.refresh(project);
				}
			} else {
				fViewer.refresh(project);
			}
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof RefreshContainer) {
				RefreshContainer c = (RefreshContainer) o;
				return c.container.equals(container) && c.project.equals(project);
			}
			return false;
		}

		@Override
		public int hashCode() {
			return container.hashCode() * 10903143 + 31181;
		}
	}

	protected final class RefreshElement implements IRefreshable {
		private Object element;

		public RefreshElement(Object element) {
			this.element = element;
		}

		@Override
		public void refresh() {
			if (element instanceof IWorkingCopy) {
				if (fViewer.testFindItem(element) != null) {
					fViewer.refresh(element);
				} else {
					fViewer.refresh(((IWorkingCopy) element).getOriginalElement());
				}
			} else if (element instanceof IBinary) {
				fViewer.refresh(element, true);
			} else {
				fViewer.refresh(element);
			}
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof RefreshElement) {
				RefreshElement c = (RefreshElement) o;
				return c.element.equals(element);
			}
			return false;
		}

		@Override
		public int hashCode() {
			return element.hashCode() * 7 + 490487;
		}
	}

	protected final class RefreshProjectState implements IRefreshable {
		private Object element;

		public RefreshProjectState(Object element) {
			this.element = element;
		}

		@Override
		public void refresh() {
			fViewer.refresh(element, true);
			// trigger a syntetic selection change so that action refresh their
			// enable state.
			fViewer.setSelection(fViewer.getSelection());
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof RefreshElement) {
				RefreshElement c = (RefreshElement) o;
				return c.element.equals(element);
			}
			return false;
		}

		@Override
		public int hashCode() {
			return element.hashCode() * 11 + 490487;
		}
	}

	protected void postContainerRefresh(final IParent container, final ICProject cproject) {
		//System.out.println("UI Container:" + cproject + " " + container);
		postRefreshable(new RefreshContainer(container, cproject));
	}

	protected void postRefresh(final Object element) {
		//System.out.println("UI refresh:" + element);
		postRefreshable(new RefreshElement(element));
	}

	protected void postAdd(final Object parent, final Object element) {
		//System.out.println("UI add:" + parent + " " + element);
		postRefreshable(new RefreshElement(parent));
	}

	protected void postRemove(final Object element) {
		//System.out.println("UI remove:" + element);
		postRefreshable(new RefreshElement(internalGetParent(element)));
	}

	protected void postProjectStateChanged(final Object root) {
		postRefreshable(new RefreshProjectState(root));
	}

	protected final void postRefreshable(final IRefreshable r) {
		Control ctrl = fViewer.getControl();
		if (ctrl != null && !ctrl.isDisposed()) {
			if (pendingRefreshes.contains(r))
				return;
			pendingRefreshes.add(r);
			ctrl.getDisplay().asyncExec(new Runnable() {
				@Override
				public void run() {
					pendingRefreshes.remove(r);
					Control ctrl = fViewer.getControl();
					if (ctrl != null && !ctrl.isDisposed()) {
						r.refresh();
					}
				}
			});
		}
	}

	@Override
	public IRegion getSubject(ITextViewer textViewer, int offset) {
		if (textViewer != null && fEditor != null) {
			IRegion region = CWordFinder.findWord(textViewer.getDocument(), offset);
			if (region != null) {
				return region;
			}
			return new Region(offset, 0);
		}
		return null;
	}

	@Override
	public String getInformation(ITextViewer textViewer, IRegion subject) {
		// deprecated API - not used anymore
		Object info = getInformation2(textViewer, subject);
		if (info != null) {
			return info.toString();
		}
		return null;
	}

	@Override
	public Object getInformation2(ITextViewer textViewer, IRegion subject) {
		if (fEditor == null)
			return null;
		try {
			ICElement element = SelectionConverter.getElementAtOffset(fEditor);
			if (element != null) {
				return element;
			}
			return SelectionConverter.getInput(fEditor);
		} catch (CModelException e) {
			return null;
		}
	}
}

Back to the top