Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4dff7db1c36cd5b80128b0628c37d12b2ae6ed82 (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
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc.
 *
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;

import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;

public class QuickFixAddSemicolon extends AbstractAstRewriteQuickFix {

	@Override
	public String getLabel() {
		return QuickFixMessages.QuickFixAddSemicolon_add_semicolon;
	}

	@Override
	public void modifyAST(IIndex index, IMarker marker) {
		IASTTranslationUnit ast;
		try {
			ITranslationUnit tu = getTranslationUnitViaEditor(marker);
			ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
		} catch (CoreException e) {
			CheckersUiActivator.log(e);
			return;
		}
		IASTNode astNode = null;
		if (isCodanProblem(marker))
			return;

		// We need to back up in the file

		// Start by finding the original reported position and line number
		int lineNum = marker.getAttribute(IMarker.LINE_NUMBER, 0) - 1;

		if (lineNum < 1) {
			return;
		}

		IDocument document = getDocument();

		int lineOffset;
		int lineLength;
		try {
			lineOffset = document.getLineOffset(lineNum);
			lineLength = document.getLineLength(lineNum);
		} catch (BadLocationException e2) {
			return;
		}

		// find the position of the reported token
		int pos = getOffset(marker, getDocument());
		String name = null;
		try {
			name = getProblemArgument(marker, 0);
		} catch (Exception e) {
			return;
		}
		if (name == null)
			return;
		FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(getDocument());
		IRegion region;
		try {
			region = dad.find(pos, name, /* forwardSearch */true, /* caseSensitive */true, /* wholeWord */false,
					/* regExSearch */false);
		} catch (BadLocationException e) {
			return;
		}

		if (region == null)
			return;

		// now we have the offset
		int offset = region.getOffset();
		IASTNode prevNode = null;

		// see if there are previous nodes on same line
		if (lineOffset < offset) {
			astNode = getASTFirstContainedNodeFromPosition(ast, lineOffset, lineLength);
			if (astNode != null) {
				IASTFileLocation fileLoc = astNode.getFileLocation();
				if (fileLoc == null)
					return;
				int length = lineLength;
				while (fileLoc.getNodeOffset() < offset) {
					prevNode = astNode;
					astNode = getASTFirstContainedNodeFromPosition(ast,
							fileLoc.getNodeOffset() + fileLoc.getNodeLength(), length);
					fileLoc = astNode.getFileLocation();
					if (fileLoc == null)
						return;
					length -= fileLoc.getNodeLength();
				}
			}
		}

		// if we didn't find the previous node on the same line, go back a line at a time and find last node on line
		while (prevNode == null) {
			lineNum -= 1;
			if (lineNum < 0)
				return; // don't bother once we have reached start of file
			try {
				lineOffset = document.getLineOffset(lineNum);
				lineLength = document.getLineLength(lineNum);
			} catch (BadLocationException e) {
				return;
			}
			int x = lineOffset;
			int leftover = lineLength;
			// get a node at a time from line and keep track of last node found
			while (x < lineOffset + lineLength) {
				astNode = getASTFirstContainedNodeFromPosition(ast, x, leftover);
				if (astNode == null)
					break;
				prevNode = astNode;
				IASTFileLocation fileLoc = astNode.getFileLocation();
				if (fileLoc == null)
					break;
				x += fileLoc.getNodeLength();
				leftover -= fileLoc.getNodeLength();
			}

		}

		IASTFileLocation location = prevNode.getFileLocation();
		if (location == null)
			return;
		int replacementLoc = location.getNodeOffset() + location.getNodeLength();
		// in the case of a Problem statement, it might include \n or \r\n as part
		// of the node, so we must just assume the semi-colon belongs at the end of
		// the line
		if (replacementLoc == offset)
			replacementLoc -= System.lineSeparator().length();
		try {
			// insert the semi-colon
			document.replace(replacementLoc, 0, ";"); //$NON-NLS-1$
		} catch (BadLocationException e1) {
			return;
		}

		try {
			// remove marker now that has been dealt with
			marker.delete();
		} catch (CoreException e) {
			CheckersUiActivator.log(e);
		}
	}

	/**
	 * @param ast
	 * @param charStart
	 * @param length
	 * @return
	 */
	private IASTNode getASTFirstContainedNodeFromPosition(IASTTranslationUnit ast, final int charStart,
			final int length) {
		IASTNode node = ast.getNodeSelector(null).findFirstContainedNode(charStart, length);
		return node;
	}

}

Back to the top