From de7ad0dcadca077fe04e3243b4cf859dda54763b Mon Sep 17 00:00:00 2001 From: Pedro Lisboa Date: Sat, 14 Dec 2024 17:32:02 -0300 Subject: [PATCH] feat: add reason extension support to ast_editor --- CHANGELOG.md | 2 ++ package.json | 1 + src/ast_editor.ml | 37 +++++++++++++++++++++++++------------ yarn.lock | 5 +++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33c2c076d..46b337df8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ # Unreleased +- Add `.re` file extension support. (#1685) + ## 1.25.0 - Add `ocaml.search-by-type` to search for values using their type signature (#1626) diff --git a/package.json b/package.json index 16f9edef6..b168cc8e9 100644 --- a/package.json +++ b/package.json @@ -1269,6 +1269,7 @@ "@vscode/vsce": "3.2.1", "esbuild": "0.24.0", "ovsx": "0.10.1", + "parcel": "2.13.2", "typescript": "5.7.2" }, "packageManager": "yarn@4.5.3", diff --git a/src/ast_editor.ml b/src/ast_editor.ml index 0174ced15..9c05337ca 100644 --- a/src/ast_editor.ml +++ b/src/ast_editor.ml @@ -45,16 +45,16 @@ let send_msg t value ~(webview : WebView.t) = module Pp_path : sig type kind = - | Structure - | Signature + | Structure of [ `Ocaml | `Reason ] + | Signature of [ `Ocaml | `Reason ] | Unknown val get_kind : document:TextDocument.t -> kind val get_pp_path : document:TextDocument.t -> string end = struct type kind = - | Structure - | Signature + | Structure of [ `Ocaml | `Reason ] + | Signature of [ `Ocaml | `Reason ] | Unknown let relative_document_path ~document = @@ -66,8 +66,10 @@ end = struct let get_kind ~document = let relative = relative_document_path ~document in match Stdlib.Filename.extension relative with - | ".ml" -> Structure - | ".mli" -> Signature + | ".ml" -> Structure `Ocaml + | ".re" -> Structure `Reason + | ".mli" -> Signature `Ocaml + | ".rei" -> Signature `Reason | _ -> Unknown ;; @@ -80,8 +82,12 @@ end = struct let fname_opt = match get_kind ~document with | Unknown -> None - | Structure -> Some (String.chop_suffix_exn ~suffix:".ml" relative ^ ".pp.ml") - | Signature -> Some (String.chop_suffix_exn ~suffix:".mli" relative ^ ".pp.mli") + | Structure `Ocaml -> + Some (String.chop_suffix_exn ~suffix:".ml" relative ^ ".pp.ml") + | Signature `Ocaml -> + Some (String.chop_suffix_exn ~suffix:".mli" relative ^ ".pp.mli") + | Structure `Reason -> Some (relative ^ ".pp.ml") + | Signature `Reason -> Some (relative ^ ".pp.mli") in (match fname_opt with | Some fname -> @@ -111,8 +117,8 @@ let transform_to_ast instance ~document ~webview = let origin_json = let text = TextDocument.getText document () in match Pp_path.get_kind ~document with - | Structure -> make_value (Dumpast.transform text `Impl) - | Signature -> make_value (Dumpast.transform text `Intf) + | Structure _ -> make_value (Dumpast.transform text `Impl) + | Signature _ -> make_value (Dumpast.transform text `Intf) | Unknown -> raise (User_error "Unknown file extension") in send_msg "ast" (Jsonoo.t_to_js origin_json) ~webview @@ -321,9 +327,16 @@ let open_pp_doc instance ~document = match fetch_pp_code ~document with | Error e -> Promise.return (Error e) | Ok pp_pp_str -> + let file_name = + match Pp_path.get_kind ~document with + | Structure `Reason -> + String.chop_suffix_exn ~suffix:".re" (TextDocument.fileName document) ^ ".ml" + | Signature `Reason -> + String.chop_suffix_exn ~suffix:".rei" (TextDocument.fileName document) ^ ".mli" + | _ -> TextDocument.fileName document + in let* doc = - Workspace.openTextDocument - (`Uri (Uri.parse ("post-ppx: " ^ TextDocument.fileName document ^ "?") ())) + Workspace.openTextDocument (`Uri (Uri.parse ("post-ppx: " ^ file_name ^ "?") ())) in Ast_editor_state.associate_origin_and_pp ast_editor_state diff --git a/yarn.lock b/yarn.lock index 68f0ba137..f4a804f95 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4495,6 +4495,7 @@ __metadata: "@vscode/vsce": "npm:3.2.1" esbuild: "npm:0.24.0" ovsx: "npm:0.10.1" + parcel: "npm:2.13.2" polka: "npm:1.0.0-next.28" sirv: "npm:3.0.0" typescript: "npm:5.7.2" @@ -5612,11 +5613,11 @@ __metadata: "typescript@patch:typescript@npm%3A5.7.2#optional!builtin": version: 5.7.2 - resolution: "typescript@patch:typescript@npm%3A5.7.2#optional!builtin::version=5.7.2&hash=5786d5" + resolution: "typescript@patch:typescript@npm%3A5.7.2#optional!builtin::version=5.7.2&hash=8c6c40" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 10c0/f3b8082c9d1d1629a215245c9087df56cb784f9fb6f27b5d55577a20e68afe2a889c040aacff6d27e35be165ecf9dca66e694c42eb9a50b3b2c451b36b5675cb + checksum: 10c0/c891ccf04008bc1305ba34053db951f8a4584b4a1bf2f68fd972c4a354df3dc5e62c8bfed4f6ac2d12e5b3b1c49af312c83a651048f818cd5b4949d17baacd79 languageName: node linkType: hard