mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-01 06:05:24 +02:00
Default name for extract constant factory, by Emanuel Graf, bug 222379.
This commit is contained in:
parent
9a46b41c3b
commit
8d07be5f20
3 changed files with 31 additions and 11 deletions
|
@ -47,7 +47,7 @@ public abstract class ExtractInputPage extends UserInputWizardPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createControl(Composite parent) {
|
public void createControl(Composite parent) {
|
||||||
control = new NameAndVisibilityComposite(parent, label);
|
control = new NameAndVisibilityComposite(parent, label, info.getName());
|
||||||
setTitle(getName());
|
setTitle(getName());
|
||||||
setPageComplete(false);
|
setPageComplete(false);
|
||||||
control.getConstantNameText().addKeyListener(new KeyAdapter(){
|
control.getConstantNameText().addKeyListener(new KeyAdapter(){
|
||||||
|
|
|
@ -11,10 +11,6 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.dialogs;
|
package org.eclipse.cdt.internal.ui.refactoring.dialogs;
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Thomas Corbat
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
import org.eclipse.swt.SWT;
|
import org.eclipse.swt.SWT;
|
||||||
import org.eclipse.swt.layout.FillLayout;
|
import org.eclipse.swt.layout.FillLayout;
|
||||||
import org.eclipse.swt.layout.GridData;
|
import org.eclipse.swt.layout.GridData;
|
||||||
|
@ -37,11 +33,11 @@ public class NameAndVisibilityComposite extends Composite {
|
||||||
private final String labelName;
|
private final String labelName;
|
||||||
private final VisibilitySelectionPanel visibilityPanel;
|
private final VisibilitySelectionPanel visibilityPanel;
|
||||||
|
|
||||||
public NameAndVisibilityComposite(Composite parent, String labelName) {
|
public NameAndVisibilityComposite(Composite parent, String labelName, String defaultName) {
|
||||||
this(parent, labelName, VisibilityEnum.v_public);
|
this(parent, labelName, VisibilityEnum.v_public, defaultName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NameAndVisibilityComposite(Composite parent, String labelName, VisibilityEnum defaultVisibility){
|
public NameAndVisibilityComposite(Composite parent, String labelName, VisibilityEnum defaultVisibility, String defaultName){
|
||||||
|
|
||||||
super(parent, SWT.NONE);
|
super(parent, SWT.NONE);
|
||||||
|
|
||||||
|
@ -49,7 +45,7 @@ public class NameAndVisibilityComposite extends Composite {
|
||||||
|
|
||||||
setLayout(new GridLayout());
|
setLayout(new GridLayout());
|
||||||
|
|
||||||
createNewMethodNameComposite(this);
|
createNewMethodNameComposite(this, defaultName);
|
||||||
visibilityPanel = new VisibilitySelectionPanel(this, defaultVisibility,SWT.NONE);
|
visibilityPanel = new VisibilitySelectionPanel(this, defaultVisibility,SWT.NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,13 +63,13 @@ public class NameAndVisibilityComposite extends Composite {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void createNewMethodNameComposite(Composite control) {
|
private void createNewMethodNameComposite(Composite control, String defaultName) {
|
||||||
Composite methodNameComposite = new Composite(control, SWT.NONE);
|
Composite methodNameComposite = new Composite(control, SWT.NONE);
|
||||||
FillLayout compositeLayout = new FillLayout(SWT.HORIZONTAL);
|
FillLayout compositeLayout = new FillLayout(SWT.HORIZONTAL);
|
||||||
GridData gridData = new GridData(SWT.FILL,SWT.BEGINNING, true, false);
|
GridData gridData = new GridData(SWT.FILL,SWT.BEGINNING, true, false);
|
||||||
gridData.horizontalAlignment = GridData.FILL;
|
gridData.horizontalAlignment = GridData.FILL;
|
||||||
methodNameComposite.setLayoutData(gridData);
|
methodNameComposite.setLayoutData(gridData);
|
||||||
methodNameComposite.setLayout(compositeLayout);
|
methodNameComposite.setLayout(compositeLayout);
|
||||||
constantName = new LabeledTextField(methodNameComposite, labelName);
|
constantName = new LabeledTextField(methodNameComposite, labelName, defaultName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,10 +133,34 @@ public class ExtractConstantRefactoring extends CRefactoring {
|
||||||
findAllNodesForReplacement(literalExpressionVector);
|
findAllNodesForReplacement(literalExpressionVector);
|
||||||
|
|
||||||
info.addNamesToUsedNames(findAllDeclaredNames());
|
info.addNamesToUsedNames(findAllDeclaredNames());
|
||||||
|
info.setName(getDefaultName(target));
|
||||||
sm.done();
|
sm.done();
|
||||||
return initStatus;
|
return initStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getDefaultName(IASTLiteralExpression literal) {
|
||||||
|
String nameString = literal.toString();
|
||||||
|
switch (literal.getKind()) {
|
||||||
|
case IASTLiteralExpression.lk_char_constant:
|
||||||
|
case ICPPASTLiteralExpression.lk_string_literal:
|
||||||
|
int beginIndex = 1;
|
||||||
|
if(nameString.startsWith("L")) { //$NON-NLS-1$
|
||||||
|
beginIndex = 2;
|
||||||
|
}
|
||||||
|
final int len= nameString.length();
|
||||||
|
if (beginIndex < len && len > 0) {
|
||||||
|
nameString = nameString.substring(beginIndex, len-1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
nameString = nameString.replaceAll("[\\W]", "_"); //$NON-NLS-1$//$NON-NLS-2$
|
||||||
|
return '_' + nameString;
|
||||||
|
}
|
||||||
|
|
||||||
private Vector<String> findAllDeclaredNames() {
|
private Vector<String> findAllDeclaredNames() {
|
||||||
Vector<String>names = new Vector<String>();
|
Vector<String>names = new Vector<String>();
|
||||||
IASTFunctionDefinition funcDef = getFunctionDefinition();
|
IASTFunctionDefinition funcDef = getFunctionDefinition();
|
||||||
|
|
Loading…
Add table
Reference in a new issue