Skip to content

Commit

Permalink
fix: diagnostics on non dune files
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

ps-id: e9c8ae3f-33d9-4cfe-88f3-36008b722c2c
  • Loading branch information
rgrinberg committed Oct 20, 2022
1 parent 664a1a9 commit 235b067
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
10 changes: 8 additions & 2 deletions ocaml-lsp-server/src/document.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ let syntax = function
| Other t -> t.syntax

let timer = function
| Other _ -> Code_error.raise "Document.dune" []
| Merlin m -> m.timer
| Other _ as t ->
Code_error.raise
"Document.timer"
[ ("t", Dyn.string @@ DocumentUri.to_string @@ uri t) ]

let text t = Text_document.text (tdoc t)

Expand Down Expand Up @@ -158,7 +161,10 @@ let await task =

let with_pipeline (t : t) f =
match t with
| Other _ -> Code_error.raise "Document.dune" []
| Other _ ->
Code_error.raise
"Document.with_pipeline"
[ ("t", Dyn.string @@ DocumentUri.to_string @@ uri t) ]
| Merlin t -> (
let* pipeline = Lazy_fiber.force t.pipeline in
let* task =
Expand Down
65 changes: 35 additions & 30 deletions ocaml-lsp-server/src/ocaml_lsp_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -150,38 +150,43 @@ let ocamlmerlin_reason = "ocamlmerlin-reason"

let set_diagnostics detached diagnostics doc =
let uri = Document.uri doc in
let async send =
let+ () =
task_if_running detached ~f:(fun () ->
let timer = Document.timer doc in
let* () = Lev_fiber.Timer.Wheel.cancel timer in
let* () = Lev_fiber.Timer.Wheel.reset timer in
let* res = Lev_fiber.Timer.Wheel.await timer in
match res with
| `Cancelled -> Fiber.return ()
| `Ok -> send ())
in
()
in
match Document.syntax doc with
| Dune | Cram | Menhir | Ocamllex -> Fiber.return ()
| Reason when Option.is_none (Bin.which ocamlmerlin_reason) ->
let no_reason_merlin =
let message =
sprintf "Could not detect %s. Please install reason" ocamlmerlin_reason
match Document.kind doc with
| `Other -> Fiber.return ()
| `Merlin _ -> (
let async send =
let+ () =
task_if_running detached ~f:(fun () ->
let timer = Document.timer doc in
let* () = Lev_fiber.Timer.Wheel.cancel timer in
let* () = Lev_fiber.Timer.Wheel.reset timer in
let* res = Lev_fiber.Timer.Wheel.await timer in
match res with
| `Cancelled -> Fiber.return ()
| `Ok -> send ())
in
Diagnostic.create
~source:Diagnostics.ocamllsp_source
~range:Range.first_line
~message
()
()
in
Diagnostics.set diagnostics (`Merlin (uri, [ no_reason_merlin ]));
async (fun () -> Diagnostics.send diagnostics (`One uri))
| Reason | Ocaml ->
async (fun () ->
let* () = Diagnostics.merlin_diagnostics diagnostics doc in
Diagnostics.send diagnostics (`One uri))
match Document.syntax doc with
| Dune | Cram | Menhir | Ocamllex -> Fiber.return ()
| Reason when Option.is_none (Bin.which ocamlmerlin_reason) ->
let no_reason_merlin =
let message =
sprintf
"Could not detect %s. Please install reason"
ocamlmerlin_reason
in
Diagnostic.create
~source:Diagnostics.ocamllsp_source
~range:Range.first_line
~message
()
in
Diagnostics.set diagnostics (`Merlin (uri, [ no_reason_merlin ]));
async (fun () -> Diagnostics.send diagnostics (`One uri))
| Reason | Ocaml ->
async (fun () ->
let* () = Diagnostics.merlin_diagnostics diagnostics doc in
Diagnostics.send diagnostics (`One uri)))

let on_initialize server (ip : InitializeParams.t) =
let state : State.t = Server.state server in
Expand Down

0 comments on commit 235b067

Please sign in to comment.