1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 09:25:31 +02:00

Configurable name style for constant and variable names.

This commit is contained in:
Sergey Prigogin 2011-05-13 06:17:30 +00:00
parent c5a85e4b4d
commit 6c51aa8adf
6 changed files with 121 additions and 35 deletions

View file

@ -48,9 +48,10 @@ import org.eclipse.cdt.internal.ui.wizards.dialogfields.TreeListDialogField;
* The preference block for configuring styles of names.
*/
public class NameStyleBlock extends OptionsConfigurationBlock {
// private static final String EXAMPLE_CONSTANT_NAME = "MY_CONSTANT"; //$NON-NLS-1$
private static final String EXAMPLE_CLASS_NAME = "MyClass"; //$NON-NLS-1$
private static final String EXAMPLE_CONSTANT_NAME = "MY_CONSTANT"; //$NON-NLS-1$
private static final String EXAMPLE_VARIABLE_NAME = "myVariable"; //$NON-NLS-1$
private static final String EXAMPLE_FIELD_NAME = "myField"; //$NON-NLS-1$
private static final String EXAMPLE_CLASS_NAME = "MyClass"; //$NON-NLS-1$
private final String[] CAPITALIZATION_VALUES = {
String.valueOf(PreferenceConstants.NAME_STYLE_CAPITALIZATION_ORIGINAL),
@ -72,6 +73,10 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
private static final Key KEY_CONSTANT_WORD_DELIMITER = getCDTUIKey(PreferenceConstants.NAME_STYLE_CONSTANT_WORD_DELIMITER);
private static final Key KEY_CONSTANT_PREFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_CONSTANT_PREFIX);
private static final Key KEY_CONSTANT_SUFFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_CONSTANT_SUFFIX);
private static final Key KEY_VARIABLE_CAPITALIZATION = getCDTUIKey(PreferenceConstants.NAME_STYLE_VARIABLE_CAPITALIZATION);
private static final Key KEY_VARIABLE_WORD_DELIMITER = getCDTUIKey(PreferenceConstants.NAME_STYLE_VARIABLE_WORD_DELIMITER);
private static final Key KEY_VARIABLE_PREFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_VARIABLE_PREFIX);
private static final Key KEY_VARIABLE_SUFFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX);
private static final Key KEY_FIELD_CAPITALIZATION = getCDTUIKey(PreferenceConstants.NAME_STYLE_FIELD_CAPITALIZATION);
private static final Key KEY_FIELD_WORD_DELIMITER = getCDTUIKey(PreferenceConstants.NAME_STYLE_FIELD_WORD_DELIMITER);
private static final Key KEY_FIELD_PREFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_FIELD_PREFIX);
@ -107,6 +112,10 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
KEY_CONSTANT_WORD_DELIMITER,
KEY_CONSTANT_PREFIX,
KEY_CONSTANT_SUFFIX,
KEY_VARIABLE_CAPITALIZATION,
KEY_VARIABLE_WORD_DELIMITER,
KEY_VARIABLE_PREFIX,
KEY_VARIABLE_SUFFIX,
KEY_FIELD_CAPITALIZATION,
KEY_FIELD_WORD_DELIMITER,
KEY_FIELD_PREFIX,
@ -149,18 +158,25 @@ public class NameStyleBlock extends OptionsConfigurationBlock {
private static Category[] createCategories() {
Category codeCategory = new Category(PreferencesMessages.NameStyleBlock_code_node);
// new Category(PreferencesMessages.NameStyleBlock_constant_node,
// PreferencesMessages.NameStyleBlock_constant_node_description, EXAMPLE_CONSTANT_NAME,
// null)
// .setCapitalizationKey(KEY_CONSTANT_CAPITALIZATION)
// .setWordDelimiterKey(KEY_CONSTANT_WORD_DELIMITER)
// .setPrefixKey(KEY_CONSTANT_PREFIX)
// .setSuffixKey(KEY_CONSTANT_SUFFIX)
// .setNameValidator(IDENTIFIER_VALIDATOR);
// TODO(sprigogin): Unhide the field name style category
new Category(PreferencesMessages.NameStyleBlock_constant_node,
PreferencesMessages.NameStyleBlock_constant_node_description, EXAMPLE_CONSTANT_NAME,
codeCategory)
.setCapitalizationKey(KEY_CONSTANT_CAPITALIZATION)
.setWordDelimiterKey(KEY_CONSTANT_WORD_DELIMITER)
.setPrefixKey(KEY_CONSTANT_PREFIX)
.setSuffixKey(KEY_CONSTANT_SUFFIX)
.setNameValidator(IDENTIFIER_VALIDATOR);
new Category(PreferencesMessages.NameStyleBlock_variable_node,
PreferencesMessages.NameStyleBlock_variable_node_description, EXAMPLE_VARIABLE_NAME,
codeCategory)
.setCapitalizationKey(KEY_VARIABLE_CAPITALIZATION)
.setWordDelimiterKey(KEY_VARIABLE_WORD_DELIMITER)
.setPrefixKey(KEY_VARIABLE_PREFIX)
.setSuffixKey(KEY_VARIABLE_SUFFIX)
.setNameValidator(IDENTIFIER_VALIDATOR);
Category fieldCategory = new Category(PreferencesMessages.NameStyleBlock_field_node,
PreferencesMessages.NameStyleBlock_field_node_description, EXAMPLE_FIELD_NAME,
null) // Hidden for now.
codeCategory)
.setCapitalizationKey(KEY_FIELD_CAPITALIZATION)
.setWordDelimiterKey(KEY_FIELD_WORD_DELIMITER)
.setPrefixKey(KEY_FIELD_PREFIX)

View file

@ -379,6 +379,8 @@ public final class PreferencesMessages extends NLS {
public static String NameStyleBlock_files_node;
public static String NameStyleBlock_constant_node;
public static String NameStyleBlock_constant_node_description;
public static String NameStyleBlock_variable_node;
public static String NameStyleBlock_variable_node_description;
public static String NameStyleBlock_field_node;
public static String NameStyleBlock_field_node_description;
public static String NameStyleBlock_getter_node;

View file

@ -440,6 +440,8 @@ NameStyleBlock_code_node=Code
NameStyleBlock_files_node=Files
NameStyleBlock_constant_node=Constant
NameStyleBlock_constant_node_description=Constant name
NameStyleBlock_variable_node=Variable
NameStyleBlock_variable_node_description=Variable name
NameStyleBlock_field_node=Class field
NameStyleBlock_field_node_description=Class field name
NameStyleBlock_getter_node=Getter Method

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@ -8,6 +8,7 @@
*
* Contributors:
* Institute for Software - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
@ -23,7 +24,9 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
@ -52,6 +55,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTEqualsInitializer;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
@ -65,9 +70,11 @@ import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringDescription;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterNameGenerator;
import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper;
import org.eclipse.cdt.internal.ui.util.NameComposer;
/**
* The central class of the Extract Constant Refactoring. Does all the work like checking pre- and
@ -154,16 +161,26 @@ public class ExtractConstantRefactoring extends CRefactoring {
}
final int len= nameString.length();
if (beginIndex < len && len > 0) {
nameString = nameString.substring(beginIndex, len-1);
nameString = nameString.substring(beginIndex, len - 1);
}
break;
default:
break;
}
nameString = nameString.replaceAll("[\\W]", "_"); //$NON-NLS-1$//$NON-NLS-2$
return '_' + nameString;
IPreferencesService preferences = Platform.getPreferencesService();
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_CONSTANT_CAPITALIZATION,
PreferenceConstants.NAME_STYLE_CAPITALIZATION_UPPER_CASE, null);
String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_CONSTANT_WORD_DELIMITER, "_", null); //$NON-NLS-1$
String prefix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_CONSTANT_PREFIX, "", null); //$NON-NLS-1$
String suffix = preferences.getString(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_CONSTANT_SUFFIX, "", null); //$NON-NLS-1$
NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix);
return composer.compose(nameString);
}
private ArrayList<String> findAllDeclaredNames() {
@ -302,7 +319,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
}
}
//Create all Changes for literals
// Create all Changes for literals
String constName = info.getName();
createLiteralToConstantChanges(constName, locLiteralsToReplace, collector);

View file

@ -6,6 +6,7 @@
*
* Contributors:
* Google - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable;
@ -19,7 +20,9 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
@ -53,6 +56,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarationStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTEqualsInitializer;
@ -68,6 +73,7 @@ import org.eclipse.cdt.internal.ui.refactoring.NameNVisibilityInformation;
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer;
import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
import org.eclipse.cdt.internal.ui.util.NameComposer;
/**
* The main class for the Extract Local Variable refactoring. This refactoring
@ -365,7 +371,8 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
@Override
public int visit(IASTExpression expression) {
// If the expression starts with a function call with a name, we should only need to guess this name
// If the expression starts with a function call with a name, we should only
// need to guess this name
if (expression == target && expression instanceof ICPPASTFunctionCallExpression) {
ICPPASTFunctionCallExpression functionCallExpression = (ICPPASTFunctionCallExpression) expression;
IASTExpression functionNameExpression = functionCallExpression.getFunctionNameExpression();
@ -410,17 +417,21 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
}
private void addTempName(String name) {
char[] tmpName = new char[name.length()];
int len = 0;
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (len == 0 && Character.isJavaIdentifierStart(c)) {
tmpName[len++] = Character.toLowerCase(c);
} else if (Character.isJavaIdentifierPart(c)) {
tmpName[len++] = c;
}
}
name = trimPrefixes(new String(tmpName, 0, len));
name = trimPrefixes(name);
IPreferencesService preferences = Platform.getPreferencesService();
int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID,
PreferenceConstants.NAME_STYLE_VARIABLE_CAPITALIZATION,
PreferenceConstants.NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE, 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);
name = composer.compose(name);
if (name.length() > 0) {
if (nameAvailable(name, guessedTempNames, scope)) {
guessedTempNames.add(name);
@ -438,7 +449,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
guessedTempNames.add(name);
}
}
return guessedTempNames.toArray(new String[0]);
return guessedTempNames.toArray(new String[guessedTempNames.size()]);
}
private String trimPrefixes(String name) {

View file

@ -1542,6 +1542,40 @@ public class PreferenceConstants {
*/
public static final String NAME_STYLE_CONSTANT_WORD_DELIMITER = "nameStyle.constant.wordDelimiter"; //$NON-NLS-1$
/**
* A named preference that controls how capitalization of a variable name.
* <p>
* Value is of type <code>Integer</code>.
*
* @since 5.3
*/
public static final String NAME_STYLE_VARIABLE_CAPITALIZATION = "nameStyle.variable.capitalization"; //$NON-NLS-1$
/**
* A named preference that controls prefix of a variable name.
* <p>
* Value is of type <code>String</code>.
*
* @since 5.3
*/
public static final String NAME_STYLE_VARIABLE_PREFIX = "nameStyle.variable.prefix"; //$NON-NLS-1$
/**
* A named preference that controls suffix of a variable name.
* <p>
* Value is of type <code>String</code>.
*
* @since 5.3
*/
public static final String NAME_STYLE_VARIABLE_SUFFIX = "nameStyle.variable.suffix"; //$NON-NLS-1$
/**
* A named preference that controls delimiter that is inserted between words
* of a variable name.
* <p>
* Value is of type <code>String</code>.
*
* @since 5.3
*/
public static final String NAME_STYLE_VARIABLE_WORD_DELIMITER = "nameStyle.variable.wordDelimiter"; //$NON-NLS-1$
/**
* A named preference that controls how capitalization of a field name.
* <p>
@ -2002,14 +2036,18 @@ public class PreferenceConstants {
CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME);
// Name Style
store.setDefault(NAME_STYLE_FIELD_CAPITALIZATION, NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE);
store.setDefault(NAME_STYLE_FIELD_PREFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_FIELD_SUFFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_FIELD_WORD_DELIMITER, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_CONSTANT_CAPITALIZATION, NAME_STYLE_CAPITALIZATION_UPPER_CASE);
store.setDefault(NAME_STYLE_CONSTANT_PREFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_CONSTANT_SUFFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_CONSTANT_WORD_DELIMITER, "_"); //$NON-NLS-1$
store.setDefault(NAME_STYLE_VARIABLE_CAPITALIZATION, NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE);
store.setDefault(NAME_STYLE_VARIABLE_PREFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_VARIABLE_SUFFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_VARIABLE_WORD_DELIMITER, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_FIELD_CAPITALIZATION, NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE);
store.setDefault(NAME_STYLE_FIELD_PREFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_FIELD_SUFFIX, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_FIELD_WORD_DELIMITER, ""); //$NON-NLS-1$
store.setDefault(NAME_STYLE_GETTER_CAPITALIZATION, NAME_STYLE_CAPITALIZATION_CAMEL_CASE);
store.setDefault(NAME_STYLE_GETTER_PREFIX, "get"); //$NON-NLS-1$
store.setDefault(NAME_STYLE_GETTER_PREFIX_FOR_BOOLEAN, "is"); //$NON-NLS-1$