chore: re-add 'runWithInputFile' to server

This commit is contained in:
Wojciech Nawrocki 2020-10-09 12:40:39 -04:00 committed by Sebastian Ullrich
parent 3cdcdac921
commit f13b703285
2 changed files with 33 additions and 11 deletions

View file

@ -307,17 +307,28 @@ partial def mainLoop : Unit → ServerM Unit
mainLoop ()
| _ => throw (userError "got invalid JSON-RPC message")
def initAndRunWorker (i o : FS.Stream) : IO Unit := do
def initAndRunWorker (i o e : FS.Stream) : IO Unit := do
-- TODO(WN): act in accordance with InitializeParams
_ ← Lsp.readLspRequestAs i "initialize" InitializeParams;
param ← Lsp.readLspNotificationAs i "textDocument/didOpen" DidOpenTextDocumentParams;
h ← FS.Handle.mk "fwlog.txt" FS.Mode.write false;
_ ← IO.setStderr (FS.Stream.ofHandle h);
_ ← IO.setStderr e; -- TODO(WN): use a stream var in WorkerM instead of global state
doc ← openDocument o param;
docRef ← IO.mkRef doc;
pendingRequestsRef ← IO.mkRef (RBMap.empty : PendingRequestMap);
runReader (mainLoop ()) (⟨i, o, docRef, pendingRequestsRef⟩ : ServerContext)
namespace Test
def runWorkerWithInputFile (fn : String) (searchPath : Option String) : IO Unit := do
o ← IO.getStdout;
e ← IO.getStderr;
FS.withFile fn FS.Mode.read (fun hFile => do
Lean.initSearchPath searchPath;
catch
(Lean.Server.initAndRunWorker (FS.Stream.ofHandle hFile) o e)
(fun err => e.putStrLn $ toString err))
end Test
end Server
end Lean
@ -327,6 +338,6 @@ o ← IO.getStdout;
e ← IO.getStderr;
Lean.initSearchPath;
catch
(Lean.Server.initAndRunWorker i o)
(fun err => e.putStrLn (toString err));
(Lean.Server.initAndRunWorker i o e)
(fun err => e.putStrLn $ toString err);
pure 0

View file

@ -140,7 +140,7 @@ abbrev FileWorkerMap := RBMap DocumentUri FileWorker (fun a b => Decidable.decid
structure ServerContext :=
(hIn hOut : FS.Stream)
(log : FS.Stream)
(hLog : FS.Stream)
(fileWorkersRef : IO.Ref FileWorkerMap)
/- We store these to pass them to workers. -/
(initParams : InitializeParams)
@ -162,7 +162,7 @@ def eraseFileWorker (uri : DocumentUri) : ServerM Unit :=
fun st => st.fileWorkersRef.modify (fun fileWorkers => fileWorkers.erase uri)
def log (msg : String) : ServerM Unit :=
fun st => st.log.putStrLn msg
fun st => st.hLog.putStrLn msg
-- TODO: this creates a long-running Task, which should be okay with upcoming API changes.
partial def fwdMsgAux (fw : FileWorker) (hOut : FS.Stream) : Unit → IO WorkerEvent
@ -401,7 +401,7 @@ catch
-- so that we can die in peace
(fun err => do shutdown; throw err)
def initAndRunWatchdog (i o : FS.Stream) : IO Unit := do
def initAndRunWatchdog (i o e : FS.Stream) : IO Unit := do
workerPath ← IO.getEnv "LEAN_WORKER_PATH";
appDir ← IO.appDir;
let workerPath := match workerPath with
@ -415,11 +415,22 @@ writeLspResponse o initRequest.id
serverInfo? := some { name := "Lean 4 server",
version? := "0.0.1" } : InitializeResult };
e ← IO.getStderr;
runReader
initAndRunWatchdogAux
(⟨i, o, e, fileWorkersRef, initRequest.param, workerPath⟩ : ServerContext)
namespace Test
def runWatchdogWithInputFile (fn : String) (searchPath : Option String) : IO Unit := do
o ← IO.getStdout;
e ← IO.getStderr;
FS.withFile fn FS.Mode.read (fun hFile => do
Lean.initSearchPath searchPath;
catch
(Lean.Server.initAndRunWatchdog (FS.Stream.ofHandle hFile) o e)
(fun err => e.putStrLn $ toString err))
end Test
end Server
end Lean
@ -431,6 +442,6 @@ o ← IO.getStdout;
e ← IO.getStderr;
Lean.initSearchPath;
catch
(Lean.Server.initAndRunWatchdog i o)
(fun err => e.putStrLn (toString err));
(Lean.Server.initAndRunWatchdog i o e)
(fun err => e.putStrLn $ toString err);
pure 0