Search in sources :

Example 1 with DefaultGroovyVariableNameValidator

use of org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator in project intellij-community by JetBrains.

the class GrChangeSignatureUsageProcessor method fixCatchBlock.

private static PsiElement fixCatchBlock(GrTryCatchStatement tryCatch, PsiClassType[] exceptions) {
    if (exceptions.length == 0)
        return tryCatch;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(tryCatch.getProject());
    final GrCatchClause[] clauses = tryCatch.getCatchClauses();
    List<String> restricted = ContainerUtil.map(clauses, grCatchClause -> {
        final GrParameter grParameter = grCatchClause.getParameter();
        return grParameter != null ? grParameter.getName() : null;
    });
    restricted = ContainerUtil.skipNulls(restricted);
    final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(tryCatch, restricted);
    GrCatchClause anchor = clauses.length == 0 ? null : clauses[clauses.length - 1];
    for (PsiClassType type : exceptions) {
        final String[] names = GroovyNameSuggestionUtil.suggestVariableNameByType(type, nameValidator);
        final GrCatchClause catchClause = factory.createCatchClause(type, names[0]);
        final GrStatement printStackTrace = factory.createStatementFromText(names[0] + ".printStackTrace()");
        catchClause.getBody().addStatementBefore(printStackTrace, null);
        anchor = tryCatch.addCatchClause(catchClause, anchor);
        JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
    }
    return tryCatch;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 2 with DefaultGroovyVariableNameValidator

use of org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator in project intellij-community by JetBrains.

the class GenerationUtil method suggestVarName.

static String suggestVarName(PsiType type, GroovyPsiElement context, ExpressionContext expressionContext) {
    final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(context, expressionContext.myUsedVarNames, true, true);
    if (type instanceof PsiPrimitiveType)
        type = TypesUtil.boxPrimitiveType(type, context.getManager(), context.getResolveScope());
    final String[] varNames = GroovyNameSuggestionUtil.suggestVariableNameByType(type, nameValidator);
    LOG.assertTrue(varNames.length > 0);
    expressionContext.myUsedVarNames.add(varNames[0]);
    return varNames[0];
}
Also used : DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)

Example 3 with DefaultGroovyVariableNameValidator

use of org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator in project intellij-community by JetBrains.

the class GenerationUtil method suggestVarName.

static String suggestVarName(GrExpression expr, ExpressionContext expressionContext) {
    final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(expr, expressionContext.myUsedVarNames, true);
    final String[] varNames = GroovyNameSuggestionUtil.suggestVariableNames(expr, nameValidator);
    LOG.assertTrue(varNames.length > 0);
    expressionContext.myUsedVarNames.add(varNames[0]);
    return varNames[0];
}
Also used : DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)

Example 4 with DefaultGroovyVariableNameValidator

use of org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator in project intellij-community by JetBrains.

the class CreateParameterForFieldIntention method addParameter.

private static void addParameter(final GrField selectedValue, final GrMethod constructor, final Project project) {
    List<GrParameterInfo> parameters = new ArrayList<>();
    GrParameter[] constructorParameters = constructor.getParameters();
    for (int i = 0; i < constructorParameters.length; i++) {
        parameters.add(new GrParameterInfo(constructorParameters[i], i));
    }
    final String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.PARAMETER, selectedValue.getName(), null, null).names;
    final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(constructor, Collections.<String>emptyList(), false);
    String parameterName = ContainerUtil.find(suggestedNames, name -> !nameValidator.validateName(name, false).isEmpty());
    if (parameterName == null) {
        parameterName = nameValidator.validateName(suggestedNames[0], true);
    }
    parameters.add(new GrParameterInfo(parameterName, "null", "", selectedValue.getTypeGroovy(), -1, false));
    PsiClassType[] exceptionTypes = constructor.getThrowsList().getReferencedTypes();
    ThrownExceptionInfo[] thrownExceptionInfos = new ThrownExceptionInfo[exceptionTypes.length];
    for (int i = 0; i < exceptionTypes.length; i++) {
        new JavaThrownExceptionInfo(i, exceptionTypes[i]);
    }
    final GrChangeInfoImpl grChangeInfo = new GrChangeInfoImpl(constructor, null, null, constructor.getName(), parameters, thrownExceptionInfos, false);
    final String finalParameterName = parameterName;
    final GrChangeSignatureProcessor processor = new GrChangeSignatureProcessor(project, grChangeInfo) {

        @Override
        protected void performRefactoring(@NotNull UsageInfo[] usages) {
            super.performRefactoring(usages);
            final GrOpenBlock block = constructor.getBlock();
            LOG.assertTrue(block != null);
            final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
            final String text;
            if (StringUtil.equals(selectedValue.getName(), finalParameterName)) {
                text = "this." + selectedValue.getName() + " = " + finalParameterName;
            } else {
                text = selectedValue.getName() + " = " + finalParameterName;
            }
            final GrStatement assignment = factory.createStatementFromText(text);
            final GrStatement statement = block.addStatementBefore(assignment, null);
            final GrReferenceExpression ref = (GrReferenceExpression) ((GrAssignmentExpression) statement).getLValue();
            if (!PsiManager.getInstance(project).areElementsEquivalent(ref.resolve(), selectedValue)) {
                PsiUtil.qualifyMemberReference(ref, selectedValue, selectedValue.getName());
            }
        }
    };
    processor.run();
}
Also used : JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) ArrayList(java.util.ArrayList) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrChangeInfoImpl(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl) GrChangeSignatureProcessor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 5 with DefaultGroovyVariableNameValidator

use of org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator in project intellij-community by JetBrains.

the class ReplaceAbstractClassInstanceByMapIntention method createName.

private static String createName(final Set<String> generatedNames, final PsiParameter param, final PsiType type, GroovyPsiElement context) {
    String name = param.getName();
    if (name == null) {
        name = GroovyNameSuggestionUtil.suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator(context, generatedNames))[0];
        assert name != null;
    }
    generatedNames.add(name);
    return name;
}
Also used : DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)

Aggregations

DefaultGroovyVariableNameValidator (org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)1 VariableKind (com.intellij.psi.codeStyle.VariableKind)1 JavaThrownExceptionInfo (com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo)1 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrChangeInfoImpl (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl)1 GrChangeSignatureProcessor (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor)1 GrParameterInfo (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo)1