-
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
Sort import completions by distance from current module #41083
Comments
So, we already do this sorting between different paths for the same symbol. This example must be three distinct
I would think we still want to keep aliases for the same symbol grouped together. So then, sort the groups by the closeness of their closest option? |
That's correct, they're all different symbols with the same name. For context the project is a monorepo that contains multiple different apps (admin website, production app, backend serverless functions). Each has its own database code but they all have similar file layout. When I'm working on a file in the admin website I would never want the other I use the ESLint import/no-restricted-paths rule to catch when I incorrectly import across an invalid boundary, but it'd be lovely if the auto import worked too. Regarding your design question: I think the distance sorting is the most likely requirement in nearly every case. The fact the developer has gone to the trouble of re-exporting higher up also implies they don't want highly specific imports. So I think |
This, like #31658 and #42005, is blocked on us not resolving full module specifiers for auto-imports until a user focuses a completion item. We can’t sort by paths because we don’t know what the paths will be when we render the list. In the simplest cases, it’s a simple relative path from the importing file to the imported one, but that can change if any of the following get involved:
We could make a rough guess without considering these things, since as far as I can tell, the current order is basically arbitrary (based on program order). I don’t have a strong opinion on whether or not that’s worth it. My hunch is that guessing would produce correct/desirable results far more often than not, but that same-named distinctly-declared exports are rare enough that almost nobody would notice. The same constraint doesn’t apply to codefixes—we have the full module specifier of every suggestion at the same time, so we can sort those by whatever we want. |
@andrewbranch Thanks for the investigation! You're right even sorting the relative paths seems like it would solve my specific use case. I agree it's a niche request — probably mainly useful to people who have projects with folders/structures that follow a single repetitive template, (i.e. exporting a |
Because I find #31658 and #42005 super compelling, I plan to investigate whether we can resolve module specifiers up front in the future with the help of some additional perf investments. Because of that, I think I don’t want to implement a best-guess sorting for completions before that. But I’ll look into sorting the codefixes today. |
Here is my suggestion of the sort criteria
Here is my explain: Why the order of 1 and 2? |
I second this and often end up getting frustrated in VSCode when imports are suggested like so: To give more context: here I edit core VSCode code but get suggestions that are a) from a d.ts file that is not even close to the one I am in ( I think the rules should be to prefer imports from modules by distance but also to prefer non d.ts / node_modules imports if possible even if the distance ends up to be greater. |
This is now more or less possible since #44713 was merged, but takes quite a bit more work on top of that to implement. I’ll plan on taking a look for 4.5. |
The auto-import feature is amazing. Something I come across daily though is the import I want is nearly always the second or third choice.
In the screenshots below VS Code suggests to import
db
from../../admin/helpers/db
before../helpers/db
I would propose that the ordering is sorted by distance from the current file. The following ESLint rule (that I use in most of my projects) does this correctly and presumably has code that can be reused: eslint-plugin-import/order (well, except we want the inverse of this — shortest distance first).
The text was updated successfully, but these errors were encountered: