Search in sources :

Example 56 with GrCodeReferenceElement

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

the class GrAnnotationImpl method getQualifiedName.

@Override
@Nullable
@NonNls
public String getQualifiedName() {
    final GrAnnotationStub stub = getStub();
    if (stub != null) {
        return stub.getPsiElement().getQualifiedName();
    }
    final GrCodeReferenceElement nameRef = getClassReference();
    final PsiElement resolved = nameRef.resolve();
    if (resolved instanceof PsiClass)
        return ((PsiClass) resolved).getQualifiedName();
    return null;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnnotationStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrAnnotationStub) NonNls(org.jetbrains.annotations.NonNls) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with GrCodeReferenceElement

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

the class GroovyPsiElementFactoryImpl method createCodeReferenceElementFromText.

@NotNull
@Override
public GrCodeReferenceElement createCodeReferenceElementFromText(@NotNull String text) {
    GroovyFile file = createGroovyFileChecked("class X extends " + text + "{}");
    PsiClass[] classes = file.getClasses();
    if (classes.length != 1)
        throw new IncorrectOperationException("cannot create code reference element for class" + text);
    GrExtendsClause extendsClause = ((GrTypeDefinition) classes[0]).getExtendsClause();
    if (extendsClause == null)
        throw new IncorrectOperationException("cannot create code reference element for class" + text);
    GrCodeReferenceElement[] refElements = extendsClause.getReferenceElementsGroovy();
    if (refElements.length != 1)
        throw new IncorrectOperationException("cannot create code reference element for class" + text);
    return refElements[0];
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with GrCodeReferenceElement

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

the class GrAnonymousClassType method getJavaClassName.

@NotNull
@Override
protected String getJavaClassName() {
    final GrCodeReferenceElement ref = myAnonymous.getBaseClassReferenceGroovy();
    final PsiElement resolved = ref.resolve();
    if (resolved instanceof PsiClass) {
        String qname = ((PsiClass) resolved).getQualifiedName();
        return qname != null ? qname : ((PsiClass) resolved).getName();
    } else {
        return ref.getClassNameText();
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement 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 60 with GrCodeReferenceElement

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

the class AnnotationAttributeCompletionResultProcessor method process.

public void process(@NotNull Consumer<LookupElement> consumer, @NotNull PrefixMatcher matcher) {
    GrCodeReferenceElement ref = myAnnotation.getClassReference();
    PsiElement resolved = ref.resolve();
    if (resolved instanceof PsiClass) {
        final PsiAnnotation annotationCollector = GrAnnotationCollector.findAnnotationCollector((PsiClass) resolved);
        if (annotationCollector != null) {
            final ArrayList<GrAnnotation> annotations = ContainerUtil.newArrayList();
            GrAnnotationCollector.collectAnnotations(annotations, myAnnotation, annotationCollector);
            Set<String> usedNames = ContainerUtil.newHashSet();
            for (GrAnnotation annotation : annotations) {
                final PsiElement resolvedAliased = annotation.getClassReference().resolve();
                if (resolvedAliased instanceof PsiClass && ((PsiClass) resolvedAliased).isAnnotationType()) {
                    for (PsiMethod method : ((PsiClass) resolvedAliased).getMethods()) {
                        if (usedNames.add(method.getName())) {
                            for (LookupElement element : GroovyCompletionUtil.createLookupElements(new GroovyResolveResultImpl(method, true), false, matcher, null)) {
                                consumer.consume(element);
                            }
                        }
                    }
                }
            }
        } else if (((PsiClass) resolved).isAnnotationType()) {
            for (PsiMethod method : ((PsiClass) resolved).getMethods()) {
                for (LookupElement element : GroovyCompletionUtil.createLookupElements(new GroovyResolveResultImpl(method, true), false, matcher, null)) {
                    consumer.consume(element);
                }
            }
        }
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiAnnotation(com.intellij.psi.PsiAnnotation) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl)

Aggregations

GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)71 NotNull (org.jetbrains.annotations.NotNull)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)12 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)5 PsiClass (com.intellij.psi.PsiClass)4 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)4 GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)4 Editor (com.intellij.openapi.editor.Editor)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3