@@ -10,12 +10,14 @@ defmodule NextLS do
10
10
alias GenLSP.Notifications.TextDocumentDidChange
11
11
alias GenLSP.Notifications.TextDocumentDidOpen
12
12
alias GenLSP.Notifications.TextDocumentDidSave
13
+ alias GenLSP.Notifications.WorkspaceDidChangeWorkspaceFolders
13
14
alias GenLSP.Requests.Initialize
14
15
alias GenLSP.Requests.Shutdown
15
16
alias GenLSP.Requests.TextDocumentDefinition
16
17
alias GenLSP.Requests.TextDocumentDocumentSymbol
17
18
alias GenLSP.Requests.TextDocumentFormatting
18
19
alias GenLSP.Requests.WorkspaceSymbol
20
+ alias GenLSP.Structures.DidChangeWorkspaceFoldersParams
19
21
alias GenLSP.Structures.DidOpenTextDocumentParams
20
22
alias GenLSP.Structures.InitializeParams
21
23
alias GenLSP.Structures.InitializeResult
@@ -28,6 +30,7 @@ defmodule NextLS do
28
30
alias GenLSP.Structures.TextDocumentItem
29
31
alias GenLSP.Structures.TextDocumentSyncOptions
30
32
alias GenLSP.Structures.TextEdit
33
+ alias GenLSP.Structures.WorkspaceFoldersChangeEvent
31
34
alias NextLS.Definition
32
35
alias NextLS.DiagnosticCache
33
36
alias NextLS.Progress
@@ -288,7 +291,7 @@ defmodule NextLS do
288
291
parent = self ( )
289
292
working_dir = URI . parse ( uri ) . path
290
293
291
- { :ok , runtime } =
294
+ { :ok , _ } =
292
295
DynamicSupervisor . start_child (
293
296
lsp . assigns . dynamic_supervisor ,
294
297
{ NextLS.Runtime.Supervisor ,
@@ -312,8 +315,6 @@ defmodule NextLS do
312
315
logger: lsp . assigns . logger
313
316
] }
314
317
)
315
-
316
- { name , % { uri: uri , runtime: runtime } }
317
318
end
318
319
319
320
{ :noreply , lsp }
@@ -379,6 +380,61 @@ defmodule NextLS do
379
380
{ :noreply , put_in ( lsp . assigns . documents [ uri ] , String . split ( text , "\n " ) ) }
380
381
end
381
382
383
+ def handle_notification (
384
+ % WorkspaceDidChangeWorkspaceFolders {
385
+ params: % DidChangeWorkspaceFoldersParams { event: % WorkspaceFoldersChangeEvent { added: added , removed: removed } }
386
+ } ,
387
+ lsp
388
+ ) do
389
+ dispatch ( lsp . assigns . registry , :runtime_supervisors , fn entries ->
390
+ names = Enum . map ( entries , fn { _ , % { name: name } } -> name end )
391
+
392
+ for % { name: name , uri: uri } <- added , name not in names do
393
+ GenLSP . log ( lsp , "[NextLS] Adding workspace folder #{ name } " )
394
+ token = token ( )
395
+ Progress . start ( lsp , token , "Initializing NextLS runtime for folder #{ name } ..." )
396
+ parent = self ( )
397
+ working_dir = URI . parse ( uri ) . path
398
+
399
+ # TODO: probably extract this to the Runtime module
400
+ { :ok , _ } =
401
+ DynamicSupervisor . start_child (
402
+ lsp . assigns . dynamic_supervisor ,
403
+ { NextLS.Runtime.Supervisor ,
404
+ path: Path . join ( working_dir , ".elixir-tools" ) ,
405
+ name: name ,
406
+ registry: lsp . assigns . registry ,
407
+ runtime: [
408
+ task_supervisor: lsp . assigns . runtime_task_supervisor ,
409
+ working_dir: working_dir ,
410
+ uri: uri ,
411
+ on_initialized: fn status ->
412
+ if status == :ready do
413
+ Progress . stop ( lsp , token , "NextLS runtime for folder #{ name } has initialized!" )
414
+ GenLSP . log ( lsp , "[NextLS] Runtime for folder #{ name } is ready..." )
415
+ send ( parent , { :runtime_ready , name , self ( ) } )
416
+ else
417
+ Progress . stop ( lsp , token )
418
+ GenLSP . error ( lsp , "[NextLS] Runtime for folder #{ name } failed to initialize" )
419
+ end
420
+ end ,
421
+ logger: lsp . assigns . logger
422
+ ] }
423
+ )
424
+ end
425
+
426
+ names = Enum . map ( removed , & & 1 . name )
427
+
428
+ for { pid , % { name: name } } <- entries , name in names do
429
+ GenLSP . log ( lsp , "[NextLS] Removing workspace folder #{ name } " )
430
+ # TODO: probably extract this to the Runtime module
431
+ DynamicSupervisor . terminate_child ( lsp . assigns . dynamic_supervisor , pid )
432
+ end
433
+ end )
434
+
435
+ { :noreply , lsp }
436
+ end
437
+
382
438
def handle_notification ( % Exit { } , lsp ) do
383
439
System . halt ( lsp . assigns . exit_code )
384
440
0 commit comments