Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit %todo istead of failwith when appropriate #981

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
- Add support for the rewatch build system for incremental compilation. https://github.com/rescript-lang/rescript-vscode/pull/965
- Add support for Linux ARM64
- Statically linked Linux binaries
- Emit `%todo` instead of `failwith("TODO")` when we can (ReScript >= v11.1). https://github.com/rescript-lang/rescript-vscode/pull/981
- Complete `%todo`. https://github.com/rescript-lang/rescript-vscode/pull/981

## 1.50.0

Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ SHELL = /bin/bash

build:
dune build
cp _build/install/default/bin/rescript-editor-analysis rescript-editor-analysis.exe
cp _build/install/default/bin/rescript-tools rescript-tools.exe
cp -f _build/install/default/bin/rescript-editor-analysis analysis/rescript-editor-analysis.exe
cp -f _build/install/default/bin/rescript-editor-analysis rescript-editor-analysis.exe
cp -f _build/install/default/bin/rescript-tools rescript-tools.exe

test:
make -C analysis test
Expand Down
11 changes: 11 additions & 0 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ let test ~path =
| "db-" -> Log.verbose := false
| "dv+" -> Debug.debugLevel := Verbose
| "dv-" -> Debug.debugLevel := Off
| "ve+" -> (
let version = String.sub rest 3 (String.length rest - 3) in
let version = String.trim version in
if Debug.verbose () then
Printf.printf "Setting version: %s\n" version;
match String.split_on_char '.' version with
| [majorRaw; minorRaw] ->
let version = (int_of_string majorRaw, int_of_string minorRaw) in
Packages.overrideRescriptVersion := Some version
| _ -> ())
| "ve-" -> Packages.overrideRescriptVersion := None
| "def" ->
print_endline
("Definition " ^ path ^ " " ^ string_of_int line ^ ":"
Expand Down
20 changes: 19 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2205,8 +2205,11 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
| _ -> items)))
| CexhaustiveSwitch {contextPath; exprLoc} ->
let range = Utils.rangeOfLoc exprLoc in
let rescriptMajor, rescriptMinor = Packages.getReScriptVersion () in
let printFailwithStr num =
"${" ^ string_of_int num ^ ":failwith(\"todo\")}"
if (rescriptMajor = 11 && rescriptMinor >= 1) || rescriptMajor >= 12 then
"${" ^ string_of_int num ^ ":%todo}"
else "${" ^ string_of_int num ^ ":failwith(\"todo\")}"
in
let withExhaustiveItem ~cases ?(startIndex = 0) (c : Completion.t) =
(* We don't need to write out `switch` here since we know that's what the
Expand Down Expand Up @@ -2285,3 +2288,18 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
| true -> Some "true"
| false -> None))
else None)
| CextensionNode prefix ->
if Utils.startsWith "todo" prefix then
let detail =
"`%todo` is used to tell the compiler that some code still needs to be \
implemented."
in
[
Completion.create "todo" ~kind:(Label "todo") ~detail ~env
~insertText:"todo";
Completion.create "todo (with payload)" ~includesSnippets:true
~kind:(Label "todo")
~detail:(detail ^ " With a payload.")
~env ~insertText:"todo(\"${0:TODO}\")";
]
else []
1 change: 1 addition & 0 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
if expr.pexp_loc |> Loc.hasPos ~pos:posNoWhite && !result = None then (
setFound ();
match expr.pexp_desc with
| Pexp_extension ({txt}, _) -> setResult (CextensionNode txt)
| Pexp_constant _ -> setResult Cnone
| Pexp_ident lid ->
let lidPath = flattenLidCheckDot lid in
Expand Down
33 changes: 19 additions & 14 deletions analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ let makePathsForModule ~projectFilesAndPaths ~dependenciesFilesAndPaths =
Hashtbl.replace pathsForModule modName paths);
pathsForModule

let overrideRescriptVersion = ref None

let getReScriptVersion () =
(* TODO: Include patch stuff when needed *)
let defaultVersion = (11, 0) in
try
let value = Sys.getenv "RESCRIPT_VERSION" in
let version =
match value |> String.split_on_char '.' with
| major :: minor :: _rest -> (
match (int_of_string_opt major, int_of_string_opt minor) with
| Some major, Some minor -> (major, minor)
| _ -> defaultVersion)
| _ -> defaultVersion
in
version
with Not_found -> defaultVersion
match !overrideRescriptVersion with
| Some overrideRescriptVersion -> overrideRescriptVersion
| None -> (
(* TODO: Include patch stuff when needed *)
let defaultVersion = (11, 0) in
try
let value = Sys.getenv "RESCRIPT_VERSION" in
let version =
match value |> String.split_on_char '.' with
| major :: minor :: _rest -> (
match (int_of_string_opt major, int_of_string_opt minor) with
| Some major, Some minor -> (major, minor)
| _ -> defaultVersion)
| _ -> defaultVersion
in
version
with Not_found -> defaultVersion)

let newBsPackage ~rootPath =
let rescriptJson = Filename.concat rootPath "rescript.json" in
Expand Down
2 changes: 2 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ module Completable = struct
type t =
| Cdecorator of string (** e.g. @module *)
| CdecoratorPayload of decoratorPayload
| CextensionNode of string (** e.g. %todo *)
| CnamedArg of contextPath * string * string list
(** e.g. (..., "label", ["l1", "l2"]) for ...(...~l1...~l2...~label...) *)
| Cnone (** e.g. don't complete inside strings *)
Expand Down Expand Up @@ -723,6 +724,7 @@ module Completable = struct
let toString = function
| Cpath cp -> "Cpath " ^ contextPathToString cp
| Cdecorator s -> "Cdecorator(" ^ str s ^ ")"
| CextensionNode s -> "CextensionNode(" ^ str s ^ ")"
| CdecoratorPayload (Module s) -> "CdecoratorPayload(module=" ^ s ^ ")"
| CdecoratorPayload (ModuleWithImportAttributes _) ->
"CdecoratorPayload(moduleWithImportAttributes)"
Expand Down
3 changes: 3 additions & 0 deletions analysis/tests/src/CompletionAttributes.res
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
// @module({from: }) external doStuff: t = "default"
// ^com

// let dd = %t
// ^com

5 changes: 5 additions & 0 deletions analysis/tests/src/ExhaustiveSwitch.res
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ let vvv = Some(x->getV)

// vvv
// ^xfm

// ^ve+ 11.1
// switch withSomeVarian
// ^com
// ^ve-
22 changes: 22 additions & 0 deletions analysis/tests/src/expected/CompletionAttributes.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,25 @@ Resolved opens 1 pervasives
"documentation": null
}]

Complete src/CompletionAttributes.res 36:14
posCursor:[36:14] posNoWhite:[36:13] Found expr:[36:12->36:14]
Completable: CextensionNode(t)
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
[{
"label": "todo",
"kind": 4,
"tags": [],
"detail": "`%todo` is used to tell the compiler that some code still needs to be implemented.",
"documentation": null,
"insertText": "todo"
}, {
"label": "todo (with payload)",
"kind": 4,
"tags": [],
"detail": "`%todo` is used to tell the compiler that some code still needs to be implemented. With a payload.",
"documentation": null,
"insertText": "todo(\"${0:TODO}\")",
"insertTextFormat": 2
}]

26 changes: 26 additions & 0 deletions analysis/tests/src/expected/ExhaustiveSwitch.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,29 @@ newText:
| Some(Three(_)) => failwith("TODO")
}


Complete src/ExhaustiveSwitch.res 40:24
XXX Not found!
Completable: CexhaustiveSwitch Value[withSomeVarian]
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Value[withSomeVarian]
Path withSomeVarian
[{
"label": "withSomeVariant",
"kind": 12,
"tags": [],
"detail": "someVariant",
"documentation": {"kind": "markdown", "value": "```rescript\ntype someVariant = One | Two | Three(option<bool>)\n```"}
}, {
"label": "withSomeVariant (exhaustive switch)",
"kind": 15,
"tags": [],
"detail": "insert exhaustive switch for value",
"documentation": null,
"filterText": "withSomeVariant",
"insertText": "withSomeVariant {\n | One => ${1:%todo}\n | Two => ${2:%todo}\n | Three(_) => ${3:%todo}\n }",
"insertTextFormat": 2
}]


Loading