Merge pull request #3989 from dwijnand/cleanup-lsp

Cleanup some parts of the LSP impl
This commit is contained in:
Dale Wijnand 2018-03-15 13:57:44 +00:00 committed by GitHub
commit 933668076a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 54 deletions

View File

@ -10,6 +10,7 @@ package internal
package server package server
import sjsonnew.JsonFormat import sjsonnew.JsonFormat
import sjsonnew.shaded.scalajson.ast.unsafe.JValue
import sjsonnew.support.scalajson.unsafe.Converter import sjsonnew.support.scalajson.unsafe.Converter
import sbt.protocol.Serialization import sbt.protocol.Serialization
import sbt.protocol.{ SettingQuery => Q } import sbt.protocol.{ SettingQuery => Q }
@ -81,9 +82,7 @@ private[sbt] object LanguageServerProtocol {
}) })
} }
/** /** Implements Language Server Protocol <https://github.com/Microsoft/language-server-protocol>. */
* Implements Language Server Protocol <https://github.com/Microsoft/language-server-protocol>.
*/
private[sbt] trait LanguageServerProtocol extends CommandChannel { self => private[sbt] trait LanguageServerProtocol extends CommandChannel { self =>
lazy val internalJsonProtocol = new InitializeOptionFormats with sjsonnew.BasicJsonProtocol {} lazy val internalJsonProtocol = new InitializeOptionFormats with sjsonnew.BasicJsonProtocol {}
@ -136,9 +135,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 = { private[sbt] def jsonRpcRespond[A: JsonFormat](event: A, execId: Option[String]): Unit = {
val m = val m =
JsonRpcResponseMessage("2.0", execId, Option(Converter.toJson[A](event).get), None) JsonRpcResponseMessage("2.0", execId, Option(Converter.toJson[A](event).get), None)
@ -146,34 +143,32 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel { self =>
publishBytes(bytes) 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 =
*/ jsonRpcRespondErrorImpl(execId, code, message, None)
private[sbt] def jsonRpcRespondError(execId: Option[String],
code: Long, /** Respond back to Language Server's client. */
message: String): Unit = { private[sbt] def jsonRpcRespondError[A: JsonFormat](
val e = JsonRpcResponseError(code, message, None) execId: Option[String],
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, data)
val m = JsonRpcResponseMessage("2.0", execId, None, Option(e)) val m = JsonRpcResponseMessage("2.0", execId, None, Option(e))
val bytes = Serialization.serializeResponseMessage(m) val bytes = Serialization.serializeResponseMessage(m)
publishBytes(bytes) publishBytes(bytes)
} }
/** /** Notify 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,
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)
publishBytes(bytes)
}
/**
* Notify to Language Server's client.
*/
private[sbt] def jsonRpcNotify[A: JsonFormat](method: String, params: A): Unit = { private[sbt] def jsonRpcNotify[A: JsonFormat](method: String, params: A): Unit = {
val m = val m =
JsonRpcNotificationMessage("2.0", method, Option(Converter.toJson[A](params).get)) JsonRpcNotificationMessage("2.0", method, Option(Converter.toJson[A](params).get))

View File

@ -8,7 +8,7 @@
package sbt package sbt
package protocol package protocol
import sjsonnew.JsonFormat import sjsonnew.{ JsonFormat, JsonWriter }
import sjsonnew.support.scalajson.unsafe.{ Parser, Converter, CompactPrinter } import sjsonnew.support.scalajson.unsafe.{ Parser, Converter, CompactPrinter }
import sjsonnew.shaded.scalajson.ast.unsafe.{ JValue, JObject, JString } import sjsonnew.shaded.scalajson.ast.unsafe.{ JValue, JObject, JString }
import java.nio.ByteBuffer import java.nio.ByteBuffer
@ -41,37 +41,31 @@ object Serialization {
CompactPrinter(json).getBytes("UTF-8") 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] = { private[sbt] def serializeResponseMessage(message: JsonRpcResponseMessage): Array[Byte] = {
import sbt.internal.protocol.codec.JsonRPCProtocol._ import sbt.internal.protocol.codec.JsonRPCProtocol._
val json: JValue = Converter.toJson[JsonRpcResponseMessage](message).get serializeResponse(message)
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")
} }
/** /** 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( private[sbt] def serializeNotificationMessage(
message: JsonRpcNotificationMessage): Array[Byte] = { message: JsonRpcNotificationMessage,
): Array[Byte] = {
import sbt.internal.protocol.codec.JsonRPCProtocol._ import sbt.internal.protocol.codec.JsonRPCProtocol._
val json: JValue = Converter.toJson[JsonRpcNotificationMessage](message).get serializeResponse(message)
val body = CompactPrinter(json) }
val bodyBytes = body.getBytes("UTF-8")
(s"Content-Length: ${bodyBytes.size}\r\n" + private[sbt] def serializeResponse[A: JsonWriter](message: A): Array[Byte] = {
s"Content-Type: $VsCode\r\n" + val json: JValue = Converter.toJson[A](message).get
"\r\n" + val body = CompactPrinter(json)
body).getBytes("UTF-8") val bodyLength = body.getBytes("UTF-8").length
Iterator(
s"Content-Length: $bodyLength",
s"Content-Type: $VsCode",
"",
body
).mkString("\r\n").getBytes("UTF-8")
} }
/** /**