@@ -120,13 +120,25 @@ module LspSymbols =
120120 let builtinSignatures =
121121 [ " ignore" , " 'a -> unit"
122122 " print" , " string -> unit"
123+ " Int.tryParse" , " string -> int option"
124+ " Float.tryParse" , " string -> float option"
125+ " Bool.tryParse" , " string -> bool option"
126+ " Int.toString" , " int -> string"
127+ " Float.toString" , " float -> string"
128+ " Bool.toString" , " bool -> string"
123129 " nameof" , " string -> string"
124130 " typeof" , " string -> type" ]
125131 |> Map.ofList
126132
127133 let builtinParamNames =
128134 [ " ignore" , [ " value" ]
129135 " print" , [ " message" ]
136+ " Int.tryParse" , [ " value" ]
137+ " Float.tryParse" , [ " value" ]
138+ " Bool.tryParse" , [ " value" ]
139+ " Int.toString" , [ " value" ]
140+ " Float.toString" , [ " value" ]
141+ " Bool.toString" , [ " value" ]
130142 " nameof" , [ " name" ]
131143 " typeof" , [ " name" ] ]
132144 |> Map.ofList
@@ -425,12 +437,40 @@ module LspSymbols =
425437 match stmt with
426438 | TypeInfer.TSLet ( name, _, t, _, _, _) when name = targetName ->
427439 Some t
440+ | TypeInfer.TSLetPattern (_, _, bindings, _, _) ->
441+ bindings |> Map.tryFind targetName
428442 | TypeInfer.TSLetRecGroup ( bindings, _, _) ->
429443 bindings
430444 |> List.tryPick ( fun ( name , _ , t , _ ) ->
431445 if name = targetName then Some t else None)
432446 | _ -> None)
433447
448+ let rec collectPatternNames ( pat : Pattern ) : string list =
449+ match pat with
450+ | PVar ( name, _) -> [ name ]
451+ | PCons ( head, tail, _) -> collectPatternNames head @ collectPatternNames tail
452+ | PTuple ( items, _) -> items |> List.collect collectPatternNames
453+ | PRecord ( fields, _) -> fields |> List.collect ( fun ( _ , p ) -> collectPatternNames p)
454+ | PMap ( clauses, tailOpt, _) ->
455+ let fromClauses =
456+ clauses
457+ |> List.collect ( fun ( k , v ) -> collectPatternNames k @ collectPatternNames v)
458+ let fromTail =
459+ match tailOpt with
460+ | Some tail -> collectPatternNames tail
461+ | None -> []
462+ fromClauses @ fromTail
463+ | PSome ( inner, _) -> collectPatternNames inner
464+ | PUnionCase (_, _, payload, _) ->
465+ match payload with
466+ | Some p -> collectPatternNames p
467+ | None -> []
468+ | PWildcard _
469+ | PLiteral _
470+ | PNil _
471+ | PNone _
472+ | PTypeRef _ -> []
473+
434474 for stmt in program do
435475 match stmt with
436476 | SType _ ->
@@ -444,6 +484,16 @@ module LspSymbols =
444484 | Some t -> result <- result |> Map.add name t
445485 | None -> ()
446486 | None -> ()
487+ | SLetPattern ( pattern, _, _, _) ->
488+ let candidate = accepted @ [ stmt ]
489+ match tryInferWithCurrent candidate with
490+ | Some typed ->
491+ accepted <- candidate
492+ for name in collectPatternNames pattern do
493+ match extractTypeForName typed name with
494+ | Some t -> result <- result |> Map.add name t
495+ | None -> ()
496+ | None -> ()
447497 | SLetRecGroup ( bindings, _, _) ->
448498 let candidate = accepted @ [ stmt ]
449499 match tryInferWithCurrent candidate with
@@ -481,6 +531,7 @@ module LspSymbols =
481531 | SType _ ->
482532 accepted <- accepted @ [ stmt ]
483533 | SLet _
534+ | SLetPattern _
484535 | SLetRecGroup _ ->
485536 let candidate = accepted @ [ stmt ]
486537 match tryInferWithCurrent candidate with
@@ -529,6 +580,9 @@ module LspSymbols =
529580 | ELet (_, value, body, _, _) ->
530581 let withValue = collectExpr acc value
531582 collectExpr withValue body
583+ | ELetPattern (_, value, body, _) ->
584+ let withValue = collectExpr acc value
585+ collectExpr withValue body
532586 | ELetRecGroup ( bindings, body, _) ->
533587 let withBindings =
534588 bindings
@@ -586,13 +640,44 @@ module LspSymbols =
586640 | ETypeOf _
587641 | ENameOf _ -> acc
588642
643+ let rec collectPatternVarSpans ( pat : Pattern ) : ( string * Span ) list =
644+ match pat with
645+ | PVar ( name, span) -> [ name, span ]
646+ | PCons ( head, tail, _) -> collectPatternVarSpans head @ collectPatternVarSpans tail
647+ | PTuple ( items, _) -> items |> List.collect collectPatternVarSpans
648+ | PRecord ( fields, _) -> fields |> List.collect ( fun ( _ , p ) -> collectPatternVarSpans p)
649+ | PMap ( clauses, tailOpt, _) ->
650+ let fromClauses =
651+ clauses
652+ |> List.collect ( fun ( k , v ) -> collectPatternVarSpans k @ collectPatternVarSpans v)
653+ let fromTail =
654+ match tailOpt with
655+ | Some tail -> collectPatternVarSpans tail
656+ | None -> []
657+ fromClauses @ fromTail
658+ | PSome ( inner, _) -> collectPatternVarSpans inner
659+ | PUnionCase (_, _, payload, _) ->
660+ match payload with
661+ | Some p -> collectPatternVarSpans p
662+ | None -> []
663+ | PWildcard _
664+ | PLiteral _
665+ | PNil _
666+ | PNone _
667+ | PTypeRef _ -> []
668+
589669 let withDeclsAndExprs =
590670 program
591671 |> List.fold ( fun state stmt ->
592672 match stmt with
593673 | SLet ( name, _, expr, _, _, span) ->
594674 let withDecl = addOccurrence name span state
595675 collectExpr withDecl expr
676+ | SLetPattern ( pattern, expr, _, _) ->
677+ let withDecls =
678+ collectPatternVarSpans pattern
679+ |> List.fold ( fun inner ( name , span ) -> addOccurrence name span inner) state
680+ collectExpr withDecls expr
596681 | SLetRecGroup ( bindings, _, _) ->
597682 bindings
598683 |> List.fold ( fun inner ( name , _ , expr , span ) ->
@@ -861,6 +946,8 @@ module LspSymbols =
861946 inScrutinee @ inCases
862947 | ELet (_, value, body, _, _) ->
863948 collectFieldVarUses fieldTypes value @ collectFieldVarUses fieldTypes body
949+ | ELetPattern (_, value, body, _) ->
950+ collectFieldVarUses fieldTypes value @ collectFieldVarUses fieldTypes body
864951 | ELetRecGroup ( bindings, body, _) ->
865952 let inBindings =
866953 bindings
@@ -1007,6 +1094,10 @@ module LspSymbols =
10071094 | ELet ( name, value, body, _, span) ->
10081095 mkBinding name span ( Ast.spanOfExpr body) None
10091096 :: ( collectExprBindings value @ collectExprBindings body)
1097+ | ELetPattern ( pattern, value, body, _) ->
1098+ let scope = Ast.spanOfExpr body
1099+ let fromPattern = collectPatternBindings scope pattern
1100+ fromPattern @ collectExprBindings value @ collectExprBindings body
10101101 | ELetRecGroup ( bindings, body, _) ->
10111102 let fromBindings =
10121103 bindings
@@ -1270,6 +1361,8 @@ module LspSymbols =
12701361 collectExpr ( collectExpr acc source) body
12711362 | ELet (_, value, body, _, _) ->
12721363 collectExpr ( collectExpr acc value) body
1364+ | ELetPattern (_, value, body, _) ->
1365+ collectExpr ( collectExpr acc value) body
12731366 | ELetRecGroup ( bindings, body, _) ->
12741367 let withBindings =
12751368 bindings |> List.fold ( fun state ( _ , _ , value , _ ) -> collectExpr state value) acc
@@ -1322,6 +1415,8 @@ module LspSymbols =
13221415 match stmt with
13231416 | SLet (_, _, expr, _, _, _) ->
13241417 collectExpr state expr
1418+ | SLetPattern (_, expr, _, _) ->
1419+ collectExpr state expr
13251420 | SLetRecGroup ( bindings, _, _) ->
13261421 bindings |> List.fold ( fun inner ( _ , _ , expr , _ ) -> collectExpr inner expr) state
13271422 | SExpr expr ->
@@ -1388,6 +1483,8 @@ module LspSymbols =
13881483 collectExpr withGuard false body) withScrutinee
13891484 | ELet (_, value, body, _, _) ->
13901485 collectExpr ( collectExpr acc false value) false body
1486+ | ELetPattern (_, value, body, _) ->
1487+ collectExpr ( collectExpr acc false value) false body
13911488 | ELetRecGroup ( bindings, body, _) ->
13921489 let withBindings =
13931490 bindings |> List.fold ( fun state ( _ , _ , value , _ ) -> collectExpr state false value) acc
0 commit comments