@@ -258,12 +258,34 @@ let reconstruct_identifier_from_tokens tokens pos =
258258 look_for_dot [item] items
259259
260260 (* RPAREN UIDENT means that we are in presence of a functor application. *)
261- | (RPAREN , _, end_pos) :: ((UIDENT s , _, _ ) as _item ) :: items
261+ | (RPAREN , _, end_pos) :: ((UIDENT _ , _, _ ) as item ) :: items
262262 when acc <> [] ->
263- let app, start_pos, items =
264- look_for_apply ~inside_paren: true [ s; " )" ] items
265- in
266- look_for_dot ((UIDENT app, start_pos, end_pos ) :: acc) items
263+ let param_items, items = group_until_lparen [item] items in
264+ begin try
265+ begin try
266+ (* Is the cursor on the parameter ? *)
267+ look_for_dot [] (List. rev param_items)
268+ with Not_found ->
269+ (* Is the cursor on the functor or before ? *)
270+ look_for_component [] items
271+ end
272+ with Not_found ->
273+ (* The cursor must be after the application [M.N(F).|t]
274+ We make a single component with the applciation and continue *)
275+ match items with
276+ | (UIDENT f , start_pos , _ ) :: items ->
277+ let app =
278+ let param = List. map ~f: (function
279+ | (DOT, _ , _ ) -> " ."
280+ | (UIDENT s , _ , _ ) -> s
281+ | _ -> raise Not_found
282+ ) param_items
283+ in
284+ Format. sprintf " %s(%s)" f (String. concat ~sep: " " param)
285+ in
286+ look_for_dot ((UIDENT app, start_pos, end_pos ) :: acc) items
287+ | _ -> raise Not_found
288+ end
267289
268290 (* An operator alone is an identifier on its own *)
269291 | (token, _, _ as item) :: items
@@ -276,16 +298,9 @@ let reconstruct_identifier_from_tokens tokens pos =
276298
277299 | [] -> raise Not_found
278300
279- (* FIXME: this function treats applications as a single component ["M(N.P)"].
280- This prevent jumping to M, N or P. *)
281- and look_for_apply ~inside_paren acc = function
282- | (LPAREN, _ , _ ) :: items when inside_paren ->
283- look_for_apply ~inside_paren: false (" (" :: acc) items
284- | (UIDENT s , _ , _ ) :: items when inside_paren ->
285- look_for_apply ~inside_paren (s :: " ." :: acc) items
286- | (UIDENT s , start_pos , _ ) :: items when not inside_paren ->
287- let item = String. concat ~sep: " " (s :: acc) in
288- item, start_pos, items
301+ and group_until_lparen acc = function
302+ | (LPAREN,_ ,_ ) :: items -> acc, items
303+ | item :: items -> group_until_lparen (item::acc) items
289304 | _ -> raise Not_found
290305
291306 and look_for_dot acc = function
0 commit comments