From b78e3be88732ba4dd0806f1a441668023731a45e Mon Sep 17 00:00:00 2001 From: Rafal Gwozdzinski Date: Tue, 14 Nov 2023 11:29:41 +0100 Subject: [PATCH 1/3] Configurable Merlin cache period --- lsp/src/cli.ml | 4 +++- ocaml-lsp-server/bin/main.ml | 6 +++++- ocaml-lsp-server/src/merlin_config.ml | 6 ++++++ ocaml-lsp-server/src/merlin_config.mli | 2 ++ ocaml-lsp-server/src/ocaml_lsp_server.ml | 4 +++- ocaml-lsp-server/src/ocaml_lsp_server.mli | 2 +- 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lsp/src/cli.ml b/lsp/src/cli.ml index 998dce270..96b34e307 100644 --- a/lsp/src/cli.ml +++ b/lsp/src/cli.ml @@ -12,6 +12,7 @@ module Arg = struct ; mutable stdio : bool ; mutable spec : (string * Arg.spec * string) list ; mutable clientProcessId : int option + ; mutable cachePeriod : int option } let port t ~name ~description = @@ -30,6 +31,7 @@ module Arg = struct ; stdio = false ; spec = [] ; clientProcessId = None + ; cachePeriod = None } in let spec = @@ -52,7 +54,7 @@ module Arg = struct let clientProcessId t = t.clientProcessId - let channel { pipe; port; stdio; spec = _; clientProcessId = _ } : + let channel { pipe; port; stdio; spec = _; clientProcessId = _; cachePeriod = _ } : (Channel.t, string) result = match (pipe, port, stdio) with | None, None, _ -> Ok Stdio diff --git a/ocaml-lsp-server/bin/main.ml b/ocaml-lsp-server/bin/main.ml index ff52dfa4e..f5165dc4c 100644 --- a/ocaml-lsp-server/bin/main.ml +++ b/ocaml-lsp-server/bin/main.ml @@ -5,6 +5,7 @@ let () = Printexc.record_backtrace true; let version = ref false in let read_dot_merlin = ref false in + let cache_period = ref None in let arg = Lsp.Cli.Arg.create () in let spec = [ ("--version", Arg.Set version, "print version") @@ -12,6 +13,8 @@ let () = , Arg.Set read_dot_merlin , "read Merlin config from .merlin files. The `dot-merlin-reader` \ package must be installed" ) + ; ( "--cache-period", Arg.Int (fun period -> cache_period := Some period) + , "set the Merlin file cache period") ] @ Cli.Arg.spec arg in @@ -39,7 +42,8 @@ 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 ~read_dot_merlin:!read_dot_merlin + ~cache_period:!cache_period) with | Ok () -> () | Error exn -> diff --git a/ocaml-lsp-server/src/merlin_config.ml b/ocaml-lsp-server/src/merlin_config.ml index 50980daed..d85aeeab5 100644 --- a/ocaml-lsp-server/src/merlin_config.ml +++ b/ocaml-lsp-server/src/merlin_config.ml @@ -47,6 +47,8 @@ module List = struct let filter_dup lst = filter_dup' ~equiv:(fun x -> x) lst end +let cache_period = ref None + module Config = struct type t = { build_path : string list @@ -60,6 +62,7 @@ module Config = struct ; reader : string list ; exclude_query_dir : bool ; use_ppx_cache : bool + ; cache_period : int option } let empty = @@ -74,6 +77,7 @@ module Config = struct ; reader = [] ; exclude_query_dir = false ; use_ppx_cache = false + ; cache_period = None } (* Parses suffixes pairs that were supplied as whitespace separated pairs @@ -133,6 +137,7 @@ module Config = struct ; reader = config.reader ; exclude_query_dir = config.exclude_query_dir ; use_ppx_cache = config.use_ppx_cache + ; cache_period = config.cache_period } let merge t (merlin : Mconfig.merlin) failures config_path = @@ -149,6 +154,7 @@ module Config = struct ; flags_to_apply = t.flags @ merlin.flags_to_apply ; failures = failures @ merlin.failures ; config_path = Some config_path + ; cache_period = Option.value !cache_period ~default:merlin.cache_period } end diff --git a/ocaml-lsp-server/src/merlin_config.mli b/ocaml-lsp-server/src/merlin_config.mli index 43995ce4d..389b1d806 100644 --- a/ocaml-lsp-server/src/merlin_config.mli +++ b/ocaml-lsp-server/src/merlin_config.mli @@ -6,6 +6,8 @@ type t val should_read_dot_merlin : bool ref +val cache_period : int option ref + val config : t -> Mconfig.t Fiber.t val destroy : t -> unit Fiber.t diff --git a/ocaml-lsp-server/src/ocaml_lsp_server.ml b/ocaml-lsp-server/src/ocaml_lsp_server.ml index f606ccd04..adbab4198 100644 --- a/ocaml-lsp-server/src/ocaml_lsp_server.ml +++ b/ocaml-lsp-server/src/ocaml_lsp_server.ml @@ -917,10 +917,12 @@ 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 ~read_dot_merlin ~cache_period () = 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.cache_period := cache_period; + Unix.putenv "__MERLIN_MASTER_PID" (string_of_int (Unix.getpid ())); Lev_fiber.run ~sigpipe:`Ignore (fun () -> let* input, output = stream_of_channel channel in diff --git a/ocaml-lsp-server/src/ocaml_lsp_server.mli b/ocaml-lsp-server/src/ocaml_lsp_server.mli index 744bb79ce..5d3a04bf1 100644 --- a/ocaml-lsp-server/src/ocaml_lsp_server.mli +++ b/ocaml-lsp-server/src/ocaml_lsp_server.mli @@ -1,4 +1,4 @@ -val run : Lsp.Cli.Channel.t -> read_dot_merlin:bool -> unit -> unit +val run : Lsp.Cli.Channel.t -> read_dot_merlin:bool -> cache_period:int option -> unit -> unit module Diagnostics = Diagnostics module Version = Version From b4239c7db7f834230e202d3ba71872488903311b Mon Sep 17 00:00:00 2001 From: Rafal Gwozdzinski Date: Tue, 14 Nov 2023 12:29:01 +0100 Subject: [PATCH 2/3] Pin merlin-lib --- ocaml-lsp-server.opam | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ocaml-lsp-server.opam b/ocaml-lsp-server.opam index e18f33dd8..7ddddd5b5 100644 --- a/ocaml-lsp-server.opam +++ b/ocaml-lsp-server.opam @@ -44,6 +44,10 @@ depends: [ "ocaml" "merlin-lib" {>= "4.9" & < "5.0"} ] +# Temporary pin until merlin gets released +pin-depends: [ + "merlin-lib.4.12" "git+https://github.com/ocaml/merlin.git#master" +] dev-repo: "git+https://github.com/ocaml/ocaml-lsp.git" build: [ ["dune" "subst"] {dev} From 03786ba4876d56237a761c6603a9227e71355cb3 Mon Sep 17 00:00:00 2001 From: Rafal Gwozdzinski Date: Thu, 16 Nov 2023 12:33:33 +0100 Subject: [PATCH 3/3] Rename to cache_lifespan --- ocaml-lsp-server/bin/main.ml | 9 +++++---- ocaml-lsp-server/src/merlin_config.ml | 10 +++++----- ocaml-lsp-server/src/merlin_config.mli | 2 +- ocaml-lsp-server/src/ocaml_lsp_server.ml | 4 ++-- ocaml-lsp-server/src/ocaml_lsp_server.mli | 2 +- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ocaml-lsp-server/bin/main.ml b/ocaml-lsp-server/bin/main.ml index f5165dc4c..e55049972 100644 --- a/ocaml-lsp-server/bin/main.ml +++ b/ocaml-lsp-server/bin/main.ml @@ -5,7 +5,7 @@ let () = Printexc.record_backtrace true; let version = ref false in let read_dot_merlin = ref false in - let cache_period = ref None in + let cache_lifespan = ref None in let arg = Lsp.Cli.Arg.create () in let spec = [ ("--version", Arg.Set version, "print version") @@ -13,8 +13,9 @@ let () = , Arg.Set read_dot_merlin , "read Merlin config from .merlin files. The `dot-merlin-reader` \ package must be installed" ) - ; ( "--cache-period", Arg.Int (fun period -> cache_period := Some period) - , "set the Merlin file cache period") + ; ( "--merlin-cache-lifespan" + , Arg.Int (fun period -> cache_lifespan := Some period) + , "set the Merlin file cache lifespan") ] @ Cli.Arg.spec arg in @@ -43,7 +44,7 @@ let () = match Exn_with_backtrace.try_with (Ocaml_lsp_server.run channel ~read_dot_merlin:!read_dot_merlin - ~cache_period:!cache_period) + ~cache_lifespan:!cache_lifespan) with | Ok () -> () | Error exn -> diff --git a/ocaml-lsp-server/src/merlin_config.ml b/ocaml-lsp-server/src/merlin_config.ml index d85aeeab5..b526e18e8 100644 --- a/ocaml-lsp-server/src/merlin_config.ml +++ b/ocaml-lsp-server/src/merlin_config.ml @@ -47,7 +47,7 @@ module List = struct let filter_dup lst = filter_dup' ~equiv:(fun x -> x) lst end -let cache_period = ref None +let cache_lifespan = ref None module Config = struct type t = @@ -62,7 +62,7 @@ module Config = struct ; reader : string list ; exclude_query_dir : bool ; use_ppx_cache : bool - ; cache_period : int option + ; cache_lifespan : int option } let empty = @@ -77,7 +77,7 @@ module Config = struct ; reader = [] ; exclude_query_dir = false ; use_ppx_cache = false - ; cache_period = None + ; cache_lifespan = None } (* Parses suffixes pairs that were supplied as whitespace separated pairs @@ -137,7 +137,7 @@ module Config = struct ; reader = config.reader ; exclude_query_dir = config.exclude_query_dir ; use_ppx_cache = config.use_ppx_cache - ; cache_period = config.cache_period + ; cache_lifespan = config.cache_lifespan } let merge t (merlin : Mconfig.merlin) failures config_path = @@ -154,7 +154,7 @@ module Config = struct ; flags_to_apply = t.flags @ merlin.flags_to_apply ; failures = failures @ merlin.failures ; config_path = Some config_path - ; cache_period = Option.value !cache_period ~default:merlin.cache_period + ; cache_lifespan = Option.value !cache_lifespan ~default:merlin.cache_lifespan } end diff --git a/ocaml-lsp-server/src/merlin_config.mli b/ocaml-lsp-server/src/merlin_config.mli index 389b1d806..88efd239e 100644 --- a/ocaml-lsp-server/src/merlin_config.mli +++ b/ocaml-lsp-server/src/merlin_config.mli @@ -6,7 +6,7 @@ type t val should_read_dot_merlin : bool ref -val cache_period : int option ref +val cache_lifespan : int option ref val config : t -> Mconfig.t Fiber.t diff --git a/ocaml-lsp-server/src/ocaml_lsp_server.ml b/ocaml-lsp-server/src/ocaml_lsp_server.ml index adbab4198..b70e04d9e 100644 --- a/ocaml-lsp-server/src/ocaml_lsp_server.ml +++ b/ocaml-lsp-server/src/ocaml_lsp_server.ml @@ -917,11 +917,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 ~cache_period () = +let run channel ~read_dot_merlin ~cache_lifespan () = 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.cache_period := cache_period; + Merlin_config.cache_lifespan := cache_lifespan; Unix.putenv "__MERLIN_MASTER_PID" (string_of_int (Unix.getpid ())); Lev_fiber.run ~sigpipe:`Ignore (fun () -> diff --git a/ocaml-lsp-server/src/ocaml_lsp_server.mli b/ocaml-lsp-server/src/ocaml_lsp_server.mli index 5d3a04bf1..442b51d95 100644 --- a/ocaml-lsp-server/src/ocaml_lsp_server.mli +++ b/ocaml-lsp-server/src/ocaml_lsp_server.mli @@ -1,4 +1,4 @@ -val run : Lsp.Cli.Channel.t -> read_dot_merlin:bool -> cache_period:int option -> unit -> unit +val run : Lsp.Cli.Channel.t -> read_dot_merlin:bool -> cache_lifespan:int option -> unit -> unit module Diagnostics = Diagnostics module Version = Version