Skip to content

Commit

Permalink
Merge pull request #7142 from adpi2/bsp-java-diags
Browse files Browse the repository at this point in the history
Fix forwarding Java diagnostics through BSP
  • Loading branch information
adpi2 committed Feb 6, 2023
2 parents 3f1ff98 + ecc4469 commit 78f9b31
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 104 deletions.
49 changes: 22 additions & 27 deletions main/src/main/scala/sbt/internal/server/BuildServerReporter.scala
Expand Up @@ -108,7 +108,7 @@ final class BuildServerReporterImpl(
val hadProblems = bspCompileState.hasAnyProblems.remove(filePath)

val reportedProblems = infos.getReportedProblems.toVector
val diagnostics = reportedProblems.flatMap(toDiagnostic)
val diagnostics = reportedProblems.map(toDiagnostic)

// publish diagnostics if:
// 1. file had any problems previously - we might want to update them with new ones
Expand Down Expand Up @@ -165,9 +165,9 @@ final class BuildServerReporterImpl(
protected override def publishDiagnostic(problem: Problem): Unit = {
for {
id <- problem.position.sourcePath.toOption
diagnostic <- toDiagnostic(problem)
filePath <- toSafePath(VirtualFileRef.of(id))
} {
val diagnostic = toDiagnostic(problem)
problemsByFile(filePath) = problemsByFile.getOrElse(filePath, Vector.empty) :+ diagnostic
val params = PublishDiagnosticsParams(
TextDocumentIdentifier(filePath.toUri),
Expand All @@ -180,32 +180,27 @@ final class BuildServerReporterImpl(
}
}

private def toDiagnostic(problem: Problem): Option[Diagnostic] = {
private def toDiagnostic(problem: Problem): Diagnostic = {
val pos = problem.position
for {
line <- pos.line.toOption.map(_.toLong - 1L)
pointer <- pos.pointer.toOption.map(_.toLong)
} yield {
val range = (
pos.startLine.toOption,
pos.startColumn.toOption,
pos.endLine.toOption,
pos.endColumn.toOption
) match {
case (Some(sl), Some(sc), Some(el), Some(ec)) =>
Range(Position(sl.toLong - 1, sc.toLong), Position(el.toLong - 1, ec.toLong))
case _ =>
Range(Position(line, pointer), Position(line, pointer + 1))
}

Diagnostic(
range,
Option(toDiagnosticSeverity(problem.severity)),
problem.diagnosticCode().toOption.map(_.code),
Option("sbt"),
problem.message
)
}
val startLineOpt = pos.startLine.toOption.map(_.toLong - 1)
val startColumnOpt = pos.startColumn.toOption.map(_.toLong)
val endLineOpt = pos.endLine.toOption.map(_.toLong - 1)
val endColumnOpt = pos.endColumn.toOption.map(_.toLong)

def toPosition(lineOpt: Option[Long], columnOpt: Option[Long]): Option[Position] =
lineOpt.map(line => Position(line, columnOpt.getOrElse(0L)))

val startPos = toPosition(startLineOpt, startColumnOpt).getOrElse(Position(0L, 0L))
val endPosOpt = toPosition(endLineOpt, endColumnOpt)
val range = Range(startPos, endPosOpt.getOrElse(startPos))

Diagnostic(
range,
Option(toDiagnosticSeverity(problem.severity)),
problem.diagnosticCode().toOption.map(_.code),
Option("sbt"),
problem.message
)
}

private def toDiagnosticSeverity(severity: Severity): Long = severity match {
Expand Down
6 changes: 6 additions & 0 deletions server-test/src/server-test/buildserver/build.sbt
Expand Up @@ -36,6 +36,12 @@ lazy val util = project.settings(

lazy val diagnostics = project

lazy val javaProj = project
.in(file("java-proj"))
.settings(
javacOptions += "-Xlint:all"
)

def somethingBad = throw new MessageOnlyException("I am a bad build target")
// other build targets should not be affected by this bad build target
lazy val badBuildTarget = project.in(file("bad-build-target"))
Expand Down
@@ -0,0 +1,12 @@
package example;

import java.util.List;
import java.util.ArrayList;

class Hello {
public static void main(String[] args) {
List list = new ArrayList<String>();
String msg = 42;
System.out.println(msg);
}
}

0 comments on commit 78f9b31

Please sign in to comment.