Skip to content

Commit 84e1386

Browse files
cknittclaude
andcommitted
Offer extensions in scope at the cursor and from interface modules
Codex review on #9085: an extension of the edited file declared after the cursor was offered although it isn't in scope there, and an extension inside a module of an interface (`module Sub: { type attr += Flag }`) was never registered, because only typed-tree extensions were. The completion scope now records the cursor position and skips later extensions of the edited file, and extensions in signatures are registered too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gLMawVgtmiKy3TMBc3 Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent b9d5edc commit 84e1386

12 files changed

Lines changed: 138 additions & 60 deletions

‎analysis/src/completion_back_end.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,6 +2674,6 @@ let module_path_at ~(full : full) ~pos =
26742674
let process_completable ~state ~debug ~full ~scope ~env ~pos ~for_hover
26752675
completable =
26762676
Type_utils.with_completion_scope ~file:full.file
2677-
~module_path:(module_path_at ~full ~pos) (fun () ->
2677+
~module_path:(module_path_at ~full ~pos) ~pos (fun () ->
26782678
process_completable_in_scope ~state ~debug ~full ~scope ~env ~pos
26792679
~for_hover completable)

‎analysis/src/process_cmt.ml‎

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,54 @@ let map_record_field {Types.ld_id; ld_type; ld_attributes; ld_optional} =
3535
deprecated = Process_attributes.find_deprecated_attribute ld_attributes;
3636
}
3737

38+
(* The constructors of [type t += ...] are registered as constructors of an
39+
open type named [t], so that completion can offer them for values of
40+
type [t]. They are not part of any type declaration, hence the stand-in
41+
declaration that carries the kind and, as its manifest, the extended
42+
type, which completion compares with the type it completes. *)
43+
let add_extension_constructor ~(env : Env.t) ~id ~name
44+
(ext : Types.extension_constructor) =
45+
let type_decl : Types.type_declaration =
46+
{
47+
type_params = [];
48+
type_arity = 0;
49+
type_kind = Type_open;
50+
type_private = Public;
51+
type_manifest = Some (Ctype.newconstr ext.ext_type_path []);
52+
type_variance = [];
53+
type_newtype_level = None;
54+
type_loc = Location.none;
55+
type_attributes = [];
56+
type_immediate = false;
57+
type_representation = Boxed;
58+
type_shape = Unstated;
59+
type_inlined_types = [];
60+
}
61+
in
62+
let stamp = Ident.binding_time id in
63+
let item =
64+
{
65+
Constructor.stamp;
66+
cname = name;
67+
args =
68+
(match ext.ext_args with
69+
| Cstr_tuple args ->
70+
Args (args |> List.map (fun t -> (t, Location.none)))
71+
| Cstr_record fields ->
72+
InlineRecord (fields |> List.map map_record_field));
73+
res = ext.ext_ret_type;
74+
type_decl = (Path.last ext.ext_type_path, type_decl);
75+
docstring = attrs_to_docstring ext.ext_attributes;
76+
deprecated =
77+
Process_attributes.find_deprecated_attribute ext.ext_attributes;
78+
}
79+
in
80+
let declared =
81+
Process_attributes.new_declared ~item ~extent:ext.ext_loc ~name ~stamp
82+
~module_path:env.module_path true ext.ext_attributes
83+
in
84+
Stamps.add_constructor env.stamps stamp declared
85+
3886
let rec for_type_signature_item ~(env : Shared_types.Env.t)
3987
~(exported : Exported.t) (item : Types.signature_item) =
4088
match item with
@@ -165,6 +213,11 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
165213
loc = declared.extent_loc;
166214
};
167215
]
216+
| Sig_typext (id, ext, _) ->
217+
add_extension_constructor ~env ~id
218+
~name:(Location.mkloc (Ident.name id) ext.ext_loc)
219+
ext;
220+
[]
168221
| _ -> []
169222

170223
and for_type_signature ~name ~env signature =
@@ -328,60 +381,11 @@ let for_type_declaration ~env ~(exported : Exported.t)
328381
loc = declared.extent_loc;
329382
}
330383

331-
(* The constructors of [type t += ...] are registered as constructors of an
332-
open type named [t], so that completion can offer them for values of
333-
type [t]. They are not part of any type declaration, hence the stand-in
334-
declaration that carries the kind and, as its manifest, the extended
335-
type, which completion compares with the type it completes. *)
336-
let add_extension_constructors ~(env : Env.t)
337-
{Typedtree.tyext_path; tyext_constructors} =
338-
let type_name = Path.last tyext_path in
339-
let type_decl : Types.type_declaration =
340-
{
341-
type_params = [];
342-
type_arity = 0;
343-
type_kind = Type_open;
344-
type_private = Public;
345-
type_manifest = Some (Ctype.newconstr tyext_path []);
346-
type_variance = [];
347-
type_newtype_level = None;
348-
type_loc = Location.none;
349-
type_attributes = [];
350-
type_immediate = false;
351-
type_representation = Boxed;
352-
type_shape = Unstated;
353-
type_inlined_types = [];
354-
}
355-
in
384+
let add_extension_constructors ~(env : Env.t) {Typedtree.tyext_constructors} =
356385
tyext_constructors
357386
|> List.iter
358-
(fun
359-
({ext_id; ext_name; ext_type; ext_loc; ext_attributes} :
360-
Typedtree.extension_constructor)
361-
->
362-
let stamp = Ident.binding_time ext_id in
363-
let item =
364-
{
365-
Constructor.stamp;
366-
cname = ext_name;
367-
args =
368-
(match ext_type.ext_args with
369-
| Cstr_tuple args ->
370-
Args (args |> List.map (fun t -> (t, Location.none)))
371-
| Cstr_record fields ->
372-
InlineRecord (fields |> List.map map_record_field));
373-
res = ext_type.ext_ret_type;
374-
type_decl = (type_name, type_decl);
375-
docstring = attrs_to_docstring ext_attributes;
376-
deprecated =
377-
Process_attributes.find_deprecated_attribute ext_attributes;
378-
}
379-
in
380-
let declared =
381-
Process_attributes.new_declared ~item ~extent:ext_loc ~name:ext_name
382-
~stamp ~module_path:env.module_path true ext_attributes
383-
in
384-
Stamps.add_constructor env.stamps stamp declared)
387+
(fun ({ext_id; ext_name; ext_type} : Typedtree.extension_constructor) ->
388+
add_extension_constructor ~env ~id:ext_id ~name:ext_name ext_type)
385389

386390
let rec for_signature_item ~env ~(exported : Exported.t)
387391
(item : Typedtree.signature_item) =

‎analysis/src/type_utils.ml‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,11 @@ let extract_function_type2 ?type_arg_context ~state ~env ~package typ =
365365
completion request runs. Extension constructors are named relative to
366366
it: unlike ordinary constructors they can't be found from the expected
367367
type, so they have to be written so that they are in scope there. *)
368-
let completion_scope : (File.t * string list) option ref = ref None
368+
let completion_scope : (File.t * string list * Pos.t) option ref = ref None
369369

370-
let with_completion_scope ~file ~module_path f =
370+
let with_completion_scope ~file ~module_path ~pos f =
371371
let previous = !completion_scope in
372-
completion_scope := Some (file, module_path);
372+
completion_scope := Some (file, module_path, pos);
373373
Fun.protect ~finally:(fun () -> completion_scope := previous) f
374374

375375
(* The stamp under which [typ] is declared in [file]. *)
@@ -388,10 +388,17 @@ let type_stamp (file : File.t) (typ : Type.t) =
388388
constructor can't be found from the expected type alone. *)
389389
let extension_constructors ~state ~package ~(current_file : File.t)
390390
~(type_file : File.t) ~type_stamp ~type_name ~decl =
391-
let current_file, scope_path =
391+
let current_file, scope_path, cursor =
392392
match !completion_scope with
393-
| Some (file, module_path) -> (file, module_path)
394-
| None -> (current_file, [])
393+
| Some (file, module_path, pos) -> (file, module_path, Some pos)
394+
| None -> (current_file, [], None)
395+
in
396+
(* An extension of the edited file is in scope from its declaration on. *)
397+
let declared_before_cursor (file : File.t) (declared : _ Declared.t) =
398+
match cursor with
399+
| Some pos when file.uri = current_file.uri ->
400+
Loc.start declared.extent_loc <= pos
401+
| _ -> true
395402
in
396403
let extends (file : File.t) (ext_decl : Types.type_declaration) =
397404
match (type_stamp, ext_decl.type_manifest) with
@@ -422,6 +429,7 @@ let extension_constructors ~state ~package ~(current_file : File.t)
422429
if
423430
name = type_name
424431
&& ext_decl.type_kind = Type_open
432+
&& declared_before_cursor file declared
425433
&& extends file ext_decl
426434
then Hashtbl.replace found (file.uri, stamp) (file, declared)));
427435
Hashtbl.to_seq_values found
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type attr = ..
2+
type attr += Top
3+
4+
module Sub = {
5+
type attr += Flag
6+
}
7+
8+
let value: attr = Top
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type attr = ..
2+
type attr += Top
3+
4+
module Sub: {
5+
type attr += Flag
6+
}
7+
8+
let value: attr
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let imported = CompletionExtensibleIface.value
2+
3+
// switch importe
4+
// ^com

‎tests/analysis_tests/tests/src/CompletionExtensibleVariant.res‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ let a: A.t = A.FromA
4141

4242
// switch value { | Fl
4343
// ^com
44+
45+
// An extension declared later is not in scope yet
46+
// switch value { | La
47+
// ^com
48+
49+
type attr += Later

‎tests/analysis_tests/tests/src/expected/CompletionExtensibleIface.res.txt‎

Whitespace-only changes.

‎tests/analysis_tests/tests/src/expected/CompletionExtensibleIface.resi.txt‎

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Complete src/CompletionExtensibleIfaceUse.res 2:17
2+
XXX Not found!
3+
Completable: CexhaustiveSwitch Value[importe]
4+
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
5+
Resolved opens 1 Stdlib
6+
ContextPath Value[importe]
7+
Path importe
8+
[
9+
{
10+
"detail": "CompletionExtensibleIface.attr",
11+
"kind": 12,
12+
"label": "imported",
13+
"tags": []
14+
},
15+
{
16+
"detail": "insert exhaustive switch for value",
17+
"filterText": "imported",
18+
"insertText": "imported {\n | CompletionExtensibleIface.Top => ${1:%todo}\n | CompletionExtensibleIface.Sub.Flag => ${2:%todo}\n | _ => ${3:%todo}\n }",
19+
"insertTextFormat": 2,
20+
"kind": 15,
21+
"label": "imported (exhaustive switch)",
22+
"tags": []
23+
}
24+
]
25+

0 commit comments

Comments
 (0)