Skip to content

Commit 2b1e6b0

Browse files
committed
basic completion for exn
1 parent 99a19ae commit 2b1e6b0

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,21 @@ let rec completeTypedValue ~full ~prefix ~completionContext ~mode
12771277
^ if !Cfg.supportsSnippets then "{$0}" else "{}")
12781278
~sortText:"A" ~kind:(Value typ) ~env ();
12791279
]
1280-
| _ -> []
1280+
| Tfunction _ -> []
1281+
| Texn env ->
1282+
[
1283+
Completion.create
1284+
(full.package.builtInCompletionModules.exnModulePath @ ["Error(error)"]
1285+
|> ident)
1286+
~kind:(Label "Catches errors from JavaScript errors.")
1287+
~docstring:
1288+
[
1289+
"Matches on a JavaScript error. Read more in the [documentation on \
1290+
catching JS \
1291+
exceptions](https://rescript-lang.org/docs/manual/latest/exception#catching-js-exceptions).";
1292+
]
1293+
~env;
1294+
]
12811295

12821296
let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
12831297
(completable : Completable.t) =

analysis/src/Packages.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let newBsPackage ~rootPath =
111111
promiseModulePath = ["Promise"];
112112
listModulePath = ["List"];
113113
resultModulePath = ["Result"];
114+
exnModulePath = ["Exn"];
114115
}
115116
else if
116117
opens_from_bsc_flags
@@ -129,6 +130,7 @@ let newBsPackage ~rootPath =
129130
promiseModulePath = ["Js"; "Promise"];
130131
listModulePath = ["List"];
131132
resultModulePath = ["Result"];
133+
exnModulePath = ["Js"; "Exn"];
132134
}
133135
else
134136
{
@@ -140,6 +142,7 @@ let newBsPackage ~rootPath =
140142
promiseModulePath = ["Js"; "Promise"];
141143
listModulePath = ["Belt"; "List"];
142144
resultModulePath = ["Belt"; "Result"];
145+
exnModulePath = ["Js"; "Exn"];
143146
});
144147
})))
145148
| None -> None)

analysis/src/SharedTypes.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ type polyVariantConstructor = {name: string; args: Types.type_expr list}
298298
type completionType =
299299
| Tuple of QueryEnv.t * Types.type_expr list * Types.type_expr
300300
| Toption of QueryEnv.t * completionType
301+
| Texn of QueryEnv.t
301302
| Tbool of QueryEnv.t
302303
| Tarray of QueryEnv.t * completionType
303304
| Tstring of QueryEnv.t
@@ -519,6 +520,7 @@ type builtInCompletionModules = {
519520
promiseModulePath: string list;
520521
listModulePath: string list;
521522
resultModulePath: string list;
523+
exnModulePath: string list;
522524
}
523525

524526
type package = {

analysis/src/TypeUtils.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ let rec extractType ~env ~package (t : Types.type_expr) =
119119
|> Option.map (fun payloadTyp -> Tarray (env, payloadTyp))
120120
| Tconstr (Path.Pident {name = "bool"}, [], _) -> Some (Tbool env)
121121
| Tconstr (Path.Pident {name = "string"}, [], _) -> Some (Tstring env)
122+
| Tconstr (Path.Pident {name = "exn"}, [], _) -> Some (Texn env)
122123
| Tconstr (path, _, _) -> (
123124
match References.digConstructor ~env ~package path with
124125
| Some (env, {item = {decl = {type_manifest = Some t1}}}) ->
@@ -421,6 +422,7 @@ let rec extractedTypeToString ?(inner = false) = function
421422
| Trecord {definition = `NameOnly name; fields} ->
422423
if inner then name else printRecordFromFields ~name fields
423424
| TinlineRecord {fields} -> printRecordFromFields fields
425+
| Texn _ -> "exn"
424426

425427
let unwrapCompletionTypeIfOption (t : SharedTypes.completionType) =
426428
match t with

analysis/tests/src/CompletionPattern.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,8 @@ let ff: recordWithFn = {someFn: () => ()}
201201

202202
// switch ff { | {someFn: }}
203203
// ^com
204+
205+
let xn: exn = Obj.magic()
206+
207+
// switch xn { | }
208+
// ^com

analysis/tests/src/expected/CompletionPattern.res.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,19 @@ Completable: Cpattern Value[s]->tuple($1)
905905
}]
906906

907907
Complete src/CompletionPattern.res 201:25
908-
posCursor:[201:25] posNoWhite:[201:24] Found expr:[201:3->201:28]
908+
posCursor:[201:25] posNoWhite:[201:24] Found expr:[201:3->204:25]
909909
posCursor:[201:25] posNoWhite:[201:24] Found pattern:[201:17->201:28]
910910
Completable: Cpattern Value[ff]->recordField(someFn)
911911
[]
912912

913+
Complete src/CompletionPattern.res 206:16
914+
XXX Not found!
915+
Completable: Cpattern Value[xn]
916+
[{
917+
"label": "Js.Exn.Error(error)",
918+
"kind": 4,
919+
"tags": [],
920+
"detail": "Catches errors from JavaScript errors.",
921+
"documentation": {"kind": "markdown", "value": "Matches on a JavaScript error. Read more in the [documentation on catching JS exceptions](https://rescript-lang.org/docs/manual/latest/exception#catching-js-exceptions)."}
922+
}]
923+

0 commit comments

Comments
 (0)