Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --context flag of dune ocaml-merlin #1238

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- Support folding of `ifthenelse` expressions (#1031)

- Add `--context` flag (#1238)
Copy link
Collaborator

@voodoos voodoos Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could add a bit more context here, at least define what "context" refers to here.


## Fixes

- Detect document kind by looking at merlin's `suffixes` config.
Expand Down
9 changes: 8 additions & 1 deletion ocaml-lsp-server/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ let () =
Printexc.record_backtrace true;
let version = ref false in
let read_dot_merlin = ref false in
let dune_context = ref None in
let arg = Lsp.Cli.Arg.create () in
let spec =
[ ("--version", Arg.Set version, "print version")
; ( "--fallback-read-dot-merlin"
, Arg.Set read_dot_merlin
, "read Merlin config from .merlin files. The `dot-merlin-reader` \
package must be installed" )
; ( "--context"
, Arg.String (fun p -> dune_context := Some p)
, "set Dune context" )
]
@ Cli.Arg.spec arg
in
Expand Down Expand Up @@ -39,7 +43,10 @@ let () =
let module Exn_with_backtrace = Stdune.Exn_with_backtrace in
match
Exn_with_backtrace.try_with
(Ocaml_lsp_server.run channel ~read_dot_merlin:!read_dot_merlin)
(Ocaml_lsp_server.run
channel
~dune_context:!dune_context
~read_dot_merlin:!read_dot_merlin)
with
| Ok () -> ()
| Error exn ->
Expand Down
9 changes: 8 additions & 1 deletion ocaml-lsp-server/src/merlin_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ open Fiber.O
module Std = Merlin_utils.Std
module Misc = Merlin_utils.Misc

let dune_context = ref None

module List = struct
include List

Expand Down Expand Up @@ -192,7 +194,12 @@ module Process = struct
let stdout_r, stdout_w = Unix.pipe () in
Unix.set_close_on_exec stdin_w;
let pid =
let argv = [ prog; "ocaml-merlin"; "--no-print-directory" ] in
let argv =
let shared = [ prog; "ocaml-merlin"; "--no-print-directory" ] in
match !dune_context with
| None -> shared
| Some dune_context -> shared @ [ "--context"; dune_context ]
in
Pid.of_int
(Spawn.spawn
~cwd:(Path dir)
Expand Down
2 changes: 2 additions & 0 deletions ocaml-lsp-server/src/merlin_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type t

val should_read_dot_merlin : bool ref

val dune_context : string option ref

val config : t -> Mconfig.t Fiber.t

val destroy : t -> unit Fiber.t
Expand Down
3 changes: 2 additions & 1 deletion ocaml-lsp-server/src/ocaml_lsp_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,11 @@ let run_in_directory =
let for_windows = !Merlin_utils.Std.System.run_in_directory in
fun () -> if Sys.win32 then for_windows else run_in_directory

let run channel ~read_dot_merlin () =
let run channel ~dune_context ~read_dot_merlin () =
Merlin_utils.Lib_config.set_program_name "ocamllsp";
Merlin_utils.Lib_config.System.set_run_in_directory (run_in_directory ());
Merlin_config.should_read_dot_merlin := read_dot_merlin;
Merlin_config.dune_context := dune_context;
Unix.putenv "__MERLIN_MASTER_PID" (string_of_int (Unix.getpid ()));
Lev_fiber.run ~sigpipe:`Ignore (fun () ->
let* input, output = stream_of_channel channel in
Expand Down
7 changes: 6 additions & 1 deletion ocaml-lsp-server/src/ocaml_lsp_server.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
val run : Lsp.Cli.Channel.t -> read_dot_merlin:bool -> unit -> unit
val run :
Lsp.Cli.Channel.t
-> dune_context:string option
-> read_dot_merlin:bool
-> unit
-> unit

module Diagnostics = Diagnostics
module Version = Version
Expand Down
Loading