Skip to content

Commit 8eb27b7

Browse files
adding testcase for merlin config with multiple executables.
change merlin to load files searching for exact match first Signed-off-by: Will Thomas <[email protected]>
1 parent f6a4b55 commit 8eb27b7

File tree

8 files changed

+58
-11
lines changed

8 files changed

+58
-11
lines changed

bin/ocaml/ocaml_merlin.ml

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,44 @@ end = struct
9999
let load_merlin_file file =
100100
(* We search for an appropriate merlin configuration in the current
101101
directory and its parents *)
102+
let basename = Path.Build.set_extension file ~ext:"" |> Path.Build.basename in
103+
let good_names =
104+
List.map
105+
~f:(fun pref -> Printf.sprintf "%s-%s" pref basename)
106+
[ "lib"; "exe"; "melange" ]
107+
in
102108
let rec find_closest path =
109+
let merlin_paths = get_merlin_files_paths path in
110+
(* Now we want to look for:
111+
1. an exact match
112+
2. an approximate match
113+
3. recursing to parent directory
114+
*)
103115
match
104-
get_merlin_files_paths path
105-
|> List.find_map ~f:(fun file_path ->
106-
(* FIXME we are racing against the build system writing these
107-
files here *)
108-
match Merlin.Processed.load_file file_path with
109-
| Error msg -> Some (Merlin_conf.make_error msg)
110-
| Ok config -> Merlin.Processed.get config ~file)
116+
List.find merlin_paths ~f:(fun p ->
117+
List.mem good_names (Path.basename p) ~equal:String.equal)
111118
with
112-
| Some p -> Some p
119+
| Some p ->
120+
(* Found exact match: we are done *)
121+
(match Merlin.Processed.load_file p with
122+
| Error msg -> Some (Merlin_conf.make_error msg)
123+
| Ok config -> Merlin.Processed.get config ~file)
113124
| None ->
114-
(match Path.Build.parent path with
115-
| None -> None
116-
| Some dir -> find_closest dir)
125+
(* looking for approximate match *)
126+
(match
127+
List.find_map merlin_paths ~f:(fun file_path ->
128+
(* FIXME we are racing against the build system writing these
129+
files here *)
130+
match Merlin.Processed.load_file file_path with
131+
| Error msg -> Some (Merlin_conf.make_error msg)
132+
| Ok config -> Merlin.Processed.get config ~file)
133+
with
134+
| Some p -> Some p
135+
| None ->
136+
(* Otherwise, recurse upwards *)
137+
(match Path.Build.parent path with
138+
| None -> None
139+
| Some dir -> find_closest dir))
117140
in
118141
match find_closest (Path.Build.parent_exn file) with
119142
| Some x -> x
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.10)

test/blackbox-tests/test-cases/merlin/merlin-multi-exes.t/multi-exes/bin/b.ml

Whitespace-only changes.

test/blackbox-tests/test-cases/merlin/merlin-multi-exes.t/multi-exes/bin/c.ml

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(executable
2+
(name b))
3+
4+
(executable
5+
(name c)
6+
(libraries lib))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(library
2+
(name lib))

test/blackbox-tests/test-cases/merlin/merlin-multi-exes.t/multi-exes/lib/lib.ml

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$ ocamlc_where="$(ocamlc -where)"
2+
$ export BUILD_PATH_PREFIX_MAP="/OCAMLC_WHERE=$ocamlc_where:$BUILD_PATH_PREFIX_MAP"
3+
4+
$ FILE=$PWD/multi-exes/bin/b.ml
5+
$ printf "(4:File%d:%s)" ${#FILE} $FILE | dune ocaml-merlin
6+
((5:ERROR71:No config found for file multi-exes/bin/b.ml. Try calling 'dune build'.))
7+
8+
$ dune build @check
9+
10+
$ printf "(4:File%d:%s)" ${#FILE} $FILE | dune ocaml-merlin | sed -E "s/[[:digit:]]+:/\?:/g"
11+
((?:INDEX?:$TESTCASE_ROOT/_build/default/multi-exes/bin/.c.eobjs/cctx.ocaml-index)(?:INDEX?:$TESTCASE_ROOT/_build/default/multi-exes/bin/.b.eobjs/cctx.ocaml-index)(?:INDEX?:$TESTCASE_ROOT/_build/default/multi-exes/lib/.lib.objs/cctx.ocaml-index)(?:STDLIB?:/OCAMLC_WHERE)(?:SOURCE_ROOT?:$TESTCASE_ROOT)(?:EXCLUDE_QUERY_DIR)(?:B?:$TESTCASE_ROOT/_build/default/multi-exes/bin/.b.eobjs/byte)(?:S?:$TESTCASE_ROOT/multi-exes/bin)(?:FLG(?:-w?:@1..3@5..28@30..39@43@46..47@49..57@61..62-?:-strict-sequence?:-strict-formats?:-short-paths?:-keep-locs?:-g))(?:UNIT_NAME?:b))
12+
13+
$ FILE=$PWD/multi-exes/bin/c.ml
14+
$ printf "(4:File%d:%s)" ${#FILE} $FILE | dune ocaml-merlin | sed -E "s/[[:digit:]]+:/\?:/g"
15+
((?:INDEX?:$TESTCASE_ROOT/_build/default/multi-exes/bin/.c.eobjs/cctx.ocaml-index)(?:INDEX?:$TESTCASE_ROOT/_build/default/multi-exes/bin/.b.eobjs/cctx.ocaml-index)(?:INDEX?:$TESTCASE_ROOT/_build/default/multi-exes/lib/.lib.objs/cctx.ocaml-index)(?:STDLIB?:/OCAMLC_WHERE)(?:SOURCE_ROOT?:$TESTCASE_ROOT)(?:EXCLUDE_QUERY_DIR)(?:B?:$TESTCASE_ROOT/_build/default/multi-exes/bin/.c.eobjs/byte)(?:B?:$TESTCASE_ROOT/_build/default/multi-exes/lib/.lib.objs/byte)(?:S?:$TESTCASE_ROOT/multi-exes/bin)(?:S?:$TESTCASE_ROOT/multi-exes/lib)(?:FLG(?:-w?:@1..3@5..28@30..39@43@46..47@49..57@61..62-?:-strict-sequence?:-strict-formats?:-short-paths?:-keep-locs?:-g))(?:UNIT_NAME?:c))

0 commit comments

Comments
 (0)