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;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||
|
@ -118,21 +120,26 @@ public class FunctionFactory {
|
|||
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
|
||||
CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
|
||||
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
|
||||
CPPASTFieldReference fieldRef = new CPPASTFieldReference();
|
||||
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
|
||||
litExpr.setValue(Keywords.cTHIS);
|
||||
fieldRef.setFieldOwner(litExpr);
|
||||
IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
|
||||
while (innerDeclarator.getNestedDeclarator() != null) {
|
||||
innerDeclarator = innerDeclarator.getNestedDeclarator();
|
||||
}
|
||||
IASTName fieldName = innerDeclarator.getName();
|
||||
fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
|
||||
fieldRef.setIsPointerDereference(true);
|
||||
binExpr.setOperand1(fieldRef);
|
||||
CPPASTName parameterName = getSetterParameterName(fieldName);
|
||||
if (Arrays.equals(fieldName.getSimpleID(), parameterName.getSimpleID())) {
|
||||
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);
|
||||
CPPASTIdExpression idExpr = new CPPASTIdExpression();
|
||||
idExpr.setName(fieldName.copy(CopyStyle.withLocations));
|
||||
CPPASTIdExpression idExpr = new CPPASTIdExpression(parameterName);
|
||||
binExpr.setOperand2(idExpr);
|
||||
exprStmt.setExpression(binExpr);
|
||||
compound.addStatement(exprStmt);
|
||||
|
@ -151,13 +158,20 @@ public class FunctionFactory {
|
|||
declarator.setName(setterName);
|
||||
}
|
||||
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(
|
||||
CopyStyle.withLocations));
|
||||
declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
|
||||
return declarator;
|
||||
}
|
||||
|
||||
private static CPPASTName getSetterParameterName(IASTName fieldName) {
|
||||
String parameterName = GetterSetterNameGenerator.generateSetterParameterName(fieldName);
|
||||
return new CPPASTName(parameterName.toCharArray());
|
||||
}
|
||||
|
||||
private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
|
||||
CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
|
||||
declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
|
||||
|
|
|
@ -62,7 +62,7 @@ public class GetterSetterNameGenerator {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static String generateSetterName(IASTName fieldName) {
|
||||
IPreferencesService preferences = Platform.getPreferencesService();
|
||||
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
|
||||
|
@ -79,6 +79,22 @@ public class GetterSetterNameGenerator {
|
|||
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.
|
||||
* 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