Skip to content

Commit e0f4648

Browse files
authored
Merge pull request ocaml#1333 from voodoos/construct-part-I.I-module-holes
Add Module holes to the parser and AST
2 parents ae21420 + f8bfb48 commit e0f4648

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4777
-4400
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ git version
88
ocaml/ocaml-lsp#375)
99
- fix location of module definitions done via functors (#1329, fixes #1199)
1010
- fix -cmt-path dirs mistakenly added to build path (#1330)
11+
- add new module holes that can replace module expressions (#1333)
1112

1213
merlin 4.2
1314
==========

src/analysis/type_enclosing.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ let from_nodes ~path =
2424
ret (Type (env, t))
2525
| Type_declaration { typ_id = id; typ_type = t} ->
2626
ret (Type_decl (env, id, t))
27+
| Module_expr {mod_type = Types.Mty_for_hole} -> None
2728
| Module_expr {mod_type = m}
2829
| Module_type {mty_type = m}
2930
| Module_binding {mb_expr = {mod_type = m}}

src/analysis/typedtrie.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ let remove_indir_me me =
214214
| Typedtree.Tmod_apply (me1, me2, _) -> `Apply (me1, me2)
215215
| Typedtree.Tmod_constraint (me, _, _, _) -> `Mod_expr me
216216
| Typedtree.Tmod_unpack _ -> `Unpack
217+
| Typedtree.Tmod_hole -> `Hole
217218

218219
let remove_indir_mty mty =
219220
match mty.Typedtree.mty_desc with
@@ -287,10 +288,13 @@ let rec build ~local_buffer ~trie browses : t =
287288
Apply { funct; arg }
288289
| `Unpack -> (* TODO! *)
289290
Leaf
291+
| `Hole ->
292+
Leaf
290293
and functor_ : _ -> Trie.functor_ = function
291294
| `Alias path
292295
| `Ident path -> Named (Namespaced_path.of_path ~namespace:`Mod path)
293296
| `Str _
297+
| `Hole
294298
| `Sg _ -> assert false
295299
| `Mod_expr me -> functor_ (remove_indir_me me)
296300
| `Mod_type _ -> assert false
@@ -380,6 +384,7 @@ let rec build ~local_buffer ~trie browses : t =
380384
| `Sg sg ->
381385
let sg = lazy (build ~local_buffer ~trie [of_signature sg]) in
382386
f (Included (Items sg))
387+
| `Hole -> f Leaf
383388
in
384389
helper packed
385390
end

src/frontend/query_commands.ml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,14 +622,25 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
622622
let verbosity = verbosity pipeline in
623623
let nodes = Mbrowse.of_typedtree (Mtyper.get_typedtree typer) in
624624
let ppf = Format.str_formatter in
625+
let print ~nodes loc env type_ () =
626+
match type_ with
627+
| `Exp type_expr ->
628+
Type_utils.print_type_with_decl ~verbosity env ppf type_expr
629+
| `Mod module_type ->
630+
(* For module_expr holes we need the type of the next enclosing
631+
to get a useful result *)
632+
match Mbrowse.enclosing (loc.Location.loc_start) [nodes] with
633+
| _ :: (_, Browse_raw.Module_expr { mod_type; _}) :: _ ->
634+
Printtyp.modtype env ppf mod_type
635+
| _ ->
636+
Printtyp.modtype env ppf module_type
637+
in
625638
let loc_and_types_of_holes node =
626-
List.map (Browse_raw.all_holes node)
627-
~f:(fun (loc, env, type_expr) ->
639+
List.map (Browse_raw.all_holes node) ~f:(
640+
fun (loc, env, type_) ->
628641
Printtyp.wrap_printing_env env ~verbosity
629-
(fun () ->
630-
Type_utils.print_type_with_decl ~verbosity env ppf type_expr);
631-
(loc, Format.flush_str_formatter ())
632-
)
642+
(print ~nodes loc env type_);
643+
(loc, Format.flush_str_formatter ()))
633644
in
634645
List.concat_map ~f:loc_and_types_of_holes nodes
635646

src/ocaml/merlin_specific/browse_raw.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ and of_module_expr_desc = function
428428
app (Module_type_constraint mtc)
429429
| Tmod_unpack (e,_) ->
430430
of_expression e
431+
| Tmod_hole -> id_fold
431432

432433
and of_structure_item_desc = function
433434
| Tstr_eval (e,_) ->
@@ -903,7 +904,14 @@ let all_holes (env, node) =
903904
exp_type;
904905
exp_env;
905906
_
906-
} -> (exp_loc, exp_env, exp_type) :: acc
907+
} -> (exp_loc, exp_env, `Exp exp_type) :: acc
908+
| Module_expr {
909+
mod_desc = Tmod_hole;
910+
mod_loc;
911+
mod_type;
912+
mod_env;
913+
_
914+
} -> (mod_loc, mod_env, `Mod mod_type) :: acc
907915
| _ -> aux acc (env, node)
908916
in
909917
fold_node f env node acc

src/ocaml/merlin_specific/browse_raw.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,8 @@ val node_is_constructor : node ->
116116

117117
val node_of_binary_part : Env.t -> Cmt_format.binary_part -> node
118118

119-
val all_holes : Env.t * node -> (Location.t * Env.t * Types.type_expr) list
119+
val all_holes :
120+
Env.t * node ->
121+
(Location.t *
122+
Env.t *
123+
[`Exp of Types.type_expr | `Mod of Types.module_type]) list

src/ocaml/merlin_specific/typer_raw.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ module Rewrite_loc = struct
529529
Pmod_constraint (u_module_expr me, u_module_type mt)
530530
| Pmod_unpack e -> Pmod_unpack (u_expression e)
531531
| Pmod_extension ext -> Pmod_extension (u_extension ext)
532+
| Pmod_hole -> Pmod_hole
532533

533534
and u_structure l = List.map ~f:u_structure_item l
534535

src/ocaml/parsing/ast_helper.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ let mk ?(loc = !default_loc) ?(attrs = []) d =
266266
let constraint_ ?loc ?attrs m mty = mk ?loc ?attrs (Pmod_constraint (m, mty))
267267
let unpack ?loc ?attrs e = mk ?loc ?attrs (Pmod_unpack e)
268268
let extension ?loc ?attrs a = mk ?loc ?attrs (Pmod_extension a)
269+
let hole ?loc ?attrs () = mk ?loc ?attrs Pmod_hole
269270
end
270271

271272
module Sig = struct

src/ocaml/parsing/ast_helper.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ module Mod:
278278
module_expr
279279
val unpack: ?loc:loc -> ?attrs:attrs -> expression -> module_expr
280280
val extension: ?loc:loc -> ?attrs:attrs -> extension -> module_expr
281+
val hole: ?loc:loc -> ?attrs:attrs -> unit -> module_expr
281282
end
282283

283284
(** Signature items *)

src/ocaml/parsing/ast_iterator.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ module M = struct
312312
sub.module_expr sub m; sub.module_type sub mty
313313
| Pmod_unpack e -> sub.expr sub e
314314
| Pmod_extension x -> sub.extension sub x
315+
| Pmod_hole -> ()
315316

316317
let iter_structure_item sub {pstr_loc = loc; pstr_desc = desc} =
317318
sub.location sub loc;

0 commit comments

Comments
 (0)