1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 17:35:35 +02:00

Added quick fix for parenthesis checker

This commit is contained in:
Alena Laskavaia 2010-05-02 02:31:03 +00:00
parent 7872cd18fd
commit a93035a04c
6 changed files with 74 additions and 16 deletions

View file

@ -13,6 +13,10 @@
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAssignmentInCondition"
problemId="org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem">
</resolution>
<resolution
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFix"
problemId="org.eclipse.cdt.codan.internal.checkers.SuggestedParenthesisProblem">
</resolution>
</extension>
</plugin>

View file

@ -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);

View file

@ -1,2 +1,3 @@
CatchByReferenceQuickFix_Message=Change to use '&'
QuickFixAssignmentInCondition_Message=Change to '=='
SuggestedParenthesisQuickFix_Message=Surround with '()'

View file

@ -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);
}
}
}

View file

@ -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()]);

View file

@ -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;
}
}