@@ -1032,14 +1032,13 @@ defmodule :_next_ls_private_compiler do
10321032 @ moduledoc false
10331033
10341034 def start do
1035+ Code . put_compiler_option ( :parser_options , columns: true , token_metadata: true )
1036+
10351037 children = [
10361038 :_next_ls_private_compiler_worker
10371039 ]
10381040
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 )
10431042 Process . unlink ( pid )
10441043 { :ok , pid }
10451044 end
@@ -1049,7 +1048,6 @@ defmodule :_next_ls_private_compiler do
10491048 def compile do
10501049 # keep stdout on this node
10511050 Process . group_leader ( self ( ) , Process . whereis ( :user ) )
1052- Code . put_compiler_option ( :parser_options , columns: true , token_metadata: true )
10531051
10541052 Code . put_compiler_option ( :tracers , [ NextLSPrivate.DepTracer | @ tracers ] )
10551053
@@ -1425,39 +1423,36 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
14251423 end
14261424 end
14271425
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 }
14371433
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
14421437
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 , [ ] )
14471439
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
14551442 { _ , state , penv } =
14561443 for p <- params , reduce: { nil , state , env } do
14571444 { _ , state , penv } ->
14581445 expand_pattern ( p , state , penv )
14591446 end
14601447
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
14611456 { blocks , state } =
14621457 for { type , block } <- blocks , reduce: { [ ] , state } do
14631458 { acc , state } ->
@@ -1467,26 +1462,21 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
14671462
14681463 arity = length ( List . wrap ( params ) )
14691464
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
14801470 end
14811471
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 ]
14871477 end
14881478
1489- { Enum . reverse ( blocks ) , state , env }
1479+ { Enum . reverse ( blocks ) , put_in ( state [ state_key ] , funcs ) , env }
14901480 end
14911481
14921482 defp expand_macro ( meta , Kernel , :@ , [ { name , _ , p } ] = args , callback , state , env ) when is_list ( p ) do
0 commit comments