Skip to content

Commit

Permalink
Merge pull request #77 from alephium/trace_setting
Browse files Browse the repository at this point in the history
Enable trace setting (VSCode)
  • Loading branch information
simerplaha authored Jan 8, 2024
2 parents f310f90 + c28db5a commit d8a16e8
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.alephium.ralph.lsp.pc.workspace.build.error.ErrorUnknownFileType
import org.alephium.ralph.lsp.server
import org.alephium.ralph.lsp.server.RalphLangServer._
import org.alephium.ralph.lsp.server.converter.DiagnosticsConverter
import org.alephium.ralph.lsp.server.state.{ServerState, ServerStateUpdater}
import org.alephium.ralph.lsp.server.state.{ServerState, ServerStateUpdater, Trace}
import org.alephium.ralph.lsp.server.MessageMethods.{WORKSPACE_WATCHED_FILES, WORKSPACE_WATCHED_FILES_ID}
import org.eclipse.lsp4j._
import org.eclipse.lsp4j.jsonrpc.{messages, CompletableFutures}
Expand All @@ -32,6 +32,7 @@ object RalphLangServer {
workspace = None,
buildErrors = None,
clientAllowsWatchedFilesDynamicRegistration = false,
trace = Trace.Off,
shutdownReceived = false
)

Expand All @@ -47,6 +48,7 @@ object RalphLangServer {
workspace = None,
buildErrors = None,
clientAllowsWatchedFilesDynamicRegistration = false,
trace = Trace.Off,
shutdownReceived = false
)
)
Expand Down Expand Up @@ -140,6 +142,7 @@ class RalphLangServer private(@volatile private var state: ServerState)(implicit
getAllowsWatchedFilesDynamicRegistration(params)

setClientAllowsWatchedFilesDynamicRegistration(maybeDynamicRegistration)
setTraceSetting(params.getTrace)

cancelChecker.checkCanceled()

Expand Down Expand Up @@ -393,6 +396,18 @@ class RalphLangServer private(@volatile private var state: ServerState)(implicit
thisServer.state = thisServer.state.copy(clientAllowsWatchedFilesDynamicRegistration = allows)
}

/** Store client configure trace setting. */
private def setTraceSetting(traceValue: String): Unit =
runSync {
Trace(traceValue) match {
case Left(error) =>
getClient() show error

case Right(trace) =>
thisServer.state = thisServer.state.copy(trace = trace)
}
}

/**
* Set the workspace and returns diagnostics to publish for current state.
*
Expand Down Expand Up @@ -486,6 +501,10 @@ class RalphLangServer private(@volatile private var state: ServerState)(implicit
throw throwable
}

/** @inheritdoc */
override def setTrace(params: SetTraceParams): Unit =
setTraceSetting(params.getValue)

override def shutdown(): CompletableFuture[AnyRef] =
runSync {
logger.info("shutdown")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ object ResponseError {
message = s"Unknown file type: $fileURI"
)

case class InvalidTraceSetting(setting: String) extends
ResponseError(
errorCode = ResponseErrorCode.InvalidRequest,
message = s"Invalid trace setting: $setting"
)

case object ShutdownRequested extends
ResponseError(
errorCode = ResponseErrorCode.InvalidRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import java.util.concurrent.{Future => JFuture}
* - On successful build compilation, this gets set to [[None]] and the workspace is updated with the new build file.
* - If there are build errors, the workspace still handles requests using the most recent valid and compiled build file
* until this build file is error-free and successfully compiled.
* @param trace Client configured setting. See also [[org.eclipse.lsp4j.TraceValue]].
*/
case class ServerState(client: Option[RalphLangClient],
listener: Option[JFuture[Void]],
workspace: Option[WorkspaceState],
buildErrors: Option[BuildState.BuildErrored],
clientAllowsWatchedFilesDynamicRegistration: Boolean,
trace: Trace,
shutdownReceived: Boolean)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.alephium.ralph.lsp.server.state

import org.alephium.ralph.lsp.server.ResponseError.InvalidTraceSetting

/**
* @see [[org.eclipse.lsp4j.TraceValue]].
*/
sealed trait Trace
object Trace {
case object Off extends Trace
case object Messages extends Trace
case object Verbose extends Trace

def apply(string: String): Either[InvalidTraceSetting, Trace] =
if (string == null)
Right(Trace.Off)
else if (string equalsIgnoreCase Trace.Off.productPrefix)
Right(Trace.Off)
else if (string equalsIgnoreCase Trace.Messages.productPrefix)
Right(Trace.Messages)
else if (string equalsIgnoreCase Trace.Verbose.productPrefix)
Right(Trace.Verbose)
else
Left(InvalidTraceSetting(string))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.alephium.ralph.lsp.server
import org.alephium.ralph.lsp.access.compiler.CompilerAccess
import org.alephium.ralph.lsp.access.file.FileAccess
import org.alephium.ralph.lsp.pc.workspace.WorkspaceState
import org.alephium.ralph.lsp.server.state.ServerState
import org.alephium.ralph.lsp.server.state.{ServerState, Trace}
import org.eclipse.lsp4j._
import org.scalamock.scalatest.MockFactory
import org.scalatest.concurrent.ScalaFutures
Expand Down Expand Up @@ -49,6 +49,7 @@ class RalphLangServerSpec extends AnyWordSpec with Matchers with MockFactory wit
workspace = Some(WorkspaceState.Created(workspaceURI)),
buildErrors = None,
clientAllowsWatchedFilesDynamicRegistration = false,
trace = Trace.Off,
shutdownReceived = false
)
}
Expand Down
21 changes: 19 additions & 2 deletions plugin-vscode/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "ralph-lsp-client",
"name": "ralph-lsp",
"description": "LSP client for Ralph - The smart contract programming language for the Alephium blockchain",
"author": "Alephium",
"license": "LGPL-3.0",
"version": "0.0.1",
"publisher": "vscode",
"publisher": "Alephium",
"repository": {
"type": "git",
"url": "https://github.com/alephium/ralph-lsp"
Expand All @@ -18,6 +18,23 @@
"workspaceContains:ralph.json"
],
"main": "./out/extension",
"contributes": {
"configuration": {
"properties": {
"ralph-lsp.trace.server": {
"scope": "window",
"type": "string",
"enum": [
"off",
"messages",
"verbose"
],
"default": "off",
"description": "Traces the communication between VS Code and Ralph language server."
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -b",
Expand Down
4 changes: 2 additions & 2 deletions plugin-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function activate(context: ExtensionContext) {

// Create the client and store it.
client = new LanguageClient(
'ralph-lsp-client',
'LSP client for Ralph',
'ralph-lsp',
'Ralph LSP',
serverOptions,
clientOptions
);
Expand Down

0 comments on commit d8a16e8

Please sign in to comment.