Closed academo closed 2 years ago
I like this idea! It's definitely cleaner than adding a separate option. I'll test it out soon.
One question that popped into my head as I looked at the code: Can we use a simpler heuristic to determine if the edit is adding to an existing import statement? For example:
tsserver
will ever break imports from a single module into multiple lines, but I could be wrong).I'm not sure whether those assumptions hold, but let me know what you think.
@jose-elias-alvarez I did múltiple tests to find the best way to do it (see the videos I attached) and TS server does suggest múltiple imports for the same module. Regarding the comma I don't like it too much because technically could be a valid file name character in some file systems.
@jose-elias-alvarez any follow up on this one?
This works well. My worry is that the difference in behavior may seem arbitrary to users, since the way
:TSLspImportAll
works is pretty opaque. Perhaps we could add an option to always organize imports and set it totrue
by default to keep the current behavior?
Yes this makes sense to me. I'll add an option for it. does avoid_reorder_import
sound good?
This works well. My worry is that the difference in behavior may seem arbitrary to users, since the way
:TSLspImportAll
works is pretty opaque. Perhaps we could add an option to always organize imports and set it totrue
by default to keep the current behavior?Yes this makes sense to me. I'll add an option for it. does
avoid_reorder_import
sound good?
Maybe always_organize_imports
?
@jose-elias-alvarez I added the changes and the new option always_organize_imports
(default to true). I also added documentation for it
@jose-elias-alvarez I made the variable change.
You can do a squash when you are merging the PR like this:
Thanks!
Based on our discussion here https://github.com/jose-elias-alvarez/nvim-lsp-ts-utils/discussions/90
TL;DR: Call sort imports only if more than 1 import from the same module was added.
Background
The current import all
import_all
and import currentimport_current
algorithms will perform an organize import action after the edits are applied.This behaviour exists to prevent situations where TS code actions require to import two dependencies from the same module. But this is not always the case for all situations.
Re-organizing imports on import can become problematic for code bases that don't have a standard for imports order, or that have one that doesn't match the default TS one.
Re-organizing imports on import all or current also creates more changes on the file that required, e.g. when a single line of code with a function call is added and after using import current or import all, the file gets much more changes related only to the imports order.
What the plugin does to auto-import all is loop through ts code-actions and apply them.
The change:
What my change does is keep track of how many imports of the same module had been applied, and if more than 1 was applied it would then perform an import sort[1]
[1] I checked the TS server and I can't find a way to only de-duplicate the imports, the sort and import are always together. It would be idea to have a TS action only to de-dupe the imports.
Extended background
There are widely two cases I considered for this PR:
In this case, ts will suggest two actions:
Applying auto import (both code actions) will result in the following incorrect code:
Applying auto import (one code action). will result in the following correct code
Videos
Add to existing import (no sorting):
Import module (no sorting):
import two from the same module (sorting):
Add two to existing import (no sorting)