/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/groovydoc/psi/impl/GrDocTagImpl.java

https://bitbucket.org/nbargnesi/idea · Java · 101 lines · 66 code · 17 blank · 18 comment · 1 complexity · 50624558eeacc6c36943fc3b989c31ee MD5 · raw file

  1. /*
  2. * Copyright 2000-2012 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jetbrains.plugins.groovy.lang.groovydoc.psi.impl;
  17. import com.intellij.lang.ASTNode;
  18. import com.intellij.psi.PsiElement;
  19. import com.intellij.psi.tree.TokenSet;
  20. import com.intellij.psi.util.PsiUtilCore;
  21. import com.intellij.util.IncorrectOperationException;
  22. import org.jetbrains.annotations.NonNls;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.Nullable;
  25. import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment;
  26. import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocParameterReference;
  27. import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag;
  28. import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTagValueToken;
  29. import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
  30. import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
  31. import java.util.List;
  32. import static org.jetbrains.plugins.groovy.lang.groovydoc.lexer.GroovyDocTokenTypes.mGDOC_COMMENT_DATA;
  33. import static org.jetbrains.plugins.groovy.lang.groovydoc.lexer.GroovyDocTokenTypes.mGDOC_TAG_NAME;
  34. import static org.jetbrains.plugins.groovy.lang.groovydoc.lexer.GroovyDocTokenTypes.mGDOC_TAG_VALUE_TOKEN;
  35. import static org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes.*;
  36. /**
  37. * @author ilyas
  38. */
  39. public class GrDocTagImpl extends GroovyDocPsiElementImpl implements GrDocTag {
  40. private static final TokenSet VALUE_BIT_SET = TokenSet
  41. .create(mGDOC_TAG_VALUE_TOKEN, GDOC_METHOD_REF, GDOC_FIELD_REF, GDOC_PARAM_REF, GDOC_REFERENCE_ELEMENT, mGDOC_COMMENT_DATA,
  42. GDOC_INLINED_TAG);
  43. public GrDocTagImpl(@NotNull ASTNode node) {
  44. super(node);
  45. }
  46. public void accept(GroovyElementVisitor visitor) {
  47. visitor.visitDocTag(this);
  48. }
  49. public String toString() {
  50. return "GroovyDocTag";
  51. }
  52. @NotNull
  53. public String getName() {
  54. return getNameElement().getText().substring(1);
  55. }
  56. @NotNull
  57. public PsiElement getNameElement() {
  58. PsiElement element = findChildByType(mGDOC_TAG_NAME);
  59. assert element != null;
  60. return element;
  61. }
  62. public GrDocComment getContainingComment() {
  63. return (GrDocComment)getParent();
  64. }
  65. @Nullable
  66. public GrDocTagValueToken getValueElement() {
  67. return findChildByClass(GrDocTagValueToken.class);
  68. }
  69. @Nullable
  70. public GrDocParameterReference getDocParameterReference() {
  71. return findChildByClass(GrDocParameterReference.class);
  72. }
  73. public PsiElement[] getDataElements() {
  74. final List<PsiElement> list = findChildrenByType(VALUE_BIT_SET);
  75. return PsiUtilCore.toPsiElementArray(list);
  76. }
  77. public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
  78. final PsiElement nameElement = getNameElement();
  79. final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
  80. final GrDocComment comment = factory.createDocCommentFromText("/** @" + name + "*/");
  81. nameElement.replace(comment.getTags()[0].getNameElement());
  82. return this;
  83. }
  84. }