Skip to content

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

Merged
merged 12 commits into from
Nov 6, 2017

Conversation

uniqueiniquity
Copy link
Contributor

@uniqueiniquity uniqueiniquity commented Oct 27, 2017

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.

@uniqueiniquity uniqueiniquity requested review from aozgaa and a user October 27, 2017 23:59
*
*/`);

verify.emptyDocCommentTemplateAt("propertybar");
Copy link

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?

Copy link
Contributor Author

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.

Copy link

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.

Copy link
Contributor Author

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.

return { commentOwner, parameters };

case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
Copy link

@ghost ghost Oct 30, 2017

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.

Copy link
Contributor Author

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.

Copy link

@ghost ghost Oct 30, 2017

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.

Copy link
Contributor Author

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.

Copy link

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?

Copy link

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?

Copy link
Contributor Author

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.

Copy link

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;
}

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

@uniqueiniquity uniqueiniquity changed the title Insert JsDoc comment templates for interfaces and method signatures Insert JsDoc comment templates for additional nodes Oct 30, 2017
// 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;
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copy link
Contributor

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 ?

Copy link
Contributor

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

case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.PropertySignature:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.EnumMember:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type alias as well.

@@ -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
Copy link
Contributor

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.

@microsoft microsoft deleted a comment Nov 2, 2017
@uniqueiniquity
Copy link
Contributor Author

I got rid of the empty template approach. Turns out it makes VS behave weirdly, whereas undefined induces the intended behavior.

@uniqueiniquity
Copy link
Contributor Author

This function will now:

  • return a single-line JsDoc comment unless there are parameters to document, in which case it will fill them in as before.
  • autocomplete everywhere except for inside strings, comments, template literals, regex literals, and JSXText

return singleLineTemplate;
}

if (parameters.length === 0) {
Copy link

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

}
for (let i = 0; i < parameters.length; i++) {
const currentName = parameters[i].name;
const paramName = currentName.kind === SyntaxKind.Identifier ?
Copy link

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,
Copy link

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.

Copy link
Contributor Author

@uniqueiniquity uniqueiniquity Nov 3, 2017

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.

Copy link

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 consts.

@uniqueiniquity uniqueiniquity merged commit 4977bf4 into microsoft:master Nov 6, 2017
@uniqueiniquity uniqueiniquity deleted the interfaceJsDoc branch November 6, 2017 17:50
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants