From de690f4e41b14dc4b55e44c1ad4e5a43f3bdf0ff Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 9 Mar 2018 12:01:53 +0000 Subject: [PATCH 1/4] Collapse 1-line scaladocs --- .../server/LanguageServerProtocol.scala | 20 +++++-------------- .../scala/sbt/protocol/Serialization.scala | 10 ++-------- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala b/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala index 761073591..0083221b0 100644 --- a/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala +++ b/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala @@ -81,9 +81,7 @@ private[sbt] object LanguageServerProtocol { }) } -/** - * Implements Language Server Protocol . - */ +/** Implements Language Server Protocol . */ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => lazy val internalJsonProtocol = new InitializeOptionFormats with sjsonnew.BasicJsonProtocol {} @@ -136,9 +134,7 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => } } - /** - * Respond back to Language Server's client. - */ + /** Respond back to Language Server's client. */ private[sbt] def jsonRpcRespond[A: JsonFormat](event: A, execId: Option[String]): Unit = { val m = JsonRpcResponseMessage("2.0", execId, Option(Converter.toJson[A](event).get), None) @@ -146,9 +142,7 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => publishBytes(bytes) } - /** - * Respond back to Language Server's client. - */ + /** Respond back to Language Server's client. */ private[sbt] def jsonRpcRespondError(execId: Option[String], code: Long, message: String): Unit = { @@ -158,9 +152,7 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => publishBytes(bytes) } - /** - * Respond back to Language Server's client. - */ + /** Respond back to Language Server's client. */ private[sbt] def jsonRpcRespondError[A: JsonFormat](execId: Option[String], code: Long, message: String, @@ -171,9 +163,7 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => publishBytes(bytes) } - /** - * Notify to Language Server's client. - */ + /** Notify to Language Server's client. */ private[sbt] def jsonRpcNotify[A: JsonFormat](method: String, params: A): Unit = { val m = JsonRpcNotificationMessage("2.0", method, Option(Converter.toJson[A](params).get)) diff --git a/protocol/src/main/scala/sbt/protocol/Serialization.scala b/protocol/src/main/scala/sbt/protocol/Serialization.scala index 75b9e7c83..1a62e53f7 100644 --- a/protocol/src/main/scala/sbt/protocol/Serialization.scala +++ b/protocol/src/main/scala/sbt/protocol/Serialization.scala @@ -41,10 +41,7 @@ object Serialization { CompactPrinter(json).getBytes("UTF-8") } - /** - * This formats the message according to JSON-RPC. - * http://www.jsonrpc.org/specification - */ + /** This formats the message according to JSON-RPC. http://www.jsonrpc.org/specification */ private[sbt] def serializeResponseMessage(message: JsonRpcResponseMessage): Array[Byte] = { import sbt.internal.protocol.codec.JsonRPCProtocol._ val json: JValue = Converter.toJson[JsonRpcResponseMessage](message).get @@ -57,10 +54,7 @@ object Serialization { body).getBytes("UTF-8") } - /** - * This formats the message according to JSON-RPC. - * http://www.jsonrpc.org/specification - */ + /** This formats the message according to JSON-RPC. http://www.jsonrpc.org/specification */ private[sbt] def serializeNotificationMessage( message: JsonRpcNotificationMessage): Array[Byte] = { import sbt.internal.protocol.codec.JsonRPCProtocol._ From 268b5111ab143fdb1ba1ca138dfc6b9425768268 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 9 Mar 2018 12:02:45 +0000 Subject: [PATCH 2/4] Format LSP methods --- .../server/LanguageServerProtocol.scala | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala b/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala index 0083221b0..311eb405f 100644 --- a/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala +++ b/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala @@ -143,9 +143,11 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => } /** Respond back to Language Server's client. */ - private[sbt] def jsonRpcRespondError(execId: Option[String], - code: Long, - message: String): Unit = { + private[sbt] def jsonRpcRespondError( + execId: Option[String], + code: Long, + message: String, + ): Unit = { val e = JsonRpcResponseError(code, message, None) val m = JsonRpcResponseMessage("2.0", execId, None, Option(e)) val bytes = Serialization.serializeResponseMessage(m) @@ -153,10 +155,12 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => } /** Respond back to Language Server's client. */ - private[sbt] def jsonRpcRespondError[A: JsonFormat](execId: Option[String], - code: Long, - message: String, - data: A): Unit = { + private[sbt] def jsonRpcRespondError[A: JsonFormat]( + execId: Option[String], + code: Long, + message: String, + data: A, + ): Unit = { val e = JsonRpcResponseError(code, message, Option(Converter.toJson[A](data).get)) val m = JsonRpcResponseMessage("2.0", execId, None, Option(e)) val bytes = Serialization.serializeResponseMessage(m) From d3ef452a5f7288eb03205d0a13789f0fc2a0821e Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 9 Mar 2018 12:02:52 +0000 Subject: [PATCH 3/4] Extract jsonRpcRespondErrorImpl --- .../server/LanguageServerProtocol.scala | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala b/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala index 311eb405f..8dc514d40 100644 --- a/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala +++ b/main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala @@ -10,6 +10,7 @@ package internal package server import sjsonnew.JsonFormat +import sjsonnew.shaded.scalajson.ast.unsafe.JValue import sjsonnew.support.scalajson.unsafe.Converter import sbt.protocol.Serialization import sbt.protocol.{ SettingQuery => Q } @@ -143,16 +144,8 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => } /** Respond back to Language Server's client. */ - private[sbt] def jsonRpcRespondError( - execId: Option[String], - code: Long, - message: String, - ): Unit = { - val e = JsonRpcResponseError(code, message, None) - val m = JsonRpcResponseMessage("2.0", execId, None, Option(e)) - val bytes = Serialization.serializeResponseMessage(m) - publishBytes(bytes) - } + private[sbt] def jsonRpcRespondError(execId: Option[String], code: Long, message: String): Unit = + jsonRpcRespondErrorImpl(execId, code, message, None) /** Respond back to Language Server's client. */ private[sbt] def jsonRpcRespondError[A: JsonFormat]( @@ -160,8 +153,16 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self => code: Long, message: String, data: A, + ): Unit = + jsonRpcRespondErrorImpl(execId, code, message, Option(Converter.toJson[A](data).get)) + + private[this] def jsonRpcRespondErrorImpl( + execId: Option[String], + code: Long, + message: String, + data: Option[JValue], ): Unit = { - val e = JsonRpcResponseError(code, message, Option(Converter.toJson[A](data).get)) + val e = JsonRpcResponseError(code, message, data) val m = JsonRpcResponseMessage("2.0", execId, None, Option(e)) val bytes = Serialization.serializeResponseMessage(m) publishBytes(bytes) From 5f56fa9f14ad48014f2fc7ce686d9a336e672f66 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 9 Mar 2018 12:06:29 +0000 Subject: [PATCH 4/4] Extract serializeResponse --- .../scala/sbt/protocol/Serialization.scala | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/protocol/src/main/scala/sbt/protocol/Serialization.scala b/protocol/src/main/scala/sbt/protocol/Serialization.scala index 1a62e53f7..21d798b80 100644 --- a/protocol/src/main/scala/sbt/protocol/Serialization.scala +++ b/protocol/src/main/scala/sbt/protocol/Serialization.scala @@ -8,7 +8,7 @@ package sbt package protocol -import sjsonnew.JsonFormat +import sjsonnew.{ JsonFormat, JsonWriter } import sjsonnew.support.scalajson.unsafe.{ Parser, Converter, CompactPrinter } import sjsonnew.shaded.scalajson.ast.unsafe.{ JValue, JObject, JString } import java.nio.ByteBuffer @@ -44,28 +44,28 @@ object Serialization { /** This formats the message according to JSON-RPC. http://www.jsonrpc.org/specification */ private[sbt] def serializeResponseMessage(message: JsonRpcResponseMessage): Array[Byte] = { import sbt.internal.protocol.codec.JsonRPCProtocol._ - val json: JValue = Converter.toJson[JsonRpcResponseMessage](message).get - val body = CompactPrinter(json) - val bodyBytes = body.getBytes("UTF-8") - - (s"Content-Length: ${bodyBytes.size}\r\n" + - s"Content-Type: $VsCode\r\n" + - "\r\n" + - body).getBytes("UTF-8") + serializeResponse(message) } /** This formats the message according to JSON-RPC. http://www.jsonrpc.org/specification */ private[sbt] def serializeNotificationMessage( - message: JsonRpcNotificationMessage): Array[Byte] = { + message: JsonRpcNotificationMessage, + ): Array[Byte] = { import sbt.internal.protocol.codec.JsonRPCProtocol._ - val json: JValue = Converter.toJson[JsonRpcNotificationMessage](message).get - val body = CompactPrinter(json) - val bodyBytes = body.getBytes("UTF-8") + serializeResponse(message) + } - (s"Content-Length: ${bodyBytes.size}\r\n" + - s"Content-Type: $VsCode\r\n" + - "\r\n" + - body).getBytes("UTF-8") + private[sbt] def serializeResponse[A: JsonWriter](message: A): Array[Byte] = { + val json: JValue = Converter.toJson[A](message).get + val body = CompactPrinter(json) + val bodyLength = body.getBytes("UTF-8").length + + Iterator( + s"Content-Length: $bodyLength", + s"Content-Type: $VsCode", + "", + body + ).mkString("\r\n").getBytes("UTF-8") } /**