Skip to content

Commit cfd17ab

Browse files
author
Frédéric Bour
committed
fix parenthesizing of identifiers during completion
1 parent a7e0fba commit cfd17ab

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

src/analysis/completion.ml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ open Query_protocol.Compl
151151
let map_entry f entry =
152152
{entry with desc = f entry.desc; info = f entry.info}
153153

154-
let make_candidate ?get_doc ~attrs ~exact ?prefix_path name ?loc ?path ty =
154+
let make_candidate ~get_doc ~attrs ~exact ~prefix_path name ?loc ?path ty =
155155
let ident = match path with
156156
| Some path ->
157157
(* this is not correct: the ident is not persistent, the printing of some
@@ -284,6 +284,8 @@ let get_candidates ?get_doc ?target_type ?prefix_path ~prefix kind ~validate env
284284
let lbl_attributes l = l.Types.lbl_attributes in
285285
let mtd_attributes t = t.Types.mtd_attributes in
286286
let md_attributes t = t.Types.md_attributes in
287+
let make_candidate ~attrs ~exact name ?loc ?path ty =
288+
make_candidate ~get_doc ~prefix_path ~attrs ~exact name ?loc ?path ty in
287289
let make_weighted_candidate ?(priority=0) ~attrs ~exact name ?loc ?path ty =
288290
(* Just like [make_candidate] but associates some metadata to the candidate.
289291
The candidates are later sorted using these metadata.
@@ -300,7 +302,7 @@ let get_candidates ?get_doc ?target_type ?prefix_path ~prefix kind ~validate env
300302
try Path.scope (Option.get path)
301303
with _ -> 0
302304
in
303-
let item = make_candidate ?get_doc ~attrs ~exact name ?loc ?path ty in
305+
let item = make_candidate ~attrs ~exact name ?loc ?path ty in
304306
(- priority, - time, name), item
305307
in
306308
let is_internal name = name = "" || name.[0] = '_' in
@@ -482,6 +484,8 @@ let complete_prefix ?get_doc ?target_type ?(kinds=[]) ~prefix ~is_label
482484
then false
483485
else (Hashtbl.add seen n (); true)
484486
in
487+
let make_candidate ~attrs ~exact name ?loc ?path ty =
488+
make_candidate ~get_doc ~attrs ~exact name ?loc ?path ty in
485489
let find ?prefix_path ~is_label prefix =
486490
let valid tag name =
487491
try
@@ -502,13 +506,14 @@ let complete_prefix ?get_doc ?target_type ?(kinds=[]) ~prefix ~is_label
502506
in
503507
let add_label_description ({Types.lbl_name = name; _} as l) candidates =
504508
if not (valid `Label name) then candidates else
505-
make_candidate ?get_doc ~exact:(name = prefix) name (`Label l) ~attrs:[]
509+
make_candidate ~prefix_path ~exact:(name = prefix) name
510+
(`Label l) ~attrs:[]
506511
:: candidates
507512
in
508513
let add_label_declaration ty ({Types.ld_id = name; _} as l) candidates =
509514
let name = Ident.name name in
510515
if not (valid `Label name) then candidates else
511-
make_candidate ?get_doc ~exact:(name = prefix) name
516+
make_candidate ~prefix_path ~exact:(name = prefix) name
512517
(`Label_decl (ty,l)) ~attrs:[]
513518
:: candidates
514519
in
@@ -548,7 +553,8 @@ let complete_prefix ?get_doc ?target_type ?(kinds=[]) ~prefix ~is_label
548553
if name = prefix && uniq (`Mod, name) then
549554
try
550555
let path, md, attrs = Type_utils.lookup_module (Longident.Lident name) env in
551-
make_candidate ?get_doc ~exact:true name ~path (`Mod md) ~attrs
556+
make_candidate ~prefix_path:(Some prefix) ~exact:true ~path name
557+
(`Mod md) ~attrs
552558
:: candidates
553559
with Not_found ->
554560
default :: candidates

src/ocaml/typing/408/oprint.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ let all_ident_chars s =
5555
let len = String.length s in
5656
loop s len 0
5757

58-
let parenthesized_ident name =
59-
(List.mem name ["or"; "mod"; "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr"])
60-
|| not (all_ident_chars name)
58+
let parenthesized_ident = function
59+
| "[]" -> false
60+
| "or" | "mod" | "land" | "lor" | "lxor" | "lsl" | "lsr" | "asr" -> true
61+
| name -> not (all_ident_chars name)
6162

6263
let value_ident ppf name =
6364
if parenthesized_ident name then

tests/completion/dune

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@
66
(setenv MERLIN %{exe:../merlin-wrapper}
77
(run %{bin:mdx} test --syntax=cram %{t}))
88
(diff? %{t} %{t}.corrected))))
9+
10+
(alias
11+
(name runtest)
12+
(deps (:t parenthesize.t) parenthesize.ml)
13+
(action
14+
(progn
15+
(setenv MERLIN %{exe:../merlin-wrapper}
16+
(run %{bin:mdx} test --syntax=cram %{t}))
17+
(diff? %{t} %{t}.corrected))))

tests/completion/parenthesize.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module MyList = struct
2+
type 'a t =
3+
| (::) of 'a * 'a t
4+
| []
5+
end
6+
7+
let _ = MyList.

tests/completion/parenthesize.t

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
$ $MERLIN single complete-prefix -position 8:1 -prefix MyList. \
2+
> -filename parenthesize.ml < parenthesize.ml | jq ".value.entries | sort_by(.name)"
3+
[
4+
{
5+
"name": "(::)",
6+
"kind": "Constructor",
7+
"desc": "'a * 'a MyList.t -> 'a MyList.t",
8+
"info": ""
9+
},
10+
{
11+
"name": "[]",
12+
"kind": "Constructor",
13+
"desc": "'a MyList.t",
14+
"info": ""
15+
},
16+
{
17+
"name": "t",
18+
"kind": "Type",
19+
"desc": "type 'a t = (::) of 'a * 'a MyList.t | []",
20+
"info": ""
21+
}
22+
]

0 commit comments

Comments
 (0)