use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class ConstructorParameterOnFieldRenameRenamer method nameToCanonicalName.
protected String nameToCanonicalName(@NonNls final String name, final PsiNamedElement element) {
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
final VariableKind variableKind = element instanceof PsiVariable ? javaCodeStyleManager.getVariableKind((PsiVariable) element) : VariableKind.FIELD;
return javaCodeStyleManager.variableNameToPropertyName(name, variableKind);
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class JavaNameSuggestionProvider method suggestProperlyCasedName.
@Nullable
private static String[] suggestProperlyCasedName(PsiElement psiElement) {
if (!(psiElement instanceof PsiNamedElement))
return null;
if (psiElement instanceof PsiFile)
return null;
String name = ((PsiNamedElement) psiElement).getName();
if (name == null)
return null;
String prefix = "";
if (psiElement instanceof PsiVariable) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(psiElement.getProject());
final VariableKind kind = codeStyleManager.getVariableKind((PsiVariable) psiElement);
prefix = codeStyleManager.getPrefixByVariableKind(kind);
if (kind == VariableKind.STATIC_FINAL_FIELD) {
final String[] words = NameUtil.splitNameIntoWords(name);
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (i > 0)
buffer.append('_');
buffer.append(StringUtil.toUpperCase(word));
}
return new String[] { buffer.toString() };
}
}
final List<String> result = new ArrayList<>();
result.add(suggestProperlyCasedName(prefix, NameUtil.splitNameIntoWords(name)));
if (name.startsWith(prefix) && !prefix.isEmpty()) {
name = name.substring(prefix.length());
result.add(suggestProperlyCasedName(prefix, NameUtil.splitNameIntoWords(name)));
}
result.add(suggestProperlyCasedName(prefix, NameUtil.splitNameIntoWords(name.toLowerCase())));
return ArrayUtil.toStringArray(result);
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class JavaNameSuggestionProvider method suggestNamesForElement.
@Nullable
private static SuggestedNameInfo suggestNamesForElement(final PsiElement element, PsiElement nameSuggestionContext) {
PsiVariable var = null;
if (element instanceof PsiVariable) {
var = (PsiVariable) element;
} else if (element instanceof PsiIdentifier) {
PsiIdentifier identifier = (PsiIdentifier) element;
if (identifier.getParent() instanceof PsiVariable) {
var = (PsiVariable) identifier.getParent();
}
}
if (var == null)
return null;
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject());
VariableKind variableKind = codeStyleManager.getVariableKind(var);
final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(variableKind, null, var.getInitializer(), var.getType());
final PsiExpression expression = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiExpression.class, false);
if (expression != null) {
return new SuggestedNameInfo.Delegate(codeStyleManager.suggestVariableName(variableKind, null, expression, var.getType()).names, nameInfo);
}
return nameInfo;
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class PropertyUtil method generateSetterPrototype.
/**
* Consider using {@link com.intellij.codeInsight.generation.GenerateMembersUtil#generateSetterPrototype(com.intellij.psi.PsiField)}
* or {@link com.intellij.codeInsight.generation.GenerateMembersUtil#generateSimpleSetterPrototype(com.intellij.psi.PsiField)}
* to add @Override annotation
*/
@Nullable
public static PsiMethod generateSetterPrototype(@NotNull PsiField field, @NotNull PsiClass containingClass, boolean returnSelf) {
Project project = field.getProject();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(field.getProject()).getElementFactory();
String name = field.getName();
boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = suggestSetterName(field);
try {
PsiMethod setMethod = factory.createMethodFromText(factory.createMethod(setName, returnSelf ? factory.createType(containingClass) : PsiType.VOID).getText(), field);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
PsiParameter param = factory.createParameter(parameterName, field.getType());
NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.PUBLIC, true);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder buffer = new StringBuilder();
buffer.append("{\n");
if (name.equals(parameterName)) {
if (!isStatic) {
buffer.append("this.");
} else {
String className = containingClass.getName();
if (className != null) {
buffer.append(className);
buffer.append(".");
}
}
}
buffer.append(name);
buffer.append("=");
buffer.append(parameterName);
buffer.append(";\n");
if (returnSelf) {
buffer.append("return this;\n");
}
buffer.append("}");
PsiCodeBlock body = factory.createCodeBlockFromText(buffer.toString(), null);
setMethod.getBody().replace(body);
setMethod = (PsiMethod) CodeStyleManager.getInstance(project).reformat(setMethod);
return setMethod;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class PropertyUtil method suggestPropertyName.
public static String suggestPropertyName(@NotNull PsiField field, @NotNull String fieldName) {
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(field.getProject());
VariableKind kind = codeStyleManager.getVariableKind(field);
String name = codeStyleManager.variableNameToPropertyName(fieldName, kind);
if (!field.hasModifierProperty(PsiModifier.STATIC) && isBoolean(field.getType())) {
if (name.startsWith(IS_PREFIX) && name.length() > IS_PREFIX.length() && Character.isUpperCase(name.charAt(IS_PREFIX.length()))) {
name = Introspector.decapitalize(name.substring(IS_PREFIX.length()));
}
}
return name;
}
Aggregations