Skip to content

Commit bc3fa89

Browse files
feat: add reason extension support to ast_editor (#1685)
1 parent 0e15824 commit bc3fa89

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Unreleased
44

5+
- Add `.re` file extension support. (#1685)
6+
57
## 1.27.0
68

79
- Add `ocaml.type-selection` that shows the type of the expression around the

src/ast_editor.ml

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ let send_msg t value ~(webview : WebView.t) =
4545

4646
module Pp_path : sig
4747
type kind =
48-
| Structure
49-
| Signature
48+
| Structure of [ `Ocaml | `Reason ]
49+
| Signature of [ `Ocaml | `Reason ]
5050
| Unknown
5151

5252
val get_kind : document:TextDocument.t -> kind
5353
val get_pp_path : document:TextDocument.t -> string
5454
end = struct
5555
type kind =
56-
| Structure
57-
| Signature
56+
| Structure of [ `Ocaml | `Reason ]
57+
| Signature of [ `Ocaml | `Reason ]
5858
| Unknown
5959

6060
let relative_document_path ~document =
@@ -66,8 +66,10 @@ end = struct
6666
let get_kind ~document =
6767
let relative = relative_document_path ~document in
6868
match Stdlib.Filename.extension relative with
69-
| ".ml" -> Structure
70-
| ".mli" -> Signature
69+
| ".ml" -> Structure `Ocaml
70+
| ".re" -> Structure `Reason
71+
| ".mli" -> Signature `Ocaml
72+
| ".rei" -> Signature `Reason
7173
| _ -> Unknown
7274
;;
7375

@@ -80,8 +82,12 @@ end = struct
8082
let fname_opt =
8183
match get_kind ~document with
8284
| Unknown -> None
83-
| Structure -> Some (String.chop_suffix_exn ~suffix:".ml" relative ^ ".pp.ml")
84-
| Signature -> Some (String.chop_suffix_exn ~suffix:".mli" relative ^ ".pp.mli")
85+
| Structure `Ocaml ->
86+
Some (String.chop_suffix_exn ~suffix:".ml" relative ^ ".pp.ml")
87+
| Signature `Ocaml ->
88+
Some (String.chop_suffix_exn ~suffix:".mli" relative ^ ".pp.mli")
89+
| Structure `Reason -> Some (relative ^ ".pp.ml")
90+
| Signature `Reason -> Some (relative ^ ".pp.mli")
8591
in
8692
(match fname_opt with
8793
| Some fname ->
@@ -111,8 +117,8 @@ let transform_to_ast instance ~document ~webview =
111117
let origin_json =
112118
let text = TextDocument.getText document () in
113119
match Pp_path.get_kind ~document with
114-
| Structure -> make_value (Dumpast.transform text `Impl)
115-
| Signature -> make_value (Dumpast.transform text `Intf)
120+
| Structure _ -> make_value (Dumpast.transform text `Impl)
121+
| Signature _ -> make_value (Dumpast.transform text `Intf)
116122
| Unknown -> raise (User_error "Unknown file extension")
117123
in
118124
send_msg "ast" (Jsonoo.t_to_js origin_json) ~webview
@@ -321,9 +327,16 @@ let open_pp_doc instance ~document =
321327
match fetch_pp_code ~document with
322328
| Error e -> Promise.return (Error e)
323329
| Ok pp_pp_str ->
330+
let file_name =
331+
match Pp_path.get_kind ~document with
332+
| Structure `Reason ->
333+
String.chop_suffix_exn ~suffix:".re" (TextDocument.fileName document) ^ ".ml"
334+
| Signature `Reason ->
335+
String.chop_suffix_exn ~suffix:".rei" (TextDocument.fileName document) ^ ".mli"
336+
| _ -> TextDocument.fileName document
337+
in
324338
let* doc =
325-
Workspace.openTextDocument
326-
(`Uri (Uri.parse ("post-ppx: " ^ TextDocument.fileName document ^ "?") ()))
339+
Workspace.openTextDocument (`Uri (Uri.parse ("post-ppx: " ^ file_name ^ "?") ()))
327340
in
328341
Ast_editor_state.associate_origin_and_pp
329342
ast_editor_state

0 commit comments

Comments
 (0)