add doc comments to groovy members

This commit is contained in:
Max Medvedev
2013-07-11 08:59:07 +04:00
parent 6c8f21e42e
commit 5898ef7d96
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,15 +18,17 @@ package org.jetbrains.plugins.groovy.lang.groovydoc.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment;
import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner;
import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
import static org.jetbrains.plugins.groovy.lang.groovydoc.parser.GroovyDocElementTypes.*;
import static org.jetbrains.plugins.groovy.lang.lexer.TokenSets.*;
import static org.jetbrains.plugins.groovy.lang.groovydoc.parser.GroovyDocElementTypes.GROOVY_DOC_COMMENT;
import static org.jetbrains.plugins.groovy.lang.lexer.TokenSets.WHITE_SPACES_OR_COMMENTS;
/**
* @author Maxim.Medvedev
@@ -38,16 +40,7 @@ public abstract class GrDocCommentUtil {
while (element != null && element.getParent() instanceof GroovyDocPsiElement) element = element.getParent();
if (element == null) return null;
while (true) {
element = element.getNextSibling();
if (element == null) return null;
final ASTNode node = element.getNode();
if (node == null) return null;
if (GROOVY_DOC_COMMENT.equals(node.getElementType()) ||
!WHITE_SPACES_OR_COMMENTS.contains(node.getElementType())) {
break;
}
}
element = skipWhiteSpacesAndStopOnDoc(element, true);
if (element instanceof GrDocCommentOwner) return (GrDocCommentOwner)element;
return null;
@@ -55,24 +48,57 @@ public abstract class GrDocCommentUtil {
@Nullable
public static GrDocComment findDocComment(GrDocCommentOwner owner) {
PsiElement element;
if (owner instanceof GrVariable && owner.getParent() instanceof GrVariableDeclaration) {
element = owner.getParent().getPrevSibling();
}
else {
element = owner.getPrevSibling();
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;
}
private static PsiElement skipWhiteSpacesAndStopOnDoc(PsiElement element, boolean forward) {
while (true) {
if (element == null) return null;
element = forward ? element.getNextSibling() : element.getPrevSibling();
if (element == null) break;
final ASTNode node = element.getNode();
if (node == null) return null;
if (node == null) break;
if (GROOVY_DOC_COMMENT.equals(node.getElementType()) ||
!WHITE_SPACES_OR_COMMENTS.contains(node.getElementType())) {
break;
}
element = element.getPrevSibling();
}
if (element instanceof GrDocComment) return (GrDocComment)element;
return null;
return element;
}
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, node.getPsi());
assert added instanceof GrDocComment;
return (GrDocComment)added;
}
}
}