From 0808ecefbe0bf7421a471f9e07274772dbed92d2 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 8 Apr 2021 19:33:31 -0400 Subject: [PATCH] fix: avoid exception prone `Hashtbl.find` --- src/server.ml | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/server.ml b/src/server.ml index 42168309..837d6100 100644 --- a/src/server.ml +++ b/src/server.ml @@ -171,26 +171,38 @@ module Make(IO : IO) = struct self#on_req_initialize ~notify_back i | Lsp.Client_request.TextDocumentHover { textDocument; position } -> let uri = textDocument.uri in - let doc_st = Hashtbl.find docs uri in - let notify_back = new notify_back ~uri ~notify_back () in - self#on_req_hover ~notify_back ~uri ~pos:position doc_st + begin match Hashtbl.find_opt docs uri with + | None -> IO.return None + | Some doc_st -> + let notify_back = new notify_back ~uri ~notify_back () in + self#on_req_hover ~notify_back ~uri ~pos:position doc_st + end | Lsp.Client_request.TextDocumentCompletion { textDocument; position; context } -> let uri = textDocument.uri in - let doc_st = Hashtbl.find docs uri in - let notify_back = new notify_back ~uri ~notify_back () in - self#on_req_completion ~notify_back ~uri - ~pos:position ~ctx:context doc_st + begin match Hashtbl.find_opt docs uri with + | None -> IO.return None + | Some doc_st -> + let notify_back = new notify_back ~uri ~notify_back () in + self#on_req_completion ~notify_back ~uri + ~pos:position ~ctx:context doc_st + end | Lsp.Client_request.TextDocumentDefinition { textDocument; position } -> let uri = textDocument.uri in let notify_back = new notify_back ~uri ~notify_back () in - let doc_st = Hashtbl.find docs uri in - self#on_req_definition ~notify_back - ~uri ~pos:position doc_st + begin match Hashtbl.find_opt docs uri with + | None -> IO.return None + | Some doc_st -> + self#on_req_definition ~notify_back + ~uri ~pos:position doc_st + end | Lsp.Client_request.TextDocumentCodeLens {textDocument} -> let uri = textDocument.uri in let notify_back = new notify_back ~uri ~notify_back () in - let doc_st = Hashtbl.find docs uri in - self#on_req_code_lens ~notify_back ~uri doc_st + begin match Hashtbl.find_opt docs uri with + | None -> IO.return [] + | Some doc_st -> + self#on_req_code_lens ~notify_back ~uri doc_st + end | Lsp.Client_request.TextDocumentCodeLensResolve cl -> let notify_back = new notify_back ~notify_back () in self#on_req_code_lens_resolve ~notify_back cl @@ -270,9 +282,9 @@ module Make(IO : IO) = struct self#on_notif_doc_did_close ~notify_back doc | Lsp.Client_notification.TextDocumentDidChange {textDocument=doc; contentChanges=c} -> let notify_back = new notify_back ~uri:doc.uri ~notify_back () in - begin match Hashtbl.find docs doc.uri with - | exception Not_found -> IO.failwith "unknown document" - | st -> + begin match Hashtbl.find_opt docs doc.uri with + | None -> IO.failwith "unknown document" + | Some st -> let old_content = st.content in let new_doc: Lsp.Text_document.t = let doc = Lsp.Text_document.make