1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-01 13:25:45 +02:00

Code streamlining.

This commit is contained in:
Sergey Prigogin 2012-01-20 19:17:27 -08:00
parent 90d34eab05
commit 60214efa55
8 changed files with 104 additions and 132 deletions

View file

@ -62,7 +62,6 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
for (NameInformation nameInfo : refactoringInfo.getParameterCandidates()) { for (NameInformation nameInfo : refactoringInfo.getParameterCandidates()) {
if (returnValue.equals(String.valueOf(nameInfo.getName().getSimpleID()))) { if (returnValue.equals(String.valueOf(nameInfo.getName().getSimpleID()))) {
refactoringInfo.setReturnVariable(nameInfo); refactoringInfo.setReturnVariable(nameInfo);
nameInfo.setUserSetIsReference(false);
break; break;
} }
} }

View file

@ -18,6 +18,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
@ -66,13 +67,13 @@ public class NodeContainer {
private final List<IASTName> references; private final List<IASTName> references;
private List<IASTName> referencesAfterCached; private List<IASTName> referencesAfterCached;
private int lastCachedReferencesHash; private int lastCachedReferencesHash;
private boolean isOutput; private boolean mustBeOutput;
private boolean isReturnValue; private boolean mustBeReturnValue;
private boolean isConst; private boolean isConst;
private boolean isWriteAccess; private boolean isWriteAccess;
private boolean userSetIsReference; private boolean isOutput;
private boolean userSetIsReturnValue; private boolean isReturnValue;
private String userSetName; private String userSetName;
private int userOrder; private int userOrder;
@ -128,8 +129,7 @@ public class NodeContainer {
return !getReferencesAfterSelection().isEmpty(); return !getReferencesAfterSelection().isEmpty();
} }
public IASTParameterDeclaration getParameterDeclaration(boolean isReference, public IASTParameterDeclaration getParameterDeclaration(INodeFactory nodeFactory) {
INodeFactory nodeFactory) {
IASTDeclarator sourceDeclarator = (IASTDeclarator) getDeclaration().getParent(); IASTDeclarator sourceDeclarator = (IASTDeclarator) getDeclaration().getParent();
IASTDeclSpecifier declSpec= null; IASTDeclSpecifier declSpec= null;
@ -159,7 +159,8 @@ public class NodeContainer {
declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations)); declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations));
} }
if (isReference && !hasReferenceOperartor(declarator)) { boolean output = isOutput() && !isReturnValue();
if (output && !hasReferenceOperator(declarator)) {
if (nodeFactory instanceof ICPPNodeFactory) { if (nodeFactory instanceof ICPPNodeFactory) {
declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false)); declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false));
} else { } else {
@ -172,7 +173,7 @@ public class NodeContainer {
return nodeFactory.newParameterDeclaration(declSpec, declarator); return nodeFactory.newParameterDeclaration(declSpec, declarator);
} }
public boolean hasReferenceOperartor(IASTDeclarator declarator) { public boolean hasReferenceOperator(IASTDeclarator declarator) {
for (IASTPointerOperator pOp : declarator.getPointerOperators()) { for (IASTPointerOperator pOp : declarator.getPointerOperators()) {
if (pOp instanceof ICPPASTReferenceOperator) { if (pOp instanceof ICPPASTReferenceOperator) {
return true; return true;
@ -215,38 +216,40 @@ public class NodeContainer {
return name.toString() + (isDeclaredInSelection() ? " (declared inside)" : ""); //$NON-NLS-1$//$NON-NLS-2$ return name.toString() + (isDeclaredInSelection() ? " (declared inside)" : ""); //$NON-NLS-1$//$NON-NLS-2$
} }
public boolean isOutput() { public boolean mustBeOutput() {
return isOutput; return mustBeOutput;
} }
public void setOutput(boolean isOutput) { public void setMustBeOutput(boolean mustBeOutput) {
this.mustBeOutput = mustBeOutput;
}
public boolean isOutput() {
return mustBeOutput || isOutput;
}
public void setIsOutput(boolean isOutput) {
Assert.isTrue(isOutput || !mustBeOutput);
this.isOutput = isOutput; this.isOutput = isOutput;
} }
public boolean mustBeReturnValue() {
return mustBeReturnValue;
}
public void setMustBeReturnValue(boolean mustBeReturnValue) {
this.mustBeReturnValue = mustBeReturnValue;
}
public boolean isReturnValue() { public boolean isReturnValue() {
return isReturnValue; return mustBeReturnValue || isReturnValue;
} }
public void setReturnValue(boolean isReturnValue) { public void setReturnValue(boolean isReturnValue) {
Assert.isTrue(isReturnValue || !mustBeReturnValue);
this.isReturnValue = isReturnValue; this.isReturnValue = isReturnValue;
} }
public boolean isUserSetIsReference() {
return userSetIsReference;
}
public void setUserSetIsReference(boolean userSetIsReference) {
this.userSetIsReference = userSetIsReference;
}
public boolean isUserSetIsReturnValue() {
return userSetIsReturnValue;
}
public void setUserSetIsReturnValue(boolean userSetIsReturnValue) {
this.userSetIsReturnValue = userSetIsReturnValue;
}
public String getUserSetName() { public String getUserSetName() {
return userSetName; return userSetName;
} }
@ -360,7 +363,7 @@ public class NodeContainer {
if (declarations.add(nameInfo.getDeclaration())) { if (declarations.add(nameInfo.getDeclaration())) {
if (nameInfo.isDeclaredInSelection()) { if (nameInfo.isDeclaredInSelection()) {
if (nameInfo.isReferencedAfterSelection()) { if (nameInfo.isReferencedAfterSelection()) {
nameInfo.setReturnValue(true); nameInfo.setMustBeReturnValue(true);
interfaceNames.add(nameInfo); interfaceNames.add(nameInfo);
} }
} else { } else {
@ -374,7 +377,7 @@ public class NodeContainer {
} }
} }
if (nameInfo.isWriteAccess() && nameInfo.isReferencedAfterSelection()) { if (nameInfo.isWriteAccess() && nameInfo.isReferencedAfterSelection()) {
nameInfo.setOutput(true); nameInfo.setMustBeOutput(true);
} }
interfaceNames.add(nameInfo); interfaceNames.add(nameInfo);
} }
@ -389,7 +392,7 @@ public class NodeContainer {
List<NameInformation> selectedNames = null; List<NameInformation> selectedNames = null;
for (NameInformation nameInfo : getInterfaceNames()) { for (NameInformation nameInfo : getInterfaceNames()) {
if (nameInfo.isReturnValue() == isReturnValue) { if (nameInfo.mustBeReturnValue() == isReturnValue) {
if (selectedNames == null) { if (selectedNames == null) {
selectedNames = new ArrayList<NameInformation>(); selectedNames = new ArrayList<NameInformation>();
} }

View file

@ -12,7 +12,6 @@
package org.eclipse.cdt.internal.ui.refactoring.extractfunction; package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.custom.TableEditor;
@ -37,16 +36,12 @@ public class ChooserComposite extends Composite {
private static final String COLUMN_NAME = Messages.ChooserComposite_Name; private static final String COLUMN_NAME = Messages.ChooserComposite_Name;
private static final String COLUMN_TYPE = Messages.ChooserComposite_Type; private static final String COLUMN_TYPE = Messages.ChooserComposite_Type;
private Button voidReturn; private Button checkboxVoidReturn;
private final ExtractFunctionInputPage page;
public ChooserComposite(Composite parent, final ExtractFunctionInformation info, public ChooserComposite(Composite parent, final ExtractFunctionInformation info,
ExtractFunctionInputPage page) { ExtractFunctionInputPage page) {
super(parent, SWT.NONE); super(parent, SWT.NONE);
this.page = page;
GridLayout layout = new GridLayout(); GridLayout layout = new GridLayout();
setLayout(layout); setLayout(layout);
@ -85,116 +80,115 @@ public class ChooserComposite extends Composite {
// Button // Button
editor = new TableEditor(table); editor = new TableEditor(table);
final Button referenceButton = new Button(table, SWT.CHECK); final Button buttonOutput = new Button(table, SWT.CHECK);
if (name.hasReferenceOperartor((IASTDeclarator) name.getDeclaration().getParent())) { if (name.hasReferenceOperator((IASTDeclarator) name.getDeclaration().getParent())) {
referenceButton.setSelection(true); buttonOutput.setSelection(true);
referenceButton.setEnabled(false); buttonOutput.setEnabled(false);
} else { } else {
referenceButton.setSelection(name.isOutput()); buttonOutput.setSelection(name.isOutput() && !name.isReturnValue());
buttonOutput.setEnabled(!name.mustBeOutput() && !name.isReturnValue());
} }
referenceButton.setBackground(table.getBackground()); buttonOutput.setBackground(table.getBackground());
referenceButton.addSelectionListener(new SelectionListener() { buttonOutput.addSelectionListener(new SelectionListener() {
@Override @Override
public void widgetDefaultSelected(SelectionEvent e) { public void widgetDefaultSelected(SelectionEvent e) {
name.setUserSetIsReference(referenceButton.getSelection()); widgetSelected(e);
onVisibilityOrReturnChange(info.getParameterCandidates());
} }
@Override @Override
public void widgetSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) {
widgetDefaultSelected(e); if (buttonOutput.isEnabled()) {
name.setIsOutput(buttonOutput.getSelection());
}
} }
}); });
referenceButton.pack(); buttonOutput.pack();
editor.minimumWidth = referenceButton.getSize().x; editor.minimumWidth = buttonOutput.getSize().x;
editor.horizontalAlignment = SWT.CENTER; editor.horizontalAlignment = SWT.CENTER;
referenceButtons.add(referenceButton); referenceButtons.add(buttonOutput);
editor.setEditor(referenceButton, item, columnIndex++); editor.setEditor(buttonOutput, item, columnIndex++);
// Cosnt Button // Const button
editor = new TableEditor(table); editor = new TableEditor(table);
final Button constButton = new Button(table, SWT.CHECK); final Button buttonConst = new Button(table, SWT.CHECK);
constButton.setSelection(name.isConst()); buttonConst.setSelection(name.isConst());
constButton.setEnabled(!name.isWriteAccess()); buttonConst.setEnabled(!name.isWriteAccess());
constButton.setBackground(table.getBackground()); buttonConst.setBackground(table.getBackground());
constButton.addSelectionListener(new SelectionListener() { buttonConst.addSelectionListener(new SelectionListener() {
@Override @Override
public void widgetDefaultSelected(SelectionEvent e) { public void widgetDefaultSelected(SelectionEvent e) {
name.setConst(constButton.getSelection()); widgetSelected(e);
onVisibilityOrReturnChange(info.getParameterCandidates());
} }
@Override @Override
public void widgetSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) {
widgetDefaultSelected(e); name.setConst(buttonConst.getSelection());
} }
}); });
constButton.pack(); buttonConst.pack();
editor.minimumWidth = constButton.getSize().x; editor.minimumWidth = buttonConst.getSize().x;
editor.horizontalAlignment = SWT.CENTER; editor.horizontalAlignment = SWT.CENTER;
// referenceButtons.add(referenceButton); // referenceButtons.add(referenceButton);
editor.setEditor(constButton, item, columnIndex++); editor.setEditor(buttonConst, item, columnIndex++);
if (info.isExtractExpression()) if (info.isExtractExpression())
continue; // Skip the return radiobutton continue; // Skip the return radiobutton
// Button // Button
editor = new TableEditor(table); editor = new TableEditor(table);
final Button returnButton = new Button(table, SWT.RADIO); final Button buttonReturn = new Button(table, SWT.RADIO);
returnButton.setSelection(name.isReturnValue()); buttonReturn.setSelection(name.mustBeReturnValue());
name.setUserSetIsReference(name.isOutput()); buttonReturn.setEnabled(info.getMandatoryReturnVariable() == null);
returnButton.setEnabled(info.getMandatoryReturnVariable() == null); buttonReturn.setBackground(table.getBackground());
returnButton.setBackground(table.getBackground()); buttonReturn.addSelectionListener(new SelectionListener() {
returnButton.addSelectionListener(new SelectionListener() {
@Override @Override
public void widgetDefaultSelected(SelectionEvent e) { public void widgetDefaultSelected(SelectionEvent e) {
name.setUserSetIsReturnValue(returnButton.getSelection()); widgetSelected(e);
if (returnButton.getSelection()) {
referenceButton.setSelection(false);
referenceButton.notifyListeners(SWT.Selection, new Event());
} else if (name.isOutput()) {
referenceButton.setSelection(true);
referenceButton.notifyListeners(SWT.Selection, new Event());
}
onVisibilityOrReturnChange(info.getParameterCandidates());
} }
@Override @Override
public void widgetSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) {
widgetDefaultSelected(e); name.setReturnValue(buttonReturn.getSelection());
if (buttonReturn.getSelection()) {
buttonOutput.setSelection(false);
buttonOutput.notifyListeners(SWT.Selection, new Event());
} else if (name.mustBeOutput()) {
buttonOutput.setSelection(true);
buttonOutput.notifyListeners(SWT.Selection, new Event());
}
} }
}); });
returnButton.pack(); buttonReturn.pack();
editor.minimumWidth = returnButton.getSize().x; editor.minimumWidth = buttonReturn.getSize().x;
editor.horizontalAlignment = SWT.CENTER; editor.horizontalAlignment = SWT.CENTER;
returnButtons.add(returnButton); returnButtons.add(buttonReturn);
editor.setEditor(returnButton, item, columnIndex++); editor.setEditor(buttonReturn, item, columnIndex++);
} }
} }
if (!info.isExtractExpression()) { if (!info.isExtractExpression()) {
voidReturn = new Button(parent, SWT.CHECK | SWT.LEFT); checkboxVoidReturn = new Button(parent, SWT.CHECK | SWT.LEFT);
voidReturn.setText(Messages.ChooserComposite_NoReturnValue); checkboxVoidReturn.setText(Messages.ChooserComposite_NoReturnValue);
voidReturn.setEnabled(info.getMandatoryReturnVariable() == null); checkboxVoidReturn.setEnabled(info.getMandatoryReturnVariable() == null);
voidReturn.addSelectionListener(new SelectionListener() { checkboxVoidReturn.addSelectionListener(new SelectionListener() {
@Override @Override
public void widgetDefaultSelected(SelectionEvent e) { public void widgetDefaultSelected(SelectionEvent e) {
info.setReturnVariable(null); widgetSelected(e);
for (Button button : returnButtons) {
if (voidReturn.getSelection()) {
button.setSelection(false);
button.notifyListeners(SWT.Selection, new Event());
}
button.setEnabled(!voidReturn.getSelection());
}
} }
@Override @Override
public void widgetSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) {
widgetDefaultSelected(e); info.setReturnVariable(null);
for (Button button : returnButtons) {
if (checkboxVoidReturn.getSelection()) {
button.setSelection(false);
button.notifyListeners(SWT.Selection, new Event());
}
button.setEnabled(!checkboxVoidReturn.getSelection());
}
} }
}); });
} }
@ -207,16 +201,4 @@ public class ChooserComposite extends Composite {
column.setText(string); column.setText(string);
column.setWidth(100); column.setWidth(100);
} }
void onVisibilityOrReturnChange(List<NameInformation> names) {
String variableUsedAfterBlock = null;
for (NameInformation information : names) {
if (information.isReferencedAfterSelection() &&
!(information.isUserSetIsReference() || information.isUserSetIsReturnValue())) {
variableUsedAfterBlock = information.getName().toString();
}
}
page.errorWithAfterUsedVariable(variableUsedAfterBlock);
}
} }

View file

@ -64,11 +64,11 @@ public class ExtractFunctionInformation {
return returnVariable; return returnVariable;
} }
public void setReturnVariable(NameInformation returnVariable) { public void setReturnVariable(NameInformation variable) {
if (returnVariable != null) { if (variable != null) {
returnVariable.setUserSetIsReturnValue(true); variable.setReturnValue(true);
} }
this.returnVariable = returnVariable; this.returnVariable = variable;
} }
public NameInformation getMandatoryReturnVariable() { public NameInformation getMandatoryReturnVariable() {
@ -76,6 +76,9 @@ public class ExtractFunctionInformation {
} }
public void setMandatoryReturnVariable(NameInformation variable) { public void setMandatoryReturnVariable(NameInformation variable) {
if (variable != null) {
variable.setMustBeReturnValue(true);
}
this.mandatoryReturnVariable = variable; this.mandatoryReturnVariable = variable;
this.returnVariable = variable; this.returnVariable = variable;
} }

View file

@ -91,13 +91,4 @@ public class ExtractFunctionInputPage extends UserInputWizardPage {
setPageComplete(false); setPageComplete(false);
} }
} }
public void errorWithAfterUsedVariable(String variableUsedAfterBlock ) {
if (variableUsedAfterBlock == null) {
setErrorMessage(null);
checkName();
} else {
setErrorMessage("The parameter '" + variableUsedAfterBlock + "' " + Messages.ExtractFunctionInputPage_1); //$NON-NLS-1$ //$NON-NLS-2$
}
}
} }

View file

@ -194,12 +194,6 @@ public class ExtractFunctionRefactoring extends CRefactoring {
NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex()); NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
info.setMethodContext(context); info.setMethodContext(context);
if (info.getMandatoryReturnVariable() != null) {
info.getMandatoryReturnVariable().setUserSetIsReturnValue(true);
}
for (NameInformation name : info.getParameterCandidates()) {
name.setUserSetIsReference(name.isOutput());
}
sm.done(); sm.done();
} finally { } finally {
unlockIndex(); unlockIndex();
@ -270,7 +264,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
} }
} }
for (NameInformation name : info.getParameterCandidates()) { for (NameInformation name : info.getParameterCandidates()) {
if (name.isUserSetIsReturnValue()) { if (name.isReturnValue()) {
info.setReturnVariable(name); info.setReturnVariable(name);
} }
} }

View file

@ -95,7 +95,7 @@ public abstract class ExtractedFunctionConstructionHelper {
Collection<NameInformation> parameterNames, INodeFactory nodeFactory) { Collection<NameInformation> parameterNames, INodeFactory nodeFactory) {
List<IASTParameterDeclaration> result = new ArrayList<IASTParameterDeclaration>(parameterNames.size()); List<IASTParameterDeclaration> result = new ArrayList<IASTParameterDeclaration>(parameterNames.size());
for (NameInformation name : parameterNames) { for (NameInformation name : parameterNames) {
result.add(name.getParameterDeclaration(name.isUserSetIsReference(), nodeFactory)); result.add(name.getParameterDeclaration(nodeFactory));
} }
return result; return result;
} }

View file

@ -79,7 +79,7 @@ final class SimilarFinderVisitor extends ASTVisitor {
if (orgName != null) { if (orgName != null) {
for (NameInformation orgNameInfo : refactoring.container.getParameterCandidates()) { for (NameInformation orgNameInfo : refactoring.container.getParameterCandidates()) {
if (orgName.equals(orgNameInfo.getDeclaration().getRawSignature()) && if (orgName.equals(orgNameInfo.getDeclaration().getRawSignature()) &&
(orgNameInfo.isOutput() || !nameInfo.isOutput())) { (orgNameInfo.mustBeOutput() || !nameInfo.mustBeOutput())) {
found = true; found = true;
break; break;
} }