/plugins/IntentionPowerPak/src/com/siyeh/ipp/comment/ChangeToCStyleCommentIntention.java

https://bitbucket.org/nbargnesi/idea · Java · 130 lines · 106 code · 9 blank · 15 comment · 19 complexity · 85b85e40356b3ed67c75f5d4dc4db284 MD5 · raw file

  1. /*
  2. * Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
  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 com.siyeh.ipp.comment;
  17. import com.intellij.openapi.util.text.StringUtil;
  18. import com.intellij.psi.*;
  19. import com.intellij.psi.tree.IElementType;
  20. import com.intellij.psi.util.PsiTreeUtil;
  21. import com.intellij.util.IncorrectOperationException;
  22. import com.siyeh.ipp.base.Intention;
  23. import com.siyeh.ipp.base.PsiElementPredicate;
  24. import org.jetbrains.annotations.NotNull;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. public class ChangeToCStyleCommentIntention extends Intention {
  28. private static final Class<PsiWhiteSpace>[] WHITESPACE_CLASS =
  29. new Class[]{PsiWhiteSpace.class};
  30. @Override
  31. @NotNull
  32. protected PsiElementPredicate getElementPredicate() {
  33. return new EndOfLineCommentPredicate();
  34. }
  35. @Override
  36. public void processIntention(PsiElement element)
  37. throws IncorrectOperationException {
  38. PsiComment firstComment = (PsiComment)element;
  39. while (true) {
  40. final PsiElement prevComment =
  41. PsiTreeUtil.skipSiblingsBackward(firstComment,
  42. WHITESPACE_CLASS);
  43. if (!isEndOfLineComment(prevComment)) {
  44. break;
  45. }
  46. assert prevComment != null;
  47. firstComment = (PsiComment)prevComment;
  48. }
  49. final JavaPsiFacade psiFacade =
  50. JavaPsiFacade.getInstance(element.getProject());
  51. final PsiElementFactory factory = psiFacade.getElementFactory();
  52. final List<PsiComment> multiLineComments = new ArrayList<PsiComment>();
  53. PsiElement nextComment = firstComment;
  54. String whiteSpace = null;
  55. while (true) {
  56. nextComment = PsiTreeUtil.skipSiblingsForward(nextComment,
  57. WHITESPACE_CLASS);
  58. if (!isEndOfLineComment(nextComment)) {
  59. break;
  60. }
  61. assert nextComment != null;
  62. if (whiteSpace == null) {
  63. final PsiElement prevSibling = nextComment.getPrevSibling();
  64. assert prevSibling != null;
  65. final String text = prevSibling.getText();
  66. whiteSpace = getIndent(text);
  67. }
  68. multiLineComments.add((PsiComment)nextComment);
  69. }
  70. final String newCommentString;
  71. if (multiLineComments.isEmpty()) {
  72. final String text = getCommentContents(firstComment);
  73. newCommentString = "/* " + text + " */";
  74. }
  75. else {
  76. final StringBuilder text = new StringBuilder();
  77. text.append("/*\n");
  78. text.append(whiteSpace);
  79. text.append(getCommentContents(firstComment));
  80. for (PsiComment multiLineComment : multiLineComments) {
  81. text.append('\n');
  82. text.append(whiteSpace);
  83. text.append(getCommentContents(multiLineComment));
  84. }
  85. text.append('\n');
  86. text.append(whiteSpace);
  87. text.append("*/");
  88. newCommentString = text.toString();
  89. }
  90. final PsiComment newComment =
  91. factory.createCommentFromText(newCommentString, element);
  92. firstComment.replace(newComment);
  93. for (PsiElement commentToDelete : multiLineComments) {
  94. commentToDelete.delete();
  95. }
  96. }
  97. private static String getIndent(String whitespace) {
  98. for (int i = whitespace.length() - 1; i >= 0; i--) {
  99. final char c = whitespace.charAt(i);
  100. if (c == '\n') {
  101. if (i == whitespace.length() - 1) {
  102. return "";
  103. }
  104. return whitespace.substring(i + 1);
  105. }
  106. }
  107. return whitespace;
  108. }
  109. private static boolean isEndOfLineComment(PsiElement element) {
  110. if (!(element instanceof PsiComment)) {
  111. return false;
  112. }
  113. final PsiComment comment = (PsiComment)element;
  114. final IElementType tokenType = comment.getTokenType();
  115. return JavaTokenType.END_OF_LINE_COMMENT.equals(tokenType);
  116. }
  117. private static String getCommentContents(@NotNull PsiComment comment) {
  118. final String text = comment.getText();
  119. return StringUtil.replace(text.substring(2), "*/", "* /").trim();
  120. }
  121. }