@@ -1032,14 +1032,13 @@ defmodule :_next_ls_private_compiler do
1032
1032
@ moduledoc false
1033
1033
1034
1034
def start do
1035
+ Code . put_compiler_option ( :parser_options , columns: true , token_metadata: true )
1036
+
1035
1037
children = [
1036
1038
:_next_ls_private_compiler_worker
1037
1039
]
1038
1040
1039
- # See https://hexdocs.pm/elixir/Supervisor.html
1040
- # for other strategies and supported options
1041
- opts = [ strategy: :one_for_one , name: :_next_ls_private_application_supervisor ]
1042
- { :ok , pid } = Supervisor . start_link ( children , opts )
1041
+ { :ok , pid } = Supervisor . start_link ( children , strategy: :one_for_one , name: :_next_ls_private_application_supervisor )
1043
1042
Process . unlink ( pid )
1044
1043
{ :ok , pid }
1045
1044
end
@@ -1049,7 +1048,6 @@ defmodule :_next_ls_private_compiler do
1049
1048
def compile do
1050
1049
# keep stdout on this node
1051
1050
Process . group_leader ( self ( ) , Process . whereis ( :user ) )
1052
- Code . put_compiler_option ( :parser_options , columns: true , token_metadata: true )
1053
1051
1054
1052
Code . put_compiler_option ( :tracers , [ NextLSPrivate.DepTracer | @ tracers ] )
1055
1053
@@ -1425,39 +1423,36 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
1425
1423
end
1426
1424
end
1427
1425
1428
- defp expand_macro ( _meta , Kernel , type , [ { name , _ , params } , [ { _ , block } ] ] , _callback , state , env )
1429
- when type in [ :def , :defp ] and is_tuple ( block ) and is_atom ( name ) and is_list ( params ) do
1430
- { _ , state , penv } =
1431
- for p <- params , reduce: { nil , state , env } do
1432
- { _ , state , penv } ->
1433
- expand_pattern ( p , state , penv )
1434
- end
1435
-
1436
- { res , state , _env } = expand ( block , state , penv )
1426
+ defp expand_macro ( _meta , Kernel , type , args , _callback , state , env )
1427
+ when type in [ :def , :defmacro , :defp , :defmacrop ] do
1428
+ # extract the name, params, guards, and blocks
1429
+ { name , params , guards , blocks } =
1430
+ case args do
1431
+ [ { :when , _ , [ { name , _ , params } | guards ] } | maybe_blocks ] ->
1432
+ { name , params , guards , maybe_blocks }
1437
1433
1438
- arity = length ( List . wrap ( params ) )
1439
- functions = Map . update ( state . functions , env . module , [ { name , arity } ] , & Keyword . put_new ( & 1 , name , arity ) )
1440
- { res , put_in ( state . functions , functions ) , env }
1441
- end
1434
+ [ { name , _ , params } | maybe_blocks ] ->
1435
+ { name , params , [ ] , maybe_blocks }
1436
+ end
1442
1437
1443
- defp expand_macro ( _meta , Kernel , type , [ { name , _ , params } , block ] , _callback , state , env )
1444
- when type in [ :defmacro , :defmacrop ] do
1445
- { _res , state , penv } = expand ( params , state , env )
1446
- { res , state , _env } = expand ( block , state , penv )
1438
+ blocks = List . first ( blocks , [ ] )
1447
1439
1448
- arity = length ( List . wrap ( params ) )
1449
- macros = Map . update ( state . macros , env . module , [ { name , arity } ] , & Keyword . put_new ( & 1 , name , arity ) )
1450
- { res , put_in ( state . macros , macros ) , env }
1451
- end
1452
-
1453
- defp expand_macro ( _meta , Kernel , type , [ { name , _ , params } , blocks ] , _callback , state , env )
1454
- when type in [ :def , :defp ] and is_atom ( name ) and is_list ( params ) and is_list ( blocks ) do
1440
+ # collect the environment from the parameters
1441
+ # parameters are always patterns
1455
1442
{ _ , state , penv } =
1456
1443
for p <- params , reduce: { nil , state , env } do
1457
1444
{ _ , state , penv } ->
1458
1445
expand_pattern ( p , state , penv )
1459
1446
end
1460
1447
1448
+ # expand guards, which includes the env from params
1449
+ { _ , state , _ } =
1450
+ for guard <- guards , reduce: { nil , state , penv } do
1451
+ { _ , state , env } ->
1452
+ expand ( guard , state , env )
1453
+ end
1454
+
1455
+ # expand the blocks, there could be `:do`, `:after`, `:catch`, etc
1461
1456
{ blocks , state } =
1462
1457
for { type , block } <- blocks , reduce: { [ ] , state } do
1463
1458
{ acc , state } ->
@@ -1467,26 +1462,21 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
1467
1462
1468
1463
arity = length ( List . wrap ( params ) )
1469
1464
1470
- functions = Map . update ( state . functions , env . module , [ { name , arity } ] , & Keyword . put_new ( & 1 , name , arity ) )
1471
- { Enum . reverse ( blocks ) , put_in ( state . functions , functions ) , env }
1472
- end
1473
-
1474
- defp expand_macro ( _meta , Kernel , type , [ { _name , _ , params } , blocks ] , _callback , state , env )
1475
- when type in [ :def , :defp ] and is_list ( params ) and is_list ( blocks ) do
1476
- { _ , state , penv } =
1477
- for p <- params , reduce: { nil , state , env } do
1478
- { _ , state , penv } ->
1479
- expand_pattern ( p , state , penv )
1465
+ # determine which key to save this function in state
1466
+ state_key =
1467
+ case type do
1468
+ type when type in [ :def , :defp ] -> :functions
1469
+ type when type in [ :defmacro , :defmacrop ] -> :macros
1480
1470
end
1481
1471
1482
- { blocks , state } =
1483
- for { type , block } <- blocks , reduce: { [ ] , state } do
1484
- { acc , state } ->
1485
- { res , state , _env } = expand ( block , state , penv )
1486
- { [ { type , res } | acc ] , state }
1472
+ funcs =
1473
+ if is_atom ( name ) do
1474
+ Map . update ( state [ state_key ] , env . module , [ { name , arity } ] , & Keyword . put_new ( & 1 , name , arity ) )
1475
+ else
1476
+ state [ state_key ]
1487
1477
end
1488
1478
1489
- { Enum . reverse ( blocks ) , state , env }
1479
+ { Enum . reverse ( blocks ) , put_in ( state [ state_key ] , funcs ) , env }
1490
1480
end
1491
1481
1492
1482
defp expand_macro ( meta , Kernel , :@ , [ { name , _ , p } ] = args , callback , state , env ) when is_list ( p ) do
0 commit comments