Search in sources :

Example 1 with VariableKind

use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.

the class DefaultQuickFixProvider method registerPriorityActions.

private static void registerPriorityActions(@NotNull QuickFixActionRegistrar registrar, @NotNull TextRange fixRange, @NotNull PsiReferenceExpression refExpr) {
    final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(refExpr.getProject());
    final Map<VariableKind, IntentionAction> map = new EnumMap<>(VariableKind.class);
    map.put(VariableKind.FIELD, new CreateFieldFromUsageFix(refExpr));
    map.put(VariableKind.STATIC_FINAL_FIELD, new CreateConstantFieldFromUsageFix(refExpr));
    if (!refExpr.isQualified()) {
        map.put(VariableKind.LOCAL_VARIABLE, new CreateLocalFromUsageFix(refExpr));
        map.put(VariableKind.PARAMETER, new CreateParameterFromUsageFix(refExpr));
    }
    final VariableKind kind = getKind(styleManager, refExpr);
    if (map.containsKey(kind)) {
        map.put(kind, PriorityIntentionActionWrapper.highPriority(map.get(kind)));
    }
    for (IntentionAction action : map.values()) {
        registrar.register(fixRange, action, null);
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) EnumMap(java.util.EnumMap) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 2 with VariableKind

use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.

the class DefaultQuickFixProvider method getKind.

@Nullable
private static VariableKind getKind(@NotNull JavaCodeStyleManager styleManager, @NotNull PsiReferenceExpression refExpr) {
    final String reference = refExpr.getText();
    boolean upperCase = true;
    for (int i = 0; i < reference.length(); i++) {
        if (!Character.isUpperCase(reference.charAt(i))) {
            upperCase = false;
            break;
        }
    }
    if (upperCase) {
        return VariableKind.STATIC_FINAL_FIELD;
    }
    for (VariableKind kind : VariableKind.values()) {
        final String prefix = styleManager.getPrefixByVariableKind(kind);
        final String suffix = styleManager.getSuffixByVariableKind(kind);
        if (prefix.isEmpty() && suffix.isEmpty()) {
            continue;
        }
        if (reference.startsWith(prefix) && reference.endsWith(suffix)) {
            return kind;
        }
    }
    if (StringUtil.isCapitalized(reference)) {
        return null;
    }
    return VariableKind.LOCAL_VARIABLE;
}
Also used : VariableKind(com.intellij.psi.codeStyle.VariableKind) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with VariableKind

use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.

the class GenerationHelper method getPropertyName.

public static String getPropertyName(FieldElement fieldElement, Project project) {
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    final VariableKind variableKind = fieldElement.isModifierStatic() ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
    final String propertyName = codeStyleManager.variableNameToPropertyName(fieldElement.getName(), variableKind);
    if (!fieldElement.isModifierStatic() && fieldElement.isBoolean()) {
        if (propertyName.startsWith("is") && propertyName.length() > "is".length() && Character.isUpperCase(propertyName.charAt("is".length()))) {
            return StringUtil.decapitalize(propertyName.substring("is".length()));
        }
    }
    return propertyName;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 4 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 5 with VariableKind

use of com.intellij.psi.codeStyle.VariableKind in project intellij-community by JetBrains.

the class ExpressionUtil method getNamesForIdentifier.

@Nullable
private static String[] getNamesForIdentifier(Project project, PsiIdentifier identifier) {
    if (identifier.getParent() instanceof PsiVariable) {
        PsiVariable var = (PsiVariable) identifier.getParent();
        if (identifier.equals(var.getNameIdentifier())) {
            JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
            VariableKind variableKind = codeStyleManager.getVariableKind(var);
            PsiExpression initializer = var.getInitializer();
            if (var instanceof PsiParameter && ((PsiParameter) var).getDeclarationScope() instanceof PsiForeachStatement) {
                //synthesize initializer
                PsiForeachStatement foreachStatement = (PsiForeachStatement) ((PsiParameter) var).getDeclarationScope();
                final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
                if (iteratedValue != null) {
                    try {
                        final PsiArrayAccessExpression expr = (PsiArrayAccessExpression) JavaPsiFacade.getInstance(iteratedValue.getProject()).getElementFactory().createExpressionFromText("a[0]", var);
                        expr.getArrayExpression().replace(iteratedValue);
                        //note: non physical with no parent
                        initializer = expr;
                    } catch (IncorrectOperationException e) {
                    //do nothing
                    }
                }
            }
            SuggestedNameInfo suggestedInfo = codeStyleManager.suggestVariableName(variableKind, null, initializer, var.getType());
            return suggestedInfo.names;
        }
    }
    return null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) 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