Skip to content

refactor: change data in bsp.Diagnostic to contain a workspaceEdit #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,13 @@ trait Build extends ScalaCliSbtModule with ScalaCliPublishModule with HasTests
Deps.collectionCompat,
Deps.javaClassName,
Deps.jsoniterCore,
Deps.scalametaTrees,
Deps.lsp4j,
Deps.nativeTestRunner,
Deps.osLib,
Deps.pprint,
Deps.scalaJsEnvNodeJs,
Deps.scalaJsTestAdapter,
Deps.scalametaTrees,
Deps.swoval,
Deps.zipInputStream
)
Expand Down Expand Up @@ -775,6 +776,7 @@ trait Cli extends SbtModule with ProtoBuildModule with CliLaunchers
Deps.jniUtils,
Deps.jsoniterCore,
Deps.libsodiumjni,
Deps.lsp4j,
Deps.metaconfigTypesafe,
Deps.pythonNativeLibs,
Deps.scalaPackager,
Expand Down Expand Up @@ -856,6 +858,7 @@ trait CliIntegration extends SbtModule with ScalaCliPublishModule with HasTests
Deps.dockerClient,
Deps.jsoniterCore,
Deps.libsodiumjni,
Deps.lsp4j,
Deps.pprint,
Deps.scalaAsync,
Deps.slf4jNop,
Expand Down
26 changes: 18 additions & 8 deletions modules/build/src/main/scala/scala/build/bsp/BspClient.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package scala.build.bsp

import ch.epfl.scala.{bsp4j => b}
import org.eclipse.lsp4j.WorkspaceEdit
import org.eclipse.lsp4j as l

import java.lang.Boolean as JBoolean
import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.{ConcurrentHashMap, ExecutorService}

import scala.build.Position.File
import scala.build.bsp.protocol.TextEdit
import scala.build.bsp.protocol.*
import scala.build.errors.{BuildException, CompositeBuildException, Diagnostic, Severity}
import scala.build.postprocessing.LineConversion
import scala.build.{BloopBuildClient, GeneratedSource, Logger}
Expand Down Expand Up @@ -214,16 +216,24 @@ class BspClient(
)(diag: Diagnostic): Seq[os.Path] =
diag.positions.flatMap {
case File(Right(path), (startLine, startC), (endL, endC)) =>
val id = new b.TextDocumentIdentifier(path.toNIO.toUri.toASCIIString)
val startPos = new b.Position(startLine, startC)
val endPos = new b.Position(endL, endC)
val range = new b.Range(startPos, endPos)
val id = new b.TextDocumentIdentifier(path.toNIO.toUri.toASCIIString)
val bstartPos = new b.Position(startLine, startC)
val bendPos = new b.Position(endL, endC)
val brange = new b.Range(bstartPos, bendPos)
val bDiag =
new b.Diagnostic(range, diag.message)
new b.Diagnostic(brange, diag.message)

diag.textEdit.foreach { textEdit =>
val bTextEdit = TextEdit(range, textEdit.newText)
bDiag.setData(bTextEdit.toJsonTree())
val lstartPos = new l.Position(startLine, startC)
val lendPos = new l.Position(endL, endC)
val lrange = new l.Range(lstartPos, lendPos)
val lTextEdit = l.TextEdit(lrange, textEdit.newText)
val workspaceEdit = new WorkspaceEdit()
workspaceEdit.setChanges(
Map(path.toNIO.toUri().toString() -> List(lTextEdit).asJava).asJava
)
val data = DiagnosticData(edits = Array(workspaceEdit))
bDiag.setData(data.toJsonTree())
}
bDiag.setSeverity(diag.severity.toBsp4j)
bDiag.setSource("scala-cli")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package scala.build.bsp.protocol

import com.google.gson.{Gson, JsonElement}
import org.eclipse.lsp4j.WorkspaceEdit

/** Representation for the data field in a bsp.Diagnostic.
*
* For now this only contains an edit, but there is nothing from keeping it having more field in
* the future.
* @param edit
* The Workspace edit attatched to the Diagnostic.
*/
case class DiagnosticData(edits: Array[WorkspaceEdit]) {

/** Go from the DiagnosticData to Json. This will result in each param being a field in the
* object.
*
* @return
* the JsonElement
*/
def toJsonTree(): JsonElement = new Gson().toJsonTree(this)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,118 @@
}
]
},
{
"name": "org.eclipse.lsp4j.ChangeAnnotation",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.CreateFile",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.CreateFileOptions",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.DeleteFile",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.DeleteFileOptions",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.Position",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.Range",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.RenameFile",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.RenameFileOptions",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.ResourceOperation",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.TextDocumentEdit",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.TextEdit",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.WorkspaceEdit",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.adapters.DocumentChangeListAdapter",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.adapters.ResourceOperationTypeAdapter",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.adapters.VersionedTextDocumentIdentifierTypeAdapter$Factory",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "org.eclipse.lsp4j.jsonrpc.json.adapters.JsonElementTypeAdapter$Factory",
"allDeclaredConstructors": true,
Expand Down Expand Up @@ -1096,7 +1208,7 @@
"allDeclaredFields": true
},
{
"name": "scala.build.bsp.protocol.TextEdit",
"name": "scala.build.bsp.protocol.DiagnosticData",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
Expand Down
20 changes: 16 additions & 4 deletions modules/cli/src/main/scala/scala/cli/internal/CliLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import ch.epfl.scala.bsp4j.Location
import ch.epfl.scala.{bsp4j => b}
import coursier.cache.CacheLogger
import coursier.cache.loggers.{FallbackRefreshDisplay, RefreshLogger}
import org.eclipse.lsp4j.WorkspaceEdit
import org.eclipse.lsp4j as l
import org.scalajs.logging.{Level => ScalaJsLevel, Logger => ScalaJsLogger, ScalaConsoleLogger}

import java.io.PrintStream

import scala.build.bsp.protocol.TextEdit
import scala.build.bsp.protocol.*
import scala.build.errors.{BuildException, CompositeBuildException, Diagnostic, Severity}
import scala.build.internal.CustomProgressBarRefreshDisplay
import scala.build.{ConsoleBloopBuildClient, Logger, Position}
Expand Down Expand Up @@ -83,9 +85,19 @@ class CliLogger(
diag.setSeverity(severity.toBsp4j)
diag.setSource("scala-cli")

for (textEdit <- textEditOpt) {
val bTextEdit = TextEdit(range, textEdit.newText)
diag.setData(bTextEdit.toJsonTree())
(textEditOpt, f.path) match {
case (Some(textEdit), Right(path)) =>
val lstartPos = new l.Position(f.startPos._1, f.startPos._2)
val lendPos = new l.Position(f.endPos._1, f.endPos._2)
val lrange = new l.Range(lstartPos, lendPos)
val bTextEdit = l.TextEdit(lrange, textEdit.newText)
val workspaceEdit = new WorkspaceEdit()
workspaceEdit.setChanges(
Map(path.toNIO.toUri().toString() -> List(bTextEdit).asJava).asJava
)
val data = DiagnosticData(edits = Array(workspaceEdit))
diag.setData(data.toJsonTree())
case _ => ()
}

for (file <- f.path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.github.plokhotnyuk.jsoniter_scala.core.*
import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.google.gson.internal.LinkedTreeMap
import com.google.gson.{Gson, JsonElement}
import org.eclipse.lsp4j.WorkspaceEdit
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError
import org.eclipse.lsp4j as l

import java.net.URI
import java.nio.file.Paths
Expand Down Expand Up @@ -1293,16 +1295,21 @@ abstract class BspTestDefinitions(val scalaVersionOpt: Option[String])
strictlyCheckMessage = false
)

val textEdit = new Gson().fromJson[TextEdit](
val data = new Gson().fromJson[DiagnosticData](
updateActionableDiagnostic.getData().asInstanceOf[JsonElement],
classOf[TextEdit]
classOf[DiagnosticData]
)

expect(textEdit.newText.contains("com.lihaoyi::os-lib:"))
expect(textEdit.range.getStart.getLine == 0)
expect(textEdit.range.getStart.getCharacter == 15)
expect(textEdit.range.getEnd.getLine == 0)
expect(textEdit.range.getEnd.getCharacter == 40)
val edits = data.edits.head.getChanges().asScala
expect(edits.size == 1)

val (_, textEdits) = edits.head
val edit: l.TextEdit = textEdits.asScala.head
expect(edit.getNewText().contains("com.lihaoyi::os-lib:"))
expect(edit.getRange().getStart().getLine() == 0)
expect(edit.getRange().getStart().getCharacter() == 15)
expect(edit.getRange().getEnd().getLine() == 0)
expect(edit.getRange().getEnd().getCharacter() == 40)
}
}
}
Expand Down Expand Up @@ -1390,4 +1397,5 @@ object BspTestDefinitions {

private final case class TextEdit(range: b.Range, newText: String)

private final case class DiagnosticData(edits: Array[WorkspaceEdit])
}
2 changes: 2 additions & 0 deletions project/deps.sc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ object Deps {
def coursierM1Cli = "2.1.0-RC4"
def jsoniterScala = "2.20.3"
def jsoniterScalaJava8 = "2.13.5.2"
def lsp4j = "0.20.1"
def scalaMeta = "4.7.1"
def scalaNative = "0.4.12"
def scalaPackager = "0.1.29"
Expand Down Expand Up @@ -126,6 +127,7 @@ object Deps {
def jsoniterMacrosJava8 =
ivy"com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-macros:${Versions.jsoniterScalaJava8}"
def libsodiumjni = ivy"org.virtuslab.scala-cli:libsodiumjni:0.0.3"
def lsp4j = ivy"org.eclipse.lsp4j:org.eclipse.lsp4j:${Versions.lsp4j}"
def macroParadise = ivy"org.scalamacros:::paradise:2.1.1"
def metaconfigTypesafe =
ivy"com.geirsson::metaconfig-typesafe-config:0.11.1"
Expand Down