eclipse-typescript icon indicating copy to clipboard operation
eclipse-typescript copied to clipboard

Organize imports

Open mihhail-lapushkin opened this issue 11 years ago • 8 comments

Would be a killer feature! Although I suppose it is quite a lot of work.

mihhail-lapushkin avatar Jan 14 '14 19:01 mihhail-lapushkin

Interesting idea - how would you organize them? Check for unused ones and then sort them alphabetically? Might not be as hard as you think actually - please feel free to submit a pull request.

derekcicerone-zz avatar Jan 14 '14 19:01 derekcicerone-zz

Well, yeah, like in Java. Ctrl/Cmd+Shift+O and it tries to find matches for currently missing classes (not sure if you can efficiently tell a missing "symbol" from a "class"). If it finds multiple definitions, then a dialog pops out. Sorting can be arbitrary.

mihhail-lapushkin avatar Jan 14 '14 20:01 mihhail-lapushkin

Yup - makes sense, I'm just not sure how well some of those concepts translate over to TypeScript.

derekcicerone-zz avatar Jan 14 '14 20:01 derekcicerone-zz

The organize imports of java do several things:

  • Remove unused imports
  • Resolve and add missing imports from "Not found" errors
  • Normalize and sort imports (depending on settings)

The main difference with java is that imports are named import myDependencyName = require("dependency/path");

So

  • "Remove unused imports" is even easier than in java
  • "Normalize and sort imports" could also be provided with some associated settings (like for example sorting alphabetically, transforming relative imports to absolute ones: require("siblingDependency") => require("path/to/siblingDependency")
  • "Resolve and add missing imports from Not found errors" is maybe harder but could be done easier assuming a naming pattern (like for example dependency name = file name)

The "resolving and add missing imports" process would be:

  1. Find all "Could not find symbol" errors
  2. Foreach errors, list typescript files having the same name than the symbol name (class or variable)
  3. If there is more than one match, either skip (easier to implement) or prompt for the file to import (better)
  4. generate import line at the top of the file

felicienfrancois avatar Jun 05 '14 09:06 felicienfrancois

And yes, It would be a killed feature ! Additionally it could also be done automatically on save, like for Java

felicienfrancois avatar Jun 05 '14 09:06 felicienfrancois

The biggest problem right now is that the language services have a bug that makes it impossible to detect unused imports: https://typescript.codeplex.com/workitem/2526

derekcicerone-zz avatar Jun 05 '14 13:06 derekcicerone-zz

Any progress on this? It would be really nice if we could at least "Resolve and add missing imports from "Not found" errors" since that is the biggest pain point right now when using CommonJS style imports.

atifsyedali avatar Mar 02 '16 20:03 atifsyedali

Sorry, no progress - I haven't had much time to work on this recently. If you'd like to take a look at implementing this feature we'd love to have the PR.

derekcicerone-zz avatar Mar 02 '16 22:03 derekcicerone-zz