Search in sources :

Example 1 with GrDocReferenceElement

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

the class JavaSafeDeleteDelegateForGroovy method createUsageInfoForParameter.

@Override
public void createUsageInfoForParameter(PsiReference reference, List<UsageInfo> usages, final PsiParameter parameter, final PsiMethod method) {
    int index = method.getParameterList().getParameterIndex(parameter);
    final PsiElement element = reference.getElement();
    GrCall call = null;
    if (element instanceof GrCall) {
        call = (GrCall) element;
    } else if (element.getParent() instanceof GrCall) {
        call = (GrCall) element.getParent();
    }
    if (call != null) {
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(call);
        //todo ???
        if (signature == null)
            return;
        GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
        //todo???
        if (argInfos == null)
            return;
        for (PsiElement arg : argInfos[index].args) {
            usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(arg, parameter, true));
        }
    } else if (element instanceof GrDocMethodReference) {
        @NonNls final StringBuilder newText = new StringBuilder();
        newText.append("/** @see ");
        GrDocReferenceElement holder = ((GrDocMethodReference) element).getReferenceHolder();
        if (holder != null) {
            newText.append(holder.getText());
        }
        newText.append('#');
        newText.append(method.getName());
        newText.append('(');
        final List<PsiParameter> parameters = new ArrayList<>(Arrays.asList(method.getParameterList().getParameters()));
        parameters.remove(parameter);
        newText.append(StringUtil.join(parameters, psiParameter -> parameter.getType().getCanonicalText(), ","));
        newText.append(")*/");
        usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, parameter, true) {

            @Override
            public void deleteElement() throws IncorrectOperationException {
                ((GrDocMethodReference) element).bindToText(method.getProject(), newText.toString());
            }
        });
    }
}
Also used : SafeDeleteReferenceJavaDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) ArrayList(java.util.ArrayList) List(java.util.List) GrDocMethodReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference)

Example 2 with GrDocReferenceElement

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

the class GroovyDocCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    final PsiElement position = parameters.getPosition();
    GrDocMemberReference reference = PsiTreeUtil.getParentOfType(position, GrDocMemberReference.class);
    if (reference == null)
        return;
    GrDocReferenceElement holder = reference.getReferenceHolder();
    PsiElement resolved;
    if (holder != null) {
        GrCodeReferenceElement referenceElement = holder.getReferenceElement();
        resolved = referenceElement != null ? referenceElement.resolve() : null;
    } else {
        resolved = PsiUtil.getContextClass(reference);
    }
    if (resolved instanceof PsiClass) {
        ResolverProcessor propertyProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
        resolved.processDeclarations(propertyProcessor, ResolveState.initial(), null, reference);
        PsiElement[] propertyCandidates = ResolveUtil.mapToElements(propertyProcessor.getCandidates());
        ResolverProcessor methodProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
        resolved.processDeclarations(methodProcessor, ResolveState.initial(), null, reference);
        PsiElement[] methodCandidates = ResolveUtil.mapToElements(methodProcessor.getCandidates());
        PsiElement[] elements = ArrayUtil.mergeArrays(propertyCandidates, methodCandidates);
        for (PsiElement psiElement : elements) {
            LookupElement element = GroovyCompletionUtil.createLookupElement((PsiNamedElement) psiElement);
            if (psiElement instanceof PsiMethod) {
                element = ((LookupElementBuilder) element).withInsertHandler(new GroovyMethodSignatureInsertHandler());
            }
            result.addElement(element);
        }
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) GroovyMethodSignatureInsertHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodSignatureInsertHandler) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 3 with GrDocReferenceElement

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

the class GrDocMemberReferenceImpl method bindToElement.

@Override
@Nullable
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    if (isReferenceTo(element))
        return this;
    if (element instanceof PsiClass) {
        GrDocReferenceElement holder = getReferenceHolder();
        if (holder != null) {
            return replace(holder.getReferenceElement().bindToElement(element).getParent());
        }
        GrDocReferenceElement ref = GroovyPsiElementFactory.getInstance(getProject()).createDocReferenceElementFromFQN(((PsiClass) element).getQualifiedName());
        return replace(ref);
    } else if (element instanceof PsiMember) {
        PsiClass clazz = ((PsiMember) element).getContainingClass();
        if (clazz == null)
            return null;
        String qName = clazz.getQualifiedName();
        String memberRefText;
        if (element instanceof PsiField) {
            memberRefText = ((PsiField) element).getName();
        } else if (element instanceof PsiMethod) {
            PsiParameterList list = ((PsiMethod) element).getParameterList();
            StringBuilder builder = new StringBuilder();
            builder.append(((PsiMethod) element).getName()).append("(");
            PsiParameter[] params = list.getParameters();
            for (int i = 0; i < params.length; i++) {
                PsiParameter parameter = params[i];
                PsiType type = parameter.getType();
                if (i > 0)
                    builder.append(", ");
                builder.append(type.getPresentableText());
            }
            builder.append(")");
            memberRefText = builder.toString();
        } else {
            return null;
        }
        GrDocMemberReference ref = GroovyPsiElementFactory.getInstance(getProject()).createDocMemberReferenceFromText(qName, memberRefText);
        return replace(ref);
    }
    return null;
}
Also used : GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrDocReferenceElement

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

the class GrDocMethodParamsImpl method getParameterTypes.

@Override
public PsiType[] getParameterTypes() {
    ArrayList<PsiType> types = new ArrayList<>();
    PsiManagerEx manager = getManager();
    GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
    for (GrDocMethodParameter parameter : getParameters()) {
        GrDocReferenceElement typeElement = parameter.getTypeElement();
        try {
            PsiType type = factory.createTypeFromText(typeElement.getText(), this);
            type = TypesUtil.boxPrimitiveType(type, manager, scope);
            types.add(type);
        } catch (IncorrectOperationException e) {
            LOG.info(e);
            types.add(null);
        }
    }
    return types.toArray(PsiType.createArray(types.size()));
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) ArrayList(java.util.ArrayList) PsiElementFactory(com.intellij.psi.PsiElementFactory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrDocMethodParameter(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodParameter) PsiType(com.intellij.psi.PsiType) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 5 with GrDocReferenceElement

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

the class GrDocFieldReferenceImpl method multiResolveImpl.

@Override
protected ResolveResult[] multiResolveImpl() {
    String name = getReferenceName();
    GrDocReferenceElement holder = getReferenceHolder();
    PsiElement resolved;
    if (holder != null) {
        GrCodeReferenceElement referenceElement = holder.getReferenceElement();
        resolved = referenceElement != null ? referenceElement.resolve() : null;
    } else {
        resolved = PsiUtil.getContextClass(GrDocCommentUtil.findDocOwner(this));
    }
    if (resolved instanceof PsiClass) {
        PropertyResolverProcessor processor = new PropertyResolverProcessor(name, this);
        resolved.processDeclarations(processor, ResolveState.initial(), resolved, this);
        GroovyResolveResult[] candidates = processor.getCandidates();
        if (candidates.length == 0) {
            PsiType thisType = JavaPsiFacade.getInstance(getProject()).getElementFactory().createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
            MethodResolverProcessor methodProcessor = new MethodResolverProcessor(name, this, false, thisType, null, PsiType.EMPTY_ARRAY);
            MethodResolverProcessor constructorProcessor = new MethodResolverProcessor(name, this, true, thisType, null, PsiType.EMPTY_ARRAY);
            resolved.processDeclarations(methodProcessor, ResolveState.initial(), resolved, this);
            resolved.processDeclarations(constructorProcessor, ResolveState.initial(), resolved, this);
            candidates = ArrayUtil.mergeArrays(methodProcessor.getCandidates(), constructorProcessor.getCandidates());
            if (candidates.length > 0) {
                candidates = new GroovyResolveResult[] { candidates[0] };
            }
        }
        return candidates;
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) PropertyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.PropertyResolverProcessor)

Aggregations

GrDocReferenceElement (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement)5 ArrayList (java.util.ArrayList)2 GrDocMemberReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 PsiElementFactory (com.intellij.psi.PsiElementFactory)1 PsiType (com.intellij.psi.PsiType)1 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 SafeDeleteReferenceJavaDeleteUsageInfo (com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 List (java.util.List)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyMethodSignatureInsertHandler (org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodSignatureInsertHandler)1 GrDocMethodParameter (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodParameter)1 GrDocMethodReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)1 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)1 MethodResolverProcessor (org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor)1