Skip to content

Commit 9df1683

Browse files
authored
Allow customization of error messages (#1532)
from voodoos/version-mismatch-message
2 parents dff6628 + 3324a5c commit 9df1683

6 files changed

Lines changed: 58 additions & 11 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ undefined
1616
- Improve locate for labels' declarations in the current buffer.
1717
(#1505, fixes #1524)
1818
- Fix locate on module without implementation (#1522, fixes #1519)
19+
- Allow program name customization when merlin is used as a library. (#1532)
1920
+ editor modes
2021
- vim: load the plugin when necessary if it wasn’t loaded before (#1511)
2122
- emacs: update CI for newer releases and fix some warnings (#1454, @mattiase)

src/frontend/ocamlmerlin/ocamlmerlin_server.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ let main () =
9292
| args -> Old_merlin.run args
9393

9494
let () =
95-
Std.Json.pretty_to_string := Yojson.Basic.pretty_to_string;
95+
Lib_config.Json.set_pretty_to_string Yojson.Basic.pretty_to_string;
9696
let `Log_file_path log_file, `Log_sections sections =
9797
Log_info.get ()
9898
in

src/kernel/mconfig_dot.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,24 +335,28 @@ let get_config { workdir; process_dir; configurator } path_abs =
335335
- If `dot-merlin-reader` is not installed and the project use `.merlin`
336336
files
337337
- There was a bug in the external reader causing a crash *)
338+
let program_name = Lib_config.program_name () in
338339
let error = Printf.sprintf
339-
"A problem occurred with merlin external configuration reader. %s If \
340-
the problem persists, please file an issue on Merlin's tracker."
340+
"A problem occurred with %s external configuration reader. %s If \
341+
the problem persists, please file an issue on %s's tracker."
342+
program_name
341343
(match configurator with
342344
| Dot_merlin -> "Check that `dot-merlin-reader` is installed."
343345
| Dune -> "Check that `dune` is installed and up-to-date.")
346+
program_name
344347
in
345348
empty_config, [ error ]
346349
| End_of_input ->
347350
(* This can happen
348351
- if a project using old-dune has not been built and Merlin wrongly tries to
349352
start `new-dune ocaml-merlin` in the absence of `.merlin` files
350353
- the process stopped in the middle of its answer (which is very unlikely) *)
354+
let program_name = Lib_config.program_name () in
351355
let error = Printf.sprintf
352-
"Merlin could not load its configuration from the external reader. %s"
356+
"%s could not load its configuration from the external reader. %s"
357+
program_name
353358
(match configurator with
354-
| Dot_merlin -> "If the problem persists, please file an issue on \
355-
Merlin's tracker."
359+
| Dot_merlin -> "If the problem persists, please file an issue."
356360
| Dune -> "Building your project with `dune` might solve this issue.")
357361
in
358362
empty_config, [ error ]

src/ocaml/typing/magic_numbers.ml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,33 @@ module Cmi = struct
3131
fprintf ppf "%a@ is not a compiled interface"
3232
Location.print_filename filename
3333
| Wrong_version_interface (filename, compiler_magic) ->
34+
let program_name = Lib_config.program_name () in
3435
begin match to_version_opt compiler_magic with
3536
| None ->
3637
fprintf ppf
37-
"%a@ seems to be compiled with a version of OCaml that is not@.\
38-
supported by Merlin."
38+
"Compiler version mismatch: this project seems to be compiled with a \
39+
version of the OCaml compiler that is not supported by this version \
40+
of %s. OCaml language support will not work properly until this \
41+
problem is fixed. \n\
42+
Hint: It seems that the project is built with a newer OCaml compiler \
43+
version that the running %s version does not know about. Make sure \
44+
your editor runs a version of %s that supports this version of the \
45+
compiler. \n\
46+
This diagnostic is based on the compiled interface file: %a"
47+
program_name program_name program_name
3948
Location.print_filename filename
4049
| Some version ->
4150
fprintf ppf
42-
"%a@ seems to be compiled with OCaml %s.@.\
43-
But this instance of Merlin handles OCaml %s."
44-
Location.print_filename filename version
51+
"Compiler version mismatch: this project seems to be compiled with \
52+
version %s of the OCaml compiler, but the running %s supports OCaml \
53+
version %s. OCaml language support will not work properly until this \
54+
problem is fixed. \n\
55+
Hint: Make sure your editor runs a version of %s that supports the \
56+
correct version of the compiler. \n\
57+
This diagnostic is based on the compiled interface file: %a"
58+
version program_name
4559
(Option.get @@ to_version_opt Config.cmi_magic_number)
60+
program_name Location.print_filename filename
4661
end
4762
| Corrupted_interface filename ->
4863
fprintf ppf "Corrupted compiled interface@ %a"

src/utils/lib_config.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let program_name = ref "Merlin"
2+
3+
let set_program_name name = program_name := name
4+
5+
let program_name () = !program_name
6+
7+
module Json = struct
8+
let set_pretty_to_string f =
9+
Std.Json.pretty_to_string := f
10+
end

src/utils/lib_config.mli

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(** When using Merlin as a library, one should use functions provided by this
2+
module to customize Merlin's behaviour. *)
3+
4+
(** [set_program] sets the name of the program that will be used in error
5+
messages. *)
6+
val set_program_name : string -> unit
7+
8+
(** [program ()] returns the name of the program as registered by
9+
[set_program]. Defaults to "Merlin". *)
10+
val program_name : unit -> string
11+
12+
module Json : sig
13+
(** Merlin's logger requires a Json pretty-printer for correct operation.
14+
[set_pretty_to_string] can be used to provide one. A common pretifier
15+
is [Yojson.Basic.pretty_to_string]. *)
16+
val set_pretty_to_string : (Std.json -> string) -> unit
17+
end

0 commit comments

Comments
 (0)