use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class GroovyPropertyUtils method generateSetterPrototype.
public static GrMethod generateSetterPrototype(PsiField field) {
Project project = field.getProject();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
String name = field.getName();
boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = getSetterName(field.getName());
final PsiClass containingClass = field.getContainingClass();
try {
GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
GrParameter param = factory.createParameter(parameterName, type);
annotateWithNullableStuff(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder builder = new StringBuilder();
if (name.equals(parameterName)) {
if (!isStatic) {
builder.append("this.");
} else {
String className = containingClass.getName();
if (className != null) {
builder.append(className);
builder.append(".");
}
}
}
builder.append(name);
builder.append("=");
builder.append(parameterName);
builder.append("\n");
GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
setMethod.getBlock().replace(body);
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 JavaCompletionSorting method getLookupObjectName.
@Nullable
private static String getLookupObjectName(Object o) {
if (o instanceof PsiVariable) {
final PsiVariable variable = (PsiVariable) o;
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(variable.getProject());
VariableKind variableKind = codeStyleManager.getVariableKind(variable);
return codeStyleManager.variableNameToPropertyName(variable.getName(), variableKind);
}
if (o instanceof PsiMethod) {
return ((PsiMethod) o).getName();
}
return null;
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class JavaMemberNameCompletionContributor method completeLocalVariableName.
private static void completeLocalVariableName(Set<LookupElement> set, PrefixMatcher matcher, PsiVariable var, boolean includeOverlapped) {
FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.variable.name");
Project project = var.getProject();
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
final VariableKind variableKind = codeStyleManager.getVariableKind(var);
String propertyName = null;
if (variableKind == VariableKind.PARAMETER) {
final PsiMethod method = PsiTreeUtil.getParentOfType(var, PsiMethod.class);
if (method != null) {
propertyName = PropertyUtil.getPropertyName(method);
}
if (method != null && method.getName().startsWith("with")) {
propertyName = StringUtil.decapitalize(method.getName().substring(4));
}
}
final PsiType type = var.getType();
SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(variableKind, propertyName, null, type, StringUtil.isEmpty(matcher.getPrefix()));
suggestedNameInfo = codeStyleManager.suggestUniqueVariableName(suggestedNameInfo, var, false);
final String[] suggestedNames = suggestedNameInfo.names;
addLookupItems(set, suggestedNameInfo, matcher, project, suggestedNames);
if (!hasStartMatches(set, matcher)) {
if (type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) && matcher.prefixMatches("object")) {
set.add(withInsertHandler(suggestedNameInfo, LookupElementBuilder.create("object")));
}
if (type.equalsToText(CommonClassNames.JAVA_LANG_STRING) && matcher.prefixMatches("string")) {
set.add(withInsertHandler(suggestedNameInfo, LookupElementBuilder.create("string")));
}
}
if (!hasStartMatches(set, matcher) && includeOverlapped) {
addLookupItems(set, null, matcher, project, getOverlappedNameVersions(matcher.getPrefix(), suggestedNames, ""));
}
PsiElement parent = PsiTreeUtil.getParentOfType(var, PsiCodeBlock.class);
if (parent == null)
parent = PsiTreeUtil.getParentOfType(var, PsiMethod.class, PsiLambdaExpression.class);
addLookupItems(set, suggestedNameInfo, matcher, project, getUnresolvedReferences(parent, false));
if (var instanceof PsiParameter && parent instanceof PsiMethod) {
addSuggestionsInspiredByFieldNames(set, matcher, var, project, codeStyleManager);
}
PsiExpression initializer = var.getInitializer();
if (initializer != null) {
SuggestedNameInfo initializerSuggestions = IntroduceVariableBase.getSuggestedName(type, initializer);
addLookupItems(set, initializerSuggestions, matcher, project, initializerSuggestions.names);
}
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class CreateFieldFromParameterActionBase method processParameter.
private void processParameter(@NotNull final Project project, @NotNull final PsiParameter myParameter, final boolean isInteractive) {
final PsiType type = getSubstitutedType(myParameter);
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
final String parameterName = myParameter.getName();
String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);
String fieldNameToCalc;
boolean isFinalToCalc;
final PsiClass targetClass = PsiTreeUtil.getParentOfType(myParameter, PsiClass.class);
final PsiMethod method = (PsiMethod) myParameter.getDeclarationScope();
final boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, type);
String[] names = suggestedNameInfo.names;
if (isInteractive) {
List<String> namesList = new ArrayList<>();
ContainerUtil.addAll(namesList, names);
String defaultName = styleManager.propertyNameToVariableName(propertyName, kind);
if (namesList.contains(defaultName)) {
Collections.swap(namesList, 0, namesList.indexOf(defaultName));
} else {
namesList.add(0, defaultName);
}
names = ArrayUtil.toStringArray(namesList);
final CreateFieldFromParameterDialog dialog = new CreateFieldFromParameterDialog(project, names, targetClass, method.isConstructor(), type);
if (!dialog.showAndGet()) {
return;
}
fieldNameToCalc = dialog.getEnteredName();
isFinalToCalc = dialog.isDeclareFinal();
suggestedNameInfo.nameChosen(fieldNameToCalc);
} else {
isFinalToCalc = !isMethodStatic && method.isConstructor();
fieldNameToCalc = names[0];
}
final boolean isFinal = isFinalToCalc;
final String fieldName = fieldNameToCalc;
ApplicationManager.getApplication().runWriteAction(() -> {
try {
performRefactoring(project, targetClass, method, myParameter, type, fieldName, isMethodStatic, isFinal);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
}
use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.
the class AssignFieldFromParameterAction method findFieldToAssign.
@Nullable
private static PsiField findFieldToAssign(@NotNull Project project, @NotNull PsiParameter myParameter) {
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
final String parameterName = myParameter.getName();
final String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);
final PsiMethod method = (PsiMethod) myParameter.getDeclarationScope();
final boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC);
final VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
final SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, FieldFromParameterUtils.getSubstitutedType(myParameter));
final String fieldName = suggestedNameInfo.names[0];
PsiClass aClass = method.getContainingClass();
if (aClass == null)
return null;
PsiField field = aClass.findFieldByName(fieldName, false);
if (field == null)
return null;
if (!field.hasModifierProperty(PsiModifier.STATIC) && isMethodStatic)
return null;
return field;
}
Aggregations