Skip to content

Commit 11b2e3e

Browse files
authored
Code.Fragment handles anonymous function (#12846)
1 parent 33be0ab commit 11b2e3e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/elixir/lib/code/fragment.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ defmodule Code.Fragment do
7878
* `{:local_call, charlist}` - the context is a local (import or local)
7979
call, such as `hello_world(` and `hello_world `
8080
81+
* `{:anonymous_call, inside_caller}` - the context is an anonymous
82+
call, such as `fun.(` and `@fun.(`.
83+
8184
* `{:module_attribute, charlist}` - the context is a module attribute,
8285
such as `@hello_wor`
8386
@@ -140,6 +143,7 @@ defmodule Code.Fragment do
140143
| {:local_or_var, charlist}
141144
| {:local_arity, charlist}
142145
| {:local_call, charlist}
146+
| {:anonymous_call, inside_caller}
143147
| {:module_attribute, charlist}
144148
| {:operator, charlist}
145149
| {:operator_arity, charlist}
@@ -164,7 +168,8 @@ defmodule Code.Fragment do
164168
| {:alias, inside_alias, charlist}
165169
| {:local_or_var, charlist}
166170
| {:module_attribute, charlist}
167-
| {:dot, inside_dot, charlist}
171+
| {:dot, inside_dot, charlist},
172+
inside_caller: {:var, charlist} | {:module_attribute, charlist}
168173
def cursor_context(fragment, opts \\ [])
169174

170175
def cursor_context(fragment, opts)
@@ -241,6 +246,14 @@ defmodule Code.Fragment do
241246
end
242247
end
243248

249+
defp call_to_cursor_context({[?., h | t], spaces}) when h not in @non_identifier do
250+
case identifier_to_cursor_context([h | t], spaces, true) do
251+
{{:local_or_var, acc}, count} -> {{:anonymous_call, {:var, acc}}, count + 1}
252+
{{:module_attribute, _} = attr, count} -> {{:anonymous_call, attr}, count + 1}
253+
{_, _} -> {:none, 0}
254+
end
255+
end
256+
244257
defp call_to_cursor_context({reverse, spaces}) do
245258
case identifier_to_cursor_context(reverse, spaces, true) do
246259
{{:local_or_var, acc}, count} -> {{:local_call, acc}, count}

lib/elixir/test/elixir/code_fragment_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ defmodule CodeFragmentTest do
124124
assert CF.cursor_context("hello(\t") == {:local_call, ~c"hello"}
125125
assert CF.cursor_context("hello(\n") == {:local_call, ~c"hello"}
126126
assert CF.cursor_context("hello(\r\n") == {:local_call, ~c"hello"}
127+
assert CF.cursor_context("...(") == {:local_call, ~c"..."}
128+
assert CF.cursor_context("...(\s") == {:local_call, ~c"..."}
127129
end
128130

129131
test "dot_arity" do
@@ -200,6 +202,16 @@ defmodule CodeFragmentTest do
200202
{:dot_call, {:alias, {:module_attribute, ~c"foo"}, ~c"Foo"}, ~c"hello"}
201203
end
202204

205+
test "anonymous_call" do
206+
assert CF.cursor_context("hello.(") == {:anonymous_call, {:var, ~c"hello"}}
207+
assert CF.cursor_context("hello.(\s") == {:anonymous_call, {:var, ~c"hello"}}
208+
assert CF.cursor_context("hello.(\t") == {:anonymous_call, {:var, ~c"hello"}}
209+
assert CF.cursor_context("hello.(\n") == {:anonymous_call, {:var, ~c"hello"}}
210+
assert CF.cursor_context("hello.(\r\n") == {:anonymous_call, {:var, ~c"hello"}}
211+
212+
assert CF.cursor_context("@hello.(") == {:anonymous_call, {:module_attribute, ~c"hello"}}
213+
end
214+
203215
test "nested expressions" do
204216
assert CF.cursor_context("Hello.world()") == :none
205217
assert CF.cursor_context("hello().") == {:dot, :expr, ~c""}

0 commit comments

Comments
 (0)