Skip to content

Commit 0812d33

Browse files
authored
NIT Misc scaladocs & refactors (#3106)
* NIT: add scaladoc for PersistentDiagnosticLogger * Remove ExecutorService from BspClient, since it was used for extracting line offset from generated sources, with the current implementation we don't read the files so no need for more threads. Original code's purpose can be seen in the commit that introduces it: 4297b07. * Nit: Add scaladoc for GeneratedSource * Nit: Simplify code in HasGeneratedSources * Apply scalafix * Bring back the needed line that updates the copy of the diagnostic
1 parent 98e98b6 commit 0812d33

File tree

5 files changed

+90
-100
lines changed

5 files changed

+90
-100
lines changed

modules/build/src/main/scala/scala/build/GeneratedSource.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ package scala.build
22

33
import scala.build.internal.WrapperParams
44

5+
/** Represents a source that's not originally in the user's workspace, yet it's a part of the
6+
* project. It can either be synthetically generated by Scala CLI, e.g. BuildInfo or just modified,
7+
* e.g. script wrappers
8+
*
9+
* @param generated
10+
* path to the file created by Scala CLI
11+
* @param reportingPath
12+
* the origin of the source:
13+
* - Left(String): there's no path that corresponds to the source it may be a snippet or a gist
14+
* etc.
15+
* - Right(os.Path): this source has been generated based on a file at this path
16+
* @param wrapperParamsOpt
17+
* if the generated source is a script wrapper then the params are present here
18+
*/
519
final case class GeneratedSource(
620
generated: os.Path,
721
reportingPath: Either[String, os.Path],

modules/build/src/main/scala/scala/build/PersistentDiagnosticLogger.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.build.errors.{BuildException, Diagnostic}
99
import scala.build.internals.FeatureType
1010
import scala.scalanative.build as sn
1111

12+
/** Used to collect and send diagnostics to the build client when operating as a BSP */
1213
class PersistentDiagnosticLogger(parent: Logger) extends Logger {
1314
private val diagBuilder = List.newBuilder[Diagnostic]
1415

modules/build/src/main/scala/scala/build/bsp/BspClient.scala

Lines changed: 52 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import scala.build.{BloopBuildClient, GeneratedSource, Logger}
1818
import scala.jdk.CollectionConverters.*
1919

2020
class BspClient(
21-
readFilesEs: ExecutorService,
2221
@volatile var logger: Logger,
2322
var forwardToOpt: Option[b.BuildClient] = None
2423
) extends b.BuildClient with BuildClientForwardStubs with BloopBuildClient
@@ -27,76 +26,68 @@ class BspClient(
2726
private def updatedPublishDiagnosticsParams(
2827
params: b.PublishDiagnosticsParams,
2928
genSource: GeneratedSource
30-
): Either[() => b.PublishDiagnosticsParams, b.PublishDiagnosticsParams] = {
29+
): b.PublishDiagnosticsParams = {
3130
val updatedUri = genSource.reportingPath.fold(
3231
_ => params.getTextDocument.getUri,
3332
_.toNIO.toUri.toASCIIString
3433
)
3534
val updatedDiagnostics =
3635
if (genSource.wrapperParamsOpt.isEmpty)
37-
Right(params.getDiagnostics)
36+
params.getDiagnostics
3837
else
39-
Left { () =>
40-
val updateLine = scalaLine => scalaLineToScLine(scalaLine, genSource.wrapperParamsOpt)
41-
params.getDiagnostics.asScala.toSeq
42-
.map { diag =>
43-
val updatedDiagOpt = for {
44-
startLine <- updateLine(diag.getRange.getStart.getLine)
45-
endLine <- updateLine(diag.getRange.getEnd.getLine)
46-
} yield {
47-
val diag0 = diag.duplicate()
48-
diag0.getRange.getStart.setLine(startLine)
49-
diag0.getRange.getEnd.setLine(endLine)
50-
51-
val scalaDiagnostic = new Gson().fromJson[b.ScalaDiagnostic](
52-
diag0.getData().asInstanceOf[JsonElement],
53-
classOf[b.ScalaDiagnostic]
54-
)
55-
56-
scalaDiagnostic.getActions().asScala.foreach { action =>
57-
for {
58-
change <- action.getEdit().getChanges().asScala
59-
startLine <- updateLine(change.getRange.getStart.getLine)
60-
endLine <- updateLine(change.getRange.getEnd.getLine)
61-
} yield {
62-
change.getRange().getStart.setLine(startLine)
63-
change.getRange().getEnd.setLine(endLine)
64-
}
38+
val updateLine = scalaLine => scalaLineToScLine(scalaLine, genSource.wrapperParamsOpt)
39+
params.getDiagnostics.asScala.toSeq
40+
.map { diag =>
41+
val updatedDiagOpt = for {
42+
startLine <- updateLine(diag.getRange.getStart.getLine)
43+
endLine <- updateLine(diag.getRange.getEnd.getLine)
44+
} yield {
45+
val diag0 = diag.duplicate()
46+
diag0.getRange.getStart.setLine(startLine)
47+
diag0.getRange.getEnd.setLine(endLine)
48+
49+
val scalaDiagnostic = new Gson().fromJson[b.ScalaDiagnostic](
50+
diag0.getData().asInstanceOf[JsonElement],
51+
classOf[b.ScalaDiagnostic]
52+
)
53+
54+
scalaDiagnostic.getActions().asScala.foreach { action =>
55+
for {
56+
change <- action.getEdit().getChanges().asScala
57+
startLine <- updateLine(change.getRange.getStart.getLine)
58+
endLine <- updateLine(change.getRange.getEnd.getLine)
59+
} yield {
60+
change.getRange().getStart.setLine(startLine)
61+
change.getRange().getEnd.setLine(endLine)
6562
}
63+
}
6664

67-
diag0.setData(scalaDiagnostic)
65+
diag0.setData(scalaDiagnostic)
6866

69-
if (
70-
diag0.getMessage.contains(
71-
"cannot be a main method since it cannot be accessed statically"
72-
)
67+
if (
68+
diag0.getMessage.contains(
69+
"cannot be a main method since it cannot be accessed statically"
70+
)
71+
)
72+
diag0.setMessage(
73+
WarningMessages.mainAnnotationNotSupported( /* annotationIgnored */ false)
7374
)
74-
diag0.setMessage(
75-
WarningMessages.mainAnnotationNotSupported( /* annotationIgnored */ false)
76-
)
7775

78-
diag0
79-
}
80-
updatedDiagOpt.getOrElse(diag)
76+
diag0
8177
}
82-
.asJava
83-
}
84-
def updatedParamsFor(
85-
updatedDiagnostics: java.util.List[b.Diagnostic]
86-
): b.PublishDiagnosticsParams = {
87-
val updatedTextDoc = new b.TextDocumentIdentifier(updatedUri)
88-
val updatedParams = new b.PublishDiagnosticsParams(
89-
updatedTextDoc,
90-
params.getBuildTarget,
91-
updatedDiagnostics,
92-
params.getReset
93-
)
94-
updatedParams.setOriginId(params.getOriginId)
95-
updatedParams
96-
}
97-
updatedDiagnostics
98-
.left.map(f => () => updatedParamsFor(f()))
99-
.map(updatedParamsFor(_))
78+
updatedDiagOpt.getOrElse(diag)
79+
}
80+
.asJava
81+
82+
val updatedTextDoc = new b.TextDocumentIdentifier(updatedUri)
83+
val updatedParams = new b.PublishDiagnosticsParams(
84+
updatedTextDoc,
85+
params.getBuildTarget,
86+
updatedDiagnostics,
87+
params.getReset
88+
)
89+
updatedParams.setOriginId(params.getOriginId)
90+
updatedParams
10091
}
10192

10293
override def onBuildPublishDiagnostics(params: b.PublishDiagnosticsParams): Unit = {
@@ -116,24 +107,11 @@ class BspClient(
116107
}
117108
}
118109

119-
def call(updatedParams0: b.PublishDiagnosticsParams): Unit =
120-
super.onBuildPublishDiagnostics(updatedParams0)
121110
updatedParamsOpt match {
122111
case None =>
123-
call(params)
124-
case Some(Right(updatedParams0)) =>
125-
call(updatedParams0)
126-
case Some(Left(updateParamsFunc)) =>
127-
val runnable =
128-
new Runnable {
129-
def run(): Unit =
130-
try call(updateParamsFunc())
131-
catch {
132-
case t: Throwable =>
133-
logger.debug(s"Caught $t while publishing updated diagnostics")
134-
}
135-
}
136-
readFilesEs.submit(runnable)
112+
super.onBuildPublishDiagnostics(params)
113+
case Some(updatedParams) =>
114+
super.onBuildPublishDiagnostics(updatedParams)
137115
}
138116
}
139117

modules/build/src/main/scala/scala/build/bsp/BspImpl.scala

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import scala.concurrent.{ExecutionContext, Future, Promise}
2929
import scala.jdk.CollectionConverters.*
3030
import scala.util.{Failure, Success}
3131

32-
/** The implementation for [[Bsp]].
32+
/** The implementation for [[Bsp]] command.
3333
*
3434
* @param argsToInputs
3535
* a function transforming terminal args to [[Inputs]]
@@ -53,6 +53,12 @@ final class BspImpl(
5353

5454
import BspImpl.{PreBuildData, PreBuildProject, buildTargetIdToEvent, responseError}
5555

56+
private val shownGlobalMessages =
57+
new java.util.concurrent.ConcurrentHashMap[String, Unit]()
58+
private var actualLocalClient: BspClient = _
59+
private var localClient: b.BuildClient with BloopBuildClient = _
60+
private val bloopSession = new BloopSession.Reference
61+
5662
/** Sends the buildTarget/didChange BSP notification to the BSP client, indicating that the build
5763
* targets defined in the current session have changed.
5864
*
@@ -257,9 +263,6 @@ final class BspImpl(
257263
client.resetBuildExceptionDiagnostics(targetId)
258264
}
259265

260-
private val shownGlobalMessages =
261-
new java.util.concurrent.ConcurrentHashMap[String, Unit]()
262-
263266
private def showGlobalWarningOnce(msg: String): Unit =
264267
shownGlobalMessages.computeIfAbsent(
265268
msg,
@@ -378,9 +381,6 @@ final class BspImpl(
378381
}
379382
}
380383

381-
private var actualLocalClient: BspClient = _
382-
private var localClient: b.BuildClient with BloopBuildClient = _
383-
384384
/** Returns a reference to the [[BspClient]], respecting the given verbosity
385385
* @param verbosity
386386
* verbosity to be passed to the resulting [[BspImpl.LoggingBspClient]]
@@ -450,33 +450,30 @@ final class BspImpl(
450450
bloopSession0
451451
}
452452

453-
private val bloopSession = new BloopSession.Reference
454-
455453
/** The logic for the actual running of the `bsp` command, initializing the BSP connection.
456454
* @param initialInputs
457455
* the initial input sources passed upon initializing the BSP connection (which are subject to
458456
* change on subsequent workspace/reload requests)
459457
*/
460-
def run(initialInputs: Inputs, initialBspOptions: BspReloadableOptions): Future[Unit] = {
458+
override def run(initialInputs: Inputs, initialBspOptions: BspReloadableOptions): Future[Unit] = {
461459
val logger = initialBspOptions.logger
462460
val verbosity = initialBspOptions.verbosity
463461

464-
actualLocalClient = new BspClient(
465-
threads.buildThreads.bloop.jsonrpc, // meh
466-
logger
467-
)
462+
actualLocalClient = new BspClient(logger)
468463
localClient = getLocalClient(verbosity)
469464

470465
val currentBloopSession = newBloopSession(initialInputs, initialBspOptions)
471466
bloopSession.update(null, currentBloopSession, "BSP server already initialized")
472467

473-
val actualLocalServer
474-
: b.BuildServer with b.ScalaBuildServer with b.JavaBuildServer with b.JvmBuildServer
475-
with ScalaScriptBuildServer with HasGeneratedSources =
476-
new BuildServerProxy(
477-
() => bloopSession.get().bspServer,
478-
() => onReload()
479-
)
468+
val actualLocalServer: b.BuildServer
469+
with b.ScalaBuildServer
470+
with b.JavaBuildServer
471+
with b.JvmBuildServer
472+
with ScalaScriptBuildServer
473+
with HasGeneratedSources = new BuildServerProxy(
474+
() => bloopSession.get().bspServer,
475+
() => onReload()
476+
)
480477

481478
val localServer: b.BuildServer with b.ScalaBuildServer with b.JavaBuildServer
482479
with b.JvmBuildServer with ScalaScriptBuildServer =
@@ -540,9 +537,8 @@ final class BspImpl(
540537
Future.firstCompletedOf(futures)(es)
541538
}
542539

543-
/** Shuts down the current Bloop session
544-
*/
545-
def shutdown(): Unit =
540+
/** Shuts down the current Bloop session */
541+
override def shutdown(): Unit =
546542
for (currentBloopSession <- bloopSession.getAndNullify())
547543
currentBloopSession.dispose()
548544

modules/build/src/main/scala/scala/build/bsp/HasGeneratedSources.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ object HasGeneratedSources {
2424
lazy val uriMap: Map[String, GeneratedSource] =
2525
sources
2626
.flatMap { g =>
27-
g.reportingPath.toOption.toSeq.map { _ =>
28-
g.generated.toNIO.toUri.toASCIIString -> g
27+
g.reportingPath match {
28+
case Left(_) => Nil
29+
case Right(_) => Seq(g.generated.toNIO.toUri.toASCIIString -> g)
2930
}
3031
}
3132
.toMap

0 commit comments

Comments
 (0)