|
| 1 | +open Stdune |
| 2 | +open Fiber.O |
| 3 | + |
| 4 | +let _PATH = |
| 5 | + Bin.parse_path (Option.value ~default:"" @@ Env.get Env.initial "PATH") |
| 6 | + |
| 7 | +let bin = Bin.which "ocamllsp" ~path:_PATH |> Option.value_exn |> Path.to_string |
| 8 | + |
| 9 | +let env = Spawn.Env.of_list [ "OCAMLLSP_TEST=true" ] |
| 10 | + |
| 11 | +module Client = Lsp_fiber.Client |
| 12 | +open Lsp.Types |
| 13 | + |
| 14 | +let%expect_test "start/stop" = |
| 15 | + let stdin_i, stdin_o = Unix.pipe ~cloexec:true () in |
| 16 | + let stdout_i, stdout_o = Unix.pipe ~cloexec:true () in |
| 17 | + let pid = |
| 18 | + Spawn.spawn ~env ~prog:bin ~argv:[ bin ] ~stdin:stdin_i ~stdout:stdout_o () |
| 19 | + in |
| 20 | + Unix.close stdin_i; |
| 21 | + Unix.close stdout_o; |
| 22 | + let handler = Client.Handler.make () in |
| 23 | + let init = |
| 24 | + let blockity = |
| 25 | + if Sys.win32 then `Blocking |
| 26 | + else ( |
| 27 | + Unix.set_nonblock stdout_i; |
| 28 | + Unix.set_nonblock stdin_o; |
| 29 | + `Non_blocking true) |
| 30 | + in |
| 31 | + let make fd what = |
| 32 | + let fd = Lev_fiber.Fd.create fd blockity in |
| 33 | + Lev_fiber.Io.create fd what |
| 34 | + in |
| 35 | + let* in_ = make stdout_i Input in |
| 36 | + let* out = make stdin_o Output in |
| 37 | + let io = Lsp_fiber.Fiber_io.make in_ out in |
| 38 | + let client = Client.make handler io () in |
| 39 | + let run_client () = |
| 40 | + let capabilities = ClientCapabilities.create () in |
| 41 | + Client.start client (InitializeParams.create ~capabilities ()) |
| 42 | + in |
| 43 | + let print_init = |
| 44 | + let+ resp = Client.initialized client in |
| 45 | + print_endline "client: server initialized with:"; |
| 46 | + InitializeResult.yojson_of_t resp |
| 47 | + |> Yojson.Safe.pretty_to_string ~std:false |
| 48 | + |> print_endline |
| 49 | + in |
| 50 | + let run = |
| 51 | + let* () = print_init in |
| 52 | + print_endline "client: shutting down server"; |
| 53 | + Client.request client Shutdown |
| 54 | + in |
| 55 | + Fiber.fork_and_join_unit run_client (fun () -> run >>> Client.stop client) |
| 56 | + in |
| 57 | + let waitpid = |
| 58 | + let+ (_ : Unix.process_status) = Lev_fiber.waitpid ~pid in |
| 59 | + () |
| 60 | + in |
| 61 | + Lev_fiber.run |
| 62 | + ~f:(fun () -> Fiber.all_concurrently_unit [ init; waitpid ]) |
| 63 | + (Lev.Loop.default ()); |
| 64 | + [%expect |
| 65 | + {| |
| 66 | + client: server initialized with: |
| 67 | + { |
| 68 | + "capabilities": { |
| 69 | + "textDocumentSync": { |
| 70 | + "openClose": true, |
| 71 | + "change": 2, |
| 72 | + "willSave": false, |
| 73 | + "willSaveWaitUntil": false, |
| 74 | + "save": true |
| 75 | + }, |
| 76 | + "completionProvider": { |
| 77 | + "triggerCharacters": [ ".", "#" ], |
| 78 | + "resolveProvider": true |
| 79 | + }, |
| 80 | + "hoverProvider": true, |
| 81 | + "signatureHelpProvider": { |
| 82 | + "triggerCharacters": [ " ", "~", "?", ":", "(" ] |
| 83 | + }, |
| 84 | + "declarationProvider": true, |
| 85 | + "definitionProvider": true, |
| 86 | + "typeDefinitionProvider": true, |
| 87 | + "referencesProvider": true, |
| 88 | + "documentHighlightProvider": true, |
| 89 | + "documentSymbolProvider": true, |
| 90 | + "codeActionProvider": { |
| 91 | + "codeActionKinds": [ |
| 92 | + "quickfix", "construct", "destruct", "inferred_intf", |
| 93 | + "put module name in identifiers", |
| 94 | + "remove module name from identifiers", "type-annotate" |
| 95 | + ] |
| 96 | + }, |
| 97 | + "codeLensProvider": { "resolveProvider": false }, |
| 98 | + "documentFormattingProvider": true, |
| 99 | + "renameProvider": { "prepareProvider": true }, |
| 100 | + "foldingRangeProvider": true, |
| 101 | + "executeCommandProvider": { "commands": [ "dune/promote" ] }, |
| 102 | + "selectionRangeProvider": true, |
| 103 | + "workspaceSymbolProvider": true, |
| 104 | + "workspace": { |
| 105 | + "workspaceFolders": { "supported": true, "changeNotifications": true } |
| 106 | + }, |
| 107 | + "experimental": { |
| 108 | + "ocamllsp": { |
| 109 | + "interfaceSpecificLangId": true, |
| 110 | + "handleSwitchImplIntf": true, |
| 111 | + "handleInferIntf": true, |
| 112 | + "handleTypedHoles": true, |
| 113 | + "handleWrappingAstNode": true, |
| 114 | + "diagnostic_promotions": true |
| 115 | + } |
| 116 | + } |
| 117 | + }, |
| 118 | + "serverInfo": { "name": "ocamllsp", "version": "dev" } |
| 119 | + } |
| 120 | + client: shutting down server |}] |
0 commit comments