-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Enable method signature completion for object literals #48168
Conversation
Things that I have to fix in this PR: for some reason, the entries are shown in the wrong order in vscode. I'm investigating that to see what's the best way to address it. Edit: updated the sort text on the method signature completion entries so that they show up lower than the normal entries. They now have a |
Looking good at a first glance—I’ll try to give it a try soon. |
@typescript-bot pack this |
Heya @RyanCavanaugh, I've started to run the tarball bundle task on this PR at 814561f. You can monitor the build here. |
Hey @RyanCavanaugh, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
I realized I put the feature behind a new flag, so it is not set by default. Going to remove that part of the code in a bit so that people can use this more easily. |
@typescript-bot pack this |
@typescript-bot pack this |
Heya @andrewbranch, I've started to run the tarball bundle task on this PR at 411bc18. You can monitor the build here. |
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
If you close the detail pane of the completions widget, the displayParts of the name entry gets inlined into the right side of the label and you can’t really tell these apart anymore: I think we discussed in an editor sync putting the signature, or something like the first line of insertText, in the left side of the label. Quick hack: No strong opinion on whether it should end in Another side note is that VS Code and the LSP spec support another newer label scheme with three parts: one for the main part of the name ( |
I think I want to try and add support for it after this PR. It's doable to do it, right? We need a new field in |
Yes, I think it should be possible. |
@@ -1062,6 +1055,135 @@ namespace ts.Completions { | |||
return undefined; | |||
} | |||
|
|||
function getEntryForObjectLiteralMethodCompletion( |
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.
Ok, so the main thing I’m wondering is if we can consolidate a few of the separate steps here. My understanding of the process is something like this:
-
When processing symbols in
tryGetObjectLikeCompletionSymbols
, you do a fast check viaisObjectLiteralMethodSymbol
and as long as that passes, you push the symbol and an origin (with no info other thankind
) onto thesymbols
array. -
After pushing all the symbols on, you update their SortText—I can’t quite tell why this can’t be done in (1).
-
At the very end of the completions process, in
createCompletionEntry
, you call into this function here for any symbols that got their origin set accordingly in (1). This function does all the hard work and introduces some extra conditions which might result in us dropping the entry altogether.
I could definitely be missing something, but I don’t see much of a reason for these three steps to be separate. It seems like you could go as far as creating the MethodDeclaration
where you currently are only doing step (1), and then store that on the origin. That way, if you can’t create the MethodDeclaration, you don’t need to bother with setting SortText. In other words, by the time you push a symbol and an origin onto those arrays, you know for sure it will be able to turn into a CompletionEntry because most of the work to do it is already done. It seems like this would consolidate the implementation a little. Does that make sense, or what did I miss?
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.
That made sense. I updated the code so that (3) is now done in (1).
About (2), we can't do that when pushing the method snippet symbols, because we need all of the object completions to have the transformed format. Still, I moved the functions that set/transform sort texts to avoid doing some checks.
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.
Makes sense about (2). Looks much better now without having to create the Set of seen symbols 👍
* upstream/main: (473 commits) Correct node used for isDefinition calculation (microsoft#48499) fix(48405): emit dummy members from a mapped type (microsoft#48481) CFA for dependent parameters typed by generic constraints (microsoft#48411) No contextual typing from return types for boolean literals (microsoft#48380) fix(47733): omit JSDoc comment template suggestion on node with existing JSDoc (microsoft#47748) Ensure that we copy empty NodeArrays during transform (microsoft#48490) Add a new compiler option `moduleSuffixes` to expand the node module resolver's search algorithm (microsoft#48189) feat(27615): Add missing member fix should work for type literals (microsoft#47212) Add label details to completion entry (microsoft#48429) Enable method signature completion for object literals (microsoft#48168) Fix string literal completions when a partially-typed string fixes inference to a type parameter (microsoft#48410) fix(48445): show errors on type-only import/export specifiers in JavaScript files (microsoft#48449) Fix newline inserted in empty block at end of formatting range (microsoft#48463) Prevent looking up symbol for as const from triggering an error (microsoft#48464) Revise accessor resolution logic and error reporting (microsoft#48459) fix(48166): skip checking module.exports in a truthiness call expression (microsoft#48337) LEGO: Merge pull request 48450 LEGO: Merge pull request 48436 fix(48031): show circularity error for self referential get accessor annotations (microsoft#48050) Revert "Fix contextual discrimination for omitted members (microsoft#43937)" (microsoft#48426) ...
Fixes #46590.
This PR enables signature completions for methods in object literals. In the following code:
we now offer as completion:
bar(x: number): void { }
:Design decisions
insertText
is simply the name of the method (e.g.bar
in the example above), which is the old behaviorinsertText
is the method declaration (e.g.bar(x: number): void {}
in the example above)The regular completion entry should appear before its corresponding signature one
bar(x: number): void {}
). If there's a need for it, we can make this configurable later, and generate other forms of declaration (e.g.bar: (x: number): void => {}
)async
(and similarly don't support the asterisk modifier for generators), see discussions inawait
completions should auto-add anasync
modifier to containing method #48093.