diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml index 980a19f6e98..7052603655e 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml @@ -13,6 +13,10 @@ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAssignmentInCondition" problemId="org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem"> + + diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java index e68a5ce2f10..525de8a6b3a 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java +++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java @@ -19,6 +19,7 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = "org.eclipse.cdt.codan.internal.checkers.ui.messages"; //$NON-NLS-1$ public static String CatchByReferenceQuickFix_Message; public static String QuickFixAssignmentInCondition_Message; + public static String SuggestedParenthesisQuickFix_Message; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/messages.properties b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/messages.properties index 5ce0a9ff7e9..2d965d9d38d 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/messages.properties +++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/messages.properties @@ -1,2 +1,3 @@ CatchByReferenceQuickFix_Message=Change to use '&' QuickFixAssignmentInCondition_Message=Change to '==' +SuggestedParenthesisQuickFix_Message=Surround with '()' diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/SuggestedParenthesisQuickFix.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/SuggestedParenthesisQuickFix.java new file mode 100644 index 00000000000..6cf54134b68 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/SuggestedParenthesisQuickFix.java @@ -0,0 +1,37 @@ +package org.eclipse.cdt.codan.internal.checkers.ui.quickfix; + +import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator; +import org.eclipse.cdt.codan.internal.checkers.ui.Messages; +import org.eclipse.cdt.codan.ui.AbstarctCodanCMarkerResolution; +import org.eclipse.core.resources.IMarker; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; + +public class SuggestedParenthesisQuickFix extends + AbstarctCodanCMarkerResolution { + public String getLabel() { + return Messages.SuggestedParenthesisQuickFix_Message; + } + + @Override + public boolean isApplicable(IMarker marker) { + int charEnd = marker.getAttribute(IMarker.CHAR_END, -1); + if (charEnd == -1) + return false; + return true; + } + + @Override + public void apply(IMarker marker, IDocument document) { + int charStart = marker.getAttribute(IMarker.CHAR_START, -1); + int charEnd = marker.getAttribute(IMarker.CHAR_END, -1); + if (charEnd == -1) + return; + try { + document.replace(charStart, 0, "("); //$NON-NLS-1$ + document.replace(charEnd+1, 0, ")"); //$NON-NLS-1$ + } catch (BadLocationException e) { + CheckersUiActivator.log(e); + } + } +} diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanProblemMarkerResolutionGenerator.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanProblemMarkerResolutionGenerator.java index 715602d3249..1282260d485 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanProblemMarkerResolutionGenerator.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanProblemMarkerResolutionGenerator.java @@ -17,6 +17,7 @@ import java.util.Iterator; import java.util.Map; import java.util.regex.Pattern; +import org.eclipse.cdt.codan.ui.AbstarctCodanCMarkerResolution; import org.eclipse.core.resources.IMarker; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; @@ -57,11 +58,14 @@ public class CodanProblemMarkerResolutionGenerator implements .iterator(); iterator.hasNext();) { ConditionalResolution res = iterator.next(); if (res.messagePattern != null) { - if (message.matches(res.messagePattern)) - list.add(res.res); - } else { - list.add(res.res); + if (!message.matches(res.messagePattern)) + continue; } + if (res.res instanceof AbstarctCodanCMarkerResolution) { + if (!((AbstarctCodanCMarkerResolution)res.res).isApplicable(marker)) + continue; + } + list.add(res.res); } if (list.size() > 0) return list.toArray(new IMarkerResolution[list.size()]); diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstarctCodanCMarkerResolution.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstarctCodanCMarkerResolution.java index 0f79b2a59e8..654dd0b9d06 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstarctCodanCMarkerResolution.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstarctCodanCMarkerResolution.java @@ -27,16 +27,17 @@ import org.eclipse.ui.part.FileEditorInput; 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} + * 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} */ public abstract class AbstarctCodanCMarkerResolution implements IMarkerResolution { /** - * Get position offset from marker. If CHAR_START attribute is not set - * for marker, line and document would be used. + * 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 @@ -56,11 +57,13 @@ public abstract class AbstarctCodanCMarkerResolution implements } return position; } - /** - * Runs this resolution. - * - * @param marker the marker to resolve - */ + + /** + * Runs this resolution. + * + * @param marker + * the marker to resolve + */ public void run(IMarker marker) { // See if there is an open editor on the file containing the marker IWorkbenchWindow w = PlatformUI.getWorkbench() @@ -97,10 +100,18 @@ public abstract class AbstarctCodanCMarkerResolution implements /** * 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; + } }