public static GrDocComment setDocComment(
      @NotNull GrDocCommentOwner owner, @Nullable GrDocComment comment) {
    GrDocComment docComment = owner.getDocComment();

    if (docComment != null) {
      if (comment == null) {
        docComment.delete();
        return null;
      } else {
        PsiElement added = docComment.replace(comment);
        assert added instanceof GrDocComment;
        return (GrDocComment) added;
      }
    } else {
      if (comment == null) return null;

      PsiElement parent = owner.getParent();

      ASTNode node = owner.getNode();
      parent.getNode().addLeaf(GroovyTokenTypes.mNLS, "\n ", node);

      PsiElement added = parent.addBefore(comment, owner);
      assert added instanceof GrDocComment;

      return (GrDocComment) added;
    }
  }
 private static void deleteMemberWithDocComment(GrDocCommentOwner docCommentOwner) {
   GrDocComment oldDoc = docCommentOwner.getDocComment();
   if (oldDoc != null) {
     oldDoc.delete();
   }
   docCommentOwner.delete();
 }
 public PsiComment findExistingDocComment(PsiComment contextElement) {
   if (contextElement instanceof GrDocComment) {
     final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextElement);
     if (owner != null) {
       return owner.getDocComment();
     }
   }
   return null;
 }
  @Nullable
  public static GrDocComment findDocComment(GrDocCommentOwner owner) {
    if (owner.getFirstChild() instanceof GrDocComment) {
      return ((GrDocComment) owner.getFirstChild());
    }

    PsiElement element =
        owner instanceof GrVariable && owner.getParent() instanceof GrVariableDeclaration
            ? owner.getParent()
            : owner;

    element = skipWhiteSpacesAndStopOnDoc(element, false);
    if (element instanceof GrDocComment) return (GrDocComment) element;
    return null;
  }
  @Override
  public String generateDocumentationContentStub(PsiComment contextComment) {
    if (!(contextComment instanceof GrDocComment)) {
      return null;
    }

    final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextComment);
    if (owner == null) return null;

    Project project = contextComment.getProject();
    final CodeDocumentationAwareCommenter commenter =
        (CodeDocumentationAwareCommenter)
            LanguageCommenters.INSTANCE.forLanguage(owner.getLanguage());

    StringBuilder builder = StringBuilderSpinAllocator.alloc();
    try {
      if (owner instanceof GrMethod) {
        final GrMethod method = (GrMethod) owner;
        JavaDocumentationProvider.generateParametersTakingDocFromSuperMethods(
            project, builder, commenter, method);

        final PsiType returnType = method.getInferredReturnType();
        if ((returnType != null || method.getModifierList().hasModifierProperty(GrModifier.DEF))
            && !PsiType.VOID.equals(returnType)) {
          builder.append(
              CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
          builder.append(LINE_SEPARATOR);
        }

        final PsiClassType[] references = method.getThrowsList().getReferencedTypes();
        for (PsiClassType reference : references) {
          builder.append(
              CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
          builder.append(reference.getClassName());
          builder.append(LINE_SEPARATOR);
        }
      } else if (owner instanceof GrTypeDefinition) {
        final PsiTypeParameterList typeParameterList = ((PsiClass) owner).getTypeParameterList();
        if (typeParameterList != null) {
          JavaDocumentationProvider.createTypeParamsListComment(
              builder, project, commenter, typeParameterList);
        }
      }
      return builder.length() > 0 ? builder.toString() : null;
    } finally {
      StringBuilderSpinAllocator.dispose(builder);
    }
  }
Exemplo n.º 6
0
 @Nullable
 public static GrDocComment findDocComment(GrDocCommentOwner owner) {
   PsiElement element = owner.getPrevSibling();
   while (true) {
     if (element == null) return null;
     final ASTNode node = element.getNode();
     if (node == null) return null;
     if (GroovyElementTypes.GROOVY_DOC_COMMENT.equals(node.getElementType())
         || !GroovyElementTypes.WHITE_SPACES_OR_COMMENTS.contains(node.getElementType())) {
       break;
     }
     element = element.getPrevSibling();
   }
   if (element instanceof GrDocComment) return (GrDocComment) element;
   return null;
 }
  public String generateDocumentationContentStub(PsiComment contextComment) {
    if (!(contextComment instanceof GrDocComment)) {
      return null;
    }

    final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextComment);
    if (owner == null) return null;

    Project project = contextComment.getProject();
    final CodeDocumentationAwareCommenter commenter =
        (CodeDocumentationAwareCommenter)
            LanguageCommenters.INSTANCE.forLanguage(owner.getLanguage());

    StringBuilder builder = StringBuilderSpinAllocator.alloc();
    try {
      if (owner instanceof GrMethod) {
        final GrMethod method = (GrMethod) owner;
        final GrParameter[] parameters = method.getParameters();
        final Map<String, String> param2Description = new HashMap<String, String>();
        final PsiMethod[] superMethods = method.findSuperMethods();

        for (PsiMethod superMethod : superMethods) {
          final PsiDocComment comment = superMethod.getDocComment();
          if (comment != null) {
            final PsiDocTag[] params = comment.findTagsByName("param");
            for (PsiDocTag param : params) {
              final PsiElement[] dataElements = param.getDataElements();
              if (dataElements != null) {
                String paramName = null;
                for (PsiElement dataElement : dataElements) {
                  if (dataElement instanceof PsiDocParamRef) {
                    paramName = dataElement.getReference().getCanonicalText();
                    break;
                  }
                }
                if (paramName != null) {
                  param2Description.put(paramName, param.getText());
                }
              }
            }
          }
        }
        for (PsiParameter parameter : parameters) {
          String description = param2Description.get(parameter.getName());
          if (description != null) {
            builder.append(CodeDocumentationUtil.createDocCommentLine("", project, commenter));
            if (description.indexOf('\n') > -1)
              description = description.substring(0, description.lastIndexOf('\n'));
            builder.append(description);
          } else {
            builder.append(
                CodeDocumentationUtil.createDocCommentLine(PARAM_TAG, project, commenter));
            builder.append(parameter.getName());
          }
          builder.append(LINE_SEPARATOR);
        }

        final PsiType returnType = method.getInferredReturnType();
        if ((returnType != null || method.getModifierList().hasModifierProperty(GrModifier.DEF))
            && returnType != PsiType.VOID) {
          builder.append(
              CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
          builder.append(LINE_SEPARATOR);
        }

        final PsiClassType[] references = method.getThrowsList().getReferencedTypes();
        for (PsiClassType reference : references) {
          builder.append(
              CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
          builder.append(reference.getClassName());
          builder.append(LINE_SEPARATOR);
        }
      } else if (owner instanceof GrTypeDefinition) {
        final PsiTypeParameterList typeParameterList = ((PsiClass) owner).getTypeParameterList();
        if (typeParameterList != null) {
          createTypeParamsListComment(builder, project, commenter, typeParameterList);
        }
      }
      return builder.length() > 0 ? builder.toString() : null;
    } finally {
      StringBuilderSpinAllocator.dispose(builder);
    }
  }