@@ -119,4 +119,58 @@ let inlay ~path ~pos ~maxLength ~debug =
119
119
| Some value ->
120
120
if String. length label > value then None else Some result
121
121
| None -> Some result)
122
- | None -> None )))
122
+ | None -> None )))
123
+
124
+ let codeLens ~path ~debug =
125
+ let lenses = ref [] in
126
+ let push loc =
127
+ let range = Utils. cmtLocToRange loc in
128
+ lenses := range :: ! lenses
129
+ in
130
+ (* Code lenses are only emitted for functions right now. So look for value bindings that are functions,
131
+ and use the loc of the value binding itself so we can look up the full function type for our code lens. *)
132
+ let value_binding (iterator : Ast_iterator.iterator )
133
+ (vb : Parsetree.value_binding ) =
134
+ (match vb with
135
+ | {
136
+ pvb_pat = {ppat_desc = Ppat_var _; ppat_loc};
137
+ pvb_expr = {pexp_desc = Pexp_fun _};
138
+ } ->
139
+ push ppat_loc
140
+ | _ -> () );
141
+ Ast_iterator. default_iterator.value_binding iterator vb
142
+ in
143
+ let iterator = {Ast_iterator. default_iterator with value_binding} in
144
+ (if Filename. check_suffix path " .res" then
145
+ let parser =
146
+ Res_driver. parsingEngine.parseImplementation ~for Printer:false
147
+ in
148
+ let {Res_driver. parsetree = structure} = parser ~filename: path in
149
+ iterator.structure iterator structure |> ignore);
150
+ ! lenses
151
+ |> List. filter_map (fun (range : Protocol.range ) ->
152
+ match Cmt. fullFromPath ~path with
153
+ | None -> None
154
+ | Some full -> (
155
+ match
156
+ References. getLocItem ~full
157
+ ~pos: (range.start.line, range.start.character + 1 )
158
+ ~debug
159
+ with
160
+ | Some {locType = Typed (_ , typeExpr , _ )} ->
161
+ Some
162
+ (Protocol. stringifyCodeLens
163
+ {
164
+ range;
165
+ command =
166
+ Some
167
+ {
168
+ (* Code lenses can run commands. An empty command string means we just want the editor
169
+ to print the text, not link to running a command. *)
170
+ command = " " ;
171
+ (* Print the type with a huge line width, because the code lens always prints on a
172
+ single line in the editor. *)
173
+ title = typeExpr |> Shared. typeToString ~line Width:400 ;
174
+ };
175
+ })
176
+ | _ -> None ))
0 commit comments