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

Add reason language to ast_editor #1685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -2,6 +2,8 @@

# Unreleased

- Add `.re` file extension support. (#1685)

## 1.26.1

- Construct: display a message when construct list is empty. (#1695)
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
Loading