Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26cba42800f2cd608a6127ffa39eac1efe058b1c (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Alena Laskavaia 
 * 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
 *    Tomasz Wesolowski - extension
 *******************************************************************************/
package org.eclipse.cdt.codan.ui;

import org.eclipse.cdt.codan.internal.core.model.CodanProblemMarker;
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CDTUITools;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Generic class for codan marker resolution (for quick fix). Use as a base
 * class for codanMarkerResolution extension. To add specific icon and
 * description client class should additionally implement
 * {@link IMarkerResolution2}
 * 
 * @since 1.1
 */
public abstract class AbstractCodanCMarkerResolution implements ICodanMarkerResolution {
	private boolean codanProblem;

	/**
	 * Get position offset from marker. If CHAR_START attribute is not set for
	 * marker, line and document would be used.
	 * 
	 * @param marker
	 * @param doc
	 * @return
	 */
	public int getOffset(IMarker marker, IDocument doc) {
		int charStart = marker.getAttribute(IMarker.CHAR_START, -1);
		int position;
		if (charStart > 0) {
			position = charStart;
		} else {
			int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1;
			try {
				position = doc.getLineOffset(line);
			} catch (BadLocationException e) {
				return -1;
			}
		}
		return position;
	}

	public boolean isCodanProblem() {
		return codanProblem;
	}

	public String getProblemArgument(IMarker marker, int index) {
		return CodanProblemMarker.getProblemArgument(marker, index);
	}

	/**
	 * Runs this resolution.
	 * 
	 * @param marker
	 *        the marker to resolve
	 */
	public void run(IMarker marker) {
		IDocument doc = openDocument(marker);
		if (doc != null) {
			codanProblem = getProblemId(marker) != null;
			apply(marker, doc);
		}
	}

	/**
	 * Apply marker resolution for given marker in given open document.
	 * 
	 * @param marker
	 * @param document
	 */
	public abstract void apply(IMarker marker, IDocument document);

	/**
	 * Override is extra checks is required to determine appicablity of marker
	 * resolution
	 * 
	 * @param marker
	 * @return
	 */
	public boolean isApplicable(IMarker marker) {
		return true;
	}

	/**
	 * Opens an editor with the document corresponding to the given problem and
	 * returns the corresponding IEditorPart. Please note that is code analysis
	 * is setup to run on reconsile this action would trigger checkers run, and
	 * original marker may be removed as a result.
	 * 
	 * @param marker
	 *        the problem marker
	 * @return the opened document
	 */
	protected IEditorPart openEditor(IMarker marker) {
		IEditorPart editorPart;
		try {
			editorPart = CodanEditorUtility.openInEditor(marker);
		} catch (PartInitException e) {
			CodanUIActivator.log(e);
			return null;
		}
		return editorPart;
	}

	/**
	 * Opens the editor and returns the document corresponding to a given
	 * marker.
	 * 
	 * @param marker
	 *        the marker to find the editor
	 * @return the corresponding document
	 */
	protected IDocument openDocument(IMarker marker) {
		return openDocument(openEditor(marker));
	}

	/**
	 * Returns the document corresponding to a given editor part.
	 * 
	 * @param editorPart
	 *        an editor part
	 * @return the document of that part
	 */
	protected IDocument openDocument(IEditorPart editorPart) {
		if (editorPart instanceof ITextEditor) {
			ITextEditor editor = (ITextEditor) editorPart;
			IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
			return doc;
		}
		return null;
	}

	/**
	 * Receives a translation unit from a given marker. Opens the editor.
	 * 
	 * @param marker
	 *        A marker in an editor to get the translation unit
	 * @return The translation unit
	 */
	protected ITranslationUnit getTranslationUnitViaEditor(IMarker marker) {
		ITranslationUnit tu = (ITranslationUnit) CDTUITools.getEditorInputCElement(openEditor(marker).getEditorInput());
		return tu;
	}

	/**
	 * Receives a translation unit from a given marker using the marker's path.
	 * 
	 * @param marker
	 *        A marker in a translation unit
	 * @return The translation unit
	 */
	protected ITranslationUnit getTranslationUnitViaWorkspace(IMarker marker) {
		IPath path = marker.getResource().getFullPath();
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(file);
		return tu;
	}

	/**
	 * Receives an ASTName enclosing a given IMarker
	 * 
	 * @param marker
	 *        The marker enclosing an ASTName
	 * @param ast
	 *        The AST to check
	 * @return The enclosing ASTName or null
	 */
	protected IASTName getASTNameFromMarker(IMarker marker, IASTTranslationUnit ast) {
		final int charStart = marker.getAttribute(IMarker.CHAR_START, -1);
		final int length = marker.getAttribute(IMarker.CHAR_END, -1) - charStart;
		return getASTNameFromPositions(ast, charStart, length);
	}

	/**
	 * @param ast
	 * @param charStart
	 * @param length
	 * @return
	 */
	protected IASTName getASTNameFromPositions(IASTTranslationUnit ast, final int charStart, final int length) {
		IASTName name = ast.getNodeSelector(null).findEnclosingName(charStart, length);
		return name;
	}

	/**
	 * Receives an {@link IIndex} corresponding to the given {@link IMarker}'s
	 * resource.
	 * 
	 * @param marker
	 *        the marker to use
	 * @return the received index
	 * @throws CoreException
	 */
	protected IIndex getIndexFromMarker(final IMarker marker) throws CoreException {
		IProject project = marker.getResource().getProject();
		ICProject cProject = CoreModel.getDefault().create(project);
		IIndex index = CCorePlugin.getIndexManager().getIndex(cProject);
		return index;
	}

	/**
	 * @param marker
	 * @return
	 */
	public String getProblemId(IMarker marker) {
		return CodanProblemMarker.getProblemId(marker);
	}

	/**
	 * @param marker
	 * @return
	 */
	public String getProblemMessage(IMarker marker) {
		return CodanProblemMarker.getMessage(marker);
	}
}

Back to the top