Search in sources :

Example 11 with VariableKind

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

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

Example 13 with VariableKind

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;
}
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)

Example 14 with VariableKind

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

Example 15 with VariableKind

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

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