Skip to content

Commit 8d0c35c

Browse files
authored
Add a new option to mute the hover response. (#1416)
* Add option to mute the hover + ensure jump CA is disabled
1 parent d54e8e5 commit 8d0c35c

File tree

7 files changed

+178
-20
lines changed

7 files changed

+178
-20
lines changed

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Unreleased
2+
3+
## Features
4+
5+
- Add a new server option `standardHover`, that can be used by clients to
6+
disable the default hover provider. When `standardHover = false`
7+
`textDocument/hover` requests always returns with empty result.
8+
19
# 1.20.1
210

311
## Fixes

ocaml-lsp-server/src/config_data.ml

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,78 @@ module ExtendedHover = struct
241241
[@@@end]
242242
end
243243

244+
module StandardHover = struct
245+
type t = { enable : bool [@default true] }
246+
[@@deriving_inline yojson] [@@yojson.allow_extra_fields]
247+
248+
let _ = fun (_ : t) -> ()
249+
250+
let t_of_yojson =
251+
(let _tp_loc = "ocaml-lsp-server/src/config_data.ml.StandardHover.t" in
252+
function
253+
| `Assoc field_yojsons as yojson ->
254+
let enable_field = ref Ppx_yojson_conv_lib.Option.None
255+
and duplicates = ref []
256+
and extra = ref [] in
257+
let rec iter = function
258+
| (field_name, _field_yojson) :: tail ->
259+
(match field_name with
260+
| "enable" ->
261+
(match Ppx_yojson_conv_lib.( ! ) enable_field with
262+
| Ppx_yojson_conv_lib.Option.None ->
263+
let fvalue = bool_of_yojson _field_yojson in
264+
enable_field := Ppx_yojson_conv_lib.Option.Some fvalue
265+
| Ppx_yojson_conv_lib.Option.Some _ ->
266+
duplicates := field_name :: Ppx_yojson_conv_lib.( ! ) duplicates)
267+
| _ -> ());
268+
iter tail
269+
| [] -> ()
270+
in
271+
iter field_yojsons;
272+
(match Ppx_yojson_conv_lib.( ! ) duplicates with
273+
| _ :: _ ->
274+
Ppx_yojson_conv_lib.Yojson_conv_error.record_duplicate_fields
275+
_tp_loc
276+
(Ppx_yojson_conv_lib.( ! ) duplicates)
277+
yojson
278+
| [] ->
279+
(match Ppx_yojson_conv_lib.( ! ) extra with
280+
| _ :: _ ->
281+
Ppx_yojson_conv_lib.Yojson_conv_error.record_extra_fields
282+
_tp_loc
283+
(Ppx_yojson_conv_lib.( ! ) extra)
284+
yojson
285+
| [] ->
286+
let enable_value = Ppx_yojson_conv_lib.( ! ) enable_field in
287+
{ enable =
288+
(match enable_value with
289+
| Ppx_yojson_conv_lib.Option.None -> true
290+
| Ppx_yojson_conv_lib.Option.Some v -> v)
291+
}))
292+
| _ as yojson ->
293+
Ppx_yojson_conv_lib.Yojson_conv_error.record_list_instead_atom _tp_loc yojson
294+
: Ppx_yojson_conv_lib.Yojson.Safe.t -> t)
295+
;;
296+
297+
let _ = t_of_yojson
298+
299+
let yojson_of_t =
300+
(function
301+
| { enable = v_enable } ->
302+
let bnds : (string * Ppx_yojson_conv_lib.Yojson.Safe.t) list = [] in
303+
let bnds =
304+
let arg = yojson_of_bool v_enable in
305+
("enable", arg) :: bnds
306+
in
307+
`Assoc bnds
308+
: t -> Ppx_yojson_conv_lib.Yojson.Safe.t)
309+
;;
310+
311+
let _ = yojson_of_t
312+
313+
[@@@end]
314+
end
315+
244316
module DuneDiagnostics = struct
245317
type t = { enable : bool [@default true] }
246318
[@@deriving_inline yojson] [@@yojson.allow_extra_fields]
@@ -461,6 +533,8 @@ type t =
461533
{ codelens : Lens.t Json.Nullable_option.t [@default None] [@yojson_drop_default ( = )]
462534
; extended_hover : ExtendedHover.t Json.Nullable_option.t
463535
[@key "extendedHover"] [@default None] [@yojson_drop_default ( = )]
536+
; standard_hover : StandardHover.t Json.Nullable_option.t
537+
[@key "standardHover"] [@default None] [@yojson_drop_default ( = )]
464538
; inlay_hints : InlayHints.t Json.Nullable_option.t
465539
[@key "inlayHints"] [@default None] [@yojson_drop_default ( = )]
466540
; dune_diagnostics : DuneDiagnostics.t Json.Nullable_option.t
@@ -480,6 +554,7 @@ let t_of_yojson =
480554
| `Assoc field_yojsons as yojson ->
481555
let codelens_field = ref Ppx_yojson_conv_lib.Option.None
482556
and extended_hover_field = ref Ppx_yojson_conv_lib.Option.None
557+
and standard_hover_field = ref Ppx_yojson_conv_lib.Option.None
483558
and inlay_hints_field = ref Ppx_yojson_conv_lib.Option.None
484559
and dune_diagnostics_field = ref Ppx_yojson_conv_lib.Option.None
485560
and syntax_documentation_field = ref Ppx_yojson_conv_lib.Option.None
@@ -507,6 +582,15 @@ let t_of_yojson =
507582
extended_hover_field := Ppx_yojson_conv_lib.Option.Some fvalue
508583
| Ppx_yojson_conv_lib.Option.Some _ ->
509584
duplicates := field_name :: Ppx_yojson_conv_lib.( ! ) duplicates)
585+
| "standardHover" ->
586+
(match Ppx_yojson_conv_lib.( ! ) standard_hover_field with
587+
| Ppx_yojson_conv_lib.Option.None ->
588+
let fvalue =
589+
Json.Nullable_option.t_of_yojson StandardHover.t_of_yojson _field_yojson
590+
in
591+
standard_hover_field := Ppx_yojson_conv_lib.Option.Some fvalue
592+
| Ppx_yojson_conv_lib.Option.Some _ ->
593+
duplicates := field_name :: Ppx_yojson_conv_lib.( ! ) duplicates)
510594
| "inlayHints" ->
511595
(match Ppx_yojson_conv_lib.( ! ) inlay_hints_field with
512596
| Ppx_yojson_conv_lib.Option.None ->
@@ -570,13 +654,15 @@ let t_of_yojson =
570654
| [] ->
571655
let ( codelens_value
572656
, extended_hover_value
657+
, standard_hover_value
573658
, inlay_hints_value
574659
, dune_diagnostics_value
575660
, syntax_documentation_value
576661
, merlin_jump_code_actions_value )
577662
=
578663
( Ppx_yojson_conv_lib.( ! ) codelens_field
579664
, Ppx_yojson_conv_lib.( ! ) extended_hover_field
665+
, Ppx_yojson_conv_lib.( ! ) standard_hover_field
580666
, Ppx_yojson_conv_lib.( ! ) inlay_hints_field
581667
, Ppx_yojson_conv_lib.( ! ) dune_diagnostics_field
582668
, Ppx_yojson_conv_lib.( ! ) syntax_documentation_field
@@ -590,6 +676,10 @@ let t_of_yojson =
590676
(match extended_hover_value with
591677
| Ppx_yojson_conv_lib.Option.None -> None
592678
| Ppx_yojson_conv_lib.Option.Some v -> v)
679+
; standard_hover =
680+
(match standard_hover_value with
681+
| Ppx_yojson_conv_lib.Option.None -> None
682+
| Ppx_yojson_conv_lib.Option.Some v -> v)
593683
; inlay_hints =
594684
(match inlay_hints_value with
595685
| Ppx_yojson_conv_lib.Option.None -> None
@@ -618,6 +708,7 @@ let yojson_of_t =
618708
(function
619709
| { codelens = v_codelens
620710
; extended_hover = v_extended_hover
711+
; standard_hover = v_standard_hover
621712
; inlay_hints = v_inlay_hints
622713
; dune_diagnostics = v_dune_diagnostics
623714
; syntax_documentation = v_syntax_documentation
@@ -667,6 +758,16 @@ let yojson_of_t =
667758
let bnd = "inlayHints", arg in
668759
bnd :: bnds)
669760
in
761+
let bnds =
762+
if None = v_standard_hover
763+
then bnds
764+
else (
765+
let arg =
766+
(Json.Nullable_option.yojson_of_t StandardHover.yojson_of_t) v_standard_hover
767+
in
768+
let bnd = "standardHover", arg in
769+
bnd :: bnds)
770+
in
670771
let bnds =
671772
if None = v_extended_hover
672773
then bnds
@@ -696,9 +797,10 @@ let _ = yojson_of_t
696797
let default =
697798
{ codelens = Some { enable = false }
698799
; extended_hover = Some { enable = false }
800+
; standard_hover = Some { enable = true }
699801
; inlay_hints = Some { hint_pattern_variables = false; hint_let_bindings = false }
700802
; dune_diagnostics = Some { enable = true }
701803
; syntax_documentation = Some { enable = false }
702-
; merlin_jump_code_actions = Some { enable = true }
804+
; merlin_jump_code_actions = Some { enable = false }
703805
}
704806
;;

ocaml-lsp-server/src/ocaml_lsp_server.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,15 @@ let on_request
628628
| TextDocumentColor _ -> now []
629629
| TextDocumentColorPresentation _ -> now []
630630
| TextDocumentHover req ->
631-
let mode =
632-
match state.configuration.data.extended_hover with
633-
| Some { enable = true } -> Hover_req.Extended_variable
634-
| Some _ | None -> Hover_req.Default
635-
in
636-
later (fun (_ : State.t) () -> Hover_req.handle rpc req mode) ()
631+
(match state.configuration.data.standard_hover with
632+
| Some { enable = false } -> now None
633+
| Some { enable = true } | None ->
634+
let mode =
635+
match state.configuration.data.extended_hover with
636+
| Some { enable = true } -> Hover_req.Extended_variable
637+
| Some _ | None -> Hover_req.Default
638+
in
639+
later (fun (_ : State.t) () -> Hover_req.handle rpc req mode) ())
637640
| TextDocumentReferences req -> later (references rpc) req
638641
| TextDocumentCodeLensResolve codeLens -> now codeLens
639642
| TextDocumentCodeLens req ->

ocaml-lsp-server/test/e2e-new/code_actions.ml

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,14 @@ module M : sig type t = I of int | B of bool end
12721272
|}]
12731273
;;
12741274
1275+
let activate_jump client =
1276+
let config =
1277+
DidChangeConfigurationParams.create
1278+
~settings:(`Assoc [ "merlinJumpCodeActions", `Assoc [ "enable", `Bool true ] ])
1279+
in
1280+
change_config ~client config
1281+
;;
1282+
12751283
let%expect_test "can jump to match target" =
12761284
let source =
12771285
{ocaml|
@@ -1288,7 +1296,11 @@ let f (x : t) (d : bool) =
12881296
let end_ = Position.create ~line:5 ~character:5 in
12891297
Range.create ~start ~end_
12901298
in
1291-
print_code_actions source range ~filter:(find_action "merlin-jump-match");
1299+
print_code_actions
1300+
~prep:activate_jump
1301+
source
1302+
range
1303+
~filter:(find_action "merlin-jump-match");
12921304
[%expect
12931305
{|
12941306
Code actions:
@@ -1327,7 +1339,11 @@ let f (x : t) (d : bool) =
13271339
let end_ = Position.create ~line:5 ~character:5 in
13281340
Range.create ~start ~end_
13291341
in
1330-
print_code_actions source range ~filter:(find_action "merlin-jump-next-case");
1342+
print_code_actions
1343+
~prep:activate_jump
1344+
source
1345+
range
1346+
~filter:(find_action "merlin-jump-next-case");
13311347
[%expect
13321348
{|
13331349
Code actions:
@@ -1364,7 +1380,11 @@ let f (x : t) (d : bool) =
13641380
let end_ = Position.create ~line:5 ~character:5 in
13651381
Range.create ~start ~end_
13661382
in
1367-
print_code_actions source range ~filter:(find_action "merlin-jump-prev-case");
1383+
print_code_actions
1384+
~prep:activate_jump
1385+
source
1386+
range
1387+
~filter:(find_action "merlin-jump-prev-case");
13681388
[%expect
13691389
{|
13701390
Code actions:
@@ -1401,7 +1421,11 @@ let f (x : t) (d : bool) =
14011421
let end_ = Position.create ~line:5 ~character:5 in
14021422
Range.create ~start ~end_
14031423
in
1404-
print_code_actions source range ~filter:(find_action "merlin-jump-let");
1424+
print_code_actions
1425+
~prep:activate_jump
1426+
source
1427+
range
1428+
~filter:(find_action "merlin-jump-let");
14051429
[%expect
14061430
{|
14071431
Code actions:
@@ -1438,7 +1462,11 @@ let f (x : t) (d : bool) =
14381462
let end_ = Position.create ~line:5 ~character:5 in
14391463
Range.create ~start ~end_
14401464
in
1441-
print_code_actions source range ~filter:(find_action "merlin-jump-fun");
1465+
print_code_actions
1466+
~prep:activate_jump
1467+
source
1468+
range
1469+
~filter:(find_action "merlin-jump-fun");
14421470
[%expect
14431471
{|
14441472
Code actions:
@@ -1476,7 +1504,11 @@ let f (x : t) (d : bool) =
14761504
let end_ = Position.create ~line:2 ~character:5 in
14771505
Range.create ~start ~end_
14781506
in
1479-
print_code_actions source range ~filter:(find_action "merlin-jump-module");
1507+
print_code_actions
1508+
~prep:activate_jump
1509+
source
1510+
range
1511+
~filter:(find_action "merlin-jump-module");
14801512
[%expect
14811513
{|
14821514
Code actions:
@@ -1517,7 +1549,11 @@ let%expect_test "can jump to module-type target" =
15171549
let end_ = Position.create ~line:4 ~character:5 in
15181550
Range.create ~start ~end_
15191551
in
1520-
print_code_actions source range ~filter:(find_action "merlin-jump-module-type");
1552+
print_code_actions
1553+
~prep:activate_jump
1554+
source
1555+
range
1556+
~filter:(find_action "merlin-jump-module-type");
15211557
[%expect
15221558
{|
15231559
Code actions:
@@ -1553,7 +1589,11 @@ let%expect_test "shouldn't find the jump target on the same line" =
15531589
let end_ = Position.create ~line:0 ~character:5 in
15541590
Range.create ~start ~end_
15551591
in
1556-
print_code_actions source range ~filter:(find_action "merlin-jump-fun");
1592+
print_code_actions
1593+
~prep:activate_jump
1594+
source
1595+
range
1596+
~filter:(find_action "merlin-jump-fun");
15571597
[%expect {|
15581598
No code actions |}]
15591599
;;

ocaml-lsp-server/test/e2e-new/lsp_helpers.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
open Test.Import
22

3+
let change_config ~client params = Client.notification client (ChangeConfiguration params)
4+
35
let open_document ~client ~uri ~source =
46
let textDocument =
57
TextDocumentItem.create ~uri ~languageId:"ocaml" ~version:0 ~text:source

ocaml-lsp-server/test/e2e-new/lsp_helpers.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
open Test.Import
22

3+
(** Send the given configuration to the language server *)
4+
val change_config : client:'a Client.t -> DidChangeConfigurationParams.t -> unit Fiber.t
5+
36
(** Opens a document with the language server. This must be done before trying
47
to access it *)
58
val open_document

ocaml-lsp-server/test/e2e-new/syntax_doc_tests.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
open! Test.Import
2+
open Lsp_helpers
23

3-
let change_config client params = Client.notification client (ChangeConfiguration params)
44
let uri = DocumentUri.of_path "test.ml"
55
let create_postion line character = Position.create ~line ~character
66

@@ -67,7 +67,7 @@ type color = Red|Blue
6767
|ocaml} in
6868
let position = create_postion 1 9 in
6969
let req client =
70-
let* () = change_config client activate_syntax_doc in
70+
let* () = change_config ~client activate_syntax_doc in
7171
let* resp = hover_req client position in
7272
let () = print_hover resp in
7373
Fiber.return ()
@@ -94,7 +94,7 @@ type color = Red|Blue
9494
|ocaml} in
9595
let position = create_postion 1 9 in
9696
let req client =
97-
let* () = change_config client deactivate_syntax_doc in
97+
let* () = change_config ~client deactivate_syntax_doc in
9898
let* resp = hover_req client position in
9999
let () = print_hover resp in
100100
Fiber.return ()
@@ -117,7 +117,7 @@ type t = ..
117117
|ocaml} in
118118
let position = create_postion 1 5 in
119119
let req client =
120-
let* () = change_config client activate_syntax_doc in
120+
let* () = change_config ~client activate_syntax_doc in
121121
let* resp = hover_req client position in
122122
let () = print_hover resp in
123123
Fiber.return ()
@@ -143,7 +143,7 @@ let%expect_test "should receive no hover response" =
143143
|ocaml} in
144144
let position = create_postion 1 5 in
145145
let req client =
146-
let* () = change_config client activate_syntax_doc in
146+
let* () = change_config ~client activate_syntax_doc in
147147
let* resp = hover_req client position in
148148
let () = print_hover resp in
149149
Fiber.return ()

0 commit comments

Comments
 (0)