Suggest import nearest current file.
See original GitHub issueSearch Terms
vs code, language service, suggestions, auto complete, import, symbol, nearest current file.
Suggestion
In VS Code the auto complete dropdown for auto importing seems to have a random order. I think the user experience could be improved by ordering the exported symbols in the dropdown in order of how close the file path is to your current file path.
Use Cases/Examples
See the example directory structure below.
- moduleA/
- constants.ts
- index.ts
- moduleB/
- constants.ts
- index.ts
Say that both moduleA/constants.ts and moduleB/constants.ts export a variable like foobar
. When I type foobar
in moduleA/index.ts it’s likely that I’m trying to auto import foobar
from moduleA/constants.ts`.
In the current approach the dropdown suggests foobar
from both moduleA/constants.ts and moduleB/constants.ts, however, the order doesn’t seem to be guaranteed so sometimes it will suggest moduleB’s foobar
first and other times moduleA’s foobar
.
I think this could be improved by ordering suggestions in terms of how close their source file path is to the current file path, such that moduleA’s foobar
always appears before moduleB’s foobar
when I’m in moduleA/index.ts. For the sake of completeness, I think it should show moduleB’s foobar
first when I’m in moduleB/index.ts.
I hope I’ve explained that clearly enough.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:5 (3 by maintainers)
Top GitHub Comments
I agree, this is currently very frustrating for me. In a project I’m working on, we utilize a state management library, and end up having many functions that share the same name in many different modules, but the function in the closest module is the one I want 99.99% of the time. Unfortunately, the language service always seems to suggest the modules I don’t want, and the closest module is buried at the end of the list quite often. Looking at the picture below, no human would order the suggestions that way.
Really want this. TS’ auto import sometimes even introduce cyclic dependencies, if a directory re-exports stuff within
index.ts
.