Skip to content

Commit

Permalink
Merge pull request #1840 from liam923/canonicalize-paths
Browse files Browse the repository at this point in the history
Canonicalize paths when finding occurrences
  • Loading branch information
voodoos authored Sep 26, 2024
2 parents bbba604 + 6323d17 commit 0f1e4f8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ unreleased
- Implement new expand-node command for expanding PPX annotations (#1745)
- Implement new inlay-hints command for adding hints on a sourcetree (#1812)
- Implement new search-by-type command for searching values by types (#1828)
- Canonicalize paths in occurrences. This helps deduplicate the results and
show more user-friendly paths. (#1840)
+ editor modes
- vim: fix python-3.12 syntax warnings in merlin.py (#1798)
- vim: Dead code / doc removal for previously deleted MerlinPhrase command (#1804)
Expand Down
10 changes: 10 additions & 0 deletions src/analysis/occurrences.ml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ let locs_of ~config ~env ~typer_result ~pos ~scope path =
(Filename.concat root file, current_buffer_path)
| None -> (file, config.query.filename)
in
let file = Misc.canonicalize_filename file in
let buf = Misc.canonicalize_filename buf in
if String.equal file buf then false
else begin
(* We ignore external results if their source was modified *)
Expand All @@ -228,6 +230,14 @@ let locs_of ~config ~env ~typer_result ~pos ~scope path =
external_locs
in
let locs = Lid_set.union buffer_locs external_locs in
(* Some of the paths may have redundant `.`s or `..`s in them. Although canonicalizing
is not necessary for correctness, it makes the output a bit nicer. *)
let canonicalize_file_in_loc ({ txt; loc } : 'a Location.loc) :
'a Location.loc =
let file = Misc.canonicalize_filename loc.loc_start.pos_fname in
{ txt; loc = set_fname ~file loc }
in
let locs = Lid_set.map canonicalize_file_in_loc locs in
let locs =
log ~title:"occurrences" "Found %i locs" (Lid_set.cardinal locs);
Lid_set.elements locs
Expand Down
54 changes: 54 additions & 0 deletions tests/test-dirs/occurrences/project-wide/pwo-canonicalize.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
$ cat > lib.ml << EOF
> let foo = "bar"
> let () = print_string foo
> EOF

$ cat > main.ml << EOF
> let () = print_string Lib.foo
> EOF

$ cat > .merlin << EOF
> INDEX project.ocaml-index
> SOURCE_ROOT .
> EOF

$ ocamlc -bin-annot -bin-annot-occurrences -c lib.ml main.ml
$ ocaml-index aggregate main.cmt lib.cmt --root . --rewrite-root

$ ocamlmerlin single occurrences -scope project -identifier-at 1:4 \
> -filename lib.ml < lib.ml | jq .value
[
{
"file": "$TESTCASE_ROOT/lib.ml",
"start": {
"line": 1,
"col": 4
},
"end": {
"line": 1,
"col": 7
}
},
{
"file": "$TESTCASE_ROOT/lib.ml",
"start": {
"line": 2,
"col": 22
},
"end": {
"line": 2,
"col": 25
}
},
{
"file": "$TESTCASE_ROOT/main.ml",
"start": {
"line": 1,
"col": 26
},
"end": {
"line": 1,
"col": 29
}
}
]

0 comments on commit 0f1e4f8

Please sign in to comment.