11defmodule Expert do
2+ alias Expert.Project
23 alias Expert.Protocol.Convert
34 alias Expert.Protocol.Id
45 alias Expert.Provider.Handlers
@@ -51,27 +52,36 @@ defmodule Expert do
5152
5253 def handle_request ( % GenLSP.Requests.Initialize { } = request , lsp ) do
5354 state = assigns ( lsp ) . state
54- Process . send_after ( self ( ) , :default_config , :timer . seconds ( 5 ) )
5555
5656 with { :ok , response , state } <- State . initialize ( state , request ) ,
5757 { :ok , response } <- Expert.Protocol.Convert . to_lsp ( response ) do
58- lsp = assign ( lsp , state: state )
58+ config = state . configuration
5959
60- { :reply , response , lsp }
60+ Task.Supervisor . start_child ( :expert_task_queue , fn ->
61+ Logger . info ( "Starting project at uri #{ config . project . root_uri } " )
62+
63+ start_result = Project.Supervisor . start ( config . project )
64+
65+ send ( Expert , { :engine_initialized , start_result } )
66+ end )
67+
68+ { :reply , response , assign ( lsp , state: state ) }
6169 else
62- { :error , { :shutdown , { :failed_to_start_child , _ , _ } } } ->
63- { :reply ,
64- % GenLSP.ErrorResponse {
65- code: GenLSP.Enumerations.ErrorCodes . server_not_initialized ( ) ,
66- message: "Failed to start node"
67- } , lsp }
70+ { :error , :already_initialized } ->
71+ response = % GenLSP.ErrorResponse {
72+ code: GenLSP.Enumerations.ErrorCodes . invalid_request ( ) ,
73+ message: "Already initialized"
74+ }
75+
76+ { :reply , response , lsp }
6877
6978 { :error , reason } ->
70- { :reply ,
71- % GenLSP.ErrorResponse {
72- code: GenLSP.Enumerations.ErrorCodes . server_not_initialized ( ) ,
73- message: to_string ( reason )
74- } , lsp }
79+ response = % GenLSP.ErrorResponse {
80+ code: GenLSP.Enumerations.ErrorCodes . server_not_initialized ( ) ,
81+ message: "Failed to initialize: #{ inspect ( reason ) } "
82+ }
83+
84+ { :reply , response , lsp }
7585 end
7686 end
7787
@@ -178,7 +188,7 @@ defmodule Expert do
178188 end
179189 end
180190
181- def handle_info ( :engine_initialized , lsp ) do
191+ def handle_info ( { :engine_initialized , { :ok , _pid } } , lsp ) do
182192 state = assigns ( lsp ) . state
183193
184194 new_state = % { state | engine_initialized?: true }
@@ -190,20 +200,10 @@ defmodule Expert do
190200 { :noreply , lsp }
191201 end
192202
193- def handle_info ( :default_config , lsp ) do
194- state = assigns ( lsp ) . state
195-
196- if state . configuration == nil do
197- Logger . warning (
198- "Did not receive workspace/didChangeConfiguration notification after 5 seconds. " <>
199- "Using default settings."
200- )
203+ def handle_info ( { :engine_initialized , { :error , reason } } , lsp ) do
204+ GenLSP . error ( get_lsp ( ) , initialization_error_message ( reason ) )
201205
202- { :ok , config } = State . default_configuration ( state )
203- { :noreply , assign ( lsp , state: % { state | configuration: config } ) }
204- else
205- { :noreply , lsp }
206- end
206+ { :noreply , lsp }
207207 end
208208
209209 defp apply_to_state ( % State { } = state , % { } = request_or_notification ) do
@@ -278,4 +278,33 @@ defmodule Expert do
278278 register_options: % Structures.DidChangeWatchedFilesRegistrationOptions { watchers: watchers }
279279 }
280280 end
281+
282+ defp initialization_error_message ( { :shutdown , { :failed_to_start_child , child , { reason , _ } } } ) do
283+ case child do
284+ { Project.Node , node_name } ->
285+ node_initialization_message ( node_name , reason )
286+
287+ child ->
288+ "Failed to start child #{ inspect ( child ) } : #{ inspect ( reason ) } "
289+ end
290+ end
291+
292+ defp initialization_error_message ( reason ) do
293+ "Failed to initialize: #{ inspect ( reason ) } "
294+ end
295+
296+ defp node_initialization_message ( name , reason ) do
297+ case reason do
298+ # NOTE:
299+ # ** (Mix.Error) httpc request failed with: ... Could not install Hex because Mix could not download metadata ...
300+ { :shutdown , { :error , :normal , message } } ->
301+ "Node #{ name } shutdown with error:\n \n #{ message } "
302+
303+ { :shutdown , { :node_exit , node_exit } } ->
304+ "Node #{ name } exit with status #{ node_exit . status } , last message:\n \n #{ node_exit . last_message } "
305+
306+ reason ->
307+ "Failed to start node #{ name } : #{ inspect ( reason ) } "
308+ end
309+ end
281310end
0 commit comments