Skip to content

Commit 5e1872f

Browse files
author
Andy
authored
getDocCommentTemplateAtPosition: Return result if in empty comment (microsoft#26026)
1 parent 644ceab commit 5e1872f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/services/jsDoc.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,16 @@ namespace ts.JsDoc {
272272
*/
273273

274274
export function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion | undefined {
275-
// Check if in a context where we don't want to perform any insertion
276-
if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) {
275+
const tokenAtPos = getTokenAtPosition(sourceFile, position);
276+
const existingDocComment = findAncestor(tokenAtPos, isJSDoc);
277+
if (existingDocComment && (existingDocComment.comment !== undefined || length(existingDocComment.tags))) {
278+
// Non-empty comment already exists.
277279
return undefined;
278280
}
279281

280-
const tokenAtPos = getTokenAtPosition(sourceFile, position);
281282
const tokenStart = tokenAtPos.getStart(sourceFile);
282-
if (!tokenAtPos || tokenStart < position) {
283+
// Don't provide a doc comment template based on a *previous* node. (But an existing empty jsdoc comment will likely start before `position`.)
284+
if (!existingDocComment && tokenStart < position) {
283285
return undefined;
284286
}
285287

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////** /**/ */
4+
////function f(p) { return p; }
5+
////
6+
/////** Doc/*1*/ */
7+
////function g(p) { return p; }
8+
9+
verify.docCommentTemplateAt("", /*newTextOffset*/ 8,
10+
`/**
11+
*
12+
* @param p
13+
*/`);
14+
15+
verify.noDocCommentTemplateAt("1");

0 commit comments

Comments
 (0)