Skip to content

Commit 018ad03

Browse files
authored
Merge pull request #1932 from Tim-ats-d/test-outline
Fix class type handling in outline
2 parents e4c6685 + 1fcd591 commit 018ad03

File tree

6 files changed

+461
-70
lines changed

6 files changed

+461
-70
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ unreleased
99
- `occurrences` now reports stale files (#1885)
1010
- `inlay-hints` fix inlay hints on function parameters (#1923)
1111
- Fix issues with ident validation and Lid comparison for occurrences (#1924)
12+
- Handle class type in outline (#1932)
1213
+ ocaml-index
1314
- Improve the granularity of index reading by segmenting the marshalization
1415
of the involved data-structures. (#1889)

src/analysis/outline.ml

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

56+
let get_class_signature_field_desc_infos = function
57+
| Typedtree.Tctf_val (outline_name, _, _, _) -> Some (outline_name, `Value)
58+
| Typedtree.Tctf_method (outline_name, _, _, _) -> Some (outline_name, `Method)
59+
| _ -> None
60+
5661
let outline_type ~env typ =
5762
let ppf, to_string = Format.to_string () in
5863
Printtyp.wrap_printing_env env (fun () ->
@@ -141,6 +146,13 @@ let rec summarize node =
141146
in
142147
let deprecated = Type_utils.is_deprecated cd.ci_attributes in
143148
Some (mk ~children ~location `Class None cd.ci_id_class_type ~deprecated)
149+
| Class_type_declaration ctd ->
150+
let children =
151+
List.concat_map (Lazy.force node.t_children) ~f:get_class_elements
152+
in
153+
let deprecated = Type_utils.is_deprecated ctd.ci_attributes in
154+
Some
155+
(mk ~children ~location `ClassType None ctd.ci_id_class_type ~deprecated)
144156
| _ -> None
145157

146158
and get_class_elements node =
@@ -151,20 +163,31 @@ and get_class_elements node =
151163
List.filter_map (Lazy.force node.t_children) ~f:(fun child ->
152164
match child.t_node with
153165
| Class_field cf -> begin
154-
match get_class_field_desc_infos cf.cf_desc with
155-
| Some (str_loc, outline_kind) ->
156-
let deprecated = Type_utils.is_deprecated cf.cf_attributes in
157-
Some
158-
{ Query_protocol.outline_name = str_loc.Location.txt;
159-
outline_kind;
160-
outline_type = None;
161-
location = str_loc.Location.loc;
162-
children = [];
163-
deprecated
164-
}
165-
| None -> None
166+
cf.cf_desc |> get_class_field_desc_infos
167+
|> Option.map ~f:(fun (str_loc, outline_kind) ->
168+
let deprecated = Type_utils.is_deprecated cf.cf_attributes in
169+
{ Query_protocol.outline_name = str_loc.Location.txt;
170+
outline_kind;
171+
outline_type = None;
172+
location = str_loc.Location.loc;
173+
children = [];
174+
deprecated
175+
})
166176
end
167177
| _ -> None)
178+
| Class_type { cltyp_desc = Tcty_signature { csig_fields; _ }; _ } ->
179+
List.filter_map csig_fields ~f:(fun field ->
180+
get_class_signature_field_desc_infos field.ctf_desc
181+
|> Option.map ~f:(fun (name, outline_kind) ->
182+
let deprecated = Type_utils.is_deprecated field.ctf_attributes in
183+
{ Query_protocol.outline_name = name;
184+
outline_kind;
185+
outline_type = None;
186+
location = field.ctf_loc;
187+
(* TODO: could we have more precised location information? *)
188+
children = [];
189+
deprecated
190+
}))
168191
| _ -> []
169192

170193
and get_mod_children node =

src/commands/query_json.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ let string_of_completion_kind = function
227227
| `MethodCall -> "#"
228228
| `Exn -> "Exn"
229229
| `Class -> "Class"
230+
| `ClassType -> "ClassType"
230231
| `Keyword -> "Keyword"
231232

232233
let with_location ?(with_file = false) ?(skip_none = false) loc assoc =

src/frontend/query_protocol.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ and item =
8888
| `Type
8989
| `Exn
9090
| `Class
91+
| `ClassType
9192
| `Method ];
9293
outline_type : string option;
9394
deprecated : bool;

tests/test-dirs/outline.t/foo.ml

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,73 @@
11
module Bar = struct
2-
type t = int
3-
module type S1 = sig
4-
type t
2+
type t = int
3+
module type S1 = sig
4+
type t
55

6-
val foo : t -> int
7-
end
6+
val foo : t -> int
7+
end
8+
9+
class type b = object end
810
end
911

1012
class type class_type_a = object
1113
method a : int -> int
1214
end
1315

14-
class class_b = object
15-
method b s = s ^ s
16-
end
16+
class class_b =
17+
object
18+
method b s = s ^ s
19+
end
1720

1821
exception Ex of char
1922

20-
type ('a, 'b) eithery =
21-
| Lefty of 'a
22-
| Righty of 'b
23+
type ('a, 'b) eithery = Lefty of 'a | Righty of 'b
24+
25+
type 'a point = { x : 'a; y : 'a; z : 'a }
26+
27+
class a = object end
28+
29+
and b = object end
30+
31+
and c = object end
32+
33+
class type ta = object end
34+
35+
and tb = object end
36+
37+
class b =
38+
object
39+
val foo = 10
40+
method bar () = print_endline "bar"
41+
end
42+
43+
and c = object end
44+
45+
class a =
46+
object
47+
val b =
48+
object
49+
method inside_a_b () =
50+
let x_inside_a_b = 10 in
51+
print_int x_inside_a_b
52+
end
53+
end
54+
55+
and b =
56+
object
57+
val foo = 10
58+
method bar = print_endline "bar"
59+
end
60+
61+
class type ta = object
62+
method baz : int -> int -> string
63+
end
64+
65+
and tb = object end
2366

24-
type 'a point =
25-
{ x : 'a
26-
; y : 'a
27-
; z : 'a
28-
}
67+
let final_let =
68+
let c =
69+
object
70+
method foo = 10
71+
end
72+
in
73+
c

0 commit comments

Comments
 (0)