Search in sources :

Example 1 with GenerationInfo

use of com.intellij.codeInsight.generation.GenerationInfo 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)

Aggregations

EmptyExpression (com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression)1 GenerateFieldOrPropertyHandler (com.intellij.codeInsight.generation.GenerateFieldOrPropertyHandler)1 GenerationInfo (com.intellij.codeInsight.generation.GenerationInfo)1 com.intellij.codeInsight.template (com.intellij.codeInsight.template)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NonNls (org.jetbrains.annotations.NonNls)1