Skip to content

Commit d23722d

Browse files
committed
Remove ReScript exceptinons and add hardcoded throw functions.
1 parent 5cd2754 commit d23722d

2 files changed

Lines changed: 32 additions & 76 deletions

File tree

analysis/src/CompletionBackEnd.ml

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -756,17 +756,6 @@ let getCompletionsForPath ~debug ~opens ~full ~pos ~exact ~scope
756756
findAllCompletions ~env ~prefix ~exact ~namesUsed ~completionContext
757757
| None -> []))
758758

759-
(* Predefined Stdlib/Pervasives exceptions. *)
760-
let predefined_exceptions : (string * bool) list =
761-
[
762-
("Not_found", true);
763-
("Invalid_argument", true);
764-
("Assert_failure", true);
765-
("Failure", true);
766-
("Match_failure", true);
767-
("Division_by_zero", false);
768-
]
769-
770759
let completionsForThrow ~(env : QueryEnv.t) ~full =
771760
let exn_typ = Predef.type_exn in
772761
let names_from_cmt =
@@ -778,21 +767,26 @@ let completionsForThrow ~(env : QueryEnv.t) ~full =
778767
let cmt_path = getCmtPath ~uri paths in
779768
ProcessCmt.exceptionsForCmt ~cmt:cmt_path
780769
in
781-
let all = names_from_cmt @ predefined_exceptions in
782-
all
783-
|> List.map (fun (name, hasArgs) ->
784-
let insertText =
785-
if hasArgs then Printf.sprintf "throw(%s($0))" name
786-
else Printf.sprintf "throw(%s)" name
787-
in
788-
let isBuiltin = List.mem (name, hasArgs) predefined_exceptions in
789-
let detail =
790-
if isBuiltin then "Built-in Exception" else "User-defined Exception"
791-
in
792-
Completion.create
793-
(Printf.sprintf "throw(%s)" name)
794-
~env ~kind:(Completion.Value exn_typ) ~includesSnippets:true
795-
~insertText ~filterText:"throw" ~detail)
770+
let completions_from_cmt =
771+
names_from_cmt
772+
|> List.map (fun (name, hasArgs) ->
773+
let insertText =
774+
if hasArgs then Printf.sprintf "throw(%s($0))" name
775+
else Printf.sprintf "throw(%s)" name
776+
in
777+
Completion.create
778+
(Printf.sprintf "throw(%s)" name)
779+
~env ~kind:(Completion.Value exn_typ) ~includesSnippets:true
780+
~insertText ~filterText:"throw")
781+
in
782+
Completion.create "JsError.throwWithMessage" ~env
783+
~kind:(Completion.Value exn_typ) ~includesSnippets:true
784+
~detail:"Throw a JavaScript error, example: `throw new Error(str)`"
785+
~insertText:"JsError.throwWithMessage(\"$0\")"
786+
:: Completion.create "JsExn.throw" ~env ~kind:(Completion.Value exn_typ)
787+
~includesSnippets:true ~insertText:"JsExn.throw($0)"
788+
~detail:"Throw any JavaScript value, example: `throw 100`"
789+
:: completions_from_cmt
796790

797791
(** Completions intended for piping, from a completion path. *)
798792
let completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom ~opens ~pos

tests/analysis_tests/tests/src/expected/Throw.res.txt

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,38 @@ Resolved opens 1 Stdlib
88
ContextPath Value[thro]
99
Path thro
1010
[{
11-
"label": "throw(MyCustomThingToThrow)",
12-
"kind": 12,
13-
"tags": [],
14-
"detail": "User-defined Exception",
15-
"documentation": null,
16-
"filterText": "throw",
17-
"insertText": "throw(MyCustomThingToThrow($0))",
18-
"insertTextFormat": 2
19-
}, {
20-
"label": "throw(NoArgsToThrow)",
21-
"kind": 12,
22-
"tags": [],
23-
"detail": "User-defined Exception",
24-
"documentation": null,
25-
"filterText": "throw",
26-
"insertText": "throw(NoArgsToThrow)",
27-
"insertTextFormat": 2
28-
}, {
29-
"label": "throw(Not_found)",
30-
"kind": 12,
31-
"tags": [],
32-
"detail": "Built-in Exception",
33-
"documentation": null,
34-
"filterText": "throw",
35-
"insertText": "throw(Not_found($0))",
36-
"insertTextFormat": 2
37-
}, {
38-
"label": "throw(Invalid_argument)",
11+
"label": "JsError.throwWithMessage",
3912
"kind": 12,
4013
"tags": [],
41-
"detail": "Built-in Exception",
14+
"detail": "Throw a JavaScript error, example: `throw new Error(str)`",
4215
"documentation": null,
43-
"filterText": "throw",
44-
"insertText": "throw(Invalid_argument($0))",
16+
"insertText": "JsError.throwWithMessage(\"$0\")",
4517
"insertTextFormat": 2
4618
}, {
47-
"label": "throw(Assert_failure)",
19+
"label": "JsExn.throw",
4820
"kind": 12,
4921
"tags": [],
50-
"detail": "Built-in Exception",
22+
"detail": "Throw any JavaScript value, example: throw 100",
5123
"documentation": null,
52-
"filterText": "throw",
53-
"insertText": "throw(Assert_failure($0))",
24+
"insertText": "JsExn.throw($0)",
5425
"insertTextFormat": 2
5526
}, {
56-
"label": "throw(Failure)",
57-
"kind": 12,
58-
"tags": [],
59-
"detail": "Built-in Exception",
60-
"documentation": null,
61-
"filterText": "throw",
62-
"insertText": "throw(Failure($0))",
63-
"insertTextFormat": 2
64-
}, {
65-
"label": "throw(Match_failure)",
27+
"label": "throw(MyCustomThingToThrow)",
6628
"kind": 12,
6729
"tags": [],
68-
"detail": "Built-in Exception",
30+
"detail": "exn",
6931
"documentation": null,
7032
"filterText": "throw",
71-
"insertText": "throw(Match_failure($0))",
33+
"insertText": "throw(MyCustomThingToThrow($0))",
7234
"insertTextFormat": 2
7335
}, {
74-
"label": "throw(Division_by_zero)",
36+
"label": "throw(NoArgsToThrow)",
7537
"kind": 12,
7638
"tags": [],
77-
"detail": "Built-in Exception",
39+
"detail": "exn",
7840
"documentation": null,
7941
"filterText": "throw",
80-
"insertText": "throw(Division_by_zero)",
42+
"insertText": "throw(NoArgsToThrow)",
8143
"insertTextFormat": 2
8244
}]
8345

0 commit comments

Comments
 (0)