Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go-to-def): report error in response #899

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions ocaml-lsp-server/src/ocaml_lsp_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -590,22 +590,37 @@ let references (state : State.t)
(* using original uri because merlin is looking only in local file *)
{ Location.uri; range }))

let definition_query server (state : State.t) uri position merlin_request =
let definition_query kind (state : State.t) uri position =
let* () = Fiber.return () in
let doc = Document_store.get state.store uri in
match Document.kind doc with
| `Other -> Fiber.return None
| `Merlin doc -> (
let position = Position.logical position in
let command = merlin_request position in
let command =
let pos = Position.logical position in
match kind with
| `Definition -> Query_protocol.Locate (None, `ML, pos)
| `Declaration -> Query_protocol.Locate (None, `MLI, pos)
| `Type_definition -> Query_protocol.Locate_type pos
in
let* result = Document.Merlin.dispatch_exn doc command in
match location_of_merlin_loc uri result with
| Ok s -> Fiber.return s
| Error message ->
let+ () =
let message = sprintf "Locate failed. %s" message in
State.log_msg server ~type_:Error ~message
| Error err_msg ->
let kind =
match kind with
| `Definition -> "definition"
| `Declaration -> "declaration"
| `Type_definition -> "type definition"
in
None)
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:Jsonrpc.Response.Error.Code.RequestFailed
~message:(sprintf "Request \"Jump to %s\" failed." kind)
~data:
(`String
(sprintf "'Locate' query to merlin returned error: %s" err_msg))
()))

let workspace_symbol server (state : State.t) (params : WorkspaceSymbolParams.t)
=
Expand Down Expand Up @@ -807,22 +822,12 @@ let on_request :
| TextDocumentHighlight req -> later highlight req
| DocumentSymbol { textDocument = { uri }; _ } -> later document_symbol uri
| TextDocumentDeclaration { textDocument = { uri }; position } ->
later
(fun state () ->
definition_query rpc state uri position (fun pos ->
Query_protocol.Locate (None, `MLI, pos)))
()
later (fun state () -> definition_query `Declaration state uri position) ()
| TextDocumentDefinition { textDocument = { uri }; position; _ } ->
later
(fun state () ->
definition_query rpc state uri position (fun pos ->
Query_protocol.Locate (None, `ML, pos)))
()
later (fun state () -> definition_query `Definition state uri position) ()
| TextDocumentTypeDefinition { textDocument = { uri }; position; _ } ->
later
(fun state () ->
definition_query rpc state uri position (fun pos ->
Query_protocol.Locate_type pos))
(fun state () -> definition_query `Type_definition state uri position)
()
| TextDocumentCompletion params ->
later (fun _ () -> Compl.complete state params) ()
Expand Down