chore: string formatting

This commit is contained in:
mhuisi 2020-12-20 18:05:11 +01:00 committed by Sebastian Ullrich
parent f656439f59
commit 883c197830
6 changed files with 31 additions and 29 deletions

View file

@ -79,7 +79,7 @@ def expect (s : String) : Quickparse Unit := fun it =>
if it.extract (it.forward s.length) = s then
success (it.forward s.length) ()
else
error it ("expected: " ++ s)
error it s!"expected: {s}"
@[inline]
def ws : Quickparse Unit := fun it =>
@ -311,7 +311,7 @@ namespace Json
def parse (s : String) : Except String Lean.Json :=
match Json.Parser.any s.mkIterator with
| Quickparse.Result.success _ res => Except.ok res
| Quickparse.Result.error it err => Except.error ("offset " ++ it.i.repr ++ ": " ++ err)
| Quickparse.Result.error it err => Except.error s!"offset {it.i.repr}: {err}"
end Json

View file

@ -71,10 +71,13 @@ partial def compress : Json → String
| bool false => "false"
| num s => s.toString
| str s => renderString s
| arr elems => "[" ++ ",".intercalate (elems.map compress).toList ++ "]"
| arr elems =>
let f := ",".intercalate (elems.map compress).toList
s!"[{f}]"
| obj kvs =>
let ckvs := kvs.fold (fun acc k j => (renderString k ++ ":" ++ compress j) :: acc) [];
"{" ++ ",".intercalate ckvs ++ "}"
let ckvs := kvs.fold (fun acc k j => s!"{renderString k}:{compress j}" :: acc) []
let ckvs := ",".intercalate ckvs
s!"{ckvs}"
instance : ToFormat Json := ⟨render⟩
instance : ToString Json := ⟨pretty⟩

View file

@ -197,7 +197,7 @@ section
let j ← h.readJson nBytes
match fromJson? j with
| some m => pure m
| none => throw $ userError ("JSON '" ++ j.compress ++ "' did not have the format of a JSON-RPC message")
| none => throw $ userError s!"JSON '{j.compress}' did not have the format of a JSON-RPC message"
def readRequestAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α) [FromJson α] : IO (Request α) := do
let m ← h.readMessage nBytes
@ -209,11 +209,11 @@ section
let j := toJson params
match fromJson? j with
| some v => pure ⟨id, expectedMethod, v⟩
| none => throw $ userError ("unexpected param '" ++ j.compress ++ "' for method '" ++ expectedMethod ++ "'")
| none => throw $ userError ("unexpected lack of param for method '" ++ expectedMethod ++ "'")
| none => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'"
| none => throw $ userError s!"Unexpected lack of param for method '{expectedMethod}'"
else
throw $ userError ("expected method '" ++ expectedMethod ++ "', got method '" ++ method ++ "'")
| _ => throw $ userError "expected request, got other type of message"
throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'"
| _ => throw $ userError "Expected request, got other type of message"
def readNotificationAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α) [FromJson α] : IO (Notification α) := do
let m ← h.readMessage nBytes
@ -225,11 +225,11 @@ section
let j := toJson params
match fromJson? j with
| some v => pure ⟨expectedMethod, v⟩
| none => throw $ userError ("unexpected param '" ++ j.compress ++ "' for method '" ++ expectedMethod ++ "'")
| none => throw $ userError ("unexpected lack of param for method '" ++ expectedMethod ++ "'")
| none => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'"
| none => throw $ userError s!"Unexpected lack of param for method '{expectedMethod}'"
else
throw $ userError ("expected method '" ++ expectedMethod ++ "', got method '" ++ method ++ "'")
| _ => throw $ userError "expected notification, got other type of message"
throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'"
| _ => throw $ userError "Expected notification, got other type of message"
def readResponseAs (h : FS.Stream) (nBytes : Nat) (expectedID : RequestID) (α) [FromJson α] : IO (Response α) := do
let m ← h.readMessage nBytes
@ -238,10 +238,10 @@ section
if id = expectedID then
match fromJson? result with
| some v => pure ⟨expectedID, v⟩
| none => throw $ userError s!"unexpected result '{result.compress}'"
| none => throw $ userError s!"Unexpected result '{result.compress}'"
else
throw $ userError s!"expected id {expectedID}, got id {id}"
| _ => throw $ userError "expected response, got other type of message"
throw $ userError s!"Expected id {expectedID}, got id {id}"
| _ => throw $ userError "Expected response, got other type of message"
end
section

View file

@ -34,7 +34,7 @@ section
| some hf =>
let tail ← readHeaderFields h
pure (hf :: tail)
| none => throw $ userError ("Invalid header field: " ++ l)
| none => throw $ userError s!"Invalid header field: {l}"
/-- Returns the Content-Length. -/
private def readLspHeader (h : FS.Stream) : IO Nat := do
@ -42,8 +42,8 @@ section
match fields.lookup "Content-Length" with
| some length => match length.toNat? with
| some n => pure n
| none => throw $ userError ("Content-Length header value '" ++ length ++ "' is not a Nat")
| none => throw $ userError ("No Content-Length header in header fields: " ++ toString fields)
| none => throw $ userError s!"Content-Length header value '{length}' is not a Nat"
| none => throw $ userError s!"No Content-Length header in header fields: {toString fields}"
def readLspMessage (h : FS.Stream) : IO Message := do
let nBytes ← readLspHeader h
@ -69,7 +69,7 @@ section
-- inlined implementation instead of using jsonrpc's writeMessage
-- to maintain the atomicity of putStr
let j := (toJson msg).compress
let header := "Content-Length: " ++ toString j.utf8ByteSize ++ "\r\n\r\n"
let header := s!"Content-Length: {toString j.utf8ByteSize}\r\n\r\n"
h.putStr (header ++ j)
h.flush

View file

@ -226,7 +226,7 @@ section MessageHandling
def parseParams (paramType : Type) [FromJson paramType] (params : Json) : ServerM paramType :=
match fromJson? params with
| some parsed => pure parsed
| none => throwServerError $ "Got param with wrong structure: " ++ params.compress
| none => throwServerError s!"Got param with wrong structure: {params.compress}"
def handleNotification (method : String) (params : Json) : ServerM Unit := do
let handle := fun paramType [FromJson paramType] (handler : paramType → ServerM Unit) =>
@ -234,7 +234,7 @@ section MessageHandling
match method with
| "textDocument/didChange" => handle DidChangeTextDocumentParams handleDidChange
| "$/cancelRequest" => handle CancelParams handleCancelRequest
| _ => throwServerError $ "Got unsupported notification method: " ++ method
| _ => throwServerError s!"Got unsupported notification method: {method}"
def queueRequest (id : RequestID) (handler : RequestID → α → ServerM Unit) (params : α)
: ServerM Unit := do
@ -249,8 +249,7 @@ section MessageHandling
match method with
| "textDocument/waitForDiagnostics" => handle WaitForDiagnosticsParam handleWaitForDiagnostics
| "textDocument/hover" => handle HoverParams handleHover
| _ => throwServerError $ "Got unsupported request: " ++ method ++
" params: " ++ toString params
| _ => throwServerError s!"Got unsupported request: {method}"
end MessageHandling
section MainLoop

View file

@ -164,7 +164,7 @@ section ServerM
def findFileWorker (uri : DocumentUri) : ServerM FileWorker := do
match (←(←read).fileWorkersRef.get).find? uri with
| some fw => fw
| none => throwServerError $ "Got unknown document URI (" ++ uri ++ ")"
| none => throwServerError s!"Got unknown document URI ({uri})"
def eraseFileWorker (uri : DocumentUri) : ServerM Unit := do
(←read).fileWorkersRef.modify (fun fileWorkers => fileWorkers.erase uri)
@ -334,7 +334,7 @@ section MessageHandling
def parseParams (paramType : Type) [FromJson paramType] (params : Json) : ServerM paramType :=
match fromJson? params with
| some parsed => pure parsed
| none => throwServerError $ "Got param with wrong structure: " ++ params.compress
| none => throwServerError s!"Got param with wrong structure: {params.compress}"
def handleRequest (id : RequestID) (method : String) (params : Json) : ServerM Unit := do
let handle := fun α [FromJson α] [ToJson α] [FileSource α] => do
@ -344,7 +344,7 @@ section MessageHandling
match method with
| "textDocument/waitForDiagnostics" => handle WaitForDiagnosticsParam
| "textDocument/hover" => handle HoverParams
| _ => throwServerError $ "Got unsupported request: " ++ method ++ " params: " ++ toString params
| _ => throwServerError s!"Got unsupported request: {method}"
def handleNotification (method : String) (params : Json) : ServerM Unit :=
let handle := (fun α [FromJson α] (handler : α → ServerM Unit) => parseParams α params >>= handler)
@ -491,7 +491,7 @@ partial def collectDiagnostics (waitForDiagnosticsId : RequestID := 0) (target :
: WatchdogM (List (Notification PublishDiagnosticsParams)) := do
writeRequest ⟨waitForDiagnosticsId, "textDocument/waitForDiagnostics", WaitForDiagnosticsParam.mk target⟩
let rec loop : WatchdogM (List (Notification PublishDiagnosticsParams)) := do
let error : IO (List (Notification PublishDiagnosticsParams)) := throw $ userError "got unexpected packet while collecting diagnostics"
let error : IO (List (Notification PublishDiagnosticsParams)) := throw $ userError "Got unexpected packet while collecting diagnostics"
match ←(←stdout).readLspMessage with
| Message.response id _ =>
if id = waitForDiagnosticsId then