Skip to content

Commit 4f3e511

Browse files
committed
Update implementations for new erlang-jupyter version
1 parent 488cabb commit 4f3e511

File tree

4 files changed

+86
-55
lines changed

4 files changed

+86
-55
lines changed

src/ierl_backend_elixir.erl

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55

66
-export([
77
init/1,
8+
89
deps/0,
9-
do_kernel_info/2,
10-
do_execute/3,
11-
do_is_complete/3,
12-
do_complete/4,
1310
opt_spec/0,
14-
language/0
11+
language/0,
12+
13+
kernel_info/2,
14+
execute/3,
15+
exec_counter/1,
16+
is_complete/3,
17+
complete/4
1518
]).
1619

1720

1821
-record(state, {
1922
bindings = [],
20-
modules
23+
modules,
24+
counter = 0
2125
}).
2226

2327

@@ -56,7 +60,7 @@ deps() ->
5660
[ierl_versions, ierl_util].
5761

5862

59-
do_kernel_info(_Msg, State) ->
63+
kernel_info(_Msg, _State) ->
6064
Content =
6165
#{
6266
implementation => ?MODULE,
@@ -70,42 +74,48 @@ do_kernel_info(_Msg, State) ->
7074
}
7175
},
7276

73-
{Content, State}.
77+
Content.
7478

7579

76-
do_execute(Code, _Msg, State) ->
80+
execute(Code, _Msg, State) ->
81+
Counter = State#state.counter,
7782
try
7883
{Res, NewBindings} =
7984
'Elixir.Code':eval_string(Code, State#state.bindings),
8085

8186
Res1 = 'Elixir.Kernel':inspect(Res),
87+
Counter1 = Counter + 1,
8288

83-
{{ok, Res1}, State#state{bindings=NewBindings}}
89+
{
90+
{ok, Res1}, Counter1,
91+
State#state{bindings=NewBindings, counter=Counter1}
92+
}
8493
catch
8594
Type:Error ->
8695
Normalized = 'Elixir.Exception':normalize(Type, Error),
8796
Msg = 'Elixir.Exception':message(Normalized),
8897
Formatted = 'Elixir.Exception':format(Type, Normalized),
8998

90-
{{error, Type, Msg, [Formatted]}, State}
99+
{{error, Type, Msg, [Formatted]}, Counter, State}
91100
end.
92101

102+
exec_counter(State) ->
103+
State#state.counter.
93104

94-
do_is_complete(Code, _Msg, State) ->
95-
Res = try
96-
'Elixir.Code':'string_to_quoted!'(Code),
97-
complete
98-
catch
99-
error:#{ '__struct__' := 'Elixir.TokenMissingError'} ->
100-
incomplete;
101-
error:_Other ->
102-
invalid
103-
end,
104-
105-
{Res, State}.
105+
is_complete(Code, _Msg, _State) ->
106+
try
107+
'Elixir.Code':'string_to_quoted!'(Code),
108+
complete
109+
catch
110+
error:#{ '__struct__' := 'Elixir.TokenMissingError'} ->
111+
incomplete;
112+
error:_Other ->
113+
invalid
114+
end.
106115

107116

108-
do_complete(Code, CursorPos, _Msg, State) ->
117+
complete(Code, CursorPos, _Msg, _State) ->
118+
% TODO: Match in the environment
109119
L = lists:sublist(binary_to_list(Code), CursorPos),
110120
Res = case 'Elixir.IEx.Autocomplete':expand(lists:reverse(L)) of
111121
{yes, Expansion, []} ->
@@ -122,7 +132,7 @@ do_complete(Code, CursorPos, _Msg, State) ->
122132
]
123133
end,
124134

125-
{[list_to_binary(R) || R <- Res], State}.
135+
[list_to_binary(R) || R <- Res].
126136

127137

128138
split_arity(Str) ->

src/ierl_backend_erlang.erl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
-export([
77
init/1,
88
deps/0,
9-
do_kernel_info/2,
10-
do_execute/3,
11-
do_is_complete/3,
12-
do_complete/4,
13-
opt_spec/0
9+
opt_spec/0,
10+
11+
kernel_info/2,
12+
execute/3,
13+
exec_counter/1,
14+
is_complete/3,
15+
complete/4
1416
]).
1517

1618

1719
-record(state, {
1820
bindings :: erl_eval:bindings(),
1921
records :: map(),
20-
modules :: map()
22+
modules :: map(),
23+
counter = 0 :: integer()
2124
}).
2225

2326

@@ -40,7 +43,7 @@ deps() ->
4043
[ierl_versions, lager, error_logger_lager_h, lager_trunc_io].
4144

4245

43-
do_kernel_info(_Msg, State) ->
46+
kernel_info(_Msg, _State) ->
4447
Content =
4548
#{
4649
implementation => ?MODULE,
@@ -53,15 +56,17 @@ do_kernel_info(_Msg, State) ->
5356
}
5457
},
5558

56-
{Content, State}.
59+
Content.
5760

5861

59-
do_execute(Code, _Msg, State) ->
62+
execute(Code, _Msg, State) ->
63+
Counter = State#state.counter,
6064
try
6165
{Value, State1} = evaluate(binary_to_list(Code), State),
6266
% TODO: Format records
6367
Str = iolist_to_binary(io_lib:format("~p~n", [Value])),
64-
{{ok, Str}, State1}
68+
Counter1 = Counter + 1,
69+
{{ok, Str}, Counter1, State1#state{counter=Counter1}}
6570
catch
6671
Type:Reason ->
6772
% lager:debug("Error: ~p:~p", [Type, Reason]),
@@ -78,11 +83,14 @@ do_execute(Code, _Msg, State) ->
7883
Stacktrace
7984
],
8085

81-
{{error, Type, Reason1, St}, State}
86+
{{error, Type, Reason1, St}, Counter, State}
8287
end.
8388

89+
exec_counter(State) ->
90+
State#state.counter.
8491

85-
do_complete(Code, CursorPos, _Msg, State) ->
92+
complete(Code, CursorPos, _Msg, _State) ->
93+
% TODO: Use the existing bindings as well
8694
L = lists:sublist(binary_to_list(Code), CursorPos),
8795
Res = case edlin_expand:expand(lists:reverse(L)) of
8896
{yes, Expansion, []} ->
@@ -93,10 +101,10 @@ do_complete(Code, CursorPos, _Msg, State) ->
93101
[Name || {Name, _Arity} <- Matches]
94102
end,
95103

96-
{[list_to_binary(R) || R <- Res], State}.
104+
[list_to_binary(R) || R <- Res].
97105

98106

99-
do_is_complete(Code, _Msg, State) ->
107+
is_complete(Code, _Msg, _State) ->
100108
% TODO: Check if module is complete by looking for two empty lines at the
101109
% end?
102110
Res = case erl_scan:string(binary_to_list(Code)) of
@@ -106,7 +114,7 @@ do_is_complete(Code, _Msg, State) ->
106114
invalid
107115
end,
108116

109-
{Res, State}.
117+
Res.
110118

111119

112120
evaluate(Expression, State) ->

src/ierl_backend_lfe.erl

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
-export([
77
init/1,
88
deps/0,
9-
do_kernel_info/2,
10-
do_execute/3,
11-
do_is_complete/3,
12-
do_complete/4,
139
opt_spec/0,
14-
language/0
10+
language/0,
11+
12+
kernel_info/2,
13+
execute/3,
14+
exec_counter/1,
15+
is_complete/3,
16+
complete/4
1517
]).
1618

1719

1820
-record(state, {
19-
env
21+
env,
22+
counter = 0
2023
}).
2124

2225

@@ -40,7 +43,7 @@ deps() ->
4043
[ierl_versions].
4144

4245

43-
do_kernel_info(_Msg, State) ->
46+
kernel_info(_Msg, _State) ->
4447
Content =
4548
#{
4649
implementation => ?MODULE,
@@ -55,18 +58,21 @@ do_kernel_info(_Msg, State) ->
5558
}
5659
},
5760

58-
{Content, State}.
61+
Content.
5962

6063

61-
do_execute(Code, _Msg, State) ->
64+
execute(Code, _Msg, State) ->
65+
Counter = State#state.counter,
6266
try
6367
{Res, NewState} =
6468
lfe_shell:run_string(binary_to_list(Code), State#state.env),
6569

6670
NewBindings = element(2, NewState),
6771
Res1 = jup_util:ensure_binary(lfe_io_pretty:term(Res)),
6872

69-
{{ok, Res1}, State#state{env=NewBindings}}
73+
Counter1 = Counter + 1,
74+
75+
{{ok, Res1}, Counter1, State#state{env=NewBindings, counter=Counter1}}
7076
catch
7177
Type:Reason ->
7278
Stacktrace = format_stacktrace(erlang:get_stacktrace()),
@@ -77,11 +83,15 @@ do_execute(Code, _Msg, State) ->
7783
| Stacktrace
7884
],
7985

80-
{{error, Type, Reason1, St}, State}
86+
{{error, Type, Reason1, St}, Counter, State}
8187
end.
8288

8389

84-
do_is_complete(Code, _Msg, State) ->
90+
exec_counter(State) ->
91+
State#state.counter.
92+
93+
94+
is_complete(Code, _Msg, _State) ->
8595
Res = case lfe_scan:string(binary_to_list(Code), 1) of
8696
{ok, Tokens, _} ->
8797
case lfe_parse:sexpr(Tokens) of
@@ -97,10 +107,12 @@ do_is_complete(Code, _Msg, State) ->
97107
invalid
98108
end,
99109

100-
{Res, State}.
110+
Res.
101111

102112

103-
do_complete(Code, CursorPos, _Msg, State) ->
113+
complete(Code, CursorPos, _Msg, State) ->
114+
% TODO: Check in the environment for completables
115+
_Env = State#state.env,
104116
L = lists:sublist(binary_to_list(Code), CursorPos),
105117
Res = case lfe_edlin_expand:expand(lists:reverse(L)) of
106118
{yes, Expansion, []} ->
@@ -111,7 +123,7 @@ do_complete(Code, CursorPos, _Msg, State) ->
111123
[Name || {Name, _Arity} <- Matches]
112124
end,
113125

114-
{[list_to_binary(R) || R <- Res], State}.
126+
[list_to_binary(R) || R <- Res].
115127

116128

117129
format_stacktrace(Stacktrace) ->

src/ierl_kernelspec.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ create(Options) ->
5959
language => jup_util:call_if_exported(
6060
BackendModule, language, [], erlang
6161
),
62+
interrupt_mode => <<"message">>,
6263
env => Env
6364
},
6465

0 commit comments

Comments
 (0)