mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Bug 348191 - Quick fix for catch by value should propose to replace by a
const reference Bug 352263 - [qf] Catch by reference quick fix places & at a wrong place when no space after the type
This commit is contained in:
parent
62c3d22313
commit
db669ba963
8 changed files with 184 additions and 10 deletions
|
@ -9,6 +9,10 @@
|
|||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFix"
|
||||
problemId="org.eclipse.cdt.codan.internal.checkers.CatchByReference">
|
||||
</resolution>
|
||||
<resolution
|
||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByConstReferenceQuickFix"
|
||||
problemId="org.eclipse.cdt.codan.internal.checkers.CatchByReference">
|
||||
</resolution>
|
||||
<resolution
|
||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixAssignmentInCondition"
|
||||
problemId="org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem">
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2009,2010 Alena Laskavaia
|
||||
* Copyright (c) 2009, 2011 Alena Laskavaia and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -17,6 +17,7 @@ import org.eclipse.osgi.util.NLS;
|
|||
*/
|
||||
public class Messages extends NLS {
|
||||
public static String CatchByReferenceQuickFix_Message;
|
||||
public static String CatchByConstReferenceQuickFix_Message;
|
||||
public static String QuickFixAssignmentInCondition_Message;
|
||||
public static String SuggestedParenthesisQuickFix_Message;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
###############################################################################
|
||||
# Copyright (c) 2010 Alena Laskavaia and others.
|
||||
# Copyright (c) 2010, 2011 Alena Laskavaia and others.
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# which accompanies this distribution, and is available at
|
||||
|
@ -9,5 +9,6 @@
|
|||
# Alena Laskavaia - initial API and implementation
|
||||
###############################################################################
|
||||
CatchByReferenceQuickFix_Message=Change to use '&'
|
||||
CatchByConstReferenceQuickFix_Message=Change to use 'const &'
|
||||
QuickFixAssignmentInCondition_Message=Change to '=='
|
||||
SuggestedParenthesisQuickFix_Message=Surround with '()'
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010, 2011 Alena Laskavaia and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Alena Laskavaia - original API and implementation in CatchByReferenceQuickFix
|
||||
* Tomasz Wesolowski - modified for const &
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
|
||||
|
||||
import org.eclipse.cdt.codan.internal.checkers.ui.Messages;
|
||||
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||
import org.eclipse.core.resources.IMarker;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
|
||||
/**
|
||||
* quick fix for catch by value
|
||||
*/
|
||||
public class CatchByConstReferenceQuickFix extends AbstractCodanCMarkerResolution {
|
||||
public String getLabel() {
|
||||
return Messages.CatchByConstReferenceQuickFix_Message;
|
||||
}
|
||||
|
||||
public void apply(IMarker marker, IDocument document) {
|
||||
CatchByReferenceQuickFix.applyCatchByReferenceQuickFix(marker, document, true);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010 Alena Laskavaia
|
||||
* Copyright (c) 2010, 2011 Alena Laskavaia and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -15,7 +15,6 @@ import org.eclipse.cdt.codan.internal.checkers.ui.Messages;
|
|||
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||
import org.eclipse.core.resources.IMarker;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
|
||||
/**
|
||||
|
@ -27,15 +26,47 @@ public class CatchByReferenceQuickFix extends AbstractCodanCMarkerResolution {
|
|||
}
|
||||
|
||||
public void apply(IMarker marker, IDocument document) {
|
||||
FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(document);
|
||||
applyCatchByReferenceQuickFix(marker, document, false);
|
||||
}
|
||||
|
||||
static void applyCatchByReferenceQuickFix(IMarker marker, IDocument document, boolean addConst) {
|
||||
try {
|
||||
int pos = getOffset(marker, document);
|
||||
dad.find(pos, " ", /* forwardSearch *///$NON-NLS-1$
|
||||
true, /* caseSensitive */false,
|
||||
/* wholeWord */false, /* regExSearch */false);
|
||||
dad.replace(" & ", /* regExReplace */false); //$NON-NLS-1$
|
||||
int left = marker.getAttribute(IMarker.CHAR_START, -1);
|
||||
int right = marker.getAttribute(IMarker.CHAR_END, -1);
|
||||
String inStr = document.get(left, right - left);
|
||||
document.replace(left, right - left, getCatchByReferenceString(inStr, addConst));
|
||||
} catch (BadLocationException e) {
|
||||
CheckersUiActivator.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a catch by reference string from a catch by value string
|
||||
*/
|
||||
static private String getCatchByReferenceString(String inStr, boolean addConst) {
|
||||
StringBuilder stringBuilder = new StringBuilder(inStr.length() + 10);
|
||||
if (addConst) {
|
||||
stringBuilder.append("const "); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
String typename;
|
||||
int space = inStr.lastIndexOf(' ');
|
||||
boolean hasDeclName = space != -1;
|
||||
if (hasDeclName) {
|
||||
typename = inStr.substring(0,space);
|
||||
} else {
|
||||
typename = inStr;
|
||||
}
|
||||
stringBuilder.append(typename);
|
||||
|
||||
stringBuilder.append(" &"); //$NON-NLS-1$
|
||||
|
||||
if (hasDeclName) {
|
||||
stringBuilder.append(" "); //$NON-NLS-1$
|
||||
String declname = inStr.substring(space+1);
|
||||
stringBuilder.append(declname);
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerT
|
|||
import org.eclipse.cdt.codan.core.internal.checkers.SuggestedParenthesisCheckerTest;
|
||||
import org.eclipse.cdt.codan.core.internal.checkers.SuspiciousSemicolonCheckerTest;
|
||||
import org.eclipse.cdt.codan.core.internal.checkers.UnusedSymbolInFileScopeCheckerTest;
|
||||
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFixTest;
|
||||
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CreateLocalVariableQuickFixTest;
|
||||
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
|
||||
|
||||
|
@ -72,6 +73,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
|
|||
// quick fixes
|
||||
suite.addTestSuite(CreateLocalVariableQuickFixTest.class);
|
||||
suite.addTestSuite(SuggestedParenthesisQuickFixTest.class);
|
||||
suite.addTestSuite(CatchByReferenceQuickFixTest.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2011 Tomasz Wesolowski and others
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Tomasz Wesolowski - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
|
||||
|
||||
import org.eclipse.cdt.codan.internal.checkers.CatchByReference;
|
||||
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||
|
||||
/**
|
||||
* @author Tomasz Wesolowski
|
||||
*/
|
||||
public class CatchByReferenceQuickFixTest extends QuickFixTestCase {
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
enableProblems(CatchByReference.ER_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCpp() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractCodanCMarkerResolution createQuickFix() {
|
||||
return null; // quick fix to be chosen per test
|
||||
}
|
||||
|
||||
// struct C {
|
||||
// };
|
||||
// void foo() {
|
||||
// try {
|
||||
// } catch (C exception) {
|
||||
// }
|
||||
// }
|
||||
@SuppressWarnings("restriction")
|
||||
public void testCatchByReference() throws Exception {
|
||||
setQuickFix(new CatchByReferenceQuickFix());
|
||||
loadcode(getAboveComment());
|
||||
String result = runQuickFixOneFile();
|
||||
assertContainedIn("catch (C & exception)", result); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
// struct C {
|
||||
// };
|
||||
// void foo() {
|
||||
// try {
|
||||
// } catch (C) {
|
||||
// }
|
||||
// }
|
||||
@SuppressWarnings("restriction")
|
||||
public void testCatchByReferenceNoDeclName() throws Exception {
|
||||
setQuickFix(new CatchByReferenceQuickFix());
|
||||
loadcode(getAboveComment());
|
||||
String result = runQuickFixOneFile();
|
||||
assertContainedIn("catch (C &)", result); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
// struct C {
|
||||
// };
|
||||
// void foo() {
|
||||
// try {
|
||||
// } catch (C exception) {
|
||||
// }
|
||||
// }
|
||||
@SuppressWarnings("restriction")
|
||||
public void testCatchByConstReference() throws Exception {
|
||||
setQuickFix(new CatchByConstReferenceQuickFix());
|
||||
loadcode(getAboveComment());
|
||||
String result = runQuickFixOneFile();
|
||||
assertContainedIn("catch (const C & exception)", result); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
// struct C {
|
||||
// };
|
||||
// void foo() {
|
||||
// try {
|
||||
// } catch (C) {
|
||||
// }
|
||||
// }
|
||||
@SuppressWarnings("restriction")
|
||||
public void testCatchByConstReferenceNoDeclName() throws Exception {
|
||||
setQuickFix(new CatchByConstReferenceQuickFix());
|
||||
loadcode(getAboveComment());
|
||||
String result = runQuickFixOneFile();
|
||||
assertContainedIn("catch (const C &)", result); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
|
@ -146,4 +146,12 @@ public abstract class QuickFixTestCase extends CheckerTestCase {
|
|||
public void assertContainedIn(String expected, String result) {
|
||||
assertTrue("Text <" + expected + "> not found in <" + result + ">", result.contains(expected)); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the quick fix to be used
|
||||
* @param quickFix
|
||||
*/
|
||||
public void setQuickFix(AbstractCodanCMarkerResolution quickFix) {
|
||||
this.quickFix = quickFix;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue