/platform/lang-impl/src/com/intellij/tools/ScanSourceCommentsAction.java

https://bitbucket.org/nbargnesi/idea · Java · 153 lines · 111 code · 24 blank · 18 comment · 13 complexity · 24c26287454870159c95d075a1cf6b99 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 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. /*
  17. * @author max
  18. */
  19. package com.intellij.tools;
  20. import com.intellij.openapi.actionSystem.AnAction;
  21. import com.intellij.openapi.actionSystem.AnActionEvent;
  22. import com.intellij.openapi.actionSystem.PlatformDataKeys;
  23. import com.intellij.openapi.diagnostic.Logger;
  24. import com.intellij.openapi.fileTypes.LanguageFileType;
  25. import com.intellij.openapi.progress.ProgressIndicator;
  26. import com.intellij.openapi.progress.ProgressManager;
  27. import com.intellij.openapi.project.Project;
  28. import com.intellij.openapi.roots.ContentIterator;
  29. import com.intellij.openapi.roots.ProjectRootManager;
  30. import com.intellij.openapi.ui.Messages;
  31. import com.intellij.openapi.vfs.VirtualFile;
  32. import com.intellij.psi.PsiComment;
  33. import com.intellij.psi.PsiFile;
  34. import com.intellij.psi.PsiManager;
  35. import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
  36. import java.io.PrintStream;
  37. import java.util.HashMap;
  38. import java.util.LinkedHashSet;
  39. import java.util.Map;
  40. import java.util.Set;
  41. public class ScanSourceCommentsAction extends AnAction {
  42. private static final Logger LOG = Logger.getInstance("#com.intellij.tools.ScanSourceCommentsAction");
  43. @Override
  44. public void actionPerformed(AnActionEvent e) {
  45. final Project p = PlatformDataKeys.PROJECT.getData(e.getDataContext());
  46. final String file =
  47. Messages.showInputDialog(p, "Enter path to the file comments will be extracted to", "Comments File Path", Messages.getQuestionIcon());
  48. try {
  49. final PrintStream stream = new PrintStream(file);
  50. stream.println("Comments in " + p.getName());
  51. ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
  52. public void run() {
  53. final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
  54. ProjectRootManager.getInstance(p).getFileIndex().iterateContent(new ContentIterator() {
  55. public boolean processFile(VirtualFile fileOrDir) {
  56. if (fileOrDir.isDirectory()) {
  57. indicator.setText("Extracting comments");
  58. indicator.setText2(fileOrDir.getPresentableUrl());
  59. }
  60. scanCommentsInFile(p, fileOrDir);
  61. return true;
  62. }
  63. });
  64. indicator.setText2("");
  65. int count = 1;
  66. for (CommentDescriptor descriptor : myComments.values()) {
  67. stream.println("#" + count + " ---------------------------------------------------------------");
  68. descriptor.print(stream);
  69. stream.println();
  70. count++;
  71. }
  72. }
  73. }, "Generating Comments", true, p);
  74. stream.close();
  75. }
  76. catch (Throwable e1) {
  77. LOG.error(e1);
  78. Messages.showErrorDialog(p, "Error writing? " + e1.getMessage(), "Problem writing");
  79. }
  80. }
  81. private final Map<String, CommentDescriptor> myComments = new HashMap<String, CommentDescriptor>();
  82. private void commentFound(VirtualFile file, String text) {
  83. String reduced = text.replaceAll("\\s", "");
  84. CommentDescriptor descriptor = myComments.get(reduced);
  85. if (descriptor == null) {
  86. descriptor = new CommentDescriptor(text);
  87. myComments.put(reduced, descriptor);
  88. }
  89. descriptor.addFile(file);
  90. }
  91. private void scanCommentsInFile(Project project, final VirtualFile vFile) {
  92. if (!vFile.isDirectory() && vFile.getFileType() instanceof LanguageFileType) {
  93. PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
  94. if (psiFile == null) return;
  95. for (PsiFile root : psiFile.getViewProvider().getAllFiles()) {
  96. root.accept(new PsiRecursiveElementWalkingVisitor() {
  97. @Override
  98. public void visitComment(PsiComment comment) {
  99. commentFound(vFile, comment.getText());
  100. }
  101. });
  102. }
  103. }
  104. }
  105. private class CommentDescriptor {
  106. private final String myText;
  107. private final Set<VirtualFile> myFiles = new LinkedHashSet<VirtualFile>();
  108. public CommentDescriptor(String text) {
  109. myText = text;
  110. }
  111. public void addFile(VirtualFile file) {
  112. myFiles.add(file);
  113. }
  114. public void print(PrintStream out) {
  115. out.println(myText);
  116. int count = myFiles.size();
  117. int i = 0;
  118. for (VirtualFile file : myFiles) {
  119. out.println(file.getPresentableUrl());
  120. count--;
  121. i++;
  122. if (i > 5 && count > 2) break;
  123. }
  124. if (count > 0) {
  125. out.println("And " + count + " more files");
  126. }
  127. }
  128. }
  129. }