mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Make setter parameter follow the name style preference.
This commit is contained in:
parent
4712cb1e66
commit
f026d893de
2 changed files with 41 additions and 11 deletions
|
@ -12,6 +12,8 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
|
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||||
|
@ -118,21 +120,26 @@ public class FunctionFactory {
|
||||||
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
|
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
|
||||||
CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
|
CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
|
||||||
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
|
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
|
||||||
CPPASTFieldReference fieldRef = new CPPASTFieldReference();
|
|
||||||
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
|
|
||||||
litExpr.setValue(Keywords.cTHIS);
|
|
||||||
fieldRef.setFieldOwner(litExpr);
|
|
||||||
IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
|
IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
|
||||||
while (innerDeclarator.getNestedDeclarator() != null) {
|
while (innerDeclarator.getNestedDeclarator() != null) {
|
||||||
innerDeclarator = innerDeclarator.getNestedDeclarator();
|
innerDeclarator = innerDeclarator.getNestedDeclarator();
|
||||||
}
|
}
|
||||||
IASTName fieldName = innerDeclarator.getName();
|
IASTName fieldName = innerDeclarator.getName();
|
||||||
fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
|
CPPASTName parameterName = getSetterParameterName(fieldName);
|
||||||
fieldRef.setIsPointerDereference(true);
|
if (Arrays.equals(fieldName.getSimpleID(), parameterName.getSimpleID())) {
|
||||||
binExpr.setOperand1(fieldRef);
|
CPPASTFieldReference fieldRef = new CPPASTFieldReference();
|
||||||
|
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
|
||||||
|
litExpr.setValue(Keywords.cTHIS);
|
||||||
|
fieldRef.setFieldOwner(litExpr);
|
||||||
|
fieldRef.setIsPointerDereference(true);
|
||||||
|
fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
|
||||||
|
binExpr.setOperand1(fieldRef);
|
||||||
|
} else {
|
||||||
|
CPPASTIdExpression idExpr = new CPPASTIdExpression(fieldName.copy(CopyStyle.withLocations));
|
||||||
|
binExpr.setOperand1(idExpr);
|
||||||
|
}
|
||||||
binExpr.setOperator(IASTBinaryExpression.op_assign);
|
binExpr.setOperator(IASTBinaryExpression.op_assign);
|
||||||
CPPASTIdExpression idExpr = new CPPASTIdExpression();
|
CPPASTIdExpression idExpr = new CPPASTIdExpression(parameterName);
|
||||||
idExpr.setName(fieldName.copy(CopyStyle.withLocations));
|
|
||||||
binExpr.setOperand2(idExpr);
|
binExpr.setOperand2(idExpr);
|
||||||
exprStmt.setExpression(binExpr);
|
exprStmt.setExpression(binExpr);
|
||||||
compound.addStatement(exprStmt);
|
compound.addStatement(exprStmt);
|
||||||
|
@ -151,13 +158,20 @@ public class FunctionFactory {
|
||||||
declarator.setName(setterName);
|
declarator.setName(setterName);
|
||||||
}
|
}
|
||||||
CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration();
|
CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration();
|
||||||
parameterDeclaration.setDeclarator(fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations));
|
IASTDeclarator parameterDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
|
||||||
|
parameterDeclarator.setName(getSetterParameterName(fieldName));
|
||||||
|
parameterDeclaration.setDeclarator(parameterDeclarator);
|
||||||
parameterDeclaration.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy(
|
parameterDeclaration.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy(
|
||||||
CopyStyle.withLocations));
|
CopyStyle.withLocations));
|
||||||
declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
|
declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
|
||||||
return declarator;
|
return declarator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CPPASTName getSetterParameterName(IASTName fieldName) {
|
||||||
|
String parameterName = GetterSetterNameGenerator.generateSetterParameterName(fieldName);
|
||||||
|
return new CPPASTName(parameterName.toCharArray());
|
||||||
|
}
|
||||||
|
|
||||||
private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
|
private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
|
||||||
CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
|
CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
|
||||||
declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
|
declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
|
||||||
|
|
|
@ -62,7 +62,7 @@ public class GetterSetterNameGenerator {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String generateSetterName(IASTName fieldName) {
|
public static String generateSetterName(IASTName fieldName) {
|
||||||
IPreferencesService preferences = Platform.getPreferencesService();
|
IPreferencesService preferences = Platform.getPreferencesService();
|
||||||
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
|
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
|
||||||
|
@ -79,6 +79,22 @@ public class GetterSetterNameGenerator {
|
||||||
return composer.compose(name);
|
return composer.compose(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String generateSetterParameterName(IASTName fieldName) {
|
||||||
|
IPreferencesService preferences = Platform.getPreferencesService();
|
||||||
|
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
|
||||||
|
PreferenceConstants.NAME_STYLE_VARIABLE_CAPITALIZATION,
|
||||||
|
PreferenceConstants.NAME_STYLE_CAPITALIZATION_ORIGINAL, null);
|
||||||
|
String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID,
|
||||||
|
PreferenceConstants.NAME_STYLE_VARIABLE_WORD_DELIMITER, "", null); //$NON-NLS-1$
|
||||||
|
String prefix = preferences.getString(CUIPlugin.PLUGIN_ID,
|
||||||
|
PreferenceConstants.NAME_STYLE_VARIABLE_PREFIX, "", null); //$NON-NLS-1$
|
||||||
|
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
|
||||||
|
PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX, "", null); //$NON-NLS-1$
|
||||||
|
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
|
||||||
|
String name = GetterSetterNameGenerator.trimFieldName(fieldName.toString());
|
||||||
|
return composer.compose(name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the trimmed field name. Leading and trailing non-alphanumeric characters are trimmed.
|
* Returns the trimmed field name. Leading and trailing non-alphanumeric characters are trimmed.
|
||||||
* If the first word of the name consists of a single letter and the name contains more than
|
* If the first word of the name consists of a single letter and the name contains more than
|
||||||
|
|
Loading…
Add table
Reference in a new issue