Skip to content

Commit

Permalink
Improve container_cursor_to_quoted with trailing fragment, closes #14087
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 23, 2024
1 parent 92f1e37 commit 4285cea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 0 additions & 3 deletions lib/elixir/lib/code/fragment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1226,10 +1226,7 @@ defmodule Code.Fragment do
defp maybe_missing_stab?([{:else, _} | _]), do: true
defp maybe_missing_stab?([{:catch, _} | _]), do: true
defp maybe_missing_stab?([{:rescue, _} | _]), do: true

defp maybe_missing_stab?([{:stab_op, _, :->} | _]), do: false
defp maybe_missing_stab?([{:eol, _}, next | _]) when elem(next, 0) != :",", do: false

defp maybe_missing_stab?([_ | tail]), do: maybe_missing_stab?(tail)
defp maybe_missing_stab?([]), do: false
end
25 changes: 24 additions & 1 deletion lib/elixir/test/elixir/code_fragment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,9 +1315,15 @@ defmodule CodeFragmentTest do
assert cc2q!("(fn x ->", trailing_fragment: ":ok end)") ==
s2q!("(fn x -> __cursor__() end)")

assert cc2q!("(fn x ->", trailing_fragment: ":ok end)") ==
assert cc2q!("(fn x ->", trailing_fragment: "\n:ok end)") ==
s2q!("(fn x -> __cursor__() end)")

assert cc2q!("(fn x when ", trailing_fragment: "-> :ok end)") ==
s2q!("(fn x when __cursor__() -> :ok end)")

assert cc2q!("(fn x when ", trailing_fragment: "->\n:ok end)") ==
s2q!("(fn x when __cursor__() -> :ok end)")

assert cc2q!("(fn") == s2q!("(__cursor__())")
assert cc2q!("(fn x") == s2q!("(__cursor__())")
assert cc2q!("(fn x,") == s2q!("(__cursor__())")
Expand All @@ -1327,6 +1333,23 @@ defmodule CodeFragmentTest do
assert cc2q!("(fn x, y -> x + y end") == s2q!("(__cursor__())")
end

test "do -> end" do
assert cc2q!("if do\nx ->\n", trailing_fragment: "y\nz ->\nw\nend") ==
s2q!("if do\nx ->\n__cursor__()\nend")

assert cc2q!("if do\nx ->\ny", trailing_fragment: "\nz ->\nw\nend") ==
s2q!("if do\nx ->\n__cursor__()\nend")

assert cc2q!("if do\nx ->\ny\n", trailing_fragment: "\nz ->\nw\nend") ==
s2q!("if do\nx ->\ny\n__cursor__()\nend")

assert cc2q!("for x <- [], reduce: %{} do\ny, ", trailing_fragment: "-> :ok\nend") ==
s2q!("for x <- [], reduce: %{} do\ny, __cursor__() -> :ok\nend")

assert cc2q!("for x <- [], reduce: %{} do\ny, z when ", trailing_fragment: "-> :ok\nend") ==
s2q!("for x <- [], reduce: %{} do\ny, z when __cursor__() -> :ok\nend")
end

test "removes tokens until opening" do
assert cc2q!("(123") == s2q!("(__cursor__())")
assert cc2q!("[foo") == s2q!("[__cursor__()]")
Expand Down

0 comments on commit 4285cea

Please sign in to comment.