Skip to content

Commit b1518ea

Browse files
committed
handle empty let assignments with constraints
1 parent 00b3f82 commit b1518ea

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

analysis/src/CompletionBackEndRevamped.ml

+15-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ let findRecordField ~env ~package ~fieldName typ =
1818
| Some (_recordEnv, fields, _decl) ->
1919
fields |> List.find_opt (fun (field : field) -> field.fname.txt = fieldName)
2020

21-
let completeEmptyPattern ~env ~package typ =
21+
type patternOrExpr = Pattern | Expr
22+
let completeEmpty ~(mode : patternOrExpr) ~env ~package typ =
23+
ignore mode;
2224
match TypeUtils.extractType ~env ~package typ with
23-
| None -> []
25+
| None ->
26+
if Debug.verbose () then
27+
print_endline "⚠️ Could not extract completable type";
28+
[]
2429
| Some (completionType, typeArgContext) -> (
2530
(* Fill this out with the different completions *)
2631
match completionType with
@@ -62,22 +67,27 @@ let processCompletable ~debug ~full ~scope ~env ~pos
6267
match completable with
6368
| Cexpression {kind; typeLoc} -> (
6469
match TypeUtils.findTypeViaLoc typeLoc ~full ~debug with
65-
| None -> []
70+
| None ->
71+
if Debug.verbose () then print_endline "⚠️ No type found for loc";
72+
[]
6673
| Some typ -> (
74+
if Debug.verbose () then
75+
print_endline ("✅ Found type at loc:" ^ Shared.typeToString typ);
6776
match kind with
77+
| Empty -> completeEmpty ~mode:Expr ~env ~package typ
6878
| Field {hint} -> findFields ~env ~package ~hint typ))
6979
| Cpattern {kind; typeLoc} -> (
7080
match TypeUtils.findTypeViaLoc typeLoc ~full ~debug with
7181
| None -> []
7282
| Some typ -> (
7383
match kind with
74-
| Empty -> completeEmptyPattern ~env ~package typ
84+
| Empty -> completeEmpty ~mode:Pattern ~env ~package typ
7585
| Field {hint; seenFields} ->
7686
findFields ~env ~package ~hint ~seenFields typ
7787
| FieldValue {fieldName} -> (
7888
match findRecordField ~env ~package ~fieldName typ with
7989
| None -> []
80-
| Some field -> completeEmptyPattern ~env ~package field.typ)))
90+
| Some field -> completeEmpty ~mode:Pattern ~env ~package field.typ)))
8191
| Cnone -> []
8292
| CextensionNode _ -> []
8393
| Cdecorator prefix ->

analysis/src/CompletionFrontEndRevamped.ml

+16
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ let completionWithParser ~currentFile ~debug ~offset ~path ~posCursor text =
165165
in
166166
let value_binding (iterator : Ast_iterator.iterator)
167167
(value_binding : Parsetree.value_binding) =
168+
(match value_binding with
169+
| {pvb_pat = {ppat_desc = Ppat_constraint (p, _)}; pvb_expr; pvb_loc}
170+
when CompletionExpressions.isExprHole pvb_expr
171+
&& locHasCursor pvb_loc
172+
&& not (locHasCursor p.ppat_loc) ->
173+
(* let x: constraint = <com> *)
174+
setResult (Cexpression {kind = Empty; typeLoc = p.ppat_loc; posOfDot})
175+
| {pvb_pat; pvb_expr; pvb_loc}
176+
when CompletionExpressions.isExprHole pvb_expr
177+
&& locHasCursor pvb_loc
178+
&& not (locHasCursor pvb_pat.ppat_loc) ->
179+
(* let x= <com> *)
180+
(* Unclear if this happens and if we need to care about it. *)
181+
setResult
182+
(Cexpression {kind = Empty; typeLoc = pvb_pat.ppat_loc; posOfDot})
183+
| _ -> ());
168184
Ast_iterator.default_iterator.value_binding iterator value_binding
169185
in
170186
let signature (iterator : Ast_iterator.iterator)

analysis/src/SharedTypes.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ module CompletableRevamped = struct
801801
| ModuleWithImportAttributes of {prefix: string}
802802
| JsxConfig of {prefix: string}
803803

804-
type completionExprKind = Field of {hint: string}
804+
type completionExprKind = Empty | Field of {hint: string}
805805

806806
type completionPatternKind =
807807
| Empty
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// == TEST: Empty let assignment expression, array
2+
let x: array<string> =
3+
// ^com
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Found Completable: Cexpression at type loc: [1:4->1:5]
2+
3+
1// Empty let assignment expression, array
4+
2let x: array<string> =
5+
│ ‾
6+
3// ^com
7+
4
8+
9+
[{
10+
"label": "[]",
11+
"kind": 12,
12+
"tags": [],
13+
"detail": "array<string>",
14+
"documentation": null,
15+
"sortText": "A",
16+
"insertText": "[$0]",
17+
"insertTextFormat": 2
18+
}]
19+

0 commit comments

Comments
 (0)