@@ -23,33 +23,93 @@ defmodule ElixirLS.Utils.WireProtocol do
2323 end
2424
2525 def io_intercepted? do
26- ! ! Process . whereis ( :raw_user )
26+ ! ! Process . whereis ( :raw_standard_error )
2727 end
2828
2929 def intercept_output ( print_fn , print_err_fn ) do
3030 raw_user = Process . whereis ( :user )
3131 raw_standard_error = Process . whereis ( :standard_error )
3232
33- :ok = :io . setopts ( raw_user , OutputDevice . get_opts ( ) )
33+ :ok = :io . setopts ( raw_user , binary: true , encoding: :latin1 )
3434
35- { :ok , user } = OutputDevice . start_link ( raw_user , print_fn )
36- { :ok , standard_error } = OutputDevice . start_link ( raw_user , print_err_fn )
35+ { :ok , intercepted_user } = OutputDevice . start_link ( raw_user , print_fn )
36+ { :ok , intercepted_standard_error } = OutputDevice . start_link ( raw_user , print_err_fn )
3737
3838 Process . unregister ( :user )
3939 Process . register ( raw_user , :raw_user )
40- Process . register ( user , :user )
40+ Process . register ( intercepted_user , :user )
4141
4242 Process . unregister ( :standard_error )
4343 Process . register ( raw_standard_error , :raw_standard_error )
44- Process . register ( standard_error , :standard_error )
44+ Process . register ( intercepted_standard_error , :standard_error )
4545
46- for process <- :erlang . processes ( ) , process not in [ raw_user , raw_standard_error ] do
47- Process . group_leader ( process , user )
46+ for process <- :erlang . processes ( ) ,
47+ process not in [
48+ raw_user ,
49+ raw_standard_error ,
50+ intercepted_user ,
51+ intercepted_standard_error
52+ ] do
53+ Process . group_leader ( process , intercepted_user )
4854 end
4955 end
5056
57+ def undo_intercept_output ( ) do
58+ intercepted_user = Process . whereis ( :user )
59+ intercepted_standard_error = Process . whereis ( :standard_error )
60+
61+ Process . unregister ( :user )
62+
63+ raw_user =
64+ try do
65+ raw_user = Process . whereis ( :raw_user )
66+ Process . unregister ( :raw_user )
67+ Process . register ( raw_user , :user )
68+ raw_user
69+ rescue
70+ ArgumentError -> nil
71+ end
72+
73+ Process . unregister ( :standard_error )
74+
75+ raw_standard_error =
76+ try do
77+ raw_standard_error = Process . whereis ( :raw_standard_error )
78+ Process . unregister ( :raw_standard_error )
79+ Process . register ( raw_standard_error , :standard_error )
80+ raw_user
81+ rescue
82+ ArgumentError -> nil
83+ end
84+
85+ if raw_user do
86+ for process <- :erlang . processes ( ) ,
87+ process not in [
88+ raw_user ,
89+ raw_standard_error ,
90+ intercepted_user ,
91+ intercepted_standard_error
92+ ] do
93+ Process . group_leader ( process , raw_user )
94+ end
95+ else
96+ init = :erlang . processes ( ) |> hd
97+
98+ for process <- :erlang . processes ( ) ,
99+ process not in [ raw_standard_error , intercepted_user , intercepted_standard_error ] do
100+ Process . group_leader ( process , init )
101+ end
102+ end
103+
104+ Process . unlink ( intercepted_user )
105+ Process . unlink ( intercepted_standard_error )
106+
107+ Process . exit ( intercepted_user , :kill )
108+ Process . exit ( intercepted_standard_error , :kill )
109+ end
110+
51111 def stream_packets ( receive_packets_fn ) do
52- PacketStream . stream ( Process . whereis ( :raw_user ) )
112+ PacketStream . stream ( Process . whereis ( :raw_user ) , true )
53113 |> Stream . each ( fn packet -> receive_packets_fn . ( packet ) end )
54114 |> Stream . run ( )
55115 end
0 commit comments