-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Insert JsDoc comment templates for additional nodes #19544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Insert JsDoc comment templates for additional nodes #19544
Conversation
* | ||
*/`); | ||
|
||
verify.emptyDocCommentTemplateAt("propertybar"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not give us the above template in this case too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can, it was just outside of the scope of this particular bug fix. I can go and add these too if we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could just return that template (with the current indentation) at any arbitrary location where we don't have specific handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any arbitrary location? I feel like it should always be leading up to a declaration or member of some sort.
src/services/jsDoc.ts
Outdated
return { commentOwner, parameters }; | ||
|
||
case SyntaxKind.ClassDeclaration: | ||
case SyntaxKind.InterfaceDeclaration: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like commentOwner
is only used in one place: if (commentOwner.getStart() < position) { return undefined; }
.
Seems like this function could just be getParameters
, but don't return parameters if you're inside the function, only if you're on top of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this current setup since we have an explicit list of what places are permissible for these templates, and it's easy to add new things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a user types /**
in an empty file and hits enter, I would expect to see an empty doc comment -- it's unambiguous that that's what they're trying to do. No sense in not giving the user formatting just because they're not above a declaration. An explicit list of places where we allow doc comments would just lead to users frustrated over why sometimes starting a comment works and sometimes it doesn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if they're in the middle of an expression? For example, what if someone's adding a comment to a parameter (foo(/*param*/ undefined)
) and then accidentally hit * twice. I guess they can just hit undo but it seems easy to trigger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we fill in the comment immediately on the second *
or do we wait for them to hit enter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In vscode you must hit enter. What if a user wants to add a single-line comment to an interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean same as my description above: if they accidentally hit a second star, then it will trigger, but otherwise it won't. Seems somehow less bad then triggering in the middle of a parenthetical block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean a single-line jsdoc comment. These seem pretty common.
/** My interface */
interface I {
/** My method */
m(): void;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh. Yeah we never use that with the current implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe we should just autocomplete to empty, one-line jsDoc comments and then if there are params, autocomplete to a multi-line and fill in the params?
src/services/jsDoc.ts
Outdated
// Check if in a context where we don't want to perform any insertion | ||
if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) { | ||
return undefined; | ||
return emptyDocComment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so why do we return an empty object here? what is wrong in undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was @Andy-MS's suggestion. Andy, could you elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #19544 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha, I misunderstood what you meant by empty doc comment template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so how is the empty file case related to isInString
or isIncomment
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want to enable them all the time that is fine, but not in strings and not in comments
src/services/jsDoc.ts
Outdated
case SyntaxKind.InterfaceDeclaration: | ||
case SyntaxKind.PropertySignature: | ||
case SyntaxKind.EnumDeclaration: | ||
case SyntaxKind.EnumMember: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type alias as well.
src/services/jsDoc.ts
Outdated
@@ -255,19 +259,21 @@ namespace ts.JsDoc { | |||
} | |||
function getCommentOwnerInfo(tokenAtPos: Node): CommentOwnerInfo | undefined { | |||
// TODO: add support for: | |||
// - enums/enum members | |||
// - interfaces | |||
// - property declarations | |||
// - potentially property assignments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not think this is comment is needed either. we handle these in BinaryExpression
section.
I got rid of the empty template approach. Turns out it makes VS behave weirdly, whereas |
This function will now:
|
src/services/jsDoc.ts
Outdated
return singleLineTemplate; | ||
} | ||
|
||
if (parameters.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Use ||
to combine with above
src/services/jsDoc.ts
Outdated
} | ||
for (let i = 0; i < parameters.length; i++) { | ||
const currentName = parameters[i].name; | ||
const paramName = currentName.kind === SyntaxKind.Identifier ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const docParams = parameters.map(({name}, i) => {
const nameText = isIdentifier(name) ? name.text : `param${i}`;
const type = isJavaScriptFile ? "{any} " : "";
return `${indentationStr} * @param ${type}${nameText}${newLine}`;
}).join();
@@ -1,7 +1,7 @@ | |||
/// <reference path='fourslash.ts' /> | |||
|
|||
const enum Indentation { | |||
Standard = 8, | |||
Standard = 3, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? It's unusual to set indentation to 3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is actually a bad name for it. It's actually the caret offset into the template. But indentation is the term consistently used for it. I could go in and change it all to Offset if warranted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe in the old version this actually was an indentation?
This really makes no sense as an enum and could just be const
s.
In response to https://developercommunity.visualstudio.com/content/problem/138023/typescript-editor-jsdoc-comments-not-auto-generate.html.
Previously, we were not inserting jsDoc comment templates before interface declarations, method signatures, property signatures, and enums, whereas we were for class declarations and method declarations. This adds this functionality in.
Also, following a suggestion by @Andy-MS, we no longer return undefined if it is inappropriate to insert a doc comment template. Instead, we return an empty template.