Insert JsDoc comment templates for additional nodes#19544
Insert JsDoc comment templates for additional nodes#19544uniqueiniquity merged 12 commits intomicrosoft:masterfrom
Conversation
| * | ||
| */`); | ||
|
|
||
| verify.emptyDocCommentTemplateAt("propertybar"); |
There was a problem hiding this comment.
Why not give us the above template in this case too?
There was a problem hiding this comment.
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.
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.
Any arbitrary location? I feel like it should always be leading up to a declaration or member of some sort.
| return { commentOwner, parameters }; | ||
|
|
||
| case SyntaxKind.ClassDeclaration: | ||
| case SyntaxKind.InterfaceDeclaration: |
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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.
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.
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.
Huh. Yeah we never use that with the current implementation.
There was a problem hiding this comment.
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?
| // 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.
so why do we return an empty object here? what is wrong in undefined?
There was a problem hiding this comment.
Was @Andy-MS's suggestion. Andy, could you elaborate?
There was a problem hiding this comment.
aha, I misunderstood what you meant by empty doc comment template.
There was a problem hiding this comment.
so how is the empty file case related to isInString or isIncomment ?
There was a problem hiding this comment.
if you want to enable them all the time that is fine, but not in strings and not in comments
| case SyntaxKind.InterfaceDeclaration: | ||
| case SyntaxKind.PropertySignature: | ||
| case SyntaxKind.EnumDeclaration: | ||
| case SyntaxKind.EnumMember: |
| // - enums/enum members | ||
| // - interfaces | ||
| // - property declarations | ||
| // - potentially property assignments |
There was a problem hiding this comment.
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:
|
| return singleLineTemplate; | ||
| } | ||
|
|
||
| if (parameters.length === 0) { |
| } | ||
| 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.
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();|
|
||
| const enum Indentation { | ||
| Standard = 8, | ||
| Standard = 3, |
There was a problem hiding this comment.
Why this change? It's unusual to set indentation to 3.
There was a problem hiding this comment.
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.
Maybe in the old version this actually was an indentation?
This really makes no sense as an enum and could just be consts.
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.