Skip to content

Commit

Permalink
feat: add reason extension support to ast_editor
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobslisboa committed Dec 13, 2024
1 parent f81b647 commit 8d013db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
Expand Down
37 changes: 25 additions & 12 deletions src/ast_editor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
;;

Expand All @@ -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 ->
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -5612,11 +5613,11 @@ __metadata:

"typescript@patch:typescript@npm%3A5.7.2#optional!builtin<compat/typescript>":
version: 5.7.2
resolution: "typescript@patch:typescript@npm%3A5.7.2#optional!builtin<compat/typescript>::version=5.7.2&hash=5786d5"
resolution: "typescript@patch:typescript@npm%3A5.7.2#optional!builtin<compat/typescript>::version=5.7.2&hash=8c6c40"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 10c0/f3b8082c9d1d1629a215245c9087df56cb784f9fb6f27b5d55577a20e68afe2a889c040aacff6d27e35be165ecf9dca66e694c42eb9a50b3b2c451b36b5675cb
checksum: 10c0/c891ccf04008bc1305ba34053db951f8a4584b4a1bf2f68fd972c4a354df3dc5e62c8bfed4f6ac2d12e5b3b1c49af312c83a651048f818cd5b4949d17baacd79
languageName: node
linkType: hard

Expand Down

0 comments on commit 8d013db

Please sign in to comment.