Skip to content

Commit cfe0f89

Browse files
committed
Add a setting to enable CodeLens only for toplevel let binding.
1 parent adf029c commit cfe0f89

File tree

4 files changed

+77
-24
lines changed

4 files changed

+77
-24
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Unreleased
2+
3+
## Fixes
4+
5+
- Make `code-lens` for toplevel let binding configurable (#1567)
6+
17
# 1.24.0
28

39
## Features

ocaml-lsp-server/docs/ocamllsp/config.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ interface config {
1515
*/
1616
extendedHover: { enable : boolean }
1717

18-
/**
19-
* Enable/Disable CodeLens
20-
* @default false
21-
* @since 1.16
22-
*/
23-
codelens: { enable : boolean }
18+
codelens: {
19+
/**
20+
* Enable/Disable CodeLens
21+
* @default false
22+
* @since 1.16
23+
*/
24+
enable : boolean,
25+
26+
/**
27+
* Enable CodeLens only for toplevel let binding
28+
* @default false
29+
* @since 1.24
30+
*/
31+
only_toplevel
32+
}
2433

2534
/**
2635
* Enable/Disable Dune diagnostics

ocaml-lsp-server/src/config_data.ml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ module InlayHints = struct
192192
end
193193

194194
module Lens = struct
195-
type t = { enable : bool [@default true] }
195+
type t =
196+
{ enable : bool [@default true]
197+
; only_toplevel : bool [@default false]
198+
}
196199
[@@deriving_inline yojson] [@@yojson.allow_extra_fields]
197200

198201
let _ = fun (_ : t) -> ()
@@ -202,6 +205,7 @@ module Lens = struct
202205
function
203206
| `Assoc field_yojsons as yojson ->
204207
let enable_field = ref Ppx_yojson_conv_lib.Option.None
208+
and only_toplevel_field = ref Ppx_yojson_conv_lib.Option.None
205209
and duplicates = ref []
206210
and extra = ref [] in
207211
let rec iter = function
@@ -214,6 +218,13 @@ module Lens = struct
214218
enable_field := Ppx_yojson_conv_lib.Option.Some fvalue
215219
| Ppx_yojson_conv_lib.Option.Some _ ->
216220
duplicates := field_name :: Ppx_yojson_conv_lib.( ! ) duplicates)
221+
| "only_toplevel" ->
222+
(match Ppx_yojson_conv_lib.( ! ) only_toplevel_field with
223+
| Ppx_yojson_conv_lib.Option.None ->
224+
let fvalue = bool_of_yojson _field_yojson in
225+
only_toplevel_field := Ppx_yojson_conv_lib.Option.Some fvalue
226+
| Ppx_yojson_conv_lib.Option.Some _ ->
227+
duplicates := field_name :: Ppx_yojson_conv_lib.( ! ) duplicates)
217228
| _ -> ());
218229
iter tail
219230
| [] -> ()
@@ -233,11 +244,18 @@ module Lens = struct
233244
(Ppx_yojson_conv_lib.( ! ) extra)
234245
yojson
235246
| [] ->
236-
let enable_value = Ppx_yojson_conv_lib.( ! ) enable_field in
247+
let enable_value, only_toplevel_value =
248+
( Ppx_yojson_conv_lib.( ! ) enable_field
249+
, Ppx_yojson_conv_lib.( ! ) only_toplevel_field )
250+
in
237251
{ enable =
238252
(match enable_value with
239253
| Ppx_yojson_conv_lib.Option.None -> true
240254
| Ppx_yojson_conv_lib.Option.Some v -> v)
255+
; only_toplevel =
256+
(match only_toplevel_value with
257+
| Ppx_yojson_conv_lib.Option.None -> false
258+
| Ppx_yojson_conv_lib.Option.Some v -> v)
241259
}))
242260
| _ as yojson ->
243261
Ppx_yojson_conv_lib.Yojson_conv_error.record_list_instead_atom _tp_loc yojson
@@ -248,8 +266,12 @@ module Lens = struct
248266

249267
let yojson_of_t =
250268
(function
251-
| { enable = v_enable } ->
269+
| { enable = v_enable; only_toplevel = v_only_toplevel } ->
252270
let bnds : (string * Ppx_yojson_conv_lib.Yojson.Safe.t) list = [] in
271+
let bnds =
272+
let arg = yojson_of_bool v_only_toplevel in
273+
("only_toplevel", arg) :: bnds
274+
in
253275
let bnds =
254276
let arg = yojson_of_bool v_enable in
255277
("enable", arg) :: bnds
@@ -921,7 +943,7 @@ let _ = yojson_of_t
921943
[@@@end]
922944

923945
let default =
924-
{ codelens = Some { enable = false }
946+
{ codelens = Some { enable = false; only_toplevel = true }
925947
; extended_hover = Some { enable = false }
926948
; standard_hover = Some { enable = true }
927949
; inlay_hints =

ocaml-lsp-server/src/ocaml_lsp_server.ml

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -363,28 +363,43 @@ module Formatter = struct
363363
;;
364364
end
365365

366-
let text_document_lens (state : State.t) { CodeLensParams.textDocument = { uri }; _ } =
366+
let text_document_lens
367+
(state : State.t)
368+
{ CodeLensParams.textDocument = { uri }; _ }
369+
~only_toplevel
370+
=
367371
let store = state.store in
368372
let doc = Document_store.get store uri in
369373
match Document.kind doc with
370374
| `Other -> Fiber.return []
371375
| `Merlin m when Document.Merlin.kind m = Intf -> Fiber.return []
372376
| `Merlin doc ->
373377
let+ outline = Document.Merlin.dispatch_exn ~name:"outline" doc Outline in
374-
let rec symbol_info_of_outline_item (item : Query_protocol.item) =
375-
let children = List.concat_map item.children ~f:symbol_info_of_outline_item in
376-
match item.outline_type with
377-
| None -> children
378-
| Some typ ->
379-
let loc = item.location in
380-
let info =
378+
if only_toplevel
379+
then
380+
List.filter_map outline ~f:(fun item ->
381+
match item.outline_type with
382+
| None -> None
383+
| Some typ ->
384+
let loc = item.location in
381385
let range = Range.of_loc loc in
382386
let command = Command.create ~title:typ ~command:"" () in
383-
CodeLens.create ~range ~command ()
384-
in
385-
info :: children
386-
in
387-
List.concat_map ~f:symbol_info_of_outline_item outline
387+
Some (CodeLens.create ~range ~command ()))
388+
else (
389+
let rec symbol_info_of_outline_item (item : Query_protocol.item) =
390+
let children = List.concat_map item.children ~f:symbol_info_of_outline_item in
391+
match item.outline_type with
392+
| None -> children
393+
| Some typ ->
394+
let loc = item.location in
395+
let info =
396+
let range = Range.of_loc loc in
397+
let command = Command.create ~title:typ ~command:"" () in
398+
CodeLens.create ~range ~command ()
399+
in
400+
info :: children
401+
in
402+
List.concat_map ~f:symbol_info_of_outline_item outline)
388403
;;
389404

390405
let selection_range
@@ -651,7 +666,8 @@ let on_request
651666
| TextDocumentCodeLensResolve codeLens -> now codeLens
652667
| TextDocumentCodeLens req ->
653668
(match state.configuration.data.codelens with
654-
| Some { enable = true } -> later text_document_lens req
669+
| Some { enable = true; only_toplevel } ->
670+
later (text_document_lens ~only_toplevel) req
655671
| _ -> now [])
656672
| TextDocumentHighlight req -> later highlight req
657673
| DocumentSymbol { textDocument = { uri }; _ } -> later document_symbol uri

0 commit comments

Comments
 (0)