From cad7d128a07ce3b82a7a0dae81e2caad52915d89 Mon Sep 17 00:00:00 2001 From: Philippe Herlin Date: Sun, 7 May 2006 12:04:13 +0000 Subject: [PATCH] Add the possibility to use the PMD violation review style git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4379 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd-eclipse/CHANGELOG.txt | 1 + pmd-eclipse/messages.properties | 1 + pmd-eclipse/nl/fr/messages.properties | 1 + .../sourceforge/pmd/eclipse/PMDConstants.java | 4 + .../sourceforge/pmd/eclipse/PMDPlugin.java | 24 +++ .../pmd/eclipse/PMDPluginConstants.java | 7 +- .../eclipse/actions/ClearReviewsAction.java | 11 +- .../pmd/eclipse/cmd/BaseVisitor.java | 5 +- .../preferences/GeneralPreferencesPage.java | 36 +++- .../eclipse/views/actions/ReviewAction.java | 180 +++++++++++------- .../eclipse/views/legacy/ReviewAction.java | 82 ++++++-- 11 files changed, 260 insertions(+), 92 deletions(-) diff --git a/pmd-eclipse/CHANGELOG.txt b/pmd-eclipse/CHANGELOG.txt index 6896e62258..9bacc178ee 100644 --- a/pmd-eclipse/CHANGELOG.txt +++ b/pmd-eclipse/CHANGELOG.txt @@ -13,6 +13,7 @@ Runtime . make CPD "working set aware" . remove some dead code . limit the number of reported violations per file and per rule (default to 5) +. Add the possibility to use the PMD violation review style ---------------------------------------------------------------------------------------- diff --git a/pmd-eclipse/messages.properties b/pmd-eclipse/messages.properties index 0b0cc697dc..583593839e 100644 --- a/pmd-eclipse/messages.properties +++ b/pmd-eclipse/messages.properties @@ -24,6 +24,7 @@ preference.pmd.label.use_dfa = Enable dataflow anomaly analysis (experimental) preference.pmd.label.max_violations_pfpr = Maximum reported violations per file per rules preference.pmd.tooltip.max_violations_pfpr = This helps limiting the report size and improving overall performances preference.pmd.message.invalid_numeric_value = Incorrect numeric value entered +preference.pmd.label.review_pmd_style = Use PMD style (// NOPMD comment) # PMD Rule set preference page preference.ruleset.title = PMD RuleSet Configuration Options diff --git a/pmd-eclipse/nl/fr/messages.properties b/pmd-eclipse/nl/fr/messages.properties index af1654a4fc..80c0435e71 100644 --- a/pmd-eclipse/nl/fr/messages.properties +++ b/pmd-eclipse/nl/fr/messages.properties @@ -23,6 +23,7 @@ preference.pmd.label.use_dfa = Activer l'analyse d'anomalie de flots de donn preference.pmd.label.max_violations_pfpr = Nombre maximal de violations reportées par fichier et par règle preference.pmd.tooltip.max_violations_pfpr = Permet de limiter le nombre total de violations et d'améliorer sensiblement les performances preference.pmd.message.invalid_numeric_value = Valeur numérique incorrecte +preference.pmd.label.review_pmd_style = Employer le style PMD (commentaire // NOPMD) # PMD Rule set preference page preference.ruleset.title = Options de configuration des règles PMD diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDConstants.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDConstants.java index 384bf43586..0a07c912a7 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDConstants.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDConstants.java @@ -6,6 +6,9 @@ package net.sourceforge.pmd.eclipse; * @version $Revision$ * * $Log$ + * Revision 1.15 2006/05/07 12:01:50 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.14 2006/05/02 20:10:49 phherlin * Limit the number of reported violations per file and per rule * @@ -79,6 +82,7 @@ public interface PMDConstants { public static final String MSGKEY_PREF_GENERAL_LABEL_MAX_VIOLATIONS_PFPR = "preference.pmd.label.max_violations_pfpr"; public static final String MSGKEY_PREF_GENERAL_TOOLTIP_MAX_VIOLATIONS_PFPR = "preference.pmd.tooltip.max_violations_pfpr"; public static final String MSGKEY_PREF_GENERAL_MESSAGE_INVALID_NUMERIC_VALUE ="preference.pmd.message.invalid_numeric_value"; + public static final String MSGKEY_PREF_GENERAL_REVIEW_PMD_STYLE = "preference.pmd.label.review_pmd_style"; public static final String MSGKEY_PREF_RULESET_TITLE = "preference.ruleset.title"; public static final String MSGKEY_PREF_RULESET_LIST = "preference.ruleset.list"; diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java index 5d76f00e98..d216ff7d8f 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java @@ -50,6 +50,9 @@ import org.osgi.framework.BundleContext; * @version $Revision$ * * $Log$ + * Revision 1.33 2006/05/07 12:01:50 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.32 2006/05/02 20:10:49 phherlin * Limit the number of reported violations per file and per rule * @@ -140,6 +143,7 @@ public class PMDPlugin extends AbstractUIPlugin implements PMDPluginConstants { private String[] priorityLabels; private String reviewAdditionalComment; private int maxViolationsPerFilePerRule; + private boolean reviewPmdStyle; /** * Private constructor ensures it remains a singleton. @@ -558,4 +562,24 @@ public class PMDPlugin extends AbstractUIPlugin implements PMDPluginConstants { getPreferenceStore().setValue(MAX_VIOLATIONS_PER_FILE_PER_RULE_PREFERENCE, this.maxViolationsPerFilePerRule); } + /** + * Get the style of violation reviews + * + * @return + */ + public boolean isReviewPmdStyle() { + this.reviewPmdStyle = getPreferenceStore().getBoolean(REVIEW_PMD_STYLE_PREFERENCE); + return this.reviewPmdStyle; + } + + /** + * Set the style of violation reviews + * + * @param int + */ + public void setReviewPmdStyle(boolean reviewPmdStyle) { + this.reviewPmdStyle = reviewPmdStyle; + getPreferenceStore().setValue(REVIEW_PMD_STYLE_PREFERENCE, this.reviewPmdStyle); + } + } \ No newline at end of file diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPluginConstants.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPluginConstants.java index 2d185fc3c0..a1d2879686 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPluginConstants.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPluginConstants.java @@ -8,6 +8,9 @@ package net.sourceforge.pmd.eclipse; * @version $Revision$ * * $Log$ + * Revision 1.12 2006/05/07 12:01:50 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.11 2006/05/02 20:10:49 phherlin * Limit the number of reported violations per file and per rule * @@ -120,7 +123,8 @@ public interface PMDPluginConstants { public static final String SETTINGS_VIEW_WARNING_FILTER = "view.warning_filter"; public static final String SETTINGS_VIEW_INFORMATION_FILTER = "view.information_filter"; - public static final String REVIEW_MARKER = "// @PMD:REVIEWED:"; + public static final String PLUGIN_STYLE_REVIEW_COMMENT = "// @PMD:REVIEWED:"; + public static final String PMD_STYLE_REVIEW_COMMENT = "// NOPMD"; public static final String REVIEW_ADDITIONAL_COMMENT_DEFAULT = "by {0} on {1}"; public static final String REVIEW_ADDITIONAL_COMMENT_PREFERENCE = PLUGIN_ID + ".review_additional_comment"; public static final int SHOW_PERSPECTIVE_ON_CHECK_DEFAULT = 1; @@ -129,6 +133,7 @@ public interface PMDPluginConstants { public static final String USE_DFA_PREFERENCE = PLUGIN_ID + ".use_dfa"; public static final String MAX_VIOLATIONS_PER_FILE_PER_RULE_PREFERENCE = PLUGIN_ID + ".max_violations_per_file_per_rule"; public static final int MAX_VIOLATIONS_PER_FILE_PER_RULE_DEFAULT = 5; + public static final String REVIEW_PMD_STYLE_PREFERENCE = PLUGIN_ID + ".review_pmd_style"; public static final String REPORT_FOLDER = "reports"; diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/ClearReviewsAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/ClearReviewsAction.java index 0183f5af95..1c62decb91 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/ClearReviewsAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/ClearReviewsAction.java @@ -44,6 +44,7 @@ import java.util.Iterator; import net.sourceforge.pmd.eclipse.PMDConstants; import net.sourceforge.pmd.eclipse.PMDPlugin; +import net.sourceforge.pmd.eclipse.PMDPluginConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -77,6 +78,9 @@ import org.eclipse.ui.IWorkbenchPart; * @version $Revision$ * * $Log$ + * Revision 1.5 2006/05/07 12:03:08 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.4 2006/01/27 00:03:11 phherlin * Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3 * Revision 1.3 2005/10/24 22:39:00 phherlin Integrating Sebastian Raffel's work Refactor command @@ -267,6 +271,8 @@ public class ClearReviewsAction implements IObjectActionDelegate, IResourceVisit while (reader.ready()) { String origLine = reader.readLine(); String line = origLine.trim(); + int index = origLine.indexOf(PMDPluginConstants.PMD_STYLE_REVIEW_COMMENT); + if (line.startsWith("/*")) { if (line.indexOf("*/") == -1) { comment = true; @@ -275,8 +281,11 @@ public class ClearReviewsAction implements IObjectActionDelegate, IResourceVisit } else if (comment && (line.indexOf("*/") != -1)) { comment = false; out.println(origLine); - } else if (!comment && line.startsWith(PMDPlugin.REVIEW_MARKER)) { + } else if (!comment && line.startsWith(PMDPluginConstants.PLUGIN_STYLE_REVIEW_COMMENT)) { noChange = false; + } else if (!comment && (index != -1)) { + noChange = false; + out.println(origLine.substring(0, index)); } else { out.println(origLine); } diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/cmd/BaseVisitor.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/cmd/BaseVisitor.java index fca274ebe8..9d1d5b0370 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/cmd/BaseVisitor.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/cmd/BaseVisitor.java @@ -67,6 +67,9 @@ import org.eclipse.ui.ResourceWorkingSetFilter; * @version $Revision$ * * $Log$ + * Revision 1.12 2006/05/07 12:03:09 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.11 2006/05/02 20:10:26 phherlin * Limit the number of reported violations per file and per rule * @@ -424,7 +427,7 @@ public class BaseVisitor { comment = line.indexOf("*/") == -1; } else if (comment && (line.indexOf("*/") != -1)) { comment = false; - } else if (!comment && line.startsWith(PMDPlugin.REVIEW_MARKER)) { + } else if (!comment && line.startsWith(PMDPlugin.PLUGIN_STYLE_REVIEW_COMMENT)) { final String tail = line.substring(17); final String ruleName = tail.substring(0, tail.indexOf(':')); pendingReviews.push(ruleName); diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/GeneralPreferencesPage.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/GeneralPreferencesPage.java index e88d68be32..53c806bba7 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/GeneralPreferencesPage.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/GeneralPreferencesPage.java @@ -34,6 +34,9 @@ import net.sourceforge.pmd.eclipse.PMDPluginConstants; * @version $Revision$ * * $Log$ + * Revision 1.11 2006/05/07 12:03:08 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.10 2006/05/02 20:11:13 phherlin * Limit the number of reported violations per file and per rule * @@ -51,6 +54,7 @@ public class GeneralPreferencesPage extends PreferencePage implements IWorkbench private Button showPerspectiveBox; private Button useDFABox; private Text maxViolationsPerFilePerRule; + private Button reviewPmdStyleBox; /** * Initialize the page @@ -144,9 +148,10 @@ public class GeneralPreferencesPage extends PreferencePage implements IWorkbench group.setLayout(new GridLayout(1, false)); // build children + this.reviewPmdStyleBox = buildReviewPmdStyleBoxButton(group); + Label separator = new Label(group, SWT.SEPARATOR | SWT.SHADOW_IN | SWT.HORIZONTAL); buildLabel(group, PMDConstants.MSGKEY_PREF_GENERAL_LABEL_ADDCOMMENT); this.additionalCommentText = buildAdditionalCommentText(group); - Label separator = new Label(group, SWT.SEPARATOR | SWT.SHADOW_IN | SWT.HORIZONTAL); buildLabel(group, PMDConstants.MSGKEY_PREF_GENERAL_LABEL_SAMPLE); this.sampleLabel = buildSampleLabel(group); updateSampleLabel(); @@ -155,13 +160,18 @@ public class GeneralPreferencesPage extends PreferencePage implements IWorkbench GridData data = new GridData(); data.horizontalAlignment = GridData.FILL; data.grabExcessHorizontalSpace = true; - this.additionalCommentText.setLayoutData(data); + this.reviewPmdStyleBox.setLayoutData(data); data = new GridData(); data.horizontalAlignment = GridData.FILL; data.grabExcessHorizontalSpace = true; separator.setLayoutData(data); + data = new GridData(); + data.horizontalAlignment = GridData.FILL; + data.grabExcessHorizontalSpace = true; + this.additionalCommentText.setLayoutData(data); + data = new GridData(); data.horizontalAlignment = GridData.FILL; data.grabExcessHorizontalSpace = true; @@ -266,6 +276,20 @@ public class GeneralPreferencesPage extends PreferencePage implements IWorkbench return text; } + /** + * Build the check box for enabling PMD review style + * @param viewGroup the parent composite + * + */ + private Button buildReviewPmdStyleBoxButton(final Composite parent) { + Button button = new Button(parent, SWT.CHECK); + button.setText(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_PREF_GENERAL_REVIEW_PMD_STYLE)); + button.setSelection(PMDPlugin.getDefault().isReviewPmdStyle()); + + return button; + } + + /** * @see org.eclipse.jface.preference.PreferencePage#performDefaults() */ @@ -329,12 +353,12 @@ public class GeneralPreferencesPage extends PreferencePage implements IWorkbench } if (this.showPerspectiveBox != null) { - PMDPlugin.getDefault().getPreferenceStore().setValue(PMDPlugin.SHOW_PERSPECTIVE_ON_CHECK_PREFERENCE, + PMDPlugin.getDefault().getPreferenceStore().setValue(PMDPluginConstants.SHOW_PERSPECTIVE_ON_CHECK_PREFERENCE, this.showPerspectiveBox.getSelection() ? 1 : -1); } if (this.useDFABox != null) { - PMDPlugin.getDefault().getPreferenceStore().setValue(PMDPlugin.USE_DFA_PREFERENCE, + PMDPlugin.getDefault().getPreferenceStore().setValue(PMDPluginConstants.USE_DFA_PREFERENCE, this.useDFABox.getSelection() ? 1 : -1); } @@ -342,6 +366,10 @@ public class GeneralPreferencesPage extends PreferencePage implements IWorkbench PMDPlugin.getDefault().setMaxViolationsPerFilePerRule(Integer.valueOf(this.maxViolationsPerFilePerRule.getText()).intValue()); } + if (this.reviewPmdStyleBox != null) { + PMDPlugin.getDefault().setReviewPmdStyle(this.reviewPmdStyleBox.getSelection()); + } + return true; } diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/actions/ReviewAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/actions/ReviewAction.java index 314a3d0a20..60301b6ae9 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/actions/ReviewAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/actions/ReviewAction.java @@ -10,6 +10,7 @@ import java.util.Date; import net.sourceforge.pmd.eclipse.PMDConstants; import net.sourceforge.pmd.eclipse.PMDPlugin; +import net.sourceforge.pmd.eclipse.PMDPluginConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,55 +36,49 @@ import org.eclipse.swt.widgets.Shell; * @version $Revision$ * * $Log$ - * Revision 1.1 2005/10/24 22:45:01 phherlin - * Integrating Sebastian Raffel's work - * - * Revision 1.5 2004/04/19 22:25:50 phherlin - * Fixing UTF-8 encoding - * - * Revision 1.4 2003/12/09 00:14:59 phherlin - * Merging from v2 development - * - * Revision 1.3 2003/11/30 22:57:43 phherlin - * Merging from eclipse-v2 development branch - * - * Revision 1.1.2.1 2003/11/30 21:16:16 phherlin - * Adapting to Eclipse v3 - * - * Revision 1.1 2003/08/14 16:10:42 phherlin - * Implementing Review feature (RFE#787086) - * + * Revision 1.2 2006/05/07 12:03:09 phherlin + * Add the possibility to use the PMD violation review style + * Revision 1.1 2005/10/24 22:45:01 phherlin Integrating Sebastian Raffel's work + * + * Revision 1.5 2004/04/19 22:25:50 phherlin Fixing UTF-8 encoding + * + * Revision 1.4 2003/12/09 00:14:59 phherlin Merging from v2 development + * + * Revision 1.3 2003/11/30 22:57:43 phherlin Merging from eclipse-v2 development branch + * + * Revision 1.1.2.1 2003/11/30 21:16:16 phherlin Adapting to Eclipse v3 + * + * Revision 1.1 2003/08/14 16:10:42 phherlin Implementing Review feature (RFE#787086) + * */ public class ReviewAction extends ViolationSelectionAction { - - private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.views.ReviewAction"); + private static Log log = LogFactory.getLog(ReviewAction.class); private IProgressMonitor monitor; - - - + /** * Constructor */ public ReviewAction(TableViewer viewer) { super(viewer); - - setImageDescriptor(PMDPlugin.getDefault().getImageDescriptor( - PMDPlugin.ICON_BUTTON_REVIEW)); - setText(PMDPlugin.getDefault().getMessage( - PMDConstants.MSGKEY_VIEW_ACTION_REVIEW)); - setToolTipText(PMDPlugin.getDefault().getMessage( - PMDConstants.MSGKEY_VIEW_TOOLTIP_REVIEW)); + + setImageDescriptor(PMDPlugin.getDefault().getImageDescriptor(PMDPlugin.ICON_BUTTON_REVIEW)); + setText(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_VIEW_ACTION_REVIEW)); + setToolTipText(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_VIEW_TOOLTIP_REVIEW)); } - - - /* @see org.eclipse.jface.action.IAction#run() */ + + /** + * @see org.eclipse.jface.action.IAction#run() + */ public void run() { final IMarker[] markers = getSelectedViolations(); + final boolean reviewPmdStyle = PMDPlugin.getDefault().isReviewPmdStyle(); + if (markers != null) { // Get confirmation if multiple markers are selected + // Not necessary when using PMD style boolean go = true; - if (markers.length > 1) { + if ((markers.length > 1) && (!reviewPmdStyle)) { String title = PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_CONFIRM_TITLE); String message = PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_CONFIRM_REVIEW_MULTIPLE_MARKERS); Shell shell = Display.getCurrent().getActiveShell(); @@ -98,7 +93,7 @@ public class ReviewAction extends ViolationSelectionAction { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { setMonitor(monitor); monitor.beginTask(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_MONITOR_REVIEW, ""), 5); - insertReview(markers[0]); + insertReview(markers[0], reviewPmdStyle); monitor.done(); } }); @@ -110,13 +105,13 @@ public class ReviewAction extends ViolationSelectionAction { } } } - - + /** * Do the insertion of the review comment + * * @param marker */ - protected void insertReview(IMarker marker) { + protected void insertReview(IMarker marker, boolean reviewPmdStyle) { try { IResource resource = marker.getResource(); if (resource instanceof IFile) { @@ -130,7 +125,11 @@ public class ReviewAction extends ViolationSelectionAction { monitorWorked(); - sourceCode = addReviewComment(sourceCode, offset, marker); + if (reviewPmdStyle) { + sourceCode = addPmdReviewComment(sourceCode, offset, marker); + } else { + sourceCode = addPluginReviewComment(sourceCode, offset, marker); + } monitorWorked(); @@ -138,17 +137,16 @@ public class ReviewAction extends ViolationSelectionAction { monitorWorked(); } else { - MessageDialog.openError( - Display.getCurrent().getActiveShell(), - getMessage(PMDConstants.MSGKEY_ERROR_TITLE), - "The file " + file.getName() + " doesn't exists ! Review aborted. Try to refresh the workspace and retry."); + MessageDialog.openError(Display.getCurrent().getActiveShell(), getMessage(PMDConstants.MSGKEY_ERROR_TITLE), + "The file " + file.getName() + + " doesn't exists ! Review aborted. Try to refresh the workspace and retry."); } } } catch (JavaModelException e) { IJavaModelStatus status = e.getJavaModelStatus(); PMDPlugin.getDefault().logError(status); - log.warn("Ignoring Java Model Exception : " + status.getMessage() ); + log.warn("Ignoring Java Model Exception : " + status.getMessage()); if (log.isDebugEnabled()) { log.debug(" code : " + status.getCode()); log.debug(" severity : " + status.getSeverity()); @@ -163,26 +161,25 @@ public class ReviewAction extends ViolationSelectionAction { PMDPlugin.getDefault().logError(getMessage(PMDConstants.MSGKEY_ERROR_IO_EXCEPTION), e); } } - - + /** * Get the monitor + * * @return */ protected IProgressMonitor getMonitor() { return monitor; } - - + /** * Set the monitor + * * @param monitor */ protected void setMonitor(IProgressMonitor monitor) { this.monitor = monitor; } - - + /** * Progress monitor */ @@ -191,8 +188,7 @@ public class ReviewAction extends ViolationSelectionAction { getMonitor().worked(1); } } - - + /** * Renvoie la position dans le code source du début de la ligne du marqueur */ @@ -215,29 +211,71 @@ public class ReviewAction extends ViolationSelectionAction { return lineStart; } - - - /** - * Insère un commentaire de révision juste au dessus de la ligne du marker - */ - private String addReviewComment(String sourceCode, int offset, IMarker marker) { - String additionalCommentPattern = PMDPlugin.getDefault().getReviewAdditionalComment(); - String additionalComment = - MessageFormat.format(additionalCommentPattern, new Object[] { System.getProperty("user.name", ""), new Date()}); + /** + * Insert a review comment with the Plugin style + */ + private String addPluginReviewComment(String sourceCode, int offset, IMarker marker) { + String additionalCommentPattern = PMDPlugin.getDefault().getReviewAdditionalComment(); + String additionalComment = MessageFormat.format(additionalCommentPattern, new Object[] { + System.getProperty("user.name", ""), new Date() }); + + // Copy the source code until the violation line not included StringBuffer sb = new StringBuffer(sourceCode.substring(0, offset)); + + // Add the review comment sb.append(computeIndent(sourceCode, offset)); - sb.append(PMDPlugin.REVIEW_MARKER); - sb.append(marker.getAttribute(PMDPlugin.KEY_MARKERATT_RULENAME, "")); + sb.append(PMDPluginConstants.PLUGIN_STYLE_REVIEW_COMMENT); + sb.append(marker.getAttribute(PMDPluginConstants.KEY_MARKERATT_RULENAME, "")); sb.append(": "); sb.append(additionalComment); sb.append(System.getProperty("line.separator")); + + // Copy the rest of the source code sb.append(sourceCode.substring(offset)); return sb.toString(); } - - + + /** + * Insert a review comment with the PMD style + */ + private String addPmdReviewComment(String sourceCode, int offset, IMarker marker) { + String result = sourceCode; + String additionalCommentPattern = PMDPlugin.getDefault().getReviewAdditionalComment(); + String additionalComment = MessageFormat.format(additionalCommentPattern, new Object[] { + System.getProperty("user.name", ""), new Date() }); + + // Find the end of line + int index = sourceCode.substring(offset).indexOf('\r'); + if (index == -1) { + index = sourceCode.substring(offset).indexOf('\n'); + if (index == -1) { + index = sourceCode.substring(offset).length(); + } + } + index += offset; + + // Insert comment only if it does not already exist + if (sourceCode.substring(offset, index).indexOf(PMDPluginConstants.PMD_STYLE_REVIEW_COMMENT) == -1) { + + // Copy the source code until the violation line included + StringBuffer sb = new StringBuffer(sourceCode.substring(0, index)); + + // Add the review comment + sb.append(' '); + sb.append(PMDPlugin.PMD_STYLE_REVIEW_COMMENT); + sb.append(' '); + sb.append(additionalComment); + + // Copy the rest of the code + sb.append(sourceCode.substring(index)); + result = sb.toString(); + } + + return result; + } + /** * Calcul l'indentation employée sur la ligne du marker */ @@ -252,21 +290,21 @@ public class ReviewAction extends ViolationSelectionAction { return indent.toString(); } - - + /** * Helper method to find message string + * * @param messageId a message id * @return the localized message OR the id if not found - */ + */ private String getMessage(String messageId) { return PMDPlugin.getDefault().getMessage(messageId, messageId); } - + private String readFile(IFile file) throws IOException, CoreException { InputStream contents = file.getContents(true); InputStreamReader reader = new InputStreamReader(contents); - + try { char[] buffer = new char[4096]; StringBuffer stringBuffer = new StringBuffer(4096); @@ -276,7 +314,7 @@ public class ReviewAction extends ViolationSelectionAction { stringBuffer.append(buffer, 0, readCount); } } - + return stringBuffer.toString(); } finally { diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/legacy/ReviewAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/legacy/ReviewAction.java index 1d40892c4c..2ab23f7599 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/legacy/ReviewAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/views/legacy/ReviewAction.java @@ -10,6 +10,7 @@ import java.util.Date; import net.sourceforge.pmd.eclipse.PMDConstants; import net.sourceforge.pmd.eclipse.PMDPlugin; +import net.sourceforge.pmd.eclipse.PMDPluginConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,6 +36,9 @@ import org.eclipse.swt.widgets.Shell; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/05/07 12:03:09 phherlin + * Add the possibility to use the PMD violation review style + * * Revision 1.1 2005/10/24 22:45:58 phherlin * Integrating Sebastian Raffel's work * Move orginal Violations view to legacy @@ -72,11 +76,14 @@ public class ReviewAction extends Action { */ public void run() { final IMarker[] markers = violationView.getSelectedViolations(); + final boolean reviewPmdStyle = PMDPlugin.getDefault().isReviewPmdStyle(); + if (markers != null) { // Get confirmation if multiple markers are selected + // Not necessary when using PMD style boolean go = true; - if (markers.length > 1) { + if ((markers.length > 1) && (!reviewPmdStyle)) { String title = PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_CONFIRM_TITLE); String message = PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_CONFIRM_REVIEW_MULTIPLE_MARKERS); Shell shell = Display.getCurrent().getActiveShell(); @@ -91,7 +98,7 @@ public class ReviewAction extends Action { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { setMonitor(monitor); monitor.beginTask(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_MONITOR_REVIEW, ""), 5); - insertReview(markers[0]); + insertReview(markers[0], reviewPmdStyle); monitor.done(); } }); @@ -108,7 +115,7 @@ public class ReviewAction extends Action { * Do the insertion of the review comment * @param marker */ - protected void insertReview(IMarker marker) { + protected void insertReview(IMarker marker, boolean reviewPmdStyle) { try { IResource resource = marker.getResource(); if (resource instanceof IFile) { @@ -122,7 +129,11 @@ public class ReviewAction extends Action { monitorWorked(); - sourceCode = addReviewComment(sourceCode, offset, marker); + if (reviewPmdStyle) { + sourceCode = addPmdReviewComment(sourceCode, offset, marker); + } else { + sourceCode = addPluginReviewComment(sourceCode, offset, marker); + } monitorWorked(); @@ -130,10 +141,9 @@ public class ReviewAction extends Action { monitorWorked(); } else { - MessageDialog.openError( - Display.getCurrent().getActiveShell(), - getMessage(PMDConstants.MSGKEY_ERROR_TITLE), - "The file " + file.getName() + " doesn't exists ! Review aborted. Try to refresh the workspace and retry."); + MessageDialog.openError(Display.getCurrent().getActiveShell(), getMessage(PMDConstants.MSGKEY_ERROR_TITLE), + "The file " + file.getName() + + " doesn't exists ! Review aborted. Try to refresh the workspace and retry."); } } @@ -205,25 +215,69 @@ public class ReviewAction extends Action { } /** - * Insère un commentaire de révision juste au dessus de la ligne du marker + * Insert a review comment with the Plugin style */ - private String addReviewComment(String sourceCode, int offset, IMarker marker) { + private String addPluginReviewComment(String sourceCode, int offset, IMarker marker) { String additionalCommentPattern = PMDPlugin.getDefault().getReviewAdditionalComment(); - String additionalComment = - MessageFormat.format(additionalCommentPattern, new Object[] { System.getProperty("user.name", ""), new Date()}); + String additionalComment = MessageFormat.format(additionalCommentPattern, new Object[] { + System.getProperty("user.name", ""), new Date() }); + // Copy the source code until the violation line not included StringBuffer sb = new StringBuffer(sourceCode.substring(0, offset)); + + // Add the review comment sb.append(computeIndent(sourceCode, offset)); - sb.append(PMDPlugin.REVIEW_MARKER); - sb.append(marker.getAttribute(PMDPlugin.KEY_MARKERATT_RULENAME, "")); + sb.append(PMDPluginConstants.PLUGIN_STYLE_REVIEW_COMMENT); + sb.append(marker.getAttribute(PMDPluginConstants.KEY_MARKERATT_RULENAME, "")); sb.append(": "); sb.append(additionalComment); sb.append(System.getProperty("line.separator")); + + // Copy the rest of the source code sb.append(sourceCode.substring(offset)); return sb.toString(); } + /** + * Insert a review comment with the PMD style + */ + private String addPmdReviewComment(String sourceCode, int offset, IMarker marker) { + String result = sourceCode; + String additionalCommentPattern = PMDPlugin.getDefault().getReviewAdditionalComment(); + String additionalComment = MessageFormat.format(additionalCommentPattern, new Object[] { + System.getProperty("user.name", ""), new Date() }); + + // Find the end of line + int index = sourceCode.substring(offset).indexOf('\r'); + if (index == -1) { + index = sourceCode.substring(offset).indexOf('\n'); + if (index == -1) { + index = sourceCode.substring(offset).length(); + } + } + index += offset; + + // Insert comment only if it does not already exist + if (sourceCode.substring(offset, index).indexOf(PMDPluginConstants.PMD_STYLE_REVIEW_COMMENT) == -1) { + + // Copy the source code until the violation line included + StringBuffer sb = new StringBuffer(sourceCode.substring(0, index)); + + // Add the review comment + sb.append(' '); + sb.append(PMDPlugin.PMD_STYLE_REVIEW_COMMENT); + sb.append(' '); + sb.append(additionalComment); + + // Copy the rest of the code + sb.append(sourceCode.substring(index)); + result = sb.toString(); + } + + return result; + } + /** * Calcul l'indentation employée sur la ligne du marker */