Skip to content

Commit

Permalink
Merge pull request #1188 from wpopielarski/worksheet-macro
Browse files Browse the repository at this point in the history
Enables worksheet macro expansion. Makes worksheet more stable.
  • Loading branch information
wpopielarski committed Jan 3, 2018
2 parents 31d47eb + 2dfe808 commit 1eb9709
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.junit.Assert
import org.junit.Test import org.junit.Test
import org.scalaide.core.SdtConstants import org.scalaide.core.SdtConstants
import org.scalaide.core.internal.builder.zinc.ResidentCompiler import org.scalaide.core.internal.builder.zinc.ResidentCompiler
import org.scalaide.core.internal.builder.zinc.ResidentCompiler.CompilationError
import org.scalaide.core.internal.builder.zinc.ResidentCompiler.CompilationFailed import org.scalaide.core.internal.builder.zinc.ResidentCompiler.CompilationFailed
import org.scalaide.core.internal.builder.zinc.ResidentCompiler.CompilationSuccess import org.scalaide.core.internal.builder.zinc.ResidentCompiler.CompilationSuccess
import org.scalaide.core.testsetup.TestProjectSetup import org.scalaide.core.testsetup.TestProjectSetup
Expand Down Expand Up @@ -46,11 +47,15 @@ class ResidentCompilerTest {
val tested = ResidentCompiler(project, output, None).get val tested = ResidentCompiler(project, output, None).get


try { try {
tested.compile(javaSrc) tested.compile(javaSrc) match {
Assert.fail(s"Expected compilation error") case CompilationFailed(CompilationError(actual, _) :: Nil) =>
Assert.assertTrue(actual == "expects to be not called")
case fail =>
Assert.fail(s"Expected compilation error, got $fail")
}
} catch { } catch {
case correct: NotImplementedError => case fail: Throwable =>
Assert.assertTrue(correct.getMessage == "expects to be not called") Assert.fail(s"Expected compilation error, got ${fail.getMessage}")
} }


} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.scalaide.util.internal.SbtUtils


import sbt.internal.inc.FreshCompilerCache import sbt.internal.inc.FreshCompilerCache
import sbt.internal.inc.IncrementalCompilerImpl import sbt.internal.inc.IncrementalCompilerImpl
import xsbti.CompileFailed
import xsbti.compile.CompileAnalysis import xsbti.compile.CompileAnalysis
import xsbti.compile.CompileOrder import xsbti.compile.CompileOrder
import xsbti.compile.CompileProgress import xsbti.compile.CompileProgress
Expand All @@ -24,7 +25,7 @@ import xsbti.compile.MiniSetup


object ResidentCompiler { object ResidentCompiler {
def apply(project: IScalaProject, compilationOutputFolder: File, extraLibsToCompile: Option[IPath], def apply(project: IScalaProject, compilationOutputFolder: File, extraLibsToCompile: Option[IPath],
monitor: SubMonitor = SubMonitor.convert(new NullProgressMonitor)) = { monitor: SubMonitor = SubMonitor.convert(new NullProgressMonitor)) = {
val installation = project.effectiveScalaInstallation val installation = project.effectiveScalaInstallation
val comps = compilers(installation, monitor) val comps = compilers(installation, monitor)
comps.toOption.map { comps => new ResidentCompiler(project, comps, compilationOutputFolder, extraLibsToCompile) } comps.toOption.map { comps => new ResidentCompiler(project, comps, compilationOutputFolder, extraLibsToCompile) }
Expand All @@ -38,43 +39,56 @@ object ResidentCompiler {
} }


class ResidentCompiler private (project: IScalaProject, comps: Compilers, compilationOutputFolder: File, class ResidentCompiler private (project: IScalaProject, comps: Compilers, compilationOutputFolder: File,
extraLibsToCompile: Option[IPath]) extends HasLogger { extraLibsToCompile: Option[IPath]) extends HasLogger {
import ResidentCompiler._ import ResidentCompiler._
private val sbtLogger = SbtUtils.defaultSbtLogger(logger) private val sbtLogger = SbtUtils.defaultSbtLogger(logger)
private val libs = extraLibsToCompile.map(_.toFile).toSeq private val libs = extraLibsToCompile.map(_.toFile).toSeq
private val zincCompiler = new IncrementalCompilerImpl private val zincCompiler = new IncrementalCompilerImpl
private val sbtReporter = new SbtBuildReporter(project) private val sbtReporter = new SbtBuildReporter(project)
private val lookup = new DefaultPerClasspathEntryLookup {} private val lookup = new DefaultPerClasspathEntryLookup {
override def definesClass(classpathEntry: File) =
Locator.NoClass
}
private val classpath = libs ++ project.scalaClasspath.userCp.map(_.toFile) toArray private val classpath = libs ++ project.scalaClasspath.userCp.map(_.toFile) toArray
private val scalacOpts = (project.effectiveScalaInstallation.version match { private val scalacOpts = (project.effectiveScalaInstallation.version match {
case SpecificScalaVersion(2, 10, _, _) => case SpecificScalaVersion(2, 10, _, _) =>
project.scalacArguments.filterNot(opt => opt == "-Xsource:2.10" || opt == "-Ymacro-expand:none") project.scalacArguments.filterNot(opt => opt == "-Xsource:2.10" || opt == "-Ymacro-expand:none")
case _ => project.scalacArguments case _ => project.scalacArguments
}) toArray }) toArray


def compile(compiledSource: File): CompilationResult = { private val problemToCompilationError: PartialFunction[xsbti.Problem, CompilationError] = {
case p if p.severity == xsbti.Severity.Error =>
val pos = p.position.line.map[Position] { pline =>
new Position {
override def line = pline
}
}.orElse { NoPosition }
CompilationError(p.message, pos)
}

private def toCompilationResult(errors: Seq[CompilationError]): CompilationResult = errors match {
case errors @ _ +: _ => CompilationFailed(errors)
case Nil => CompilationSuccess
}

def compile(compiledSource: File): CompilationResult = try {
import java.nio.file.Files
Files.lines(compiledSource.toPath).toArray.foreach(a => logger.warn(a))
def incOptions: IncOptions = IncOptions.of() def incOptions: IncOptions = IncOptions.of()
def output = new EclipseMultipleOutput(Seq(compiledSource.toPath.getParent.toFile -> compilationOutputFolder)) def output = new EclipseMultipleOutput(Seq(compiledSource.toPath.getParent.toFile -> compilationOutputFolder))
def cache = new FreshCompilerCache def cache = new FreshCompilerCache


sbtReporter.reset()
zincCompiler.compile(comps.scalac, comps.javac, Array(compiledSource), classpath, output, cache, scalacOpts, zincCompiler.compile(comps.scalac, comps.javac, Array(compiledSource), classpath, output, cache, scalacOpts,
javaOptions = Array(), Optional.empty[CompileAnalysis], Optional.empty[MiniSetup], lookup, sbtReporter, javaOptions = Array(), Optional.empty[CompileAnalysis], Optional.empty[MiniSetup], lookup, sbtReporter,
CompileOrder.ScalaThenJava, skip = false, Optional.empty[CompileProgress], incOptions, extra = Array(), CompileOrder.ScalaThenJava, skip = false, Optional.empty[CompileProgress], incOptions, extra = Array(),
sbtLogger) sbtLogger)


import xsbti.Severity._ toCompilationResult(sbtReporter.problems.collect(problemToCompilationError))
val errors = sbtReporter.problems.collect { } catch {
case p if p.severity == Error => case compileFailed: CompileFailed =>
val pos = p.position.line.map[Position] { pline => toCompilationResult(compileFailed.problems.collect(problemToCompilationError))
new Position { case anyException: Throwable =>
override def line = pline CompilationFailed(Seq(CompilationError(anyException.getMessage, NoPosition)))
}
}.orElse { NoPosition }
CompilationError(p.message, pos)
}
errors.toSeq match {
case errors @ _ +: _ => CompilationFailed(errors)
case Nil => CompilationSuccess
}
} }
} }
Original file line number Original file line Diff line number Diff line change
@@ -1,24 +1,25 @@
package org.scalaide.core.internal.project package org.scalaide.core.internal.project


import org.scalaide.core.internal.jdt.util.ClasspathContainerSetter
import org.eclipse.jface.preference.IPreferenceStore
import org.scalaide.util.internal.CompilerUtils
import scala.util.Try
import org.scalaide.ui.internal.preferences.CompilerSettings
import org.eclipse.jface.util.IPropertyChangeListener
import org.scalaide.util.internal.SettingConverterUtil
import org.scalaide.core.IScalaPlugin
import org.eclipse.core.resources.IMarker
import scala.tools.nsc.settings.ScalaVersion import scala.tools.nsc.settings.ScalaVersion
import scala.tools.nsc.settings.SpecificScalaVersion
import scala.util.Failure import scala.util.Failure
import org.eclipse.jface.util.PropertyChangeEvent
import scala.util.Success import scala.util.Success
import org.scalaide.core.resources.MarkerFactory import scala.util.Try

import org.eclipse.core.resources.IMarker
import org.eclipse.core.runtime.Path import org.eclipse.core.runtime.Path
import org.eclipse.jface.preference.IPreferenceStore
import org.eclipse.jface.util.IPropertyChangeListener
import org.eclipse.jface.util.PropertyChangeEvent
import org.scalaide.core.IScalaPlugin import org.scalaide.core.IScalaPlugin
import org.scalaide.core.ScalaInstallationChange import org.scalaide.core.ScalaInstallationChange
import org.scalaide.core.SdtConstants import org.scalaide.core.SdtConstants
import org.scalaide.core.internal.compiler.ScalaPresentationCompiler import org.scalaide.core.internal.compiler.ScalaPresentationCompiler
import org.scalaide.core.internal.jdt.util.ClasspathContainerSetter
import org.scalaide.core.resources.MarkerFactory
import org.scalaide.ui.internal.preferences.CompilerSettings
import org.scalaide.util.internal.CompilerUtils
import org.scalaide.util.internal.SettingConverterUtil


trait InstallationManagement { this: ScalaProject => trait InstallationManagement { this: ScalaProject =>


Expand Down Expand Up @@ -162,10 +163,17 @@ trait InstallationManagement { this: ScalaProject =>
turnOnProjectSpecificSettings("requested Xsource change") turnOnProjectSpecificSettings("requested Xsource change")
val scalaVersionString = CompilerUtils.shortString(scalaVersion) val scalaVersionString = CompilerUtils.shortString(scalaVersion)
// initial space here is important // initial space here is important
val optionString = s" -Xsource:$scalaVersionString -Ymacro-expand:none" val optionString = scalaVersion match {
case SpecificScalaVersion(2, 10, _, _) => s" -Xsource:$scalaVersionString -Ymacro-expand:none"
case _ => s" -Xsource:$scalaVersionString"
}
val extraArgsFilter = scalaVersion match {
case SpecificScalaVersion(2, 10, _, _) => (s: String) => !s.startsWith("-Xsource") && !s.startsWith("-Ymacro-expand")
case _ => (s: String) => !s.startsWith("-Xsource")
}
eclipseLog.debug(s"Adding $optionString to compiler arguments of ${this.underlying.getName()} because of: $reason") eclipseLog.debug(s"Adding $optionString to compiler arguments of ${this.underlying.getName()} because of: $reason")
val extraArgs = ScalaPresentationCompiler.defaultScalaSettings().splitParams(storage.getString(CompilerSettings.ADDITIONAL_PARAMS)) val extraArgs = ScalaPresentationCompiler.defaultScalaSettings().splitParams(storage.getString(CompilerSettings.ADDITIONAL_PARAMS))
val curatedArgs = extraArgs.filter { s => !s.startsWith("-Xsource") && !s.startsWith("-Ymacro-expand") } val curatedArgs = extraArgs.filter(extraArgsFilter)
storage.setValue(CompilerSettings.ADDITIONAL_PARAMS, curatedArgs.mkString(" ") + optionString) storage.setValue(CompilerSettings.ADDITIONAL_PARAMS, curatedArgs.mkString(" ") + optionString)
} }


Expand Down

0 comments on commit 1eb9709

Please sign in to comment.