Search in sources :

Example 1 with GrAnnotation

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

the class GrKindWeigher method weigh.

@Override
public Comparable weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
    final PsiElement position = location.getCompletionParameters().getPosition();
    if (!(position.getContainingFile() instanceof GroovyFileBase))
        return null;
    Object o = element.getObject();
    if (o instanceof ResolveResult) {
        o = ((ResolveResult) o).getElement();
    }
    final PsiElement parent = position.getParent();
    final PsiElement qualifier = parent instanceof GrReferenceElement ? ((GrReferenceElement) parent).getQualifier() : null;
    if (qualifier == null) {
        if (o instanceof NamedArgumentDescriptor) {
            switch(((NamedArgumentDescriptor) o).getPriority()) {
                case ALWAYS_ON_TOP:
                    return NotQualifiedKind.onTop;
                case AS_LOCAL_VARIABLE:
                    return NotQualifiedKind.local;
                default:
                    return NotQualifiedKind.unknown;
            }
        }
        if (o instanceof PsiVariable && !(o instanceof PsiField)) {
            return NotQualifiedKind.local;
        }
        PsiTypeLookupItem item = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
        if (item != null && item.getBracketsCount() > 0) {
            return NotQualifiedKind.arrayType;
        }
        if (isPriorityKeyword(o))
            return NotQualifiedKind.local;
        if (isLightElement(o))
            return NotQualifiedKind.unknown;
        if (o instanceof PsiClass) {
            if (((PsiClass) o).isAnnotationType() && GrMainCompletionProvider.AFTER_AT.accepts(position)) {
                final GrAnnotation annotation = PsiTreeUtil.getParentOfType(position, GrAnnotation.class);
                if (annotation != null) {
                    PsiElement annoParent = annotation.getParent();
                    PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
                    PsiAnnotation.TargetType[] elementTypeFields = GrAnnotationImpl.getApplicableElementTypeFields(ownerToUse);
                    if (AnnotationTargetUtil.findAnnotationTarget((PsiClass) o, elementTypeFields) != null) {
                        return NotQualifiedKind.restrictedClass;
                    }
                }
            }
            if (GrMainCompletionProvider.IN_CATCH_TYPE.accepts(position) && InheritanceUtil.isInheritor((PsiClass) o, CommonClassNames.JAVA_LANG_THROWABLE)) {
                return NotQualifiedKind.restrictedClass;
            }
        }
        if (o instanceof PsiMember) {
            final PsiClass containingClass = ((PsiMember) o).getContainingClass();
            if (isAccessor((PsiMember) o))
                return NotQualifiedKind.accessor;
            if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
                return NotQualifiedKind.unknown;
            if (o instanceof PsiClass)
                return NotQualifiedKind.innerClass;
            if (PsiTreeUtil.isContextAncestor(containingClass, position, false))
                return NotQualifiedKind.currentClassMember;
            return NotQualifiedKind.member;
        }
        return NotQualifiedKind.unknown;
    } else {
        if (o instanceof PsiEnumConstant)
            return QualifiedKind.enumConstant;
        if (isLightElement(o))
            return QualifiedKind.unknown;
        if (o instanceof PsiMember) {
            if (isTrashMethod((PsiMember) o))
                return QualifiedKind.unknown;
            if (isAccessor((PsiMember) o))
                return QualifiedKind.accessor;
            if (isQualifierClassMember((PsiMember) o, qualifier)) {
                return QualifiedKind.currentClassMember;
            }
            if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
                return QualifiedKind.unknown;
            if (o instanceof PsiClass)
                return QualifiedKind.innerClass;
            return QualifiedKind.member;
        }
        return QualifiedKind.unknown;
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 2 with GrAnnotation

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

the class GroovyAnnotationAttributeInfoHandler method showParameterInfo.

@Override
public void showParameterInfo(@NotNull GrAnnotationArgumentList argumentList, @NotNull CreateParameterInfoContext context) {
    final GrAnnotation parent = (GrAnnotation) argumentList.getParent();
    final PsiElement resolved = parent.getClassReference().resolve();
    if (resolved instanceof PsiClass && ((PsiClass) resolved).isAnnotationType()) {
        final PsiAnnotationMethod[] methods = extractAnnotationMethodsFromClass((PsiClass) resolved);
        context.setItemsToShow(methods);
        context.showHint(argumentList, argumentList.getTextRange().getStartOffset(), this);
        final PsiAnnotationMethod currentMethod = findAnnotationMethod(context.getFile(), context.getEditor());
        if (currentMethod != null) {
            context.setHighlightedElement(currentMethod);
        }
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)

Example 3 with GrAnnotation

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

the class GrabDependencies method prepareQueries.

static Map<String, String> prepareQueries(PsiFile file) {
    final Set<GrAnnotation> grabs = new LinkedHashSet<>();
    final Set<GrAnnotation> excludes = new THashSet<>();
    final Set<GrAnnotation> resolvers = new THashSet<>();
    file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof GrAnnotation) {
                GrAnnotation anno = (GrAnnotation) element;
                String qname = anno.getQualifiedName();
                if (GrabAnnos.GRAB_ANNO.equals(qname))
                    grabs.add(anno);
                else if (GrabAnnos.GRAB_EXCLUDE_ANNO.equals(qname))
                    excludes.add(anno);
                else if (GrabAnnos.GRAB_RESOLVER_ANNO.equals(qname))
                    resolvers.add(anno);
            }
            super.visitElement(element);
        }
    });
    Function<GrAnnotation, String> mapper = grAnnotation -> grAnnotation.getText();
    String common = StringUtil.join(excludes, mapper, " ") + " " + StringUtil.join(resolvers, mapper, " ");
    LinkedHashMap<String, String> result = new LinkedHashMap<>();
    for (GrAnnotation grab : grabs) {
        String grabText = grab.getText();
        result.put(grabText, (grabText + " " + common).trim());
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) THashSet(gnu.trove.THashSet) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyScriptRunConfiguration(org.jetbrains.plugins.groovy.runner.GroovyScriptRunConfiguration) Library(com.intellij.openapi.roots.libraries.Library) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Task(com.intellij.openapi.progress.Task) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) Messages(com.intellij.openapi.ui.Messages) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) OrderRootType(com.intellij.openapi.roots.OrderRootType) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) NotificationType(com.intellij.notification.NotificationType) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) JavaSdkType(com.intellij.openapi.projectRoots.JavaSdkType) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) Function(com.intellij.util.Function) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) NotificationDisplayType(com.intellij.notification.NotificationDisplayType) java.util(java.util) WriteAction(com.intellij.openapi.application.WriteAction) ExecutionException(com.intellij.execution.ExecutionException) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) ContainerUtil(com.intellij.util.containers.ContainerUtil) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) NotificationGroup(com.intellij.notification.NotificationGroup) Project(com.intellij.openapi.project.Project) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) ProcessOutputTypes(com.intellij.execution.process.ProcessOutputTypes) MalformedURLException(java.net.MalformedURLException) StringUtil(com.intellij.openapi.util.text.StringUtil) DefaultGroovyScriptRunner(org.jetbrains.plugins.groovy.runner.DefaultGroovyScriptRunner) Key(com.intellij.openapi.util.Key) Editor(com.intellij.openapi.editor.Editor) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) ExceptionUtil(com.intellij.util.ExceptionUtil) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Result(com.intellij.openapi.application.Result) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) JavaParameters(com.intellij.execution.configurations.JavaParameters) PathUtil(com.intellij.util.PathUtil) CantRunException(com.intellij.execution.CantRunException) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) THashSet(gnu.trove.THashSet)

Example 4 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation 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 5 with GrAnnotation

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

the class GroovyOverrideImplementUtil method setupAnnotations.

private static void setupAnnotations(@NotNull GrTypeDefinition aClass, @NotNull PsiMethod method, @NotNull GrMethod result) {
    if (OverrideImplementUtil.isInsertOverride(method, aClass)) {
        result.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE);
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    final PsiParameter[] originalParams = method.getParameterList().getParameters();
    GrParameter[] parameters = result.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        GrParameter parameter = parameters[i];
        PsiParameter original = originalParams[i];
        for (PsiAnnotation annotation : original.getModifierList().getAnnotations()) {
            final GrModifierList modifierList = parameter.getModifierList();
            String qname = annotation.getQualifiedName();
            if (qname != null && modifierList.findAnnotation(qname) == null) {
                if (annotation instanceof GrAnnotation) {
                    modifierList.add(annotation);
                } else {
                    modifierList.add(factory.createAnnotationFromText(annotation.getText()));
                }
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

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