Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 1 addition & 4 deletions ocaml-lsp-server/src/dune.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ module Poll =
| s -> Ok (`Mtime s.st_mtime)
;;

let read_file s =
Fiber.of_thunk (fun () ->
Fiber.return (Result.try_with (fun () -> Io.String_path.read_file s)))
;;
let read_file s = Fiber.of_thunk (fun () -> Fiber.return (Io.read_file s))
end)

type config =
Expand Down
26 changes: 25 additions & 1 deletion ocaml-lsp-server/src/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ include struct
module Table = Table
module Tuple = Tuple
module Unix_env = Env
module Io = Io
module Map = Map
module Monoid = Monoid
module Pid = Pid
Expand All @@ -18,6 +17,31 @@ include struct
let sprintf = sprintf
end

module Io = struct
open Base

let read_file f =
Base.Result.try_with (fun () ->
let fd = Unix.openfile f [ O_CLOEXEC; O_RDONLY ] 0 in
Exn.protect
~finally:(fun () -> Unix.close fd)
~f:(fun () ->
match Unix.fstat fd with
| { Unix.st_size; _ } ->
let buf = Bytes.create st_size in
let rec loop pos remains =
if remains > 0
then (
let read = Unix.read fd buf pos remains in
if read = 0
then failwith (sprintf "unable to read all of %s" f)
else loop (pos + read) (remains - read))
in
loop 0 st_size;
Stdlib.Bytes.unsafe_to_string buf))
;;
end

include struct
open Base
module Queue = Queue
Expand Down
6 changes: 3 additions & 3 deletions ocaml-lsp-server/src/inference.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ let language_id_of_fname s =
let open_document_from_file (state : State.t) uri =
let filename = Uri.to_path uri in
Fiber.of_thunk (fun () ->
match Io.String_path.read_file filename with
| exception Sys_error _ ->
match Io.read_file filename with
| Error _ ->
Log.log ~section:"debug" (fun () ->
Log.msg "Unable to open file" [ "filename", `String filename ]);
Fiber.return None
| text ->
| Ok text ->
let languageId = language_id_of_fname filename in
let text_document = TextDocumentItem.create ~uri ~languageId ~version:0 ~text in
let params = DidOpenTextDocumentParams.create ~textDocument:text_document in
Expand Down
Loading