Skip to content

Commit b76d042

Browse files
committed
refactor: simplify merlin diagnostics
Updating the document with no changes is no longer needed because we don't reuse the pipeline. Signed-off-by: Rudi Grinberg <me@rgrinberg.com> <!-- ps-id: a79143e1-01bc-4565-b900-ee5d97474ced -->
1 parent 2d216d5 commit b76d042

File tree

5 files changed

+17
-42
lines changed

5 files changed

+17
-42
lines changed

ocaml-lsp-server/src/document.ml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,15 @@ module Single_pipeline : sig
128128
-> f:(Mpipeline.t -> 'a)
129129
-> ('a, Exn_with_backtrace.t) result Fiber.t
130130
end = struct
131-
type t =
132-
{ thread : Lev_fiber.Thread.t
133-
; mutable last : (Text_document.t * Mconfig.t * Mpipeline.t) option
134-
}
131+
type t = { thread : Lev_fiber.Thread.t }
135132

136-
let create thread = { thread; last = None }
133+
let create thread = { thread }
137134

138135
let use t ~doc ~config ~f =
139136
let* config = Merlin_config.config config in
140137
let make_pipeline =
141-
match t.last with
142-
| Some (doc', config', pipeline) when doc' == doc && config == config' ->
143-
fun () -> pipeline
144-
| _ ->
145-
let source = Msource.make (Text_document.text doc) in
146-
fun () -> Mpipeline.make config source
138+
let source = Msource.make (Text_document.text doc) in
139+
fun () -> Mpipeline.make config source
147140
in
148141
let task =
149142
match
@@ -152,15 +145,15 @@ end = struct
152145
let pipeline = make_pipeline () in
153146
let res = Mpipeline.with_pipeline pipeline (fun () -> f pipeline) in
154147
let stop = Unix.time () in
155-
(res, pipeline, start, stop))
148+
(res, start, stop))
156149
with
157150
| Error `Stopped -> assert false
158151
| Ok task -> task
159152
in
160153
let* res = await task in
161154
match res with
162155
| Error exn -> Fiber.return (Error exn)
163-
| Ok (res, pipeline, start, stop) ->
156+
| Ok (res, start, stop) ->
164157
let event =
165158
let module Event = Chrome_trace.Event in
166159
let dur = Event.Timestamp.of_float_seconds (stop -. start) in
@@ -172,7 +165,6 @@ end = struct
172165
in
173166
Event.complete ~dur fields
174167
in
175-
t.last <- Some (doc, config, pipeline);
176168
let+ () = Metrics.report event in
177169
Ok res
178170
end

ocaml-lsp-server/src/document_store.ml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,12 @@ let get_semantic_tokens_cache : t -> Uri.t -> semantic_tokens_cache option =
190190
let doc = get' t uri in
191191
doc.semantic_tokens_cache
192192

193-
let change_all t ~f =
194-
let all =
195-
Table.foldi ~init:[] t.db ~f:(fun uri doc acc -> (uri, doc) :: acc)
196-
in
197-
Fiber.parallel_iter all ~f:(fun (uri, doc) ->
198-
let+ doc =
199-
match doc.document with
200-
| None -> Fiber.return doc
201-
| Some document ->
202-
let+ document = f document in
203-
{ doc with document = Some document }
204-
in
205-
Table.set t.db uri doc)
193+
let parallel_iter t ~f =
194+
let all = Table.fold ~init:[] t.db ~f:(fun doc acc -> doc :: acc) in
195+
Fiber.parallel_iter all ~f:(fun doc ->
196+
match doc.document with
197+
| None -> Fiber.return ()
198+
| Some document -> f document)
206199

207200
let fold t ~init ~f =
208201
Table.fold t.db ~init ~f:(fun doc acc ->

ocaml-lsp-server/src/document_store.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ val close_document : t -> Uri.t -> unit Fiber.t
3030

3131
val fold : t -> init:'acc -> f:(Document.t -> 'acc -> 'acc) -> 'acc
3232

33-
val change_all : t -> f:(Document.t -> Document.t Fiber.t) -> unit Fiber.t
33+
val parallel_iter : t -> f:(Document.t -> unit Fiber.t) -> unit Fiber.t
3434

3535
val close_all : t -> unit Fiber.t

ocaml-lsp-server/src/dune.ml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,11 @@ end = struct
263263
match p with
264264
| Failed | Interrupted | Success ->
265265
let* () =
266-
Document_store.change_all document_store ~f:(fun doc ->
266+
Document_store.parallel_iter document_store ~f:(fun doc ->
267267
match Document.kind doc with
268-
| `Other -> Fiber.return doc
268+
| `Other -> Fiber.return ()
269269
| `Merlin merlin ->
270-
let doc = Document.update_text doc [] in
271-
let+ () =
272-
Diagnostics.merlin_diagnostics diagnostics merlin
273-
in
274-
doc)
270+
Diagnostics.merlin_diagnostics diagnostics merlin)
275271
in
276272
Diagnostics.send diagnostics `All
277273
| _ -> Fiber.return ())

ocaml-lsp-server/src/ocaml_lsp_server.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,7 @@ let on_notification server (notification : Client_notification.t) :
758758
( Log.log ~section:"on receive DidSaveTextDocument" @@ fun () ->
759759
Log.msg "saved document is not in the store" [] );
760760
Fiber.return state
761-
| Some _ ->
762-
let doc =
763-
Document_store.change_document store uri ~f:(fun doc ->
764-
(* we need [update_text] with no changes to get a new merlin
765-
pipeline; otherwise the diagnostics don't get updated *)
766-
Document.update_text doc [])
767-
in
761+
| Some doc ->
768762
let+ () = set_diagnostics state.detached (State.diagnostics state) doc in
769763
state)
770764
| ChangeWorkspaceFolders change ->

0 commit comments

Comments
 (0)