Search in sources :

Example 6 with GrTypeArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList in project intellij-community by JetBrains.

the class GrReferenceElementImpl method getTypeArguments.

@Override
@NotNull
public PsiType[] getTypeArguments() {
    final GrTypeArgumentList typeArgsList = getTypeArgumentList();
    if (typeArgsList == null)
        return PsiType.EMPTY_ARRAY;
    final GrTypeElement[] args = typeArgsList.getTypeArgumentElements();
    if (args.length == 0)
        return PsiType.EMPTY_ARRAY;
    PsiType[] result = PsiType.createArray(args.length);
    for (int i = 0; i < result.length; i++) {
        result[i] = args[i].getType();
    }
    return result;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GrTypeArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList in project intellij-community by JetBrains.

the class GrCreateSubclassAction method startTemplate.

private static void startTemplate(GrTypeParameterList oldTypeParameterList, final Project project, final GrTypeDefinition psiClass, final GrTypeDefinition targetClass, boolean includeClassName) {
    PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(project);
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    GrCodeReferenceElement stubRef = elementFactory.createCodeReferenceElementFromClass(psiClass);
    try {
        GrReferenceList clause = psiClass.isInterface() ? targetClass.getImplementsClause() : targetClass.getExtendsClause();
        if (clause == null) {
            GrReferenceList stubRefList = psiClass.isInterface() ? elementFactory.createImplementsClause() : elementFactory.createExtendsClause();
            clause = (GrExtendsClause) targetClass.addAfter(stubRefList, targetClass.getNameIdentifierGroovy());
        }
        GrCodeReferenceElement ref = (GrCodeReferenceElement) clause.add(stubRef);
        if (psiClass.hasTypeParameters() || includeClassName) {
            final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.getContainingFile(), targetClass);
            final TemplateBuilderImpl templateBuilder = editor == null || ApplicationManager.getApplication().isUnitTestMode() ? null : (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass);
            if (includeClassName && templateBuilder != null) {
                templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName());
            }
            if (oldTypeParameterList != null && oldTypeParameterList.getTypeParameters().length > 0) {
                GrTypeArgumentList existingList = ref.getTypeArgumentList();
                final GrTypeParameterList typeParameterList = (GrTypeParameterList) targetClass.addAfter(elementFactory.createTypeParameterList(), targetClass.getNameIdentifierGroovy());
                GrTypeArgumentList argList;
                if (existingList == null) {
                    GrCodeReferenceElement codeRef = elementFactory.createCodeReferenceElementFromText("A<T>");
                    argList = ((GrTypeArgumentList) ref.add(codeRef.getTypeArgumentList()));
                    argList.getTypeArgumentElements()[0].delete();
                } else {
                    argList = existingList;
                }
                for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) {
                    final PsiElement param = argList.add(elementFactory.createTypeElement(jfactory.createType(parameter)));
                    if (templateBuilder != null) {
                        templateBuilder.replaceElement(param, param.getText());
                    }
                    typeParameterList.add(elementFactory.createTypeParameter(parameter.getName(), parameter.getExtendsListTypes()));
                }
            }
            if (templateBuilder != null) {
                templateBuilder.setEndVariableBefore(ref);
                final Template template = templateBuilder.buildTemplate();
                template.addEndVariable();
                final PsiFile containingFile = targetClass.getContainingFile();
                PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                final TextRange textRange = targetClass.getTextRange();
                final RangeMarker startClassOffset = editor.getDocument().createRangeMarker(textRange.getStartOffset(), textRange.getEndOffset());
                startClassOffset.setGreedyToLeft(true);
                startClassOffset.setGreedyToRight(true);
                editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
                CreateFromUsageBaseFix.startTemplate(editor, template, project, new TemplateEditingAdapter() {

                    @Override
                    public void templateFinished(Template template, boolean brokenOff) {
                        chooseAndImplement(psiClass, project, PsiTreeUtil.getParentOfType(containingFile.findElementAt(startClassOffset.getStartOffset()), GrTypeDefinition.class), editor);
                    }
                }, getTitle(psiClass));
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Template(com.intellij.codeInsight.template.Template) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)

Example 8 with GrTypeArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList in project intellij-community by JetBrains.

the class ImportStaticIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, final Editor editor) throws IncorrectOperationException {
    final PsiElement resolved = resolve(element);
    if (!(resolved instanceof PsiMember))
        return;
    final PsiClass containingClass = ((PsiMember) resolved).getContainingClass();
    if (containingClass == null)
        return;
    String originalName = ((PsiMember) resolved).getName();
    final String name = resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod) resolved) ? GroovyPropertyUtils.getPropertyName((PsiMethod) resolved) : originalName;
    final String qname = containingClass.getQualifiedName();
    if (name == null)
        return;
    final PsiFile containingFile = element.getContainingFile();
    if (!(containingFile instanceof GroovyFile))
        return;
    final GroovyFile file = (GroovyFile) containingFile;
    file.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            if (name.equals(expression.getReferenceName())) {
                PsiElement resolved = expression.resolve();
                if (resolved != null) {
                    expression.putUserData(TEMP_REFERENT_USER_DATA, resolved);
                }
            }
        }
    });
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final GrImportStatement tempImport = factory.createImportStatementFromText(qname + "." + name, true, false, null);
    final GrImportStatement importStatement = file.addImport(tempImport);
    boolean isAnythingShortened = shortenUsages(resolved, containingFile);
    if (!isAnythingShortened) {
        importStatement.delete();
        return;
    }
    file.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            GrTypeArgumentList typeArgumentList = expression.getTypeArgumentList();
            if (typeArgumentList != null && typeArgumentList.getFirstChild() != null) {
                expression.putUserData(TEMP_REFERENT_USER_DATA, null);
                return;
            }
            if (name.equals(expression.getReferenceName())) {
                if (expression.isQualified()) {
                    GrExpression qualifier = expression.getQualifierExpression();
                    if (qualifier instanceof GrReferenceExpression) {
                        PsiElement aClass = ((GrReferenceExpression) qualifier).resolve();
                        if (aClass == ((PsiMember) resolved).getContainingClass()) {
                            GrReferenceAdjuster.shortenReference(expression);
                        }
                    }
                } else {
                    PsiElement referent = expression.getUserData(TEMP_REFERENT_USER_DATA);
                    if (referent instanceof PsiMember && ((PsiMember) referent).hasModifierProperty(PsiModifier.STATIC) && referent != expression.resolve()) {
                        expression.bindToElement(referent);
                    }
                }
            }
            expression.putUserData(TEMP_REFERENT_USER_DATA, null);
        }
    });
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)8 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 PsiElement (com.intellij.psi.PsiElement)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 ASTNode (com.intellij.lang.ASTNode)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 PsiTypeParameter (com.intellij.psi.PsiTypeParameter)1 PsiTypeParameterListOwner (com.intellij.psi.PsiTypeParameterListOwner)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1