Skip to content

Commit 5ef73a5

Browse files
committed
return info about pids, ports and funs in debugger evaluator
1 parent 41ad1f7 commit 5ef73a5

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

apps/elixir_ls_debugger/lib/debugger/variables.ex

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ defmodule ElixirLS.Debugger.Variables do
1616
end
1717
end
1818

19+
def child_type(var) when is_function(var), do: :named
20+
21+
def child_type(var) when is_pid(var) do
22+
case :erlang.process_info(var) do
23+
:undefined -> :indexed
24+
results -> :named
25+
end
26+
end
27+
28+
def child_type(var) when is_port(var) do
29+
case :erlang.port_info(var) do
30+
:undefined -> :indexed
31+
results -> :named
32+
end
33+
end
34+
1935
def child_type(_var), do: nil
2036

2137
def children(var, start, count) when is_list(var) do
@@ -64,6 +80,27 @@ defmodule ElixirLS.Debugger.Variables do
6480
end
6581
end
6682

83+
def children(var, start, count) when is_function(var) do
84+
:erlang.fun_info(var)
85+
|> children(start, count)
86+
end
87+
88+
def children(var, start, count) when is_pid(var) do
89+
case :erlang.process_info(var) do
90+
:undefined -> ["process is not alive"]
91+
results -> results
92+
end
93+
|> children(start, count)
94+
end
95+
96+
def children(var, start, count) when is_port(var) do
97+
case :erlang.port_info(var) do
98+
:undefined -> ["port is not open"]
99+
results -> results
100+
end
101+
|> children(start, count)
102+
end
103+
67104
def children(_var, _start, _count) do
68105
[]
69106
end
@@ -84,6 +121,25 @@ defmodule ElixirLS.Debugger.Variables do
84121
map_size(var)
85122
end
86123

124+
def num_children(var) when is_function(var) do
125+
:erlang.fun_info(var)
126+
|> Enum.count()
127+
end
128+
129+
def num_children(var) when is_pid(var) do
130+
case :erlang.process_info(var) do
131+
:undefined -> 1
132+
results -> results |> Enum.count()
133+
end
134+
end
135+
136+
def num_children(var) when is_port(var) do
137+
case :erlang.port_info(var) do
138+
:undefined -> 1
139+
results -> results |> Enum.count()
140+
end
141+
end
142+
87143
def num_children(_var) do
88144
0
89145
end

apps/elixir_ls_debugger/test/variables_test.exs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ defmodule ElixirLS.Debugger.VariablesTest do
7979

8080
assert Variables.num_children(:erlang.make_ref()) == 0
8181

82-
assert Variables.num_children(fn -> :ok end) == 0
82+
# As of OTP 24 10 values but it's better not to hardcode that
83+
assert Variables.num_children(fn -> :ok end) != 0
8384

84-
assert Variables.num_children(spawn(fn -> :ok end)) == 0
85+
# As of OTP 24 16 values but it's better not to hardcode that
86+
assert Variables.num_children(self()) != 0
8587

86-
assert Variables.num_children(hd(:erlang.ports())) == 0
88+
# As of OTP 24 7 values but it's better not to hardcode that
89+
assert Variables.num_children(hd(:erlang.ports())) != 0
8790

8891
assert Variables.num_children([]) == 0
8992
assert Variables.num_children([1]) == 1
@@ -173,5 +176,23 @@ defmodule ElixirLS.Debugger.VariablesTest do
173176
assert Variables.children(<<0::size(17)>>, 1, 10) == [{"1", 0}, {"2", <<0::size(1)>>}]
174177
assert Variables.children(<<0::size(17)>>, 1, 1) == [{"1", 0}]
175178
end
179+
180+
test "fun" do
181+
children = Variables.children(fn -> :ok end, 0, 10)
182+
assert children[:module] == ElixirLS.Debugger.VariablesTest
183+
assert children[:type] == :local
184+
assert children[:arity] == 0
185+
end
186+
187+
test "pid" do
188+
children = Variables.children(self(), 0, 10)
189+
assert children[:trap_exit] == false
190+
assert children[:status] == :running
191+
end
192+
193+
test "port" do
194+
children = Variables.children(hd(:erlang.ports()), 0, 10)
195+
assert children[:name] == 'forker'
196+
end
176197
end
177198
end

0 commit comments

Comments
 (0)