Search in sources :

Example 6 with ChooseTypeExpression

use of org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression in project intellij-community by JetBrains.

the class GrSetStrongTypeIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    PsiElement elementToBuildTemplate;
    GrVariable[] variables;
    if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
        variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
        elementToBuildTemplate = parent.getParent();
    } else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
        variables = new GrVariable[] { (GrVariable) parent };
        elementToBuildTemplate = parent.getParent().getParent();
    } else if (parent instanceof GrVariableDeclaration) {
        variables = ((GrVariableDeclaration) parent).getVariables();
        elementToBuildTemplate = parent;
    } else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
        variables = new GrVariable[] { (GrVariable) parent };
        elementToBuildTemplate = parent.getParent().getParent();
    } else if (parent instanceof GrVariable) {
        variables = new GrVariable[] { ((GrVariable) parent) };
        elementToBuildTemplate = parent;
    } else {
        return;
    }
    ArrayList<TypeConstraint> types = new ArrayList<>();
    if (parent.getParent() instanceof GrForInClause) {
        types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
    } else {
        for (GrVariable variable : variables) {
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                PsiType type = initializer.getType();
                if (type != null) {
                    types.add(SupertypeConstraint.create(type));
                }
            }
            if (variable instanceof GrParameter) {
                final PsiParameter parameter = (PsiParameter) variable;
                final PsiType type = getClosureParameterType(parameter);
                if (type != null) {
                    types.add(SupertypeConstraint.create(type));
                }
            }
        }
    }
    final String originalText = elementToBuildTemplate.getText();
    final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
    final PsiElement replaceElement = typeInfo.elementToReplace;
    TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
    ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
    TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
    builder.replaceElement(replaceElement, chooseTypeExpression);
    final Document document = editor.getDocument();
    final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
    rangeMarker.setGreedyToRight(true);
    rangeMarker.setGreedyToLeft(true);
    final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
    final Template template = builder.buildTemplate();
    TextRange range = afterPostprocess.getTextRange();
    document.deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager templateManager = TemplateManager.getInstance(project);
    templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (brokenOff) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    if (rangeMarker.isValid()) {
                        document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
                        editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
                    }
                });
            }
        }
    });
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) ArrayList(java.util.ArrayList) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) Template(com.intellij.codeInsight.template.Template) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiParameter(com.intellij.psi.PsiParameter) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Example 7 with ChooseTypeExpression

use of org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression in project intellij-community by JetBrains.

the class CreateClassFix method generateConstructor.

private static void generateConstructor(@NotNull PsiElement refElement, @NotNull String name, @NotNull PsiType[] argTypes, @NotNull GrTypeDefinition targetClass, @NotNull Project project) {
    WriteAction.run(() -> {
        ChooseTypeExpression[] paramTypesExpressions = new ChooseTypeExpression[argTypes.length];
        String[] paramTypes = new String[argTypes.length];
        String[] paramNames = new String[argTypes.length];
        for (int i = 0; i < argTypes.length; i++) {
            PsiType argType = argTypes[i];
            if (argType == null)
                argType = TypesUtil.getJavaLangObject(refElement);
            paramTypes[i] = "Object";
            paramNames[i] = "o" + i;
            TypeConstraint[] constraints = { SupertypeConstraint.create(argType) };
            paramTypesExpressions[i] = new ChooseTypeExpression(constraints, refElement.getManager(), targetClass.getResolveScope());
        }
        GrMethod method = GroovyPsiElementFactory.getInstance(project).createConstructorFromText(name, paramTypes, paramNames, "{\n}");
        method = (GrMethod) targetClass.addBefore(method, null);
        final PsiElement context = PsiTreeUtil.getParentOfType(refElement, PsiMethod.class, PsiClass.class, PsiFile.class);
        IntentionUtils.createTemplateForMethod(argTypes, paramTypesExpressions, method, targetClass, TypeConstraint.EMPTY_ARRAY, true, context);
    });
}
Also used : TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) SupertypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Example 8 with ChooseTypeExpression

use of org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression in project intellij-community by JetBrains.

the class GroovyCreateFieldFromUsageHelper method setupTemplateImpl.

@Override
public Template setupTemplateImpl(PsiField f, Object expectedTypes, PsiClass targetClass, Editor editor, PsiElement context, boolean createConstantField, PsiSubstitutor substitutor) {
    GrVariableDeclaration fieldDecl = (GrVariableDeclaration) f.getParent();
    GrField field = (GrField) fieldDecl.getVariables()[0];
    TemplateBuilderImpl builder = new TemplateBuilderImpl(fieldDecl);
    Project project = context.getProject();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (expectedTypes instanceof TypeConstraint[]) {
        GrTypeElement typeElement = fieldDecl.getTypeElementGroovy();
        assert typeElement != null;
        ChooseTypeExpression expr = new ChooseTypeExpression((TypeConstraint[]) expectedTypes, PsiManager.getInstance(project), typeElement.getResolveScope());
        builder.replaceElement(typeElement, expr);
    } else if (expectedTypes instanceof ExpectedTypeInfo[]) {
        new GuessTypeParameters(factory).setupTypeElement(field.getTypeElement(), (ExpectedTypeInfo[]) expectedTypes, substitutor, builder, context, targetClass);
    }
    if (createConstantField) {
        field.setInitializerGroovy(factory.createExpressionFromText("0", null));
        builder.replaceElement(field.getInitializerGroovy(), new EmptyExpression());
    }
    fieldDecl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(fieldDecl);
    Template template = builder.buildTemplate();
    TextRange range = fieldDecl.getTextRange();
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    if (expectedTypes instanceof ExpectedTypeInfo[]) {
        if (((ExpectedTypeInfo[]) expectedTypes).length > 1)
            template.setToShortenLongNames(false);
    }
    return template;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GuessTypeParameters(com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters) TextRange(com.intellij.openapi.util.TextRange) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) EmptyExpression(com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression) Template(com.intellij.codeInsight.template.Template) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Example 9 with ChooseTypeExpression

use of org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression in project intellij-community by JetBrains.

the class CreateLocalVariableFromUsageFix method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiFile file = element.getContainingFile();
    PsiClassType type = JavaPsiFacade.getInstance(project).getElementFactory().createTypeByFQClassName("Object", GlobalSearchScope.allScope(project));
    GrVariableDeclaration decl = GroovyPsiElementFactory.getInstance(project).createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, myRefExpression.getReferenceName());
    int offset = myRefExpression.getTextRange().getStartOffset();
    GrStatement anchor = findAnchor(file, offset);
    TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints(myRefExpression);
    if (myRefExpression.equals(anchor)) {
        decl = myRefExpression.replaceWithStatement(decl);
    } else {
        decl = myOwner.addVariableDeclarationBefore(decl, anchor);
    }
    GrTypeElement typeElement = decl.getTypeElementGroovy();
    assert typeElement != null;
    ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), typeElement.getResolveScope());
    TemplateBuilderImpl builder = new TemplateBuilderImpl(decl);
    builder.replaceElement(typeElement, expr);
    decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
    Template template = builder.buildTemplate();
    Editor newEditor = positionCursor(project, myOwner.getContainingFile(), decl);
    TextRange range = decl.getTextRange();
    newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    manager.startTemplate(newEditor, template);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) TextRange(com.intellij.openapi.util.TextRange) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Template(com.intellij.codeInsight.template.Template) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor)

Aggregations

TypeConstraint (org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)9 ChooseTypeExpression (org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression)9 TextRange (com.intellij.openapi.util.TextRange)5 Template (com.intellij.codeInsight.template.Template)3 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)3 SupertypeConstraint (org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint)3 TemplateManager (com.intellij.codeInsight.template.TemplateManager)2 Document (com.intellij.openapi.editor.Document)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 PsiType (com.intellij.psi.PsiType)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 ParameterNameExpression (org.jetbrains.plugins.groovy.template.expressions.ParameterNameExpression)2 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 EmptyExpression (com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression)1