Search in sources :

Example 1 with EmptyExpression

use of com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression in project intellij-community by JetBrains.

the class CreateFieldOrPropertyFix method generateMembers.

private void generateMembers(final Project project, final Editor editor, final PsiFile file) {
    try {
        List<? extends GenerationInfo> prototypes = new GenerateFieldOrPropertyHandler(myName, myType, myMemberType, myAnnotations).generateMemberPrototypes(myClass, ClassMember.EMPTY_ARRAY);
        prototypes = GenerateMembersUtil.insertMembersAtOffset(myClass, editor.getCaretModel().getOffset(), prototypes);
        if (prototypes.isEmpty())
            return;
        final PsiElement scope = prototypes.get(0).getPsiMember().getContext();
        assert scope != null;
        final Expression expression = new EmptyExpression() {

            @Override
            public com.intellij.codeInsight.template.Result calculateResult(final ExpressionContext context) {
                return new TextResult(myType.getCanonicalText());
            }
        };
        final TemplateBuilderImpl builder = new TemplateBuilderImpl(scope);
        boolean first = true;
        @NonNls final String TYPE_NAME_VAR = "TYPE_NAME_VAR";
        for (GenerationInfo prototype : prototypes) {
            final PsiTypeElement typeElement = PropertyUtil.getPropertyTypeElement(prototype.getPsiMember());
            if (first) {
                first = false;
                builder.replaceElement(typeElement, TYPE_NAME_VAR, expression, true);
            } else {
                builder.replaceElement(typeElement, TYPE_NAME_VAR, TYPE_NAME_VAR, false);
            }
        }
        PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
        editor.getCaretModel().moveToOffset(scope.getTextRange().getStartOffset());
        TemplateManager.getInstance(project).startTemplate(editor, builder.buildInlineTemplate());
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : com.intellij.codeInsight.template(com.intellij.codeInsight.template) NonNls(org.jetbrains.annotations.NonNls) EmptyExpression(com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression) EmptyExpression(com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression) GenerateFieldOrPropertyHandler(com.intellij.codeInsight.generation.GenerateFieldOrPropertyHandler) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GenerationInfo(com.intellij.codeInsight.generation.GenerationInfo)

Example 2 with EmptyExpression

use of com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression 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 3 with EmptyExpression

use of com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression in project intellij-community by JetBrains.

the class XmlTagInplaceRenamer method buildTemplate.

private static Template buildTemplate(@NotNull final XmlTag tag, @NotNull final Pair<ASTNode, ASTNode> pair) {
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(tag);
    final ASTNode selected = pair.first;
    final ASTNode other = pair.second;
    builder.replaceElement(selected.getPsi(), PRIMARY_VARIABLE_NAME, new EmptyExpression() {

        @Override
        public Result calculateQuickResult(final ExpressionContext context) {
            return new TextResult(selected.getText());
        }

        @Override
        public Result calculateResult(final ExpressionContext context) {
            return new TextResult(selected.getText());
        }
    }, true);
    if (other != null) {
        builder.replaceElement(other.getPsi(), OTHER_VARIABLE_NAME, PRIMARY_VARIABLE_NAME, false);
    }
    return builder.buildInlineTemplate();
}
Also used : ASTNode(com.intellij.lang.ASTNode) EmptyExpression(com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression)

Aggregations

EmptyExpression (com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression)3 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 GuessTypeParameters (com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters)1 GenerateFieldOrPropertyHandler (com.intellij.codeInsight.generation.GenerateFieldOrPropertyHandler)1 GenerationInfo (com.intellij.codeInsight.generation.GenerationInfo)1 com.intellij.codeInsight.template (com.intellij.codeInsight.template)1 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 ASTNode (com.intellij.lang.ASTNode)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NonNls (org.jetbrains.annotations.NonNls)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)1 TypeConstraint (org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)1 ChooseTypeExpression (org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression)1