use of com.intellij.lang.documentation.CodeDocumentationProvider 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);
}
Aggregations