Skip to content
This repository
Browse code

Source position mapper support

  • Loading branch information...
commit 702ee44e0eec72289b742acb85044cdd1c0c5869 1 parent 5e2fc5f
James Roper jroper authored committed
2  compile/AnalyzingCompiler.scala
@@ -20,7 +20,7 @@ final class AnalyzingCompiler(val scalaInstance: xsbti.compile.ScalaInstance, va
20 20 {
21 21 val arguments = (new CompilerArguments(scalaInstance, cp))(Nil, classpath, None, options)
22 22 val output = CompileOutput(singleOutput)
23   - compile(sources, changes, arguments, output, callback, new LoggerReporter(maximumErrors, log), cache, log, None)
  23 + compile(sources, changes, arguments, output, callback, new LoggerReporter(maximumErrors, log, p => p), cache, log, None)
24 24 }
25 25
26 26 def compile(sources: Seq[File], changes: DependencyChanges, options: Seq[String], output: Output, callback: AnalysisCallback, reporter: Reporter, cache: GlobalsCache, log: Logger, progressOpt: Option[CompileProgress]): Unit =
11 compile/LoggerReporter.scala
@@ -45,7 +45,7 @@ object LoggerReporter
45 45 }
46 46 }
47 47
48   -class LoggerReporter(maximumErrors: Int, log: Logger) extends xsbti.Reporter
  48 +class LoggerReporter(maximumErrors: Int, log: Logger, sourcePositionMapper: Position => Position = {p => p}) extends xsbti.Reporter
49 49 {
50 50 val positions = new mutable.HashMap[PositionKey, Severity]
51 51 val count = new EnumMap[Severity, Int](classOf[Severity])
@@ -115,15 +115,16 @@ class LoggerReporter(maximumErrors: Int, log: Logger) extends xsbti.Reporter
115 115
116 116 def log(pos: Position, msg: String, severity: Severity): Unit =
117 117 {
118   - allProblems += problem("", pos, msg, severity)
  118 + val mappedPos = sourcePositionMapper(pos)
  119 + allProblems += problem("", mappedPos, msg, severity)
119 120 severity match
120 121 {
121 122 case Warn | Error =>
122 123 {
123   - if(!testAndLog(pos, severity))
124   - display(pos, msg, severity)
  124 + if(!testAndLog(mappedPos, severity))
  125 + display(mappedPos, msg, severity)
125 126 }
126   - case _ => display(pos, msg, severity)
  127 + case _ => display(mappedPos, msg, severity)
127 128 }
128 129 }
129 130
13 main/Defaults.scala
@@ -103,6 +103,7 @@ object Defaults extends BuildCommon
103 103 classpathTypes :== Set("jar", "bundle", "hk2-jar", "orbit"),
104 104 aggregate :== true,
105 105 maxErrors :== 100,
  106 + sourcePositionMappers :== Nil,
106 107 showTiming :== true,
107 108 timingFormat :== Aggregation.defaultFormat,
108 109 showSuccess :== true,
@@ -577,15 +578,17 @@ object Defaults extends BuildCommon
577 578 def compileInputsSettings: Seq[Setting[_]] = {
578 579 val optionsPair = TaskKey.local[(Seq[String], Seq[String])]
579 580 Seq(optionsPair <<= (scalacOptions, javacOptions) map Util.pairID,
580   - compileInputs <<= (dependencyClasspath, sources, compilers, optionsPair, classDirectory, compileOrder, compileIncSetup, maxErrors, streams) map {
581   - (cp, srcs, cs, optsPair, classes, order, incSetup, maxErr, s) =>
582   - Compiler.inputs(classes +: data(cp), srcs, classes, optsPair._1, optsPair._2, maxErr, order)(cs, incSetup, s.log)
  581 + compileInputs <<= (dependencyClasspath, sources, compilers, optionsPair, classDirectory, compileOrder, compileIncSetup, maxErrors, streams, sourcePositionMappers) map {
  582 + (cp, srcs, cs, optsPair, classes, order, incSetup, maxErr, s, spms) =>
  583 + Compiler.inputs(classes +: data(cp), srcs, classes, optsPair._1, optsPair._2, maxErr, spms, order)(cs, incSetup, s.log)
583 584 })
584 585 }
585 586 def printWarningsTask: Initialize[Task[Unit]] =
586   - (streams, compile, maxErrors) map { (s, analysis, max) =>
  587 + (streams, compile, maxErrors, sourcePositionMappers) map { (s, analysis, max, spms) =>
587 588 val problems = analysis.infos.allInfos.values.flatMap(i => i.reportedProblems++ i.unreportedProblems)
588   - val reporter = new LoggerReporter(max, s.log)
  589 + val reporter = new LoggerReporter(max, s.log,
  590 + spms.foldRight({p: xsbti.Position => p}) { (mapper, mappers) => {p: xsbti.Position => mapper(p).getOrElse(mappers(p))}}
  591 + )
589 592 problems foreach { p => reporter.display(p.position, p.message, p.severity) }
590 593 }
591 594
1  main/Keys.scala
@@ -160,6 +160,7 @@ object Keys
160 160 val doc = TaskKey[File]("doc", "Generates API documentation.", AMinusTask)
161 161 val copyResources = TaskKey[Seq[(File,File)]]("copy-resources", "Copies resources to the output directory.", AMinusTask)
162 162 val aggregate = SettingKey[Boolean]("aggregate", "Configures task aggregation.", BMinusSetting)
  163 + val sourcePositionMappers = TaskKey[Seq[xsbti.Position => Option[xsbti.Position]]]("source-position-mappers", "Maps positions in generated source files to the original source it was generated from", DTask)
163 164
164 165 // package keys
165 166 val packageBin = TaskKey[File]("package-bin", "Produces a main artifact, such as a binary jar.", ATask)
12 main/actions/Compiler.scala
@@ -16,7 +16,7 @@ object Compiler
16 16 val DefaultMaxErrors = 100
17 17
18 18 final case class Inputs(compilers: Compilers, config: Options, incSetup: IncSetup)
19   - final case class Options(classpath: Seq[File], sources: Seq[File], classesDirectory: File, options: Seq[String], javacOptions: Seq[String], maxErrors: Int, order: CompileOrder)
  19 + final case class Options(classpath: Seq[File], sources: Seq[File], classesDirectory: File, options: Seq[String], javacOptions: Seq[String], maxErrors: Int, sourcePositionMapper: Position => Position, order: CompileOrder)
20 20 final case class IncSetup(analysisMap: File => Option[Analysis], definesClass: DefinesClass, skip: Boolean, cacheFile: File, cache: GlobalsCache)
21 21 final case class Compilers(scalac: AnalyzingCompiler, javac: JavaTool)
22 22
@@ -28,12 +28,14 @@ object Compiler
28 28 val cacheFile = outputDirectory / "cache_old_style"
29 29 val augClasspath = classesDirectory.asFile +: classpath
30 30 val incSetup = IncSetup(Map.empty, definesClass, false, cacheFile, CompilerCache.fresh)
31   - inputs(augClasspath, sources, classesDirectory, options, javacOptions, maxErrors, order)(compilers, incSetup, log)
  31 + inputs(augClasspath, sources, classesDirectory, options, javacOptions, maxErrors, Nil, order)(compilers, incSetup, log)
32 32 }
33   - def inputs(classpath: Seq[File], sources: Seq[File], classesDirectory: File, options: Seq[String], javacOptions: Seq[String], maxErrors: Int, order: CompileOrder)(implicit compilers: Compilers, incSetup: IncSetup, log: Logger): Inputs =
  33 + def inputs(classpath: Seq[File], sources: Seq[File], classesDirectory: File, options: Seq[String], javacOptions: Seq[String], maxErrors: Int, sourcePositionMappers: Seq[Position => Option[Position]], order: CompileOrder)(implicit compilers: Compilers, incSetup: IncSetup, log: Logger): Inputs =
34 34 new Inputs(
35 35 compilers,
36   - new Options(classpath, sources, classesDirectory, options, javacOptions, maxErrors, order),
  36 + new Options(classpath, sources, classesDirectory, options, javacOptions, maxErrors,
  37 + sourcePositionMappers.foldRight({p: Position => p}) { (mapper, mappers) => {p: Position => mapper(p).getOrElse(mappers(p))}},
  38 + order),
37 39 incSetup
38 40 )
39 41
@@ -76,6 +78,6 @@ object Compiler
76 78 import in.incSetup._
77 79
78 80 val agg = new AggressiveCompile(cacheFile)
79   - agg(scalac, javac, sources, classpath, CompileOutput(classesDirectory), cache, None, options, javacOptions, analysisMap, definesClass, new LoggerReporter(maxErrors, log), order, skip)(log)
  81 + agg(scalac, javac, sources, classpath, CompileOutput(classesDirectory), cache, None, options, javacOptions, analysisMap, definesClass, new LoggerReporter(maxErrors, log, sourcePositionMapper), order, skip)(log)
80 82 }
81 83 }

0 comments on commit 702ee44

Please sign in to comment.
Something went wrong with that request. Please try again.