expert icon indicating copy to clipboard operation
expert copied to clipboard

The aliases order incorrect after trigger organize aliases code action

Open wingyplus opened this issue 5 months ago • 1 comments

Here is the screen record

https://github.com/user-attachments/assets/8f440287-9e2b-4f16-b481-ebf2cdc7789e

You will see that after trigger organize aliases code action, the alias is changing from:

alias A
alias C
alias D
alias B

to

alias B
alias C
alias D  alias A

I expected that the alias should be ordered to:

alias A
alias B
alias C
alias D

Here is the LSP debug log from LspLog in the neovim

[DEBUG][2025-09-26 22:36:43] .../vim/lsp/rpc.lua:277	"rpc.send"	{ id = 46, jsonrpc = "2.0", method = "textDocument/codeAction", params = { context = { diagnostics = {}, triggerKind = 1 }, range = { ["end"] = <1>{ character = 0, line = 17 }, start = <table 1> }, textDocument = { uri = "file:///Users/thanabodee/src/github.com/wingyplus/hello/lib/hello.ex" } } }
[DEBUG][2025-09-26 22:36:43] .../vim/lsp/rpc.lua:391	"rpc.receive"	{ id = 46, jsonrpc = "2.0", result = { { edit = { changes = { ["file:///Users/thanabodee/src/github.com/wingyplus/hello/lib/hello.ex"] = { { newText = "", range = { ["end"] = { character = 0, line = 21 }, start = { character = 0, line = 18 } } }, { newText = "\n", range = { ["end"] = { character = 0, line = 18 }, start = { character = 0, line = 17 } } }, { newText = "  alias A\n  alias B\n  alias C\n  alias D", range = { ["end"] = { character = 0, line = 17 }, start = { character = 0, line = 17 } } } } } }, kind = "source.organizeImports", title = "Organize aliases" } } }

And my neovim info:

NVIM v0.11.4
Build type: Release
LuaJIT 2.1.1741730670

wingyplus avatar Sep 26 '25 15:09 wingyplus

Try

commit 4712c39236c1a9130ef67de2191ddfd266b4295c
Author: Iuri Mateus <[email protected]>
Date:   Sat Sep 6 09:16:54 2025 -0300

    fix(engine/code_mod/aliases.ex): merge adjacent edits with different texts

diff --git a/apps/engine/lib/engine/code_mod/aliases.ex b/apps/engine/lib/engine/code_mod/aliases.ex
index 5153eb62..b2fd3ee1 100644
--- a/apps/engine/lib/engine/code_mod/aliases.ex
+++ b/apps/engine/lib/engine/code_mod/aliases.ex
@@ -83,10 +83,10 @@ defmodule Engine.CodeMod.Aliases do
         alias_text
       end
 
-    edits = remove_old_aliases(aliases)
+    edits =
+      remove_old_aliases(aliases) ++ [Edit.new(alias_text, new_alias_range)]
 
-    edits ++
-      [Edit.new(alias_text, new_alias_range)]
+    merge_adjacent_edits(edits)
   end
 
   defp aliases_in_scope(%Scope{} = scope) do
@@ -146,9 +146,9 @@ defmodule Engine.CodeMod.Aliases do
   defp merge_adjacent_edits([edit | rest]) do
     rest
     |> Enum.reduce([edit], fn %Edit{} = current, [%Edit{} = last | rest] = edits ->
-      with {same_text, same_text} <- {last.text, current.text},
-           {same, same} <- {to_tuple(current.range.end), to_tuple(last.range.start)} do
+      with {same, same} <- {to_tuple(current.range.end), to_tuple(last.range.start)} do
         collapsed = put_in(current.range.end, last.range.end)
+        collapsed = %{collapsed | text: current.text <> last.text}
 
         [collapsed | rest]
       else

iurimateus avatar Oct 21 '25 19:10 iurimateus