From f026d893de6ebbc4f27c32aa80a53a14342980af Mon Sep 17 00:00:00 2001 From: sprigogin Date: Thu, 14 Jul 2011 14:35:42 -0700 Subject: [PATCH] Make setter parameter follow the name style preference. --- .../gettersandsetters/FunctionFactory.java | 34 +++++++++++++------ .../GetterSetterNameGenerator.java | 18 +++++++++- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java index b8f7de5d12f..83fed4468a9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java @@ -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); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java index 45b68f479d4..0b721b69eee 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterNameGenerator.java @@ -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