Search in sources :

Example 6 with VariableKind

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;
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) NonNls(org.jetbrains.annotations.NonNls) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VariableKind(com.intellij.psi.codeStyle.VariableKind) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 7 with VariableKind

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;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableKind(com.intellij.psi.codeStyle.VariableKind) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with VariableKind

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);
    }
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 9 with VariableKind

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);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 10 with VariableKind

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;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VariableKind (com.intellij.psi.codeStyle.VariableKind)21 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)18 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)9 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NonNls (org.jetbrains.annotations.NonNls)3 ArrayList (java.util.ArrayList)2 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 EnumMap (java.util.EnumMap)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 DefaultGroovyVariableNameValidator (org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)1