Closed mjbvz closed 2 years ago
This was a known limitation of the PR for 4.5, but probably won't be difficult to add in 4.6.
@andrewbranch Should the completion of object literal elements be allowed with the includeCompletionsWithClassMemberSnippets
option, or should a new option be added?
@gabritto and I briefly discussed this when coming up with the name at the time. IIRC we didn’t really feel great about any options and mostly just lamented that we don’t have a way of presenting hierarchical preferences. One related thought is that for object literals we might want to give an option for whether to always use arrow function properties or to match the declaring interface—I personally tend to use arrow functions for object literal members even when the member is written as a method signature on the interface. If we do want to make that configurable, I think we would need a separate option. @gabritto @DanielRosenwasser @mjbvz what do you think?
@andrewbranch I think we can surface these settings sort of hierarchically in VS Code if we want. Right now have
javascript.suggest.includeCompletionsWithClassMemberSnippets
but we could expand this to something like:
javascript.suggest.classMemberSnippets.enabled
javascript.suggest.classMemberSnippets.methodStyle: "arrow"
We'd keep two distinct settings on the TS side, this would just be for the VS Code settings UI
Let's make sure we decide on on this before 4.5 goes so I can rename the VS Code setting if needed
@mjbvz that only affects the presentation of the setting title in the UI, right?
This is better than nothing, but it still appears as a flat list and is kind of hard to navigate. But, properly organizing the data leaves room for UI improvements later. I like your suggestion for the setting name, though I’m now having stronger doubts about using the term “class” to control something for object literals as well. Maybe at that point we can do javascript.suggest.objectMemberSnippets.*
.
I think that change wouldn’t require any changes on the TS side right now; the new name can still map to the existing TS preference name.
@andrewbranch I think we can surface these settings sort of hierarchically in VS Code if we want. Right now have
javascript.suggest.includeCompletionsWithClassMemberSnippets
but we could expand this to something like:
javascript.suggest.classMemberSnippets.enabled javascript.suggest.classMemberSnippets.methodStyle: "arrow"
We'd keep two distinct settings on the TS side, this would just be for the VS Code settings UI
I agree with @andrewbranch, I think this is better than what we have now, but I think we want to have something more structured for these options in the future.
Also, a few other things on my mind:
javascript
option when we don't yet support this feature in JS and don't have concrete plans for it (yet -- I opened an issue: #46738).suggest.methods
and grouped under that, suggest.methods.classes
and suggest.methods.objects
(illustrative names).we'd want to separately enable completions for objects vs classes
Agreed. And you’re right, there are other significant differences. Parameters are contextually typed in object literals so there’s no need to write their type annotations. Also, in a class member position you can be fairly sure that a user starting to type a method name intends to type a whole method declaration. Not so in object literals, where it is possible to write a property assignment to an existing function instead:
import * as ts from "typescript";
const host: ts.LanguageServiceHost = {
fileExis/*|*/
};
At this location we have no heuristics that would indicate whether the user intends to write a function literal / method declaration or a property assignment:
const host: ts.LanguageServiceHost = {
fileExists: ts.sys.fileExists,
};
so we cannot replace the completion containing just the fileExists
name. We would need to offer both options, and the two completion items need to be easy to disambiguate in the list. We need to be careful that users don’t find themselves automatically getting whole method declarations when they meant to only get a property name. It sounds like this feature needs a bit of careful UX design work.
Related to the arrow function discussion on SO, see How to make VS Code autocomplete a callable property to an arrow function instead of a regular function?
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
4.5.0-dev.20211029
💻 Code
For the code:
Trigger suggestions inside the object literal
🙁 Actual behavior
We get a suggestion for
bar
but accepting it only completesbar
🙂 Expected behavior
Accepting the completion for
bar
should also insert the method signature: