~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAssignmentInCondition.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2013-10-03 20:30:16 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20131003203016-d4ug6l0xgosasumq
Tags: 8.2.1-1
* New upstream release.
* Updated autotools documentation sources.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*******************************************************************************
2
 
 * Copyright (c) 2009, 2010 Andrew Gvozdev
 
2
 * Copyright (c) 2009, 2013 Andrew Gvozdev and others
3
3
 * All rights reserved. This program and the accompanying materials
4
4
 * are made available under the terms of the Eclipse Public License v1.0
5
5
 * which accompanies this distribution, and is available at
6
6
 * http://www.eclipse.org/legal/epl-v10.html
7
7
 *
8
8
 * Contributors:
9
 
 *     Andrew Gvozdev  - initial API and implementation
 
9
 *     Andrew Gvozdev - initial API and implementation
 
10
 *     Nathan Ridge
10
11
 *******************************************************************************/
11
12
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
12
13
 
13
14
import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
14
15
import org.eclipse.cdt.codan.internal.checkers.ui.Messages;
15
 
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
 
16
import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
 
17
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
 
18
import org.eclipse.cdt.core.dom.ast.IASTNode;
 
19
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
 
20
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
 
21
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
 
22
import org.eclipse.cdt.core.index.IIndex;
 
23
import org.eclipse.cdt.core.model.ITranslationUnit;
16
24
import org.eclipse.core.resources.IMarker;
 
25
import org.eclipse.core.runtime.CoreException;
17
26
import org.eclipse.jface.text.BadLocationException;
18
27
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
19
 
import org.eclipse.jface.text.IDocument;
20
28
 
21
29
/**
22
30
 * quick fix for assignment in condition
23
31
 */
24
 
public class QuickFixAssignmentInCondition extends AbstractCodanCMarkerResolution {
 
32
public class QuickFixAssignmentInCondition extends AbstractAstRewriteQuickFix {
25
33
        @Override
26
34
        public String getLabel() {
27
35
                return Messages.QuickFixAssignmentInCondition_Message;
28
36
        }
29
37
 
30
38
        @Override
31
 
        public void apply(IMarker marker, IDocument document) {
32
 
                int pos = getOffset(marker, document);
 
39
        public void modifyAST(IIndex index, IMarker marker) {
33
40
                try {
34
 
                        FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(document);
35
 
                        dad.find(pos, "=", /* forwardSearch *///$NON-NLS-1$
36
 
                                        true, /* caseSensitive */false,
37
 
                                        /* wholeWord */false, /* regExSearch */false);
38
 
                        dad.replace("==", /* regExReplace */false); //$NON-NLS-1$
 
41
                        IASTTranslationUnit ast = getTranslationUnitViaEditor(marker).getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
 
42
                        int markerStart = marker.getAttribute(IMarker.CHAR_START, -1);
 
43
                        int markerEnd = marker.getAttribute(IMarker.CHAR_END, -1);
 
44
                        if (markerStart == -1 || markerEnd == -1 || markerStart >= markerEnd)
 
45
                                return;
 
46
                        IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
 
47
                        IASTNode containedNode = nodeSelector.findEnclosingNode(markerStart, markerEnd - markerStart);
 
48
                        if (containedNode instanceof IASTBinaryExpression) {
 
49
                                IASTBinaryExpression expr = (IASTBinaryExpression) containedNode;
 
50
                                IASTNodeLocation[] leftSubexprLocations = expr.getOperand1().getNodeLocations();
 
51
                                if (leftSubexprLocations.length != 1)  // don't handle expressions in macro expansions
 
52
                                        return;
 
53
                                IASTNodeLocation leftSubexprLocation = leftSubexprLocations[0];
 
54
                                int leftSubexprEnd = leftSubexprLocation.getNodeOffset() + leftSubexprLocation.getNodeLength();
 
55
                                
 
56
                                // Assignment operator will be following the end of the left subexpression. 
 
57
                                FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(getDocument());
 
58
                                adapter.find(leftSubexprEnd, 
 
59
                                                 "=",                                 ///$NON-NLS-1$
 
60
                                                 true,    /* forwardSearch */ 
 
61
                                                 false,   /* caseSensitive */ 
 
62
                                                 false,   /* wholeWord */
 
63
                                                 false);  /* regExSearch */      
 
64
                                adapter.replace("==", false /* regExReplace */);  ///$NON-NLS-1$
 
65
                        }
 
66
                } catch (CoreException e) {
 
67
                        CheckersUiActivator.log(e);
39
68
                } catch (BadLocationException e) {
40
69
                        CheckersUiActivator.log(e);
41
70
                }