Skip to content

Commit 4d7ce2e

Browse files
committed
Try completions if path didn't have any results
1 parent cb88ad9 commit 4d7ce2e

File tree

3 files changed

+65
-53
lines changed

3 files changed

+65
-53
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,34 +1076,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10761076
| Some (Tpromise (env, typ), _env) ->
10771077
[Completion.create "dummy" ~env ~kind:(Completion.Value typ)]
10781078
| _ -> [])
1079-
| CPId {path = ["throw"]; completionContext = Value; loc = _} ->
1080-
let exn_typ = Ctype.newconstr Predef.path_exn [] in
1081-
let names_from_cmt =
1082-
let moduleName = env.file.moduleName in
1083-
match Hashtbl.find_opt full.package.pathsForModule moduleName with
1084-
| None -> []
1085-
| Some paths -> (
1086-
let uri = getUri paths in
1087-
let cmt_path = getCmtPath ~uri paths in
1088-
match Shared.tryReadCmt cmt_path with
1089-
| None -> []
1090-
| Some infos -> exceptions_from_cmt_infos infos)
1091-
in
1092-
let all = names_from_cmt @ predefined_exceptions in
1093-
all
1094-
|> List.map (fun (name, hasArgs) ->
1095-
let insertText =
1096-
if hasArgs then Printf.sprintf "(%s($0))" name
1097-
else Printf.sprintf "(%s)" name
1098-
in
1099-
let isBuiltin = List.mem (name, hasArgs) predefined_exceptions in
1100-
let detail =
1101-
if isBuiltin then "Built-in Exception"
1102-
else "User-defined Exception"
1103-
in
1104-
Completion.create name ~env ~kind:(Completion.Value exn_typ)
1105-
~includesSnippets:true ~insertText ~detail)
1106-
| CPId {path; completionContext; loc} ->
1079+
| CPId {path; completionContext; loc} -> (
11071080
if Debug.verbose () then print_endline "[ctx_path]--> CPId";
11081081
(* Looks up the type of an identifier.
11091082
@@ -1141,7 +1114,37 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11411114
| _ -> byPath
11421115
else byPath
11431116
in
1144-
result
1117+
match (result, path) with
1118+
| [], [prefix] when Utils.startsWith "throw" prefix ->
1119+
let exn_typ = Ctype.newconstr Predef.path_exn [] in
1120+
let names_from_cmt =
1121+
let moduleName = env.file.moduleName in
1122+
match Hashtbl.find_opt full.package.pathsForModule moduleName with
1123+
| None -> []
1124+
| Some paths -> (
1125+
let uri = getUri paths in
1126+
let cmt_path = getCmtPath ~uri paths in
1127+
match Shared.tryReadCmt cmt_path with
1128+
| None -> []
1129+
| Some infos -> exceptions_from_cmt_infos infos)
1130+
in
1131+
let all = names_from_cmt @ predefined_exceptions in
1132+
all
1133+
|> List.map (fun (name, hasArgs) ->
1134+
let insertText =
1135+
if hasArgs then Printf.sprintf "throw(%s($0))" name
1136+
else Printf.sprintf "throw(%s)" name
1137+
in
1138+
let isBuiltin = List.mem (name, hasArgs) predefined_exceptions in
1139+
let detail =
1140+
if isBuiltin then "Built-in Exception"
1141+
else "User-defined Exception"
1142+
in
1143+
Completion.create
1144+
(Printf.sprintf "throw(%s)" name)
1145+
~env ~kind:(Completion.Value exn_typ) ~includesSnippets:true
1146+
~insertText ~filterText:"throw" ~detail)
1147+
| _ -> result)
11451148
| CPApply (cp, labels) -> (
11461149
if Debug.verbose () then print_endline "[ctx_path]--> CPApply";
11471150
match
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
exception MyCustomThingToThrow(string)
22
exception NoArgsToThrow
33

4-
// let x = () => throw
5-
// ^com
4+
// let x = () => thro
5+
// ^com
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,83 @@
1-
Complete src/Throw.res 3:22
2-
posCursor:[3:22] posNoWhite:[3:21] Found expr:[3:11->3:22]
3-
posCursor:[3:22] posNoWhite:[3:21] Found expr:[3:17->3:22]
4-
Pexp_ident throw:[3:17->3:22]
5-
Completable: Cpath Value[throw]
1+
Complete src/Throw.res 3:21
2+
posCursor:[3:21] posNoWhite:[3:20] Found expr:[3:11->3:21]
3+
posCursor:[3:21] posNoWhite:[3:20] Found expr:[3:17->3:21]
4+
Pexp_ident thro:[3:17->3:21]
5+
Completable: Cpath Value[thro]
66
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
77
Resolved opens 1 Stdlib
8-
ContextPath Value[throw]
8+
ContextPath Value[thro]
9+
Path thro
910
[{
10-
"label": "MyCustomThingToThrow",
11+
"label": "throw(MyCustomThingToThrow)",
1112
"kind": 12,
1213
"tags": [],
1314
"detail": "User-defined Exception",
1415
"documentation": null,
15-
"insertText": "(MyCustomThingToThrow($0))",
16+
"filterText": "throw",
17+
"insertText": "throw(MyCustomThingToThrow($0))",
1618
"insertTextFormat": 2
1719
}, {
18-
"label": "NoArgsToThrow",
20+
"label": "throw(NoArgsToThrow)",
1921
"kind": 12,
2022
"tags": [],
2123
"detail": "User-defined Exception",
2224
"documentation": null,
23-
"insertText": "(NoArgsToThrow)",
25+
"filterText": "throw",
26+
"insertText": "throw(NoArgsToThrow)",
2427
"insertTextFormat": 2
2528
}, {
26-
"label": "Not_found",
29+
"label": "throw(Not_found)",
2730
"kind": 12,
2831
"tags": [],
2932
"detail": "Built-in Exception",
3033
"documentation": null,
31-
"insertText": "(Not_found($0))",
34+
"filterText": "throw",
35+
"insertText": "throw(Not_found($0))",
3236
"insertTextFormat": 2
3337
}, {
34-
"label": "Invalid_argument",
38+
"label": "throw(Invalid_argument)",
3539
"kind": 12,
3640
"tags": [],
3741
"detail": "Built-in Exception",
3842
"documentation": null,
39-
"insertText": "(Invalid_argument($0))",
43+
"filterText": "throw",
44+
"insertText": "throw(Invalid_argument($0))",
4045
"insertTextFormat": 2
4146
}, {
42-
"label": "Assert_failure",
47+
"label": "throw(Assert_failure)",
4348
"kind": 12,
4449
"tags": [],
4550
"detail": "Built-in Exception",
4651
"documentation": null,
47-
"insertText": "(Assert_failure($0))",
52+
"filterText": "throw",
53+
"insertText": "throw(Assert_failure($0))",
4854
"insertTextFormat": 2
4955
}, {
50-
"label": "Failure",
56+
"label": "throw(Failure)",
5157
"kind": 12,
5258
"tags": [],
5359
"detail": "Built-in Exception",
5460
"documentation": null,
55-
"insertText": "(Failure($0))",
61+
"filterText": "throw",
62+
"insertText": "throw(Failure($0))",
5663
"insertTextFormat": 2
5764
}, {
58-
"label": "Match_failure",
65+
"label": "throw(Match_failure)",
5966
"kind": 12,
6067
"tags": [],
6168
"detail": "Built-in Exception",
6269
"documentation": null,
63-
"insertText": "(Match_failure($0))",
70+
"filterText": "throw",
71+
"insertText": "throw(Match_failure($0))",
6472
"insertTextFormat": 2
6573
}, {
66-
"label": "Division_by_zero",
74+
"label": "throw(Division_by_zero)",
6775
"kind": 12,
6876
"tags": [],
6977
"detail": "Built-in Exception",
7078
"documentation": null,
71-
"insertText": "(Division_by_zero)",
79+
"filterText": "throw",
80+
"insertText": "throw(Division_by_zero)",
7281
"insertTextFormat": 2
7382
}]
7483

0 commit comments

Comments
 (0)