Skip to content

Commit 50815da

Browse files
committed
Swap topWrapperLineCount value with wrapperParams case class
Remove userCodeNesting level variable
1 parent cafa83c commit 50815da

19 files changed

+68
-50
lines changed

modules/build/src/main/scala/scala/build/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ object Build {
11311131
.map { source =>
11321132
val relPath = source.generated.relativeTo(generatedSrcRoot).toString
11331133
val reportingPath = source.reportingPath.fold(s => s, _.last)
1134-
(relPath, (reportingPath, scalaLineToScLineShift(source.topWrapperLineCount)))
1134+
(relPath, (reportingPath, scalaLineToScLineShift(source.wrapperParamsOpt)))
11351135
}
11361136
.toMap
11371137

modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import java.net.URI
77
import java.nio.file.Paths
88

99
import scala.build.errors.Severity
10+
import scala.build.internal.WrapperParams
1011
import scala.build.internal.util.ConsoleUtils.ScalaCliConsole
1112
import scala.build.options.Scope
1213
import scala.build.postprocessing.LineConversion.scalaLineToScLine
@@ -43,13 +44,13 @@ class ConsoleBloopBuildClient(
4344
private def postProcessDiagnostic(
4445
path: os.Path,
4546
diag: bsp4j.Diagnostic,
46-
diagnosticMappings: Map[os.Path, (Either[String, os.Path], Int)]
47+
diagnosticMappings: Map[os.Path, (Either[String, os.Path], Option[WrapperParams])]
4748
): Option[(Either[String, os.Path], bsp4j.Diagnostic)] =
48-
diagnosticMappings.get(path).map { case (originalPath, lineOffset) =>
49+
diagnosticMappings.get(path).map { case (originalPath, wrapperParamsOpt) =>
4950
(
5051
originalPath,
51-
scalaLineToScLine(diag.getRange.getStart.getLine, lineOffset),
52-
scalaLineToScLine(diag.getRange.getStart.getLine, lineOffset)
52+
scalaLineToScLine(diag.getRange.getStart.getLine, wrapperParamsOpt),
53+
scalaLineToScLine(diag.getRange.getStart.getLine, wrapperParamsOpt)
5354
)
5455
}.collect { case (originalPath, Some(scLineStart), Some(scLineEnd)) =>
5556
val start = new bsp4j.Position(scLineStart, diag.getRange.getStart.getCharacter)
@@ -73,7 +74,7 @@ class ConsoleBloopBuildClient(
7374
val diagnosticMappings = generatedSources.valuesIterator
7475
.flatMap(_.iterator)
7576
.map { source =>
76-
source.generated -> (source.reportingPath, source.topWrapperLineCount)
77+
source.generated -> (source.reportingPath, source.wrapperParamsOpt)
7778
}
7879
.toMap
7980

modules/build/src/main/scala/scala/build/CrossSources.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ object CrossSources {
339339
val baseReqs0 = baseReqs(m.scopePath)
340340
WithBuildRequirements(
341341
m.requirements.fold(baseReqs0)(_ orElse baseReqs0),
342-
Sources.InMemory(m.originalPath, m.relPath, m.code, m.ignoreLen)
342+
Sources.InMemory(m.originalPath, m.relPath, m.code, m.wrapperParamsOpt)
343343
) -> m.directivesPositions
344344
}
345345
val unwrappedScriptsWithDirectivePositions
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package scala.build
22

3+
import scala.build.internal.WrapperParams
4+
35
final case class GeneratedSource(
46
generated: os.Path,
57
reportingPath: Either[String, os.Path],
6-
topWrapperLineCount: Int
8+
wrapperParamsOpt: Option[WrapperParams]
79
)

modules/build/src/main/scala/scala/build/Sources.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import coursier.cache.ArchiveCache
44
import coursier.util.Task
55

66
import scala.build.input.Inputs
7-
import scala.build.internal.CodeWrapper
7+
import scala.build.internal.{CodeWrapper, WrapperParams}
88
import scala.build.options.{BuildOptions, Scope}
99
import scala.build.preprocessing.*
1010

@@ -39,7 +39,7 @@ final case class Sources(
3939
(
4040
inMemSource.originalPath.map(_._2),
4141
inMemSource.generatedRelPath,
42-
inMemSource.topWrapperLineCount
42+
inMemSource.wrapperParamsOpt
4343
)
4444
}
4545

@@ -51,8 +51,8 @@ final case class Sources(
5151
.foreach(os.remove(_))
5252

5353
generated.map {
54-
case (reportingPath, path, topWrapperLineCount) =>
55-
GeneratedSource(generatedSrcRoot / path, reportingPath, topWrapperLineCount)
54+
case (reportingPath, path, wrapperParamsOpt) =>
55+
GeneratedSource(generatedSrcRoot / path, reportingPath, wrapperParamsOpt)
5656
}
5757
}
5858

@@ -70,17 +70,17 @@ object Sources {
7070
originalPath: Either[String, (os.SubPath, os.Path)],
7171
generatedRelPath: os.RelPath,
7272
generatedContent: String,
73-
topWrapperLineCount: Int
73+
wrapperParamsOpt: Option[WrapperParams]
7474
)
7575

7676
final case class UnwrappedScript(
7777
originalPath: Either[String, (os.SubPath, os.Path)],
7878
generatedRelPath: os.RelPath,
79-
wrapScriptFun: CodeWrapper => (String, Int)
79+
wrapScriptFun: CodeWrapper => (String, WrapperParams)
8080
) {
8181
def wrap(wrapper: CodeWrapper): InMemory = {
82-
val (content, topWrapperLineCount) = wrapScriptFun(wrapper)
83-
InMemory(originalPath, generatedRelPath, content, topWrapperLineCount)
82+
val (content, wrapperParams) = wrapScriptFun(wrapper)
83+
InMemory(originalPath, generatedRelPath, content, Some(wrapperParams))
8484
}
8585
}
8686

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class BspClient(
3131
_.toNIO.toUri.toASCIIString
3232
)
3333
val updatedDiagnostics =
34-
if (genSource.topWrapperLineCount == 0)
34+
if (genSource.wrapperParamsOpt.isEmpty)
3535
Right(params.getDiagnostics)
3636
else
3737
Left { () =>
38-
val updateLine = scalaLine => scalaLineToScLine(scalaLine, genSource.topWrapperLineCount)
38+
val updateLine = scalaLine => scalaLineToScLine(scalaLine, genSource.wrapperParamsOpt)
3939
params.getDiagnostics.asScala.toSeq
4040
.map { diag =>
4141
val updatedDiagOpt = for {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class BspServer(
291291
item.setTopWrapper(
292292
content
293293
.linesIterator
294-
.take(s.topWrapperLineCount)
294+
.take(s.wrapperParamsOpt.map(_.topWrapperLineCount).getOrElse(0))
295295
.mkString("", System.lineSeparator(), System.lineSeparator())
296296
)
297297
item.setBottomWrapper("}") // meh

modules/build/src/main/scala/scala/build/internal/ClassCodeWrapper.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package scala.build.internal
66
* Scala 3 feature 'export'<br> Incompatible with native JS members - the wrapper is a class
77
*/
88
case object ClassCodeWrapper extends CodeWrapper {
9-
private val userCodeNestingLevel = 1
109
def apply(
1110
code: String,
1211
pkgName: Seq[Name],
@@ -59,6 +58,6 @@ $mainObjectCode
5958
""")
6059
// format: on
6160

62-
(top, bottom, userCodeNestingLevel)
61+
(top, bottom)
6362
}
6463
}

modules/build/src/main/scala/scala/build/internal/ObjectCodeWrapper.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package scala.build.internal
55
* threads from script
66
*/
77
case object ObjectCodeWrapper extends CodeWrapper {
8-
private val userCodeNestingLevel = 1
98
def apply(
109
code: String,
1110
pkgName: Seq[Name],
@@ -65,6 +64,6 @@ $mainObjectCode
6564
""")
6665
// format: on
6766

68-
(top, bottom, userCodeNestingLevel)
67+
(top, bottom)
6968
}
7069
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package scala.build.postprocessing
22

3+
import scala.build.internal.WrapperParams
4+
35
object LineConversion {
4-
def scalaLineToScLine(lineScala: Int, startOffsetInScala: Int): Option[Int] = {
5-
val lineSc = lineScala - startOffsetInScala
6-
if (lineSc >= 0) Some(lineSc) else None
7-
}
6+
def scalaLineToScLine(lineScala: Int, wrapperParamsOpt: Option[WrapperParams]): Option[Int] =
7+
wrapperParamsOpt match {
8+
case Some(wrapperParams) =>
9+
val lineSc = lineScala - wrapperParams.topWrapperLineCount
10+
11+
if (lineSc >= 0 && lineSc < wrapperParams.userCodeLineCount) Some(lineSc) else None
12+
case _ => None
13+
}
814

9-
def scalaLineToScLineShift(startOffsetInScala: Int): Int = -startOffsetInScala
15+
def scalaLineToScLineShift(wrapperParamsOpt: Option[WrapperParams]): Int =
16+
wrapperParamsOpt match {
17+
case Some(wrapperParams) => wrapperParams.topWrapperLineCount * -1
18+
case _ => 0
19+
}
1020
}

0 commit comments

Comments
 (0)