Search in sources :

Example 1 with GrDocMemberReference

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference 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 2 with GrDocMemberReference

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

the class GroovyMethodSignatureInsertHandler method shortenReferences.

private static void shortenReferences(final Project project, final Editor editor, InsertionContext context, int offset) {
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    final PsiElement element = context.getFile().findElementAt(offset);
    final GrDocMemberReference tagValue = PsiTreeUtil.getParentOfType(element, GrDocMemberReference.class);
    if (tagValue != null) {
        try {
            JavaCodeStyleManager.getInstance(project).shortenClassReferences(tagValue);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
    PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference)

Example 3 with GrDocMemberReference

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference 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 GrDocMemberReference

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

the class GroovyPsiElementFactoryImpl method createDocMemberReferenceNameFromText.

@NotNull
@Override
public PsiElement createDocMemberReferenceNameFromText(@NotNull String idText) {
    GrDocMemberReference reference = createDocMemberReferenceFromText("Foo", idText);
    LOG.assertTrue(reference != null, idText);
    return reference.getReferenceNameElement();
}
Also used : GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GrDocMemberReference

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

the class GroovyDocCheckInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitDocMethodReference(@NotNull GrDocMethodReference reference) {
            checkGrDocMemberReference(reference);
        }

        @Override
        public void visitDocFieldReference(@NotNull GrDocFieldReference reference) {
            checkGrDocMemberReference(reference);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            GroovyResolveResult resolveResult = refElement.advancedResolve();
            if (refElement.getReferenceName() == null)
                return;
            if (PsiTreeUtil.getParentOfType(refElement, GroovyDocPsiElement.class, true, GrMember.class, GrCodeBlock.class) == null)
                return;
            final PsiElement resolved = resolveResult.getElement();
            if (resolved != null)
                return;
            final PsiElement toHighlight = refElement.getReferenceNameElement();
            registerError(toHighlight, GroovyBundle.message("cannot.resolve", refElement.getReferenceName()));
        }

        private void checkGrDocMemberReference(final GrDocMemberReference reference) {
            if (reference.resolve() != null)
                return;
            registerError(reference.getReferenceNameElement(), GroovyBundle.message("cannot.resolve", reference.getReferenceName()));
        }
    };
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrDocFieldReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocFieldReference) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) GrDocMethodReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrDocMemberReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference)5 NotNull (org.jetbrains.annotations.NotNull)2 GrDocReferenceElement (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 PsiElement (com.intellij.psi.PsiElement)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Nullable (org.jetbrains.annotations.Nullable)1 BaseInspectionVisitor (org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor)1 GroovyMethodSignatureInsertHandler (org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodSignatureInsertHandler)1 GrDocFieldReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocFieldReference)1 GrDocMethodReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference)1 GroovyDocPsiElement (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)1 ResolverProcessor (org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor)1