From 54fb19964c2bffb16fd65ebd058ca2a53c84abfa Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 24 May 2024 15:05:11 +0200 Subject: [PATCH 1/2] emit %todo istead of failwith when appropriate --- Makefile | 5 +-- analysis/src/Commands.ml | 11 +++++++ analysis/src/CompletionBackEnd.ml | 20 ++++++++++- analysis/src/CompletionFrontEnd.ml | 1 + analysis/src/Packages.ml | 33 +++++++++++-------- analysis/src/SharedTypes.ml | 2 ++ analysis/tests/src/CompletionAttributes.res | 3 ++ analysis/tests/src/ExhaustiveSwitch.res | 5 +++ .../src/expected/CompletionAttributes.res.txt | 22 +++++++++++++ .../src/expected/ExhaustiveSwitch.res.txt | 26 +++++++++++++++ 10 files changed, 111 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index e693761a0..a81885099 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index c861c5b01..ab668b2a6 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -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 ^ ":" diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index 5b7a96779..60199b716 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -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 @@ -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 [] diff --git a/analysis/src/CompletionFrontEnd.ml b/analysis/src/CompletionFrontEnd.ml index 29d09cc47..24879da80 100644 --- a/analysis/src/CompletionFrontEnd.ml +++ b/analysis/src/CompletionFrontEnd.ml @@ -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 diff --git a/analysis/src/Packages.ml b/analysis/src/Packages.ml index 20818f264..c3fd7d691 100644 --- a/analysis/src/Packages.ml +++ b/analysis/src/Packages.ml @@ -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 diff --git a/analysis/src/SharedTypes.ml b/analysis/src/SharedTypes.ml index 11578527f..d8af43484 100644 --- a/analysis/src/SharedTypes.ml +++ b/analysis/src/SharedTypes.ml @@ -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 *) @@ -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)" diff --git a/analysis/tests/src/CompletionAttributes.res b/analysis/tests/src/CompletionAttributes.res index 7c8809bb5..674580a9e 100644 --- a/analysis/tests/src/CompletionAttributes.res +++ b/analysis/tests/src/CompletionAttributes.res @@ -34,3 +34,6 @@ // @module({from: }) external doStuff: t = "default" // ^com +// let dd = %t +// ^com + diff --git a/analysis/tests/src/ExhaustiveSwitch.res b/analysis/tests/src/ExhaustiveSwitch.res index dd83f1ad5..82fca239f 100644 --- a/analysis/tests/src/ExhaustiveSwitch.res +++ b/analysis/tests/src/ExhaustiveSwitch.res @@ -36,3 +36,8 @@ let vvv = Some(x->getV) // vvv // ^xfm + +// ^ve+ 11.1 +// switch withSomeVarian +// ^com +// ^ve- diff --git a/analysis/tests/src/expected/CompletionAttributes.res.txt b/analysis/tests/src/expected/CompletionAttributes.res.txt index 989a4c77d..3fa299ef7 100644 --- a/analysis/tests/src/expected/CompletionAttributes.res.txt +++ b/analysis/tests/src/expected/CompletionAttributes.res.txt @@ -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 + }] + diff --git a/analysis/tests/src/expected/ExhaustiveSwitch.res.txt b/analysis/tests/src/expected/ExhaustiveSwitch.res.txt index 6aba8c923..627db72ac 100644 --- a/analysis/tests/src/expected/ExhaustiveSwitch.res.txt +++ b/analysis/tests/src/expected/ExhaustiveSwitch.res.txt @@ -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)\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 + }] + + From ae14b8a63b0998902a33d0f4d2a780636e233377 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 24 May 2024 15:07:42 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 740b58619..29be466e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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