@Nullable
    private PsiComment createJavaDocStub(
        final CodeInsightSettings settings, final PsiComment comment, final Project project) {
      if (settings.JAVADOC_STUB_ON_ENTER) {
        final DocumentationProvider langDocumentationProvider =
            LanguageDocumentation.INSTANCE.forLanguage(comment.getParent().getLanguage());

        @Nullable final CodeDocumentationProvider docProvider;
        if (langDocumentationProvider instanceof CompositeDocumentationProvider) {
          docProvider =
              ((CompositeDocumentationProvider) langDocumentationProvider)
                  .getFirstCodeDocumentationProvider();
        } else {
          docProvider =
              langDocumentationProvider instanceof CodeDocumentationProvider
                  ? (CodeDocumentationProvider) langDocumentationProvider
                  : null;
        }

        if (docProvider != null) {
          if (docProvider.findExistingDocComment(comment) != comment) return comment;
          String docStub = docProvider.generateDocumentationContentStub(comment);

          if (docStub != null && docStub.length() != 0) {
            myOffset =
                CharArrayUtil.shiftForwardUntil(
                    myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR);
            myOffset =
                CharArrayUtil.shiftForward(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR);
            myDocument.insertString(myOffset, docStub);
          }
        }

        PsiDocumentManager.getInstance(project).commitAllDocuments();
        return PsiTreeUtil.getNonStrictParentOfType(
            myFile.findElementAt(myOffset), PsiComment.class);
      }
      return comment;
    }
예제 #2
0
  private static void process(
      @NotNull final PsiFile file,
      @NotNull final Editor editor,
      @NotNull final Project project,
      int offset) {
    PsiElement elementAtOffset = file.findElementAt(offset);
    if (elementAtOffset == null) {
      return;
    }

    Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
    final CodeDocumentationProvider docProvider;
    final DocumentationProvider langDocumentationProvider =
        LanguageDocumentation.INSTANCE.forLanguage(language);
    if (langDocumentationProvider instanceof CompositeDocumentationProvider) {
      docProvider =
          ((CompositeDocumentationProvider) langDocumentationProvider)
              .getFirstCodeDocumentationProvider();
    } else if (langDocumentationProvider instanceof CodeDocumentationProvider) {
      docProvider = (CodeDocumentationProvider) langDocumentationProvider;
    } else {
      docProvider = null;
    }
    if (docProvider == null) {
      return;
    }

    final Pair<PsiElement, PsiComment> pair = docProvider.parseContext(elementAtOffset);
    if (pair == null) {
      return;
    }

    Commenter c = LanguageCommenters.INSTANCE.forLanguage(language);
    if (!(c instanceof CodeDocumentationAwareCommenter)) {
      return;
    }
    final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) c;
    final Runnable task;
    if (pair.second == null || pair.second.getTextRange().isEmpty()) {
      task =
          new Runnable() {
            @Override
            public void run() {
              generateComment(pair.first, editor, docProvider, commenter, project);
            }
          };
    } else {
      final DocCommentFixer fixer = DocCommentFixer.EXTENSION.forLanguage(language);
      if (fixer == null) {
        return;
      } else {
        task =
            new Runnable() {
              @Override
              public void run() {
                fixer.fixComment(project, editor, pair.second);
              }
            };
      }
    }
    final Runnable command =
        new Runnable() {
          @Override
          public void run() {
            ApplicationManager.getApplication().runWriteAction(task);
          }
        };
    CommandProcessor.getInstance().executeCommand(project, command, "Fix documentation", null);
  }