Search in sources :

Example 1 with DocCommentFixer

use of com.intellij.codeInsight.documentation.DocCommentFixer in project intellij-community by JetBrains.

the class FixDocCommentAction method generateOrFixComment.

/**
   * Generates comment if it's not exist or try to fix if exists
   *
   * @param element     target element for which a comment should be generated
   * @param project     current project
   * @param editor      target editor
   */
public static void generateOrFixComment(@NotNull final PsiElement element, @NotNull final Project project, @NotNull final Editor editor) {
    Language language = element.getLanguage();
    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(element);
    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 = () -> generateComment(pair.first, editor, docProvider, commenter, project);
    } else {
        final DocCommentFixer fixer = DocCommentFixer.EXTENSION.forLanguage(language);
        if (fixer == null) {
            return;
        } else {
            task = () -> fixer.fixComment(project, editor, pair.second);
        }
    }
    final Runnable command = () -> ApplicationManager.getApplication().runWriteAction(task);
    CommandProcessor.getInstance().executeCommand(project, command, "Fix documentation", null);
}
Also used : CodeDocumentationProvider(com.intellij.lang.documentation.CodeDocumentationProvider) CompositeDocumentationProvider(com.intellij.lang.documentation.CompositeDocumentationProvider) DocCommentFixer(com.intellij.codeInsight.documentation.DocCommentFixer) CodeDocumentationProvider(com.intellij.lang.documentation.CodeDocumentationProvider) CompositeDocumentationProvider(com.intellij.lang.documentation.CompositeDocumentationProvider) DocumentationProvider(com.intellij.lang.documentation.DocumentationProvider)

Aggregations

DocCommentFixer (com.intellij.codeInsight.documentation.DocCommentFixer)1 CodeDocumentationProvider (com.intellij.lang.documentation.CodeDocumentationProvider)1 CompositeDocumentationProvider (com.intellij.lang.documentation.CompositeDocumentationProvider)1 DocumentationProvider (com.intellij.lang.documentation.DocumentationProvider)1