-
-
Notifications
You must be signed in to change notification settings - Fork 18
POF: Do not reorder imports if not necessary #102
Conversation
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:
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 to true
by default to keep the current behavior?
lua/nvim-lsp-ts-utils/import-all.lua
Outdated
for _, edit in ipairs(edits) do | ||
if edit.newText then | ||
for module in string.gmatch(edit.newText, "import.*from (.+)[;\n]?") do | ||
local import = module:gsub("\n", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local import = module:gsub("\n", "") | |
local import = vim.trim(module) |
Maybe it also makes sense to rename this to something like source
, since import
makes me think of the variable getting imported.
Yes this makes sense to me. I'll add an option for it. does |
Maybe |
@jose-elias-alvarez I added the changes and the new option |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than my suggestion I think this looks good! I'm happy to merge this once commits are squashed.
lua/nvim-lsp-ts-utils/import-all.lua
Outdated
return | ||
end | ||
|
||
local import = module:gsub("\n", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local import = module:gsub("\n", "") | |
local source = vim.trim(module) |
I think I suggested this before, but I think changing the variable name to source
and using vim.trim()
makes it a little easier to parse this at a glance. (Of course, the other uses of the variable have to be updated, too.)
@jose-elias-alvarez I made the variable change. |
Thanks! |
Based on our discussion here #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)