diff --git a/platform/analysis-api/src/com/intellij/lang/documentation/CodeDocumentationProvider.java b/platform/analysis-api/src/com/intellij/lang/documentation/CodeDocumentationProvider.java index d57600a1b9a6..29c36b0cf5bb 100644 --- a/platform/analysis-api/src/com/intellij/lang/documentation/CodeDocumentationProvider.java +++ b/platform/analysis-api/src/com/intellij/lang/documentation/CodeDocumentationProvider.java @@ -1,10 +1,12 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.lang.documentation; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiComment; import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -51,4 +53,16 @@ public interface CodeDocumentationProvider extends DocumentationProvider { */ @Nullable String generateDocumentationContentStub(PsiComment contextComment); + + /** + * Works like {@link #generateDocumentationContentStub(PsiComment)}, but inserts generated content into the document. + * @param contextComment context psi comment + * @param document document to insert generated content into + * @param offset offset in the document to insert generated content at + * @return true if documentation content was generated and inserted into the document, false otherwise + */ + @ApiStatus.Internal + default boolean insertDocumentationContentStub(@NotNull PsiComment contextComment, @NotNull Document document, int offset) { + return false; + } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java index 1d941676c991..6796f1137929 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.editorActions; @@ -611,12 +611,20 @@ public class EnterHandler extends BaseEnterHandler { if (docProvider != null) { if (docProvider.findExistingDocComment(comment) != comment) return comment; - String docStub = DumbService.getInstance(project).computeWithAlternativeResolveEnabled(() -> docProvider.generateDocumentationContentStub(comment)); - if (docStub != null && !docStub.isEmpty()) { - myOffset = CharArrayUtil.shiftForwardUntil(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR); - myOffset = CharArrayUtil.shiftForward(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR); - myDocument.insertString(myOffset, docStub); + int newOffset = CharArrayUtil.shiftForwardUntil(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR); + newOffset = CharArrayUtil.shiftForward(myDocument.getCharsSequence(), newOffset, LINE_SEPARATOR); + if (docProvider.insertDocumentationContentStub(comment, myDocument, newOffset)) { + myOffset = newOffset; + } + else { + String docStub = DumbService.getInstance(project) + .computeWithAlternativeResolveEnabled(() -> docProvider.generateDocumentationContentStub(comment)); + + if (docStub != null && !docStub.isEmpty()) { + myOffset = newOffset; + myDocument.insertString(myOffset, docStub); + } } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java index 568f98e1bb1b..ffa2542b3e4c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.editorActions; import com.intellij.application.options.CodeStyle; @@ -227,10 +227,16 @@ public final class FixDocCommentAction extends EditorAction { return; } - String stub = documentationProvider.generateDocumentationContentStub(pair.second); - if (stub != null) { - int insertionOffset = commentStartOffset + commentBodyRelativeOffset; - document.insertString(insertionOffset, stub); + int insertionOffset = commentStartOffset + commentBodyRelativeOffset; + boolean inserted = documentationProvider.insertDocumentationContentStub(pair.second, document, insertionOffset); + if (!inserted) { + String stub = documentationProvider.generateDocumentationContentStub(pair.second); + if (stub != null) { + document.insertString(insertionOffset, stub); + inserted = true; + } + } + if (inserted) { docManager.commitDocument(document); pair = documentationProvider.parseContext(anchor); }