Skip to content

Commit a8e770c

Browse files
committed
handle exec as "sbt/exec"
1 parent f188e60 commit a8e770c

File tree

7 files changed

+85
-18
lines changed

7 files changed

+85
-18
lines changed

main/src/main/scala/sbt/internal/server/LanguageServerProtocol.scala

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel {
3030
import sbt.internal.langserver.codec.JsonProtocol._
3131
import internalJsonProtocol._
3232

33+
def json =
34+
request.params.getOrElse(
35+
throw LangServerError(ErrorCodes.InvalidParams,
36+
s"param is expected on '${request.method}' method."))
3337
log.debug(s"onRequestMessage: $request")
3438
request.method match {
3539
case "initialize" =>
3640
if (authOptions(ServerAuthentication.Token)) {
37-
val json =
38-
request.params.getOrElse(
39-
throw LangServerError(ErrorCodes.InvalidParams,
40-
"param is expected on 'initialize' method."))
4141
val param = Converter.fromJson[InitializeParams](json).get
4242
val optionJson = param.initializationOptions.getOrElse(
4343
throw LangServerError(ErrorCodes.InvalidParams,
@@ -51,6 +51,9 @@ private[sbt] trait LanguageServerProtocol extends CommandChannel {
5151
langRespond(InitializeResult(serverCapabilities), Option(request.id))
5252
case "textDocument/didSave" =>
5353
append(Exec("compile", Some(request.id), Some(CommandSource(name))))
54+
case "sbt/exec" =>
55+
val param = Converter.fromJson[SbtExecParams](json).get
56+
append(Exec(param.commandLine, Some(request.id), Some(CommandSource(name))))
5457
case _ => ()
5558
}
5659
}

main/src/main/scala/sbt/internal/server/NetworkChannel.scala

+4-10
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,11 @@ final class NetworkChannel(val name: String,
102102
tillEndOfLine match {
103103
case Some(chunk) =>
104104
chunk.headOption match {
105-
case None => // ignore blank line
105+
case None => // ignore blank line
106106
case Some(Curly) =>
107-
Serialization
108-
.deserializeCommand(chunk)
109-
.fold(
110-
errorDesc =>
111-
log.error(
112-
s"Got invalid chunk from client (${new String(chunk.toArray, "UTF-8")}): " + errorDesc),
113-
onCommand
114-
)
115-
resetChannelState()
107+
// When Content-Length header is not found, interpret the line as JSON message.
108+
handleBody(chunk)
109+
process()
116110
case Some(_) =>
117111
val str = (new String(chunk.toArray, "UTF-8")).trim
118112
handleHeader(str) match {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
3+
*/
4+
5+
// DO NOT EDIT MANUALLY
6+
package sbt.internal.langserver
7+
/** Command to execute sbt command. */
8+
final class SbtExecParams private (
9+
val commandLine: String) extends Serializable {
10+
11+
12+
13+
override def equals(o: Any): Boolean = o match {
14+
case x: SbtExecParams => (this.commandLine == x.commandLine)
15+
case _ => false
16+
}
17+
override def hashCode: Int = {
18+
37 * (37 * (17 + "sbt.internal.langserver.SbtExecParams".##) + commandLine.##)
19+
}
20+
override def toString: String = {
21+
"SbtExecParams(" + commandLine + ")"
22+
}
23+
protected[this] def copy(commandLine: String = commandLine): SbtExecParams = {
24+
new SbtExecParams(commandLine)
25+
}
26+
def withCommandLine(commandLine: String): SbtExecParams = {
27+
copy(commandLine = commandLine)
28+
}
29+
}
30+
object SbtExecParams {
31+
32+
def apply(commandLine: String): SbtExecParams = new SbtExecParams(commandLine)
33+
}

protocol/src/main/contraband-scala/sbt/internal/langserver/codec/JsonProtocol.scala

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ trait JsonProtocol extends sjsonnew.BasicJsonProtocol
1717
with sbt.internal.langserver.codec.ServerCapabilitiesFormats
1818
with sbt.internal.langserver.codec.InitializeResultFormats
1919
with sbt.internal.langserver.codec.PublishDiagnosticsParamsFormats
20+
with sbt.internal.langserver.codec.SbtExecParamsFormats
2021
object JsonProtocol extends JsonProtocol
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
3+
*/
4+
5+
// DO NOT EDIT MANUALLY
6+
package sbt.internal.langserver.codec
7+
import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError }
8+
trait SbtExecParamsFormats { self: sjsonnew.BasicJsonProtocol =>
9+
implicit lazy val SbtExecParamsFormat: JsonFormat[sbt.internal.langserver.SbtExecParams] = new JsonFormat[sbt.internal.langserver.SbtExecParams] {
10+
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): sbt.internal.langserver.SbtExecParams = {
11+
jsOpt match {
12+
case Some(js) =>
13+
unbuilder.beginObject(js)
14+
val commandLine = unbuilder.readField[String]("commandLine")
15+
unbuilder.endObject()
16+
sbt.internal.langserver.SbtExecParams(commandLine)
17+
case None =>
18+
deserializationError("Expected JsObject but found None")
19+
}
20+
}
21+
override def write[J](obj: sbt.internal.langserver.SbtExecParams, builder: Builder[J]): Unit = {
22+
builder.beginObject()
23+
builder.addField("commandLine", obj.commandLine)
24+
builder.endObject()
25+
}
26+
}
27+
}

protocol/src/main/contraband/lsp.contra

+7
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,10 @@ type PublishDiagnosticsParams {
110110
## An array of diagnostic information items.
111111
diagnostics: [sbt.internal.langserver.Diagnostic]
112112
}
113+
114+
# sbt extension
115+
116+
## Command to execute sbt command.
117+
type SbtExecParams {
118+
commandLine: String!
119+
}

server.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

2-
3-
### ExecCommand
2+
### initialize
43

54
```json
6-
{ "type": "ExecCommand", "commandLine": "compile" }
5+
{ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "initializationOptions": { "token": "************" } } }
76
```
87

8+
### ExecCommand
9+
910
```json
10-
{ "type": "ExecCommand", "commandLine": "eval Thread.sleep(10000)" }
11+
{ "jsonrpc": "2.0", "id": 1, "method": "sbt/exec", "params": { "commandLine": "compile" } }
1112
```
13+

0 commit comments

Comments
 (0)