mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-25 18:05:33 +02:00
Added quick fix for parenthesis checker
This commit is contained in:
parent
7872cd18fd
commit
a93035a04c
6 changed files with 74 additions and 16 deletions
|
@ -13,6 +13,10 @@
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAssignmentInCondition"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAssignmentInCondition"
|
||||||
problemId="org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem">
|
problemId="org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem">
|
||||||
</resolution>
|
</resolution>
|
||||||
|
<resolution
|
||||||
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFix"
|
||||||
|
problemId="org.eclipse.cdt.codan.internal.checkers.SuggestedParenthesisProblem">
|
||||||
|
</resolution>
|
||||||
</extension>
|
</extension>
|
||||||
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
|
@ -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$
|
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 CatchByReferenceQuickFix_Message;
|
||||||
public static String QuickFixAssignmentInCondition_Message;
|
public static String QuickFixAssignmentInCondition_Message;
|
||||||
|
public static String SuggestedParenthesisQuickFix_Message;
|
||||||
static {
|
static {
|
||||||
// initialize resource bundle
|
// initialize resource bundle
|
||||||
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
|
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
CatchByReferenceQuickFix_Message=Change to use '&'
|
CatchByReferenceQuickFix_Message=Change to use '&'
|
||||||
QuickFixAssignmentInCondition_Message=Change to '=='
|
QuickFixAssignmentInCondition_Message=Change to '=='
|
||||||
|
SuggestedParenthesisQuickFix_Message=Surround with '()'
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.codan.ui.AbstarctCodanCMarkerResolution;
|
||||||
import org.eclipse.core.resources.IMarker;
|
import org.eclipse.core.resources.IMarker;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IConfigurationElement;
|
import org.eclipse.core.runtime.IConfigurationElement;
|
||||||
|
@ -57,11 +58,14 @@ public class CodanProblemMarkerResolutionGenerator implements
|
||||||
.iterator(); iterator.hasNext();) {
|
.iterator(); iterator.hasNext();) {
|
||||||
ConditionalResolution res = iterator.next();
|
ConditionalResolution res = iterator.next();
|
||||||
if (res.messagePattern != null) {
|
if (res.messagePattern != null) {
|
||||||
if (message.matches(res.messagePattern))
|
if (!message.matches(res.messagePattern))
|
||||||
list.add(res.res);
|
continue;
|
||||||
} else {
|
|
||||||
list.add(res.res);
|
|
||||||
}
|
}
|
||||||
|
if (res.res instanceof AbstarctCodanCMarkerResolution) {
|
||||||
|
if (!((AbstarctCodanCMarkerResolution)res.res).isApplicable(marker))
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
list.add(res.res);
|
||||||
}
|
}
|
||||||
if (list.size() > 0)
|
if (list.size() > 0)
|
||||||
return list.toArray(new IMarkerResolution[list.size()]);
|
return list.toArray(new IMarkerResolution[list.size()]);
|
||||||
|
|
|
@ -27,16 +27,17 @@ import org.eclipse.ui.part.FileEditorInput;
|
||||||
import org.eclipse.ui.texteditor.ITextEditor;
|
import org.eclipse.ui.texteditor.ITextEditor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic class for codan marker resolution (for quick fix).
|
* Generic class for codan marker resolution (for quick fix). Use as a base
|
||||||
* Use as a base class for codanMarkerResolution extension.
|
* class for codanMarkerResolution extension. To add specific icon and
|
||||||
* To add specific icon and description client class should additionally
|
* description client class should additionally implement
|
||||||
* implement {@link IMarkerResolution2}
|
* {@link IMarkerResolution2}
|
||||||
*/
|
*/
|
||||||
public abstract class AbstarctCodanCMarkerResolution implements
|
public abstract class AbstarctCodanCMarkerResolution implements
|
||||||
IMarkerResolution {
|
IMarkerResolution {
|
||||||
/**
|
/**
|
||||||
* Get position offset from marker. If CHAR_START attribute is not set
|
* Get position offset from marker. If CHAR_START attribute is not set for
|
||||||
* for marker, line and document would be used.
|
* marker, line and document would be used.
|
||||||
|
*
|
||||||
* @param marker
|
* @param marker
|
||||||
* @param doc
|
* @param doc
|
||||||
* @return
|
* @return
|
||||||
|
@ -56,11 +57,13 @@ public abstract class AbstarctCodanCMarkerResolution implements
|
||||||
}
|
}
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Runs this resolution.
|
/**
|
||||||
*
|
* Runs this resolution.
|
||||||
* @param marker the marker to resolve
|
*
|
||||||
*/
|
* @param marker
|
||||||
|
* the marker to resolve
|
||||||
|
*/
|
||||||
public void run(IMarker marker) {
|
public void run(IMarker marker) {
|
||||||
// See if there is an open editor on the file containing the marker
|
// See if there is an open editor on the file containing the marker
|
||||||
IWorkbenchWindow w = PlatformUI.getWorkbench()
|
IWorkbenchWindow w = PlatformUI.getWorkbench()
|
||||||
|
@ -97,10 +100,18 @@ public abstract class AbstarctCodanCMarkerResolution implements
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply marker resolution for given marker in given open document.
|
* Apply marker resolution for given marker in given open document.
|
||||||
|
*
|
||||||
* @param marker
|
* @param marker
|
||||||
* @param document
|
* @param document
|
||||||
*/
|
*/
|
||||||
public abstract void apply(IMarker marker, IDocument 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue