Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 675ddcc4990781de3c5611a37e0ad48b3ebca0ba (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 Alena Laskavaia 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:
 *     Alena Laskavaia  - initial API and implementation
 *     Alex Ruiz (Google)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.ui.cxx;

import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
import org.eclipse.cdt.codan.internal.core.CodanRunner;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.ui.ICEditor;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * @author Alena Laskavaia
 */
public class Startup implements IStartup {
	@Override
	public void earlyStartup() {
		registerListeners();
	}

	/**
	 * Register part listener for editor to install c ast reconcile listener
	 */
	private void registerListeners() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		workbench.getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				IWorkbenchWindow active = workbench.getActiveWorkbenchWindow();
				final IWorkbenchPage page = active.getActivePage();
				IPartListener2 partListener = new IPartListener2() {
					CodanCReconciler reconciler;

					@Override
					public void partActivated(IWorkbenchPartReference partRef) {
					}

					@Override
					public void partDeactivated(IWorkbenchPartReference partRef) {
					}

					@Override
					public void partOpened(IWorkbenchPartReference partRef) {
						IWorkbenchPart part = partRef.getPart(false);
						// We need to be very careful since this code may be executed in
						// an invironement where CDT is installed, but is not actively used.
						// By checking for ICEditor first we avoid loading CEditor class if
						// the part is not a C/C++ editor. Loading of CEditor class can be very
						// expensive since it triggers loading of many other classes.
						if (part instanceof ICEditor && part instanceof CEditor) {
							CEditor editor = (CEditor) part;
							if (reconciler == null) {
								reconciler = new CodanCReconciler();
							}
							reconciler.install(editor);
							IResource resource = (IResource) editor.getEditorInput().getAdapter(IResource.class);
							if (resource != null) {
								processResource(resource);
							}
						}
					}

					private void processResource(final IResource resource) {
						Job job = new Job(NLS.bind(Messages.Startup_AnalyzingFile, resource.getName())) {
							@Override
							protected IStatus run(IProgressMonitor monitor) {
								CodanRunner.processResource(resource, CheckerLaunchMode.RUN_ON_FILE_OPEN, monitor);
								return Status.OK_STATUS;
							}
						};
						job.setRule(resource);
						job.setSystem(true);
						job.schedule();
					}

					@Override
					public void partHidden(IWorkbenchPartReference partRef) {
					}

					@Override
					public void partVisible(IWorkbenchPartReference partRef) {
					}

					@Override
					public void partClosed(IWorkbenchPartReference partRef) {
						IWorkbenchPart part = partRef.getPart(false);
						if (reconciler != null && part instanceof ICEditor && part instanceof CEditor) {
							reconciler.uninstall((CEditor) part);
						}
					}

					@Override
					public void partBroughtToTop(IWorkbenchPartReference partRef) {
					}

					@Override
					public void partInputChanged(IWorkbenchPartReference partRef) {
					}
				};

				page.addPartListener(partListener);
				// Check current open editors.
				IEditorReference[] editorReferences = page.getEditorReferences();
				for (IEditorReference ref : editorReferences) {
					partListener.partOpened(ref);
				}
			}
		});
	}
}

Back to the top