-
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
Auto-importing modules as namespaces? #23830
Comments
Can you clarify what you're typing and when you'd expect this to show up? |
But I’m open to anything that helps with |
If something is already imported as a namespace, we ought to (but don't currently) prefix a completion with the namespace name, rather than adding a new named import. As a second issue we might consider adding an option to always make a namespace import even if one doesn't already exist. |
I don’t think that that would be useful:
|
So the suggestion here is to offer the names of modules in the completion list with an additional action to import them? i.e. |
Yes! But I’m not sure what you mean by “in the global scope”. |
Oh, and it could be complemented by a quick fix offering to namespace-import |
My only worry is the amount of unrelated names we put in the completion list. |
True, but we already add in every possible named export in a project context. |
This just gets very annoying for anything in Node that's a built-in.
|
Issue related to this: #19630 Perhaps TS could suggest namespace imports based on existing imports in the project? E.g. if the module import * as FooBar from 'foo/bar'; … in other modules, then when I type |
Has there been any movement on this? I noticed that VS code now sometimes adds a bracketed name to the module name where the imported function is coming from.. |
Maybe we could we limit this to ambient module declarations, and packages listed in dependencies, devDependencies, and peerDependencies? |
That would happen automatically at this point: #32517 |
So really the concerns are pretty limited at this point - would the main thing here be to just grab the list of ambients and dependencies and ensure that in the completion list their names
|
I think the latter suggestion is too complex/expensive, but the rest of these cases seem like they should be an easy fix. |
@andrewbranch Perhaps VS Code could provide an API so extensions can contribute import suggestions (which appear in the suggestions drop down when typing and as quick fixes), then someone could write an extension to do this. Do you know if anything like that exists? |
I’ve looked into this a good bit and decided I don’t want to do it in general. We can’t know the conventions by which the namespace export is named: I investigated this:
The @OliverJAsh’s suggestion of picking up on namespace import names used elsewhere in the program is probably the only solution here that can provide a decent UX, but I think it’s too much complexity for the value it delivers. (It would be too expensive to look up these associations from other files on demand when we’re generating a completions list, so we’d have to introduce a new cache, which means you have to think about cache invalidation.)
As far as I know, these APIs are separate, but there is nothing stopping a single extension from sharing code to contribute to both APIs. |
I’m not sure what the best way is to fix this, but: auto-importing is incredibly helpful. Alas, it only works for named exports. However, imports such as the following are frequent in Node.js and there, you get no help:
Possible solution: offer an auto-import if:
foo
followed by a dot andfoo
is the base name of a module.The text was updated successfully, but these errors were encountered: