mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +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
f636cb6bcc
commit
f434c8c556
8 changed files with 184 additions and 10 deletions
|
@ -9,6 +9,10 @@
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFix"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFix"
|
||||||
problemId="org.eclipse.cdt.codan.internal.checkers.CatchByReference">
|
problemId="org.eclipse.cdt.codan.internal.checkers.CatchByReference">
|
||||||
</resolution>
|
</resolution>
|
||||||
|
<resolution
|
||||||
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByConstReferenceQuickFix"
|
||||||
|
problemId="org.eclipse.cdt.codan.internal.checkers.CatchByReference">
|
||||||
|
</resolution>
|
||||||
<resolution
|
<resolution
|
||||||
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">
|
||||||
|
|
|
@ -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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -17,6 +17,7 @@ import org.eclipse.osgi.util.NLS;
|
||||||
*/
|
*/
|
||||||
public class Messages extends NLS {
|
public class Messages extends NLS {
|
||||||
public static String CatchByReferenceQuickFix_Message;
|
public static String CatchByReferenceQuickFix_Message;
|
||||||
|
public static String CatchByConstReferenceQuickFix_Message;
|
||||||
public static String QuickFixAssignmentInCondition_Message;
|
public static String QuickFixAssignmentInCondition_Message;
|
||||||
public static String SuggestedParenthesisQuickFix_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
|
# All rights reserved. This program and the accompanying materials
|
||||||
# are made available under the terms of the Eclipse Public License v1.0
|
# are made available under the terms of the Eclipse Public License v1.0
|
||||||
# which accompanies this distribution, and is available at
|
# which accompanies this distribution, and is available at
|
||||||
|
@ -9,5 +9,6 @@
|
||||||
# Alena Laskavaia - initial API and implementation
|
# Alena Laskavaia - initial API and implementation
|
||||||
###############################################################################
|
###############################################################################
|
||||||
CatchByReferenceQuickFix_Message=Change to use '&'
|
CatchByReferenceQuickFix_Message=Change to use '&'
|
||||||
|
CatchByConstReferenceQuickFix_Message=Change to use 'const &'
|
||||||
QuickFixAssignmentInCondition_Message=Change to '=='
|
QuickFixAssignmentInCondition_Message=Change to '=='
|
||||||
SuggestedParenthesisQuickFix_Message=Surround with '()'
|
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
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* 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.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||||
import org.eclipse.core.resources.IMarker;
|
import org.eclipse.core.resources.IMarker;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
|
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,15 +26,47 @@ public class CatchByReferenceQuickFix extends AbstractCodanCMarkerResolution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void apply(IMarker marker, IDocument document) {
|
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 {
|
try {
|
||||||
int pos = getOffset(marker, document);
|
int left = marker.getAttribute(IMarker.CHAR_START, -1);
|
||||||
dad.find(pos, " ", /* forwardSearch *///$NON-NLS-1$
|
int right = marker.getAttribute(IMarker.CHAR_END, -1);
|
||||||
true, /* caseSensitive */false,
|
String inStr = document.get(left, right - left);
|
||||||
/* wholeWord */false, /* regExSearch */false);
|
document.replace(left, right - left, getCatchByReferenceString(inStr, addConst));
|
||||||
dad.replace(" & ", /* regExReplace */false); //$NON-NLS-1$
|
|
||||||
} catch (BadLocationException e) {
|
} catch (BadLocationException e) {
|
||||||
CheckersUiActivator.log(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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,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.SuggestedParenthesisCheckerTest;
|
||||||
import org.eclipse.cdt.codan.core.internal.checkers.SuspiciousSemicolonCheckerTest;
|
import org.eclipse.cdt.codan.core.internal.checkers.SuspiciousSemicolonCheckerTest;
|
||||||
import org.eclipse.cdt.codan.core.internal.checkers.UnusedSymbolInFileScopeCheckerTest;
|
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.CreateLocalVariableQuickFixTest;
|
||||||
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
|
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
|
||||||
|
|
||||||
|
@ -70,6 +71,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
|
||||||
// quick fixes
|
// quick fixes
|
||||||
suite.addTestSuite(CreateLocalVariableQuickFixTest.class);
|
suite.addTestSuite(CreateLocalVariableQuickFixTest.class);
|
||||||
suite.addTestSuite(SuggestedParenthesisQuickFixTest.class);
|
suite.addTestSuite(SuggestedParenthesisQuickFixTest.class);
|
||||||
|
suite.addTestSuite(CatchByReferenceQuickFixTest.class);
|
||||||
return suite;
|
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) {
|
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$
|
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