Skip to content
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

Bsp wrapper fixes #2171

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ object Build {
.map { source =>
val relPath = source.generated.relativeTo(generatedSrcRoot).toString
val reportingPath = source.reportingPath.fold(s => s, _.last)
(relPath, (reportingPath, scalaLineToScLineShift(source.topWrapperLineCount)))
(relPath, (reportingPath, scalaLineToScLineShift(source.wrapperParamsOpt)))
}
.toMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.net.URI
import java.nio.file.Paths

import scala.build.errors.Severity
import scala.build.internal.WrapperParams
import scala.build.internal.util.ConsoleUtils.ScalaCliConsole
import scala.build.options.Scope
import scala.build.postprocessing.LineConversion.scalaLineToScLine
Expand Down Expand Up @@ -43,13 +44,13 @@ class ConsoleBloopBuildClient(
private def postProcessDiagnostic(
path: os.Path,
diag: bsp4j.Diagnostic,
diagnosticMappings: Map[os.Path, (Either[String, os.Path], Int)]
diagnosticMappings: Map[os.Path, (Either[String, os.Path], Option[WrapperParams])]
): Option[(Either[String, os.Path], bsp4j.Diagnostic)] =
diagnosticMappings.get(path).map { case (originalPath, lineOffset) =>
diagnosticMappings.get(path).map { case (originalPath, wrapperParamsOpt) =>
(
originalPath,
scalaLineToScLine(diag.getRange.getStart.getLine, lineOffset),
scalaLineToScLine(diag.getRange.getStart.getLine, lineOffset)
scalaLineToScLine(diag.getRange.getStart.getLine, wrapperParamsOpt),
scalaLineToScLine(diag.getRange.getStart.getLine, wrapperParamsOpt)
)
}.collect { case (originalPath, Some(scLineStart), Some(scLineEnd)) =>
val start = new bsp4j.Position(scLineStart, diag.getRange.getStart.getCharacter)
Expand All @@ -73,7 +74,7 @@ class ConsoleBloopBuildClient(
val diagnosticMappings = generatedSources.valuesIterator
.flatMap(_.iterator)
.map { source =>
source.generated -> (source.reportingPath, source.topWrapperLineCount)
source.generated -> (source.reportingPath, source.wrapperParamsOpt)
}
.toMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ object CrossSources {
val baseReqs0 = baseReqs(m.scopePath)
WithBuildRequirements(
m.requirements.fold(baseReqs0)(_ orElse baseReqs0),
Sources.InMemory(m.originalPath, m.relPath, m.code, m.ignoreLen)
Sources.InMemory(m.originalPath, m.relPath, m.code, m.wrapperParamsOpt)
) -> m.directivesPositions
}
val unwrappedScriptsWithDirectivePositions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package scala.build

import scala.build.internal.WrapperParams

final case class GeneratedSource(
generated: os.Path,
reportingPath: Either[String, os.Path],
topWrapperLineCount: Int
wrapperParamsOpt: Option[WrapperParams]
)
16 changes: 8 additions & 8 deletions modules/build/src/main/scala/scala/build/Sources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import coursier.cache.ArchiveCache
import coursier.util.Task

import scala.build.input.Inputs
import scala.build.internal.CodeWrapper
import scala.build.internal.{CodeWrapper, WrapperParams}
import scala.build.options.{BuildOptions, Scope}
import scala.build.preprocessing.*

Expand Down Expand Up @@ -39,7 +39,7 @@ final case class Sources(
(
inMemSource.originalPath.map(_._2),
inMemSource.generatedRelPath,
inMemSource.topWrapperLineCount
inMemSource.wrapperParamsOpt
)
}

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

generated.map {
case (reportingPath, path, topWrapperLineCount) =>
GeneratedSource(generatedSrcRoot / path, reportingPath, topWrapperLineCount)
case (reportingPath, path, wrapperParamsOpt) =>
GeneratedSource(generatedSrcRoot / path, reportingPath, wrapperParamsOpt)
}
}

Expand All @@ -70,17 +70,17 @@ object Sources {
originalPath: Either[String, (os.SubPath, os.Path)],
generatedRelPath: os.RelPath,
generatedContent: String,
topWrapperLineCount: Int
wrapperParamsOpt: Option[WrapperParams]
)

final case class UnwrappedScript(
originalPath: Either[String, (os.SubPath, os.Path)],
generatedRelPath: os.RelPath,
wrapScriptFun: CodeWrapper => (String, Int)
wrapScriptFun: CodeWrapper => (String, WrapperParams)
) {
def wrap(wrapper: CodeWrapper): InMemory = {
val (content, topWrapperLineCount) = wrapScriptFun(wrapper)
InMemory(originalPath, generatedRelPath, content, topWrapperLineCount)
val (content, wrapperParams) = wrapScriptFun(wrapper)
InMemory(originalPath, generatedRelPath, content, Some(wrapperParams))
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/build/src/main/scala/scala/build/bsp/BspClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class BspClient(
_.toNIO.toUri.toASCIIString
)
val updatedDiagnostics =
if (genSource.topWrapperLineCount == 0)
if (genSource.wrapperParamsOpt.isEmpty)
Right(params.getDiagnostics)
else
Left { () =>
val updateLine = scalaLine => scalaLineToScLine(scalaLine, genSource.topWrapperLineCount)
val updateLine = scalaLine => scalaLineToScLine(scalaLine, genSource.wrapperParamsOpt)
params.getDiagnostics.asScala.toSeq
.map { diag =>
val updatedDiagOpt = for {
Expand Down
7 changes: 6 additions & 1 deletion modules/build/src/main/scala/scala/build/bsp/BspServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ class BspServer(
s.reportingPath.toSeq.map(_.toNIO.toUri.toASCIIString).map { uri =>
val item = new WrappedSourceItem(uri, s.generated.toNIO.toUri.toASCIIString)
val content = os.read(s.generated)
item.setTopWrapper(content.take(s.topWrapperLineCount))
item.setTopWrapper(
content
.linesIterator
.take(s.wrapperParamsOpt.map(_.topWrapperLineCount).getOrElse(0))
.mkString("", System.lineSeparator(), System.lineSeparator())
)
item.setBottomWrapper("}") // meh
item
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package scala.build.internal
* Scala 3 feature 'export'<br> Incompatible with native JS members - the wrapper is a class
*/
case object ClassCodeWrapper extends CodeWrapper {
private val userCodeNestingLevel = 1
def apply(
code: String,
pkgName: Seq[Name],
Expand Down Expand Up @@ -59,6 +58,6 @@ $mainObjectCode
""")
// format: on

(top, bottom, userCodeNestingLevel)
(top, bottom)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package scala.build.internal
* threads from script
*/
case object ObjectCodeWrapper extends CodeWrapper {
private val userCodeNestingLevel = 1
def apply(
code: String,
pkgName: Seq[Name],
Expand Down Expand Up @@ -65,6 +64,6 @@ $mainObjectCode
""")
// format: on

(top, bottom, userCodeNestingLevel)
(top, bottom)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package scala.build.postprocessing

import scala.build.internal.WrapperParams

object LineConversion {
def scalaLineToScLine(lineScala: Int, startOffsetInScala: Int): Option[Int] = {
val lineSc = lineScala - startOffsetInScala
if (lineSc >= 0) Some(lineSc) else None
}
def scalaLineToScLine(lineScala: Int, wrapperParamsOpt: Option[WrapperParams]): Option[Int] =
wrapperParamsOpt match {
case Some(wrapperParams) =>
val lineSc = lineScala - wrapperParams.topWrapperLineCount

if (lineSc >= 0 && lineSc < wrapperParams.userCodeLineCount) Some(lineSc) else None
case _ => None
}

def scalaLineToScLineShift(startOffsetInScala: Int): Int = -startOffsetInScala
def scalaLineToScLineShift(wrapperParamsOpt: Option[WrapperParams]): Int =
wrapperParamsOpt match {
case Some(wrapperParams) => wrapperParams.topWrapperLineCount * -1
case _ => 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ case object SemanticDbPostProcessor extends PostProcessor {
SemanticdbProcessor.postProcess(
os.read(originalSource),
originalSource.relativeTo(workspace),
if (source.topWrapperLineCount == 0)
n => Some(n)
else
scalaLine => scalaLineToScLine(scalaLine, source.topWrapperLineCount),
scalaLine => scalaLineToScLine(scalaLine, source.wrapperParamsOpt),
semDbFile,
finalSemDbFile
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ case object DataPreprocessor extends Preprocessor {
originalPath = Left(file.source),
relPath = file.subPath,
code = content,
ignoreLen = 0,
wrapperParamsOpt = None,
options = Some(preprocessedDirectives.globalUsings),
optionsWithTargetRequirements = preprocessedDirectives.usingsWithReqs,
requirements = Some(BuildRequirements()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final case class JavaPreprocessor(
originalPath = Left(v.source),
relPath = relPath,
code = content,
ignoreLen = 0,
wrapperParamsOpt = None,
options = Some(preprocessedDirectives.globalUsings),
optionsWithTargetRequirements = preprocessedDirectives.usingsWithReqs,
requirements = Some(BuildRequirements()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ case object MarkdownPreprocessor extends Preprocessor {
originalPath = reportingPath.map(subPath -> _),
relPath = os.rel / (subPath / os.up) / s"${subPath.last}$generatedSourceNameSuffix",
processedCode,
ignoreLen = 0,
wrapperParamsOpt = None,
options = Some(processingOutput.opts),
optionsWithTargetRequirements = processingOutput.optsWithReqs,
requirements = Some(processingOutput.globalReqs),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scala.build.preprocessing

import scala.build.Position
import scala.build.internal.CodeWrapper
import scala.build.internal.{CodeWrapper, WrapperParams}
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}

sealed abstract class PreprocessedSource extends Product with Serializable {
Expand Down Expand Up @@ -38,7 +38,7 @@ object PreprocessedSource {
originalPath: Either[String, (os.SubPath, os.Path)],
relPath: os.RelPath,
code: String,
ignoreLen: Int,
wrapperParamsOpt: Option[WrapperParams],
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
Expand All @@ -61,7 +61,7 @@ object PreprocessedSource {
mainClassOpt: Option[String],
scopePath: ScopePath,
directivesPositions: Option[Position.File],
wrapScriptFun: CodeWrapper => (String, Int)
wrapScriptFun: CodeWrapper => (String, WrapperParams)
) extends PreprocessedSource

final case class NoSourceCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ case object ScalaPreprocessor extends Preprocessor {
originalPath = Right((f.subPath, f.path)),
relPath = f.subPath,
code = updatedCode,
ignoreLen = 0,
wrapperParamsOpt = None,
options = Some(options),
optionsWithTargetRequirements = optionsWithReqs,
requirements = Some(requirements),
Expand Down Expand Up @@ -162,7 +162,7 @@ case object ScalaPreprocessor extends Preprocessor {
originalPath = Left(v.source),
relPath = relPath,
updatedContentOpt.getOrElse(content),
ignoreLen = 0,
wrapperParamsOpt = None,
options = Some(options),
optionsWithTargetRequirements = optionsWithTargetRequirements,
requirements = Some(requirements),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.input.{Inputs, ScalaCliInvokeData, Script, SingleElement, VirtualScript}
import scala.build.internal.util.WarningMessages
import scala.build.internal.{AmmUtil, ClassCodeWrapper, CodeWrapper, Name, ObjectCodeWrapper}
import scala.build.internal.{
AmmUtil,
ClassCodeWrapper,
CodeWrapper,
Name,
ObjectCodeWrapper,
WrapperParams
}
import scala.build.options.{BuildOptions, BuildRequirements, Platform, SuppressWarningOptions}
import scala.build.preprocessing.PreprocessedSource
import scala.build.preprocessing.ScalaPreprocessor.ProcessingOutput
Expand Down Expand Up @@ -133,7 +140,7 @@ case object ScriptPreprocessor extends Preprocessor {
wrapperName: Name,
scriptCode: String,
scriptPath: String
): CodeWrapper => (String, Int) = {
): CodeWrapper => (String, WrapperParams) = {
(codeWrapper: CodeWrapper) =>
if (containsMainAnnot) logger.diagnostic(
codeWrapper match {
Expand All @@ -143,13 +150,13 @@ case object ScriptPreprocessor extends Preprocessor {
}
)

val (code, topWrapperLineCount, _) = codeWrapper.wrapCode(
val (code, wrapperParams) = codeWrapper.wrapCode(
packageStrings,
wrapperName,
scriptCode,
scriptPath
)
(code, topWrapperLineCount)
(code, wrapperParams)
}

/** Get correct script wrapper depending on the platform and version of Scala. For Scala 2 or
Expand Down