diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 23fe3e63a9d..773d0b8b846 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -272,14 +272,16 @@ namespace ts.JsDoc { */ export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion | undefined { - // Check if in a context where we don't want to perform any insertion - if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) { + const tokenAtPos = getTokenAtPosition(sourceFile, position); + const existingDocComment = findAncestor(tokenAtPos, isJSDoc); + if (existingDocComment && (existingDocComment.comment !== undefined || length(existingDocComment.tags))) { + // Non-empty comment already exists. return undefined; } - const tokenAtPos = getTokenAtPosition(sourceFile, position); const tokenStart = tokenAtPos.getStart(sourceFile); - if (!tokenAtPos || tokenStart < position) { + // Don't provide a doc comment template based on a *previous* node. (But an existing empty jsdoc comment will likely start before `position`.) + if (!existingDocComment && tokenStart < position) { return undefined; } diff --git a/tests/cases/fourslash/docCommentTemplate_insideEmptyComment.ts b/tests/cases/fourslash/docCommentTemplate_insideEmptyComment.ts new file mode 100644 index 00000000000..f521c4a2c51 --- /dev/null +++ b/tests/cases/fourslash/docCommentTemplate_insideEmptyComment.ts @@ -0,0 +1,15 @@ +/// + +/////** /**/ */ +////function f(p) { return p; } +//// +/////** Doc/*1*/ */ +////function g(p) { return p; } + +verify.docCommentTemplateAt("", /*newTextOffset*/ 8, +`/** + * + * @param p + */`); + +verify.noDocCommentTemplateAt("1");