Search in sources :

Example 1 with GrAnnotationNameValuePair

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

the class GrAnnotationAttributeCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement position = parameters.getPosition();
    PsiElement parent = position.getParent();
    if (parent instanceof GrAnnotationNameValuePair && position == ((GrAnnotationNameValuePair) parent).getNameIdentifierGroovy()) {
        GrAnnotation annotation = PsiImplUtil.getAnnotation((GrAnnotationNameValuePair) parent);
        if (annotation != null) {
            new AnnotationAttributeCompletionResultProcessor(annotation).process(result, PrefixMatcher.ALWAYS_TRUE);
        }
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiElement(com.intellij.psi.PsiElement)

Example 2 with GrAnnotationNameValuePair

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

the class CustomAnnotationChecker method checkAnnotationArguments.

public static void checkAnnotationArguments(@NotNull AnnotationHolder holder, @NotNull PsiClass annotation, @NotNull GrCodeReferenceElement refToHighlight, @NotNull GrAnnotationNameValuePair[] attributes, boolean checkMissedAttributes) {
    Set<String> usedAttrs = new HashSet<>();
    if (attributes.length > 0) {
        final PsiElement identifier = attributes[0].getNameIdentifierGroovy();
        if (attributes.length == 1 && identifier == null) {
            checkAnnotationValue(annotation, attributes[0], "value", usedAttrs, attributes[0].getValue(), holder);
        } else {
            for (GrAnnotationNameValuePair attribute : attributes) {
                final String name = attribute.getName();
                if (name != null) {
                    final PsiElement toHighlight = attribute.getNameIdentifierGroovy();
                    assert toHighlight != null;
                    checkAnnotationValue(annotation, toHighlight, name, usedAttrs, attribute.getValue(), holder);
                }
            }
        }
    }
    List<String> missedAttrs = new ArrayList<>();
    final PsiMethod[] methods = annotation.getMethods();
    for (PsiMethod method : methods) {
        final String name = method.getName();
        if (usedAttrs.contains(name) || method instanceof PsiAnnotationMethod && ((PsiAnnotationMethod) method).getDefaultValue() != null) {
            continue;
        }
        missedAttrs.add(name);
    }
    if (checkMissedAttributes && !missedAttrs.isEmpty()) {
        holder.createErrorAnnotation(refToHighlight, GroovyBundle.message("missed.attributes", StringUtil.join(missedAttrs, ", ")));
    }
}
Also used : GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) ArrayList(java.util.ArrayList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 3 with GrAnnotationNameValuePair

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

the class GrAliasAnnotationChecker method checkArgumentList.

@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    final PsiAnnotation annotationCollector = GrAnnotationCollector.findAnnotationCollector(annotation);
    if (annotationCollector == null) {
        return false;
    }
    final ArrayList<GrAnnotation> annotations = ContainerUtil.newArrayList();
    final Set<String> usedAttributes = GrAnnotationCollector.collectAnnotations(annotations, annotation, annotationCollector);
    AliasedAnnotationHolder aliasedHolder = new AliasedAnnotationHolder(holder, annotation);
    AnnotationChecker checker = new AnnotationChecker(aliasedHolder);
    for (GrAnnotation aliased : annotations) {
        checker.checkAnnotationArgumentList(aliased);
    }
    final GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    final String aliasQName = annotation.getQualifiedName();
    if (attributes.length == 1 && attributes[0].getNameIdentifierGroovy() == null && !usedAttributes.contains("value")) {
        holder.createErrorAnnotation(attributes[0], GroovyBundle.message("at.interface.0.does.not.contain.attribute", aliasQName, "value"));
    }
    for (GrAnnotationNameValuePair pair : attributes) {
        final PsiElement nameIdentifier = pair.getNameIdentifierGroovy();
        if (nameIdentifier != null && !usedAttributes.contains(pair.getName())) {
            holder.createErrorAnnotation(nameIdentifier, GroovyBundle.message("at.interface.0.does.not.contain.attribute", aliasQName, pair.getName()));
        }
    }
    return true;
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GrAnnotationNameValuePair

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

the class TypeCheckedAnnotationChecker method checkArgumentList.

@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    final GrCodeReferenceElement classReference = annotation.getClassReference();
    PsiElement resolved = classReference.resolve();
    if (!(resolved instanceof PsiClass && GroovyCommonClassNames.GROOVY_TRANSFORM_TYPE_CHECKED.equals(((PsiClass) resolved).getQualifiedName()))) {
        return false;
    }
    String sdkVersion = GroovyConfigUtils.getInstance().getSDKVersion(annotation);
    if (!("2.1".equals(sdkVersion) || "2.1.0".equals(sdkVersion)))
        return false;
    GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    checkAnnotationArguments(holder, (PsiClass) resolved, classReference, attributes, false);
    return true;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GrAnnotationNameValuePair

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

the class SpockUnrollReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    GrAnnotationNameValuePair nvp = (GrAnnotationNameValuePair) element.getParent();
    String name = nvp.getName();
    if (name != null && !name.equals("value"))
        return PsiReference.EMPTY_ARRAY;
    PsiElement argumentList = nvp.getParent();
    if (!(argumentList instanceof GrAnnotationArgumentList))
        return PsiReference.EMPTY_ARRAY;
    PsiElement eAnnotation = argumentList.getParent();
    if (!(eAnnotation instanceof GrAnnotation))
        return PsiReference.EMPTY_ARRAY;
    GrAnnotation annotation = (GrAnnotation) eAnnotation;
    String shortName = annotation.getShortName();
    if (!shortName.equals("Unroll") && !shortName.equals("spock.lang.Unroll"))
        return PsiReference.EMPTY_ARRAY;
    PsiElement modifierList = annotation.getParent();
    if (!(modifierList instanceof GrModifierList))
        return PsiReference.EMPTY_ARRAY;
    PsiElement eMethod = modifierList.getParent();
    if (!(eMethod instanceof GrMethod))
        return PsiReference.EMPTY_ARRAY;
    final GrMethod method = (GrMethod) eMethod;
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
    TextRange rangeInElement = manipulator.getRangeInElement(element);
    String text = rangeInElement.substring(element.getText());
    final List<SpockVariableReference> references = new ArrayList<>();
    Matcher matcher = PATTERN.matcher(text);
    while (matcher.find()) {
        TextRange range = new TextRange(rangeInElement.getStartOffset() + matcher.start(1), rangeInElement.getStartOffset() + matcher.end(1));
        references.add(new SpockVariableReference(element, range, references, method));
    }
    return references.toArray(new PsiReference[references.size()]);
}
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) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) Matcher(java.util.regex.Matcher) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrAnnotationNameValuePair (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair)10 PsiElement (com.intellij.psi.PsiElement)4 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)4 PsiClass (com.intellij.psi.PsiClass)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrAnnotationMemberValue (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiAnnotationOwner (com.intellij.psi.PsiAnnotationOwner)1 LightElement (com.intellij.psi.impl.light.LightElement)1 Processor (com.intellij.util.Processor)1 HashSet (com.intellij.util.containers.HashSet)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1