question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggest import nearest current file.

See original GitHub issue

Search 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:open
  • Created 5 years ago
  • Reactions:10
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
AshleyGrantcommented, Nov 28, 2018

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.

image

1reaction
be5inviscommented, Apr 13, 2019

Really want this. TS’ auto import sometimes even introduce cyclic dependencies, if a directory re-exports stuff within index.ts.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring the Style of Imports in JavaScript and TypeScript
For TypeScript, you can use the paths that are relative to the nearest tsconfig.json file with the option “Use paths relative to tsconfig.json”, ......
Read more >
Import python module NOT on path - Stack Overflow
I want to import it in IDLE or python session. How do I go about this? I could find no documentation on how...
Read more >
MATLAB detectImportOptions - MathWorks
This MATLAB function locates a table in a file and returns its import options. ... If the file is not in the current...
Read more >
eslint-plugin-import - npm
Ensure imports point to a file/module that can be resolved. ... relative to the source's nearest package.json or the process's current ...
Read more >
Tips for New Importers and Exporters
In particular, we suggest viewing the information contained in the section ... This will lead you to information on CBP import requirements, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found