Skip to content

Commit 1158e3e

Browse files
committed
Fix occurrences with let punning
1 parent 1c7e3d1 commit 1158e3e

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

src/analysis/index_occurrences.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ let decl_of_path_or_lid env namespace path lid =
2525
end
2626
| _ -> Env_lookup.by_path path namespace env
2727

28+
let should_ignore_lid (lid : Longident.t Location.loc) = Location.is_none lid.loc
29+
2830
let iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid =
2931
let add uid loc = Stamped_hashtable.add index ~stamp (uid, loc) () in
3032
let f ~namespace env path (lid : Longident.t Location.loc) =
3133
log ~title:"index_buffer" "Path: %a" Logger.fmt (Fun.flip Path.print path);
32-
let not_ghost { Location.loc = { loc_ghost; _ }; _ } = not loc_ghost in
3334
let lid = { lid with loc = set_fname ~file:current_buffer_path lid.loc } in
3435
let index_decl () =
3536
begin
@@ -42,7 +43,7 @@ let iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid =
4243
add decl.uid lid
4344
end
4445
in
45-
if not_ghost lid then
46+
if not (should_ignore_lid lid) then
4647
match Env.shape_of_path ~namespace env path with
4748
| exception Not_found -> ()
4849
| path_shape ->

src/analysis/occurrences.ml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,25 @@ let locs_of ~config ~env ~typer_result ~pos ~scope path =
200200
Option.map external_locs ~f:(fun (index, locs) ->
201201
let stats = Stat_check.create ~cache_size:128 index in
202202
( Lid_set.filter
203-
(fun { loc; _ } ->
204-
(* We ignore external results that concern the current buffer *)
205-
let file = loc.Location.loc_start.Lexing.pos_fname in
206-
let file, buf =
207-
match config.merlin.source_root with
208-
| Some root ->
209-
(Filename.concat root file, current_buffer_path)
210-
| None -> (file, config.query.filename)
203+
(fun ({ loc; _ } as lid) ->
204+
let is_current_buffer =
205+
(* We filter external results that concern the current buffer *)
206+
let file = loc.Location.loc_start.Lexing.pos_fname in
207+
let file, buf =
208+
match config.merlin.source_root with
209+
| Some root ->
210+
(Filename.concat root file, current_buffer_path)
211+
| None -> (file, config.query.filename)
212+
in
213+
let file = Misc.canonicalize_filename file in
214+
let buf = Misc.canonicalize_filename buf in
215+
String.equal file buf
211216
in
212-
let file = Misc.canonicalize_filename file in
213-
let buf = Misc.canonicalize_filename buf in
214-
if String.equal file buf then false
217+
let should_be_ignored =
218+
(* We ignore results that don't have a location *)
219+
Index_occurrences.should_ignore_lid lid
220+
in
221+
if is_current_buffer || should_be_ignored then false
215222
else begin
216223
(* We ignore external results if their source was modified *)
217224
let check = Stat_check.check stats ~file in

tests/test-dirs/let-punning.t/run.t

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@ Test that finding occurrences of a variable includes usages in a punned let. i.e
248248
finding occurrences of x on line 1 returns the definition on line 1 and the usage on
249249
line 2.
250250

251-
TODO: fix these tests
252-
253251
let*
254252
$ occurrences 12:8
255253
Occurrences of:
@@ -258,6 +256,9 @@ let*
258256
Occurrence at 12:8-9:
259257
let a = return 1 in
260258
^
259+
Occurrence at 13:9-10:
260+
let* a in
261+
^
261262

262263
parallel let*
263264
$ occurrences 18:8
@@ -267,13 +268,19 @@ parallel let*
267268
Occurrence at 18:8-9:
268269
let a = return 1 in
269270
^
271+
Occurrence at 20:9-10:
272+
let* a and* b in
273+
^
270274
$ occurrences 19:8
271275
Occurrences of:
272276
let b = return 1 in
273277
^
274278
Occurrence at 19:8-9:
275279
let b = return 1 in
276280
^
281+
Occurrence at 20:16-17:
282+
let* a and* b in
283+
^
277284

278285
sequential let*
279286
$ occurrences 25:8
@@ -283,10 +290,16 @@ sequential let*
283290
Occurrence at 25:8-9:
284291
let a = return 1 in
285292
^
293+
Occurrence at 27:9-10:
294+
let* a in
295+
^
286296
$ occurrences 26:8
287297
Occurrences of:
288298
let b = return 1 in
289299
^
290300
Occurrence at 26:8-9:
291301
let b = return 1 in
292302
^
303+
Occurrence at 28:9-10:
304+
let* b in
305+
^

tests/test-dirs/occurrences/punning.t/run.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Convenience function for querying occurrences
99

1010
Get occurrences of an identifier that is used as the expression part of a punned let
1111
expression
12-
FIXME: this should also include the occurrence on line 5
1312
$ occurrences 4:6
1413
4:6-7
14+
5:7-8
1515

1616
Get occurrences, with the cursor pointing at the identifier in a punned let.
1717
Merlin returns the occurrences of the new variable bound in that let, rather than the

0 commit comments

Comments
 (0)