Search in sources :

Example 11 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrModifierListImpl method addAnnotation.

@Override
@NotNull
public GrAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
    final PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(qualifiedName, getResolveScope());
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    GrAnnotation annotation;
    if (psiClass != null && psiClass.isAnnotationType()) {
        annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@xxx"), null);
        annotation.getClassReference().bindToElement(psiClass);
    } else {
        annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@" + qualifiedName), null);
    }
    final PsiElement parent = getParent();
    if (!(parent instanceof GrParameter)) {
        final ASTNode node = annotation.getNode();
        final ASTNode treeNext = node.getTreeNext();
        if (treeNext != null) {
            getNode().addLeaf(TokenType.WHITE_SPACE, "\n", treeNext);
        } else {
            parent.getNode().addLeaf(TokenType.WHITE_SPACE, "\n", getNode().getTreeNext());
        }
    }
    return annotation;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) ASTNode(com.intellij.lang.ASTNode) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrVariableDeclarationImpl method findSuitableModifier.

private PsiElement findSuitableModifier() {
    final GrModifierList list = getModifierList();
    PsiElement defModifier = list.getModifier(GrModifier.DEF);
    if (defModifier != null)
        return defModifier;
    PsiElement finalModifier = list.getModifier(PsiModifier.FINAL);
    if (finalModifier != null)
        return finalModifier;
    for (PsiElement element : list.getModifiers()) {
        if (!(element instanceof GrAnnotation)) {
            return element;
        }
    }
    return null;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)

Example 13 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrLightAnnotation method addAttribute.

public void addAttribute(PsiNameValuePair pair) {
    if (pair instanceof GrAnnotationNameValuePair) {
        myAnnotationArgList.addAttribute((GrAnnotationNameValuePair) pair);
    } else {
        GrAnnotationMemberValue newValue = new AnnotationArgConverter().convert(pair.getValue());
        if (newValue == null)
            return;
        String name = pair.getName();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(pair.getProject());
        String annotationText;
        annotationText = name != null ? "@A(" + name + "=" + newValue.getText() + ")" : "@A(" + newValue.getText() + ")";
        GrAnnotation annotation = factory.createAnnotationFromText(annotationText);
        myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
    }
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) AnnotationArgConverter(org.jetbrains.plugins.groovy.lang.psi.impl.AnnotationArgConverter)

Example 14 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrLightModifierList method getText.

@Override
public String getText() {
    StringBuilder buffer = new StringBuilder();
    for (GrAnnotation annotation : myAnnotations) {
        buffer.append(annotation.getText());
        buffer.append(' ');
    }
    for (@GrModifier.GrModifierConstant String modifier : GrModifier.GROOVY_MODIFIERS) {
        if (hasExplicitModifier(modifier)) {
            buffer.append(modifier);
            buffer.append(' ');
        }
    }
    if (buffer.length() > 0) {
        buffer.delete(buffer.length() - 1, buffer.length());
    }
    return buffer.toString();
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)

Example 15 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrAnnotationStub method getPsiElement.

public GrAnnotation getPsiElement() {
    GrAnnotation annotation = SoftReference.dereference(myPsiRef);
    if (annotation != null) {
        return annotation;
    }
    try {
        annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText(myText, getPsi());
        myPsiRef = new SoftReference<>(annotation);
        return annotation;
    } catch (IncorrectOperationException e) {
        LOG.error("Bad annotation in repository!", e);
        return null;
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)30 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)9 NotNull (org.jetbrains.annotations.NotNull)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)5 PsiElement (com.intellij.psi.PsiElement)4 GrAnnotationNameValuePair (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair)4 PsiAnnotation (com.intellij.psi.PsiAnnotation)3 Nullable (org.jetbrains.annotations.Nullable)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GrAnnotationMemberValue (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)1 CantRunException (com.intellij.execution.CantRunException)1 ExecutionException (com.intellij.execution.ExecutionException)1 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1