Skip to content

Commit 124bb34

Browse files
voodoosTheLortex
andcommitted
Allow to set up merlin in a variant of the default context (ocaml#4145)
* Add test * Test with parsing failure on mutltiple merlin contexts * Allow to set up merlin in a variant of the default context. * Add changelog entry Signed-off-by: Ulysse Gérard <thevoodoos@gmail.com> Co-authored-by: Lucas Pluvinage <lucas@tarides.com>
1 parent 9bf450e commit 124bb34

File tree

10 files changed

+127
-12
lines changed

10 files changed

+127
-12
lines changed

CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.9.0 (unreleased)
2+
------------------
3+
4+
- Allow to set up merlin in a variant of the default context
5+
(#4145, @TheLortex, @voodoos)
6+
17
2.8.5 (28/03/2021)
28
------------------
39

src/dune_rules/context.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ let instantiate_context env (workspace : Workspace.t)
743743
; fdo_target_exe
744744
; dynamically_linked_foreign_archives
745745
; instrument_with
746+
; merlin = _
746747
} ->
747748
let merlin =
748749
workspace.merlin_context = Some (Workspace.Context.name context)
@@ -772,10 +773,10 @@ let instantiate_context env (workspace : Workspace.t)
772773
; fdo_target_exe
773774
; dynamically_linked_foreign_archives
774775
; instrument_with
776+
; merlin
775777
}
776778
; switch
777779
; root
778-
; merlin
779780
} ->
780781
let env = extend_paths ~env paths in
781782
create_for_opam ~root ~env_nodes ~env ~profile ~switch ~name ~merlin

src/dune_rules/workspace.ml

+15-10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module Context = struct
5151
; fdo_target_exe : Path.t option
5252
; dynamically_linked_foreign_archives : bool
5353
; instrument_with : Lib_name.t list
54+
; merlin : bool
5455
}
5556

5657
let to_dyn = Dyn.Encoder.opaque
@@ -67,6 +68,7 @@ module Context = struct
6768
; fdo_target_exe
6869
; dynamically_linked_foreign_archives
6970
; instrument_with
71+
; merlin
7072
} t =
7173
Profile.equal profile t.profile
7274
&& List.equal Target.equal targets t.targets
@@ -81,6 +83,7 @@ module Context = struct
8183
&& Bool.equal dynamically_linked_foreign_archives
8284
t.dynamically_linked_foreign_archives
8385
&& List.equal Lib_name.equal instrument_with t.instrument_with
86+
&& Bool.equal merlin t.merlin
8487

8588
let fdo_suffix t =
8689
match t.fdo_target_exe with
@@ -140,7 +143,8 @@ module Context = struct
140143
and+ instrument_with =
141144
field ~default:instrument_with "instrument_with"
142145
(Dune_lang.Syntax.since syntax (2, 7) >>> repeat Lib_name.decode)
143-
and+ loc = loc in
146+
and+ loc = loc
147+
and+ merlin = field_b "merlin" in
144148
Option.iter host_context ~f:(fun _ ->
145149
match targets with
146150
| [ Target.Native ] -> ()
@@ -161,6 +165,7 @@ module Context = struct
161165
; fdo_target_exe
162166
; dynamically_linked_foreign_archives
163167
; instrument_with
168+
; merlin
164169
}
165170
end
166171

@@ -169,29 +174,25 @@ module Context = struct
169174
{ base : Common.t
170175
; switch : string
171176
; root : string option
172-
; merlin : bool
173177
}
174178

175-
let to_dyn { base; switch; root; merlin } =
179+
let to_dyn { base; switch; root } =
176180
let open Dyn.Encoder in
177181
record
178182
[ ("base", Common.to_dyn base)
179183
; ("switch", string switch)
180184
; ("root", option string root)
181-
; ("merlin", bool merlin)
182185
]
183186

184-
let equal { base; switch; root; merlin } t =
187+
let equal { base; switch; root } t =
185188
Common.equal base t.base
186189
&& String.equal switch t.switch
187190
&& Option.equal String.equal root t.root
188-
&& Bool.equal merlin t.merlin
189191

190192
let t ~profile ~instrument_with ~x =
191193
let+ loc_switch, switch = field "switch" (located string)
192194
and+ name = field_o "name" Context_name.decode
193195
and+ root = field_o "root" string
194-
and+ merlin = field_b "merlin"
195196
and+ base = Common.t ~profile ~instrument_with in
196197
let name =
197198
match name with
@@ -209,7 +210,7 @@ module Context = struct
209210
])
210211
in
211212
let base = { base with targets = Target.add base.targets x; name } in
212-
{ base; switch; root; merlin }
213+
{ base; switch; root }
213214
end
214215

215216
module Default = struct
@@ -306,6 +307,7 @@ module Context = struct
306307
; fdo_target_exe = None
307308
; dynamically_linked_foreign_archives = true
308309
; instrument_with = Option.value instrument_with ~default:[]
310+
; merlin = false
309311
}
310312
end
311313

@@ -418,10 +420,13 @@ let t ?x ?profile:cmdline_profile ?instrument_with:cmdline_instrument_with () =
418420
Context_name.Set.union !defined_names
419421
(Context_name.Set.of_list (Context.all_names ctx));
420422
match (ctx, acc) with
421-
| Opam { merlin = true; _ }, Some _ ->
423+
| Opam { base = { merlin = true; _ }; _ }, Some _
424+
| Default { merlin = true; _ }, Some _ ->
422425
User_error.raise ~loc:(Context.loc ctx)
423426
[ Pp.text "you can only have one context for merlin" ]
424-
| Opam { merlin = true; _ }, None -> Some name
427+
| Opam { base = { merlin = true; _ }; _ }, None
428+
| Default { merlin = true; _ }, None ->
429+
Some name
425430
| _ -> acc)
426431
in
427432
let contexts =

src/dune_rules/workspace.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Context : sig
3030
the runtime system. *)
3131
; dynamically_linked_foreign_archives : bool
3232
; instrument_with : Lib_name.t list
33+
; merlin : bool
3334
}
3435
end
3536

@@ -40,7 +41,6 @@ module Context : sig
4041
is left opaque as we leave to opam to interpret it. *)
4142
; switch : string
4243
; root : string option
43-
; merlin : bool
4444
}
4545
end
4646

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(library
2+
(name foo))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 2.9)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let bar = 42
2+
let r = Result.Ok "3"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
$ export BUILD_PATH_PREFIX_MAP=\
2+
> "OPAM_PREFIX=$(opam config var prefix):$BUILD_PATH_PREFIX_MAP"
3+
4+
If Merlin field is absent, default context is chosen
5+
6+
$ cat >dune-workspace <<EOF
7+
> (lang dune 2.9)
8+
> (context (default))
9+
> (context
10+
> (default
11+
> (name cross)))
12+
> EOF
13+
14+
$ dune build
15+
16+
$ [ ! -d _build/cross/.merlin-conf ] && echo "No config in cross"
17+
No config in cross
18+
19+
$ ls -a _build/default/.merlin-conf
20+
.
21+
..
22+
lib-foo
23+
24+
$ dune ocaml-merlin --dump-config="$(pwd)"
25+
Foo
26+
((STDLIB OPAM_PREFIX/lib/ocaml)
27+
(EXCLUDE_QUERY_DIR)
28+
(B
29+
$TESTCASE_ROOT/_build/default/.foo.objs/byte)
30+
(S
31+
$TESTCASE_ROOT)
32+
(FLG
33+
(-w
34+
@1..3@5..28@30..39@43@46..47@49..57@61..62-40
35+
-strict-sequence
36+
-strict-formats
37+
-short-paths
38+
-keep-locs)))
39+
40+
If Merlin field is present, this context is chosen
41+
42+
$ cat >dune-workspace <<EOF
43+
> (lang dune 2.8)
44+
> (context (default))
45+
> (context
46+
> (default
47+
> (name cross)
48+
> (merlin)))
49+
> EOF
50+
51+
$ dune build
52+
53+
$ ls -a _build/cross/.merlin-conf
54+
.
55+
..
56+
lib-foo
57+
58+
$ [ ! -d _build/default/.merlin-conf ] && echo "No config in default"
59+
No config in default
60+
61+
$ dune ocaml-merlin --dump-config="$(pwd)"
62+
Foo
63+
((STDLIB OPAM_PREFIX/lib/ocaml)
64+
(EXCLUDE_QUERY_DIR)
65+
(B
66+
$TESTCASE_ROOT/_build/cross/.foo.objs/byte)
67+
(S
68+
$TESTCASE_ROOT)
69+
(FLG
70+
(-w
71+
@1..3@5..28@30..39@43@46..47@49..57@61..62-40
72+
-strict-sequence
73+
-strict-formats
74+
-short-paths
75+
-keep-locs)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(lang dune 1.0)
2+
3+
(context
4+
(default
5+
(merlin)))
6+
7+
(context
8+
(opam
9+
(switch foo-switch)
10+
(name foo-name)
11+
(profile foo-profile)
12+
(merlin)))

test/blackbox-tests/test-cases/workspaces.t/run.t

+11
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ Workspaces also allow you to set the env for a context:
5050
(c_flags ())
5151
(cxx_flags ())
5252
(menhir_flags ())
53+
54+
$ dune build --root multiple-merlin-contexts
55+
Entering directory 'multiple-merlin-contexts'
56+
File "dune-workspace", line 8, characters 1-82:
57+
8 | (opam
58+
9 | (switch foo-switch)
59+
10 | (name foo-name)
60+
11 | (profile foo-profile)
61+
12 | (merlin)))
62+
Error: you can only have one context for merlin
63+
[1]

0 commit comments

Comments
 (0)