Skip to content

Commit e3ee758

Browse files
authored
Emit %todo istead of failwith when appropriate (#981)
* emit %todo istead of failwith when appropriate * changelog
1 parent fe858f6 commit e3ee758

11 files changed

+113
-17
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
- Add support for the rewatch build system for incremental compilation. https://github.com/rescript-lang/rescript-vscode/pull/965
2828
- Add support for Linux ARM64
2929
- Statically linked Linux binaries
30+
- Emit `%todo` instead of `failwith("TODO")` when we can (ReScript >= v11.1). https://github.com/rescript-lang/rescript-vscode/pull/981
31+
- Complete `%todo`. https://github.com/rescript-lang/rescript-vscode/pull/981
3032

3133
## 1.50.0
3234

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ SHELL = /bin/bash
22

33
build:
44
dune build
5-
cp _build/install/default/bin/rescript-editor-analysis rescript-editor-analysis.exe
6-
cp _build/install/default/bin/rescript-tools rescript-tools.exe
5+
cp -f _build/install/default/bin/rescript-editor-analysis analysis/rescript-editor-analysis.exe
6+
cp -f _build/install/default/bin/rescript-editor-analysis rescript-editor-analysis.exe
7+
cp -f _build/install/default/bin/rescript-tools rescript-tools.exe
78

89
test:
910
make -C analysis test

analysis/src/Commands.ml

+11
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ let test ~path =
338338
| "db-" -> Log.verbose := false
339339
| "dv+" -> Debug.debugLevel := Verbose
340340
| "dv-" -> Debug.debugLevel := Off
341+
| "ve+" -> (
342+
let version = String.sub rest 3 (String.length rest - 3) in
343+
let version = String.trim version in
344+
if Debug.verbose () then
345+
Printf.printf "Setting version: %s\n" version;
346+
match String.split_on_char '.' version with
347+
| [majorRaw; minorRaw] ->
348+
let version = (int_of_string majorRaw, int_of_string minorRaw) in
349+
Packages.overrideRescriptVersion := Some version
350+
| _ -> ())
351+
| "ve-" -> Packages.overrideRescriptVersion := None
341352
| "def" ->
342353
print_endline
343354
("Definition " ^ path ^ " " ^ string_of_int line ^ ":"

analysis/src/CompletionBackEnd.ml

+19-1
Original file line numberDiff line numberDiff line change
@@ -2205,8 +2205,11 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
22052205
| _ -> items)))
22062206
| CexhaustiveSwitch {contextPath; exprLoc} ->
22072207
let range = Utils.rangeOfLoc exprLoc in
2208+
let rescriptMajor, rescriptMinor = Packages.getReScriptVersion () in
22082209
let printFailwithStr num =
2209-
"${" ^ string_of_int num ^ ":failwith(\"todo\")}"
2210+
if (rescriptMajor = 11 && rescriptMinor >= 1) || rescriptMajor >= 12 then
2211+
"${" ^ string_of_int num ^ ":%todo}"
2212+
else "${" ^ string_of_int num ^ ":failwith(\"todo\")}"
22102213
in
22112214
let withExhaustiveItem ~cases ?(startIndex = 0) (c : Completion.t) =
22122215
(* We don't need to write out `switch` here since we know that's what the
@@ -2285,3 +2288,18 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
22852288
| true -> Some "true"
22862289
| false -> None))
22872290
else None)
2291+
| CextensionNode prefix ->
2292+
if Utils.startsWith "todo" prefix then
2293+
let detail =
2294+
"`%todo` is used to tell the compiler that some code still needs to be \
2295+
implemented."
2296+
in
2297+
[
2298+
Completion.create "todo" ~kind:(Label "todo") ~detail ~env
2299+
~insertText:"todo";
2300+
Completion.create "todo (with payload)" ~includesSnippets:true
2301+
~kind:(Label "todo")
2302+
~detail:(detail ^ " With a payload.")
2303+
~env ~insertText:"todo(\"${0:TODO}\")";
2304+
]
2305+
else []

analysis/src/CompletionFrontEnd.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
10331033
if expr.pexp_loc |> Loc.hasPos ~pos:posNoWhite && !result = None then (
10341034
setFound ();
10351035
match expr.pexp_desc with
1036+
| Pexp_extension ({txt}, _) -> setResult (CextensionNode txt)
10361037
| Pexp_constant _ -> setResult Cnone
10371038
| Pexp_ident lid ->
10381039
let lidPath = flattenLidCheckDot lid in

analysis/src/Packages.ml

+19-14
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@ let makePathsForModule ~projectFilesAndPaths ~dependenciesFilesAndPaths =
1111
Hashtbl.replace pathsForModule modName paths);
1212
pathsForModule
1313

14+
let overrideRescriptVersion = ref None
15+
1416
let getReScriptVersion () =
15-
(* TODO: Include patch stuff when needed *)
16-
let defaultVersion = (11, 0) in
17-
try
18-
let value = Sys.getenv "RESCRIPT_VERSION" in
19-
let version =
20-
match value |> String.split_on_char '.' with
21-
| major :: minor :: _rest -> (
22-
match (int_of_string_opt major, int_of_string_opt minor) with
23-
| Some major, Some minor -> (major, minor)
24-
| _ -> defaultVersion)
25-
| _ -> defaultVersion
26-
in
27-
version
28-
with Not_found -> defaultVersion
17+
match !overrideRescriptVersion with
18+
| Some overrideRescriptVersion -> overrideRescriptVersion
19+
| None -> (
20+
(* TODO: Include patch stuff when needed *)
21+
let defaultVersion = (11, 0) in
22+
try
23+
let value = Sys.getenv "RESCRIPT_VERSION" in
24+
let version =
25+
match value |> String.split_on_char '.' with
26+
| major :: minor :: _rest -> (
27+
match (int_of_string_opt major, int_of_string_opt minor) with
28+
| Some major, Some minor -> (major, minor)
29+
| _ -> defaultVersion)
30+
| _ -> defaultVersion
31+
in
32+
version
33+
with Not_found -> defaultVersion)
2934

3035
let newBsPackage ~rootPath =
3136
let rescriptJson = Filename.concat rootPath "rescript.json" in

analysis/src/SharedTypes.ml

+2
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ module Completable = struct
642642
type t =
643643
| Cdecorator of string (** e.g. @module *)
644644
| CdecoratorPayload of decoratorPayload
645+
| CextensionNode of string (** e.g. %todo *)
645646
| CnamedArg of contextPath * string * string list
646647
(** e.g. (..., "label", ["l1", "l2"]) for ...(...~l1...~l2...~label...) *)
647648
| Cnone (** e.g. don't complete inside strings *)
@@ -723,6 +724,7 @@ module Completable = struct
723724
let toString = function
724725
| Cpath cp -> "Cpath " ^ contextPathToString cp
725726
| Cdecorator s -> "Cdecorator(" ^ str s ^ ")"
727+
| CextensionNode s -> "CextensionNode(" ^ str s ^ ")"
726728
| CdecoratorPayload (Module s) -> "CdecoratorPayload(module=" ^ s ^ ")"
727729
| CdecoratorPayload (ModuleWithImportAttributes _) ->
728730
"CdecoratorPayload(moduleWithImportAttributes)"

analysis/tests/src/CompletionAttributes.res

+3
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434
// @module({from: }) external doStuff: t = "default"
3535
// ^com
3636

37+
// let dd = %t
38+
// ^com
39+

analysis/tests/src/ExhaustiveSwitch.res

+5
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ let vvv = Some(x->getV)
3636

3737
// vvv
3838
// ^xfm
39+
40+
// ^ve+ 11.1
41+
// switch withSomeVarian
42+
// ^com
43+
// ^ve-

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

+22
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,25 @@ Resolved opens 1 pervasives
266266
"documentation": null
267267
}]
268268

269+
Complete src/CompletionAttributes.res 36:14
270+
posCursor:[36:14] posNoWhite:[36:13] Found expr:[36:12->36:14]
271+
Completable: CextensionNode(t)
272+
Package opens Pervasives.JsxModules.place holder
273+
Resolved opens 1 pervasives
274+
[{
275+
"label": "todo",
276+
"kind": 4,
277+
"tags": [],
278+
"detail": "`%todo` is used to tell the compiler that some code still needs to be implemented.",
279+
"documentation": null,
280+
"insertText": "todo"
281+
}, {
282+
"label": "todo (with payload)",
283+
"kind": 4,
284+
"tags": [],
285+
"detail": "`%todo` is used to tell the compiler that some code still needs to be implemented. With a payload.",
286+
"documentation": null,
287+
"insertText": "todo(\"${0:TODO}\")",
288+
"insertTextFormat": 2
289+
}]
290+

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

+26
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,29 @@ newText:
149149
| Some(Three(_)) => failwith("TODO")
150150
}
151151

152+
153+
Complete src/ExhaustiveSwitch.res 40:24
154+
XXX Not found!
155+
Completable: CexhaustiveSwitch Value[withSomeVarian]
156+
Package opens Pervasives.JsxModules.place holder
157+
Resolved opens 1 pervasives
158+
ContextPath Value[withSomeVarian]
159+
Path withSomeVarian
160+
[{
161+
"label": "withSomeVariant",
162+
"kind": 12,
163+
"tags": [],
164+
"detail": "someVariant",
165+
"documentation": {"kind": "markdown", "value": "```rescript\ntype someVariant = One | Two | Three(option<bool>)\n```"}
166+
}, {
167+
"label": "withSomeVariant (exhaustive switch)",
168+
"kind": 15,
169+
"tags": [],
170+
"detail": "insert exhaustive switch for value",
171+
"documentation": null,
172+
"filterText": "withSomeVariant",
173+
"insertText": "withSomeVariant {\n | One => ${1:%todo}\n | Two => ${2:%todo}\n | Three(_) => ${3:%todo}\n }",
174+
"insertTextFormat": 2
175+
}]
176+
177+

0 commit comments

Comments
 (0)