Skip to content

Commit 8f0b2f9

Browse files
committed
Add option to not print types when getting the outline
1 parent c1cb3d6 commit 8f0b2f9

File tree

8 files changed

+62
-33
lines changed

8 files changed

+62
-33
lines changed

src/analysis/outline.ml

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ let get_class_field_desc_infos = function
5353
| Typedtree.Tcf_method (str_loc, _, _) -> Some (str_loc, `Method)
5454
| _ -> None
5555

56-
let outline_type ~env typ =
57-
let ppf, to_string = Format.to_string () in
58-
Printtyp.wrap_printing_env env (fun () ->
59-
Type_utils.print_type_with_decl ~verbosity:(Mconfig.Verbosity.Lvl 0) env
60-
ppf typ);
61-
Some (to_string ())
62-
63-
let rec summarize node =
56+
let outline_type ~include_types ~env typ =
57+
match include_types with
58+
| true ->
59+
let ppf, to_string = Format.to_string () in
60+
Printtyp.wrap_printing_env env (fun () ->
61+
Type_utils.print_type_with_decl ~verbosity:(Mconfig.Verbosity.Lvl 0) env
62+
ppf typ);
63+
Some (to_string ())
64+
| false -> None
65+
66+
let rec summarize ~include_types node =
6467
let location = node.t_loc in
6568
match node.t_node with
6669
| Value_binding vb ->
@@ -69,15 +72,15 @@ let rec summarize node =
6972
match id_of_patt vb.vb_pat with
7073
| None -> None
7174
| Some ident ->
72-
let typ = outline_type ~env:node.t_env vb.vb_pat.pat_type in
75+
let typ = outline_type ~include_types ~env:node.t_env vb.vb_pat.pat_type in
7376
Some (mk ~location ~deprecated `Value typ ident)
7477
end
7578
| Value_description vd ->
7679
let deprecated = Type_utils.is_deprecated vd.val_attributes in
77-
let typ = outline_type ~env:node.t_env vd.val_val.val_type in
80+
let typ = outline_type ~include_types ~env:node.t_env vd.val_val.val_type in
7881
Some (mk ~location ~deprecated `Value typ vd.val_id)
7982
| Module_declaration md ->
80-
let children = get_mod_children node in
83+
let children = get_mod_children ~include_types node in
8184
begin
8285
match md.md_id with
8386
| None -> None
@@ -86,7 +89,7 @@ let rec summarize node =
8689
Some (mk ~children ~location ~deprecated `Module None id)
8790
end
8891
| Module_binding mb ->
89-
let children = get_mod_children node in
92+
let children = get_mod_children ~include_types node in
9093
begin
9194
match mb.mb_id with
9295
| None -> None
@@ -95,7 +98,7 @@ let rec summarize node =
9598
Some (mk ~children ~location ~deprecated `Module None id)
9699
end
97100
| Module_type_declaration mtd ->
98-
let children = get_mod_children node in
101+
let children = get_mod_children ~include_types node in
99102
let deprecated = Type_utils.is_deprecated mtd.mtd_attributes in
100103
Some (mk ~deprecated ~children ~location `Modtype None mtd.mtd_id)
101104
| Type_declaration td ->
@@ -120,7 +123,7 @@ let rec summarize node =
120123
let name = Path.name te.tyext_path in
121124
let children =
122125
List.filter_map (Lazy.force node.t_children) ~f:(fun x ->
123-
summarize x >>| fun x ->
126+
summarize ~include_types x >>| fun x ->
124127
{ x with Query_protocol.outline_kind = `Constructor })
125128
in
126129
let deprecated = Type_utils.is_deprecated te.tyext_attributes in
@@ -167,24 +170,25 @@ and get_class_elements node =
167170
| _ -> None)
168171
| _ -> []
169172

170-
and get_mod_children node =
171-
List.concat_map (Lazy.force node.t_children) ~f:remove_mod_indir
173+
and get_mod_children ~include_types node =
174+
List.concat_map (Lazy.force node.t_children) ~f:(remove_mod_indir ~include_types)
172175

173-
and remove_mod_indir node =
176+
and remove_mod_indir ~include_types node =
174177
match node.t_node with
175178
| Module_expr _ | Module_type _ ->
176-
List.concat_map (Lazy.force node.t_children) ~f:remove_mod_indir
177-
| _ -> remove_top_indir node
179+
List.concat_map (Lazy.force node.t_children) ~f:(remove_mod_indir ~include_types)
180+
| _ -> remove_top_indir ~include_types node
178181

179-
and remove_top_indir t =
182+
and remove_top_indir ~include_types t =
180183
match t.t_node with
181184
| Structure _ | Signature _ ->
182-
List.concat_map ~f:remove_top_indir (Lazy.force t.t_children)
185+
List.concat_map ~f:(remove_top_indir ~include_types) (Lazy.force t.t_children)
183186
| Signature_item _ | Structure_item _ ->
184-
List.filter_map (Lazy.force t.t_children) ~f:summarize
187+
List.filter_map (Lazy.force t.t_children) ~f:(summarize ~include_types)
185188
| _ -> []
186189

187-
let get browses = List.concat @@ List.rev_map ~f:remove_top_indir browses
190+
let get ~include_types browses =
191+
List.concat @@ List.rev_map ~f:(remove_top_indir ~include_types) browses
188192

189193
let shape cursor nodes =
190194
let rec aux node =

src/analysis/outline.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
2727
)* }}} *)
2828

29-
val get : Browse_tree.t list -> Query_protocol.outline
29+
val get : include_types:bool -> Browse_tree.t list -> Query_protocol.outline
3030
val shape : Lexing.position -> Browse_tree.t list -> Query_protocol.shape list

src/commands/new_commands.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,19 @@ let all_commands =
536536
| `Ident_at pos, scope ->
537537
run buffer (Query_protocol.Occurrences (`Ident_at pos, scope))
538538
end;
539-
command "outline" ~spec:[]
539+
command "outline"
540+
~spec:
541+
[ optional "-include-types" "Don't print"
542+
(Marg.bool (fun include_types _ -> include_types))
543+
]
540544
~doc:
541545
"Returns a tree of objects `{'start': position, 'end': position, \
542546
'name': string, 'kind': string, 'children': subnodes}` describing the \
543547
content of the buffer."
544-
~default:()
548+
~default:true
545549
begin
546-
fun buffer () -> run buffer Query_protocol.Outline
550+
fun buffer include_types ->
551+
run buffer (Query_protocol.Outline { include_types })
547552
end;
548553
command "path-of-source"
549554
~doc:

src/commands/query_json.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ let dump (type a) : a t -> json =
156156
("hint-pattern-variable", `Bool hint_pattern_var);
157157
("avoid-ghost-location", `Bool ghost)
158158
]
159-
| Outline -> mk "outline" []
159+
| Outline { include_types } ->
160+
mk "outline" [ ("include-types", `Bool include_types) ]
160161
| Errors { lexing; parsing; typing } ->
161162
let args =
162163
if lexing && parsing && typing then []
@@ -502,7 +503,7 @@ let json_of_response (type a) (query : a t) (response : a) : json =
502503
]
503504
in
504505
`List [ assoc; `List (List.map ~f:Json.string strs) ]
505-
| Outline, outlines -> `List (json_of_outline outlines)
506+
| Outline _, outlines -> `List (json_of_outline outlines)
506507
| Shape _, shapes -> `List (List.map ~f:json_of_shape shapes)
507508
| Inlay_hints _, result -> json_of_inlay_hints result
508509
| Errors _, errors -> `List (List.map ~f:json_of_error errors)

src/frontend/ocamlmerlin/old/old_IO.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ let request_of_json context =
178178
(Locate (Some path, ml_or_mli choice, mandatory_position pos, None)))
179179
| `String "jump" :: `String target :: pos ->
180180
request (Query (Jump (target, mandatory_position pos)))
181-
| [ `String "outline" ] -> request (Query Outline)
181+
| [ `String "outline" ] -> request (Query (Outline { include_types = true }))
182182
| [ `String "shape"; pos ] -> request (Query (Shape (pos_of_json pos)))
183183
| [ `String "occurrences"; `String "ident"; `String "at"; jpos ] ->
184184
request (Query (Occurrences (`Ident_at (pos_of_json jpos), `Buffer)))

src/frontend/query_commands.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,10 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a = function
748748
| _ :: _ -> raise Construct.Not_a_hole
749749
| [] -> raise No_nodes
750750
end
751-
| Outline ->
751+
| Outline { include_types } ->
752752
let typer = Mpipeline.typer_result pipeline in
753753
let browse = Mbrowse.of_typedtree (Mtyper.get_typedtree typer) in
754-
Outline.get [ Browse_tree.of_browse browse ]
754+
Outline.get ~include_types [ Browse_tree.of_browse browse ]
755755
| Shape pos ->
756756
let typer = Mpipeline.typer_result pipeline in
757757
let browse = Mbrowse.of_typedtree (Mtyper.get_typedtree typer) in

src/frontend/query_protocol.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ type _ t =
258258
| Inlay_hints :
259259
Msource.position * Msource.position * bool * bool * bool
260260
-> (Lexing.position * string) list t
261-
| Outline (* *) : outline t
261+
| Outline (* *) : { include_types : bool } -> outline t
262262
| Shape (* *) : Msource.position -> shape list t
263263
| Errors (* *) : error_filter -> Location.error list t
264264
| Dump : Std.json list -> Std.json t

tests/test-dirs/outline.t/run.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,22 @@
242242
$ $MERLIN single outline -short-paths < path.ml | jq '.value[].type'
243243
"a"
244244
null
245+
246+
Check that when we pass "-include-types false", every "type" is null.
247+
$ $MERLIN single outline -include-types false < foo.ml \
248+
> | jq '.value | .. | objects | select(has("type")) | .type'
249+
null
250+
null
251+
null
252+
null
253+
null
254+
null
255+
null
256+
null
257+
null
258+
null
259+
null
260+
null
261+
null
262+
null
263+
null

0 commit comments

Comments
 (0)