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

Initial move to using twirl for template parsing. #217

Merged
merged 1 commit into from
Jul 22, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions org.scala-ide.play2.tests/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="lib" path="target/lib/mockito-all-1.9.0.jar"/>
<classpathentry kind="lib" path="target/lib/twirl-compiler.jar"/>
<classpathentry kind="lib" path="target/lib/twirl-parser.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
4 changes: 3 additions & 1 deletion org.scala-ide.play2.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ Require-Bundle: org.scala-lang.scala-library,
Import-Package: org.scalaide.core.testsetup,
org.aspectj.weaver.loadtime.definition
Bundle-ClassPath: .,
target/lib/mockito-all-1.9.0.jar
target/lib/mockito-all-1.9.0.jar,
target/lib/twirl-compiler.jar,
target/lib/twirl-parser.jar
20 changes: 20 additions & 0 deletions org.scala-ide.play2.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-compiler.artifactId}</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-parser.artifactId}</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -44,6 +52,18 @@
<artifactId>mockito-all</artifactId>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-compiler.artifactId}</artifactId>
<destFileName>twirl-compiler.jar</destFileName>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-parser.artifactId}</artifactId>
<destFileName>twirl-parser.jar</destFileName>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class TemplateRegionParserTest {
assertTrue("Document regions are not complete", complete(actual, code.length()))
if (actual.size == expected.size) {
for ((actualDocRegion, expectedDocRegion) <- (actual zip expected)) {
assertTrue(s"Document region: '$actualDocRegion' does not match expected: '$expectedDocRegion'", regionsAreSame(actualDocRegion, expectedDocRegion))
assertTrue(s"Document region: '$actualDocRegion/${actualDocRegion.getType()}' does not match expected: '$expectedDocRegion'/${expectedDocRegion.getType}", regionsAreSame(actualDocRegion, expectedDocRegion))

val actualTextRegions = actualDocRegion.getRegions().toArray().toList
assertTrue(s"Document region, $actualDocRegion, has overlap in child text regions", noOverlap(actualTextRegions))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package play.mvc;

abstract class Result {}
abstract public class Result {}
9 changes: 3 additions & 6 deletions org.scala-ide.play2/.classpath
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="target/lib/play.jar"/>
<classpathentry kind="lib" path="target/lib/scala-arm.jar"/>
<classpathentry kind="lib" path="target/lib/scala-io-core.jar"/>
<classpathentry kind="lib" path="target/lib/scala-io-file.jar"/>
<classpathentry kind="lib" path="target/lib/templates.jar"/>
<classpathentry exported="true" kind="lib" path="target/lib/twirl-compiler.jar"/>
<classpathentry exported="true" kind="lib" path="target/lib/twirl-parser.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 5 additions & 6 deletions org.scala-ide.play2/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ Require-Bundle:
org.eclipse.wst.jsdt.core;bundle-version="1.1.102"
Import-Package:
com.ibm.icu.text;apply-aspects:=false;org.eclipse.swt.graphics;apply-aspects:=false,
scala.tools.eclipse.contribution.weaving.jdt.ui.javaeditor.formatter;apply-aspects:=false
scala.tools.eclipse.contribution.weaving.jdt.ui.javaeditor.formatter;apply-aspects:=false,
scala.xml,
scala.util.parsing.input
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Activator: org.scalaide.play2.PlayPlugin
Bundle-ClassPath: .,
target/lib/play.jar,
target/lib/scala-arm.jar,
target/lib/scala-io-core.jar,
target/lib/scala-io-file.jar,
target/lib/templates.jar
target/lib/twirl-compiler.jar,
target/lib/twirl-parser.jar
4 changes: 3 additions & 1 deletion org.scala-ide.play2/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ bin.includes = META-INF/,\
icons/,\
about.ini,\
about.png,\
target/lib/
target/lib/,\
target/lib/twirl-compiler.jar,\
target/lib/twirl-parser.jar
47 changes: 10 additions & 37 deletions org.scala-ide.play2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,12 @@

<dependencies>
<dependency>
<groupId>play</groupId>
<artifactId>${play.artifactId}</artifactId>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-compiler.artifactId}</artifactId>
</dependency>
<dependency>
<groupId>play</groupId>
<artifactId>${templates.artifactId}</artifactId>
</dependency>
<dependency>
<groupId>com.github.scala-incubator.io</groupId>
<artifactId>${scala.io.core.artifactId}</artifactId>
</dependency>
<dependency>
<groupId>com.github.scala-incubator.io</groupId>
<artifactId>${scala.io.file.artifactId}</artifactId>
</dependency>
<dependency>
<groupId>${scala.arm.groupId}</groupId>
<artifactId>${scala.arm.artifactId}</artifactId>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-parser.artifactId}</artifactId>
</dependency>
</dependencies>

Expand All @@ -49,29 +37,14 @@
<configuration>
<artifactItems>
<artifactItem>
<groupId>play</groupId>
<artifactId>${play.artifactId}</artifactId>
<destFileName>play.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>play</groupId>
<artifactId>${templates.artifactId}</artifactId>
<destFileName>templates.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.github.scala-incubator.io</groupId>
<artifactId>${scala.io.core.artifactId}</artifactId>
<destFileName>scala-io-core.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.github.scala-incubator.io</groupId>
<artifactId>${scala.io.file.artifactId}</artifactId>
<destFileName>scala-io-file.jar</destFileName>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-compiler.artifactId}</artifactId>
<destFileName>twirl-compiler.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>${scala.arm.groupId}</groupId>
<artifactId>${scala.arm.artifactId}</artifactId>
<destFileName>scala-arm.jar</destFileName>
<groupId>com.typesafe.play</groupId>
<artifactId>${twirl-parser.artifactId}</artifactId>
<destFileName>twirl-parser.jar</destFileName>
</artifactItem>
</artifactItems>
<stripVersion>true</stripVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ private[action] object MembersComputer {
// if it's a class name, then we need to use a typeName
val typeName = compiler.newTypeName(name)
val sym = compiler.rootMirror.findMemberFromRoot(typeName)
sym.initialize
sym
}

private def findModuleOrPackageFromRoot(name: String): compiler.Symbol = {
// module have term name
val termName = compiler.newTermName(name)
val sym = compiler.rootMirror.findMemberFromRoot(termName)
sym.initialize
sym
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.scalaide.play2.PlayPlugin
import org.scalaide.play2.PlayProject
import org.scalaide.play2.templateeditor.compiler.CompilerUsing
import org.scalaide.play2.templateeditor.compiler.PositionHelper
import play.templates.GeneratedSourceVirtual
import play.twirl.compiler.GeneratedSourceVirtual
import org.scalaide.editor.CompilationUnit
import org.scalaide.editor.CompilationUnitProvider
import org.scalaide.ui.internal.actions.ToggleScalaNatureAction
Expand Down Expand Up @@ -88,7 +88,7 @@ case class TemplateCompilationUnit(_workspaceFile: IFile, val usesInclusiveDot:
}

/** Return contents of template file. */
def getTemplateContents: String = document.map(_.get).getOrElse(scalax.file.Path(file.file).string())
def getTemplateContents: String = document.map(_.get).getOrElse(scala.io.Source.fromFile(file.file).mkString)

override def currentProblems: List[IProblem] = {
playProject.withPresentationCompiler { pc =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import java.io.File
import scala.util.Failure
import scala.util.Try
import org.scalaide.play2.PlayProject
import play.templates.GeneratedSourceVirtual
import play.templates.ScalaTemplateCompiler
import play.templates.ScalaTemplateCompiler._
import play.templates.TemplateCompilationError
import scalax.file.Path
import play.twirl.compiler.GeneratedSourceVirtual
import play.twirl.compiler.TwirlCompiler
import play.twirl.compiler.TwirlCompiler._
import play.twirl.compiler.TemplateCompilationError
import org.scalaide.play2.properties.PlayPreferences
import org.scalaide.logging.HasLogger
import scala.io.Codec
/**
* a helper for using template compiler
*/
object CompilerUsing extends HasLogger {
val templateCompiler = ScalaTemplateCompiler
val templateCompiler = TwirlCompiler
val defaultTemplateImports = """
import models._
import controllers._
Expand Down Expand Up @@ -44,6 +44,7 @@ import views.html._
"play.api.templates.Html",
"play.api.templates.HtmlFormat",
defaultTemplateImports + playProject.additionalTemplateImports(extension),
Codec.default,
inclusiveDot
)
} recoverWith {
Expand Down Expand Up @@ -83,7 +84,7 @@ case class TemplateToScalaCompilationError(source: File, message: String, offset

object PositionHelper {
def convertLineColumnToOffset(source: File, line: Int, column: Int): Int = {
convertLineColumnToOffset(Path(source).string, line, column)
convertLineColumnToOffset(scala.io.Source.fromFile(source).mkString, line, column)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably some encoding we should pass around (current code preserves the original, that relied on the default encoding as well).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's wise to enforce an encoding choice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was thinking more about using the Eclipse encoding. But it needn't be part of this PR.

}

def convertLineColumnToOffset(content: String, line: Int, column: Int): Int = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package org.scalaide.play2.templateeditor.compiler

import org.eclipse.jdt.core.compiler.IProblem
import org.scalaide.play2.PlayProject
import org.scalaide.play2.templateeditor.TemplateCompilationUnit
//import scala.tools.eclipse.javaelements.ScalaSourceFile
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem
//import scala.tools.eclipse.javaelements.ScalaCompilationUnit
import scalax.file.Path
import scala.tools.nsc.util.SourceFile
import scala.tools.nsc.util.BatchSourceFile
import play.templates.GeneratedSource
import play.twirl.compiler.GeneratedSource
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.io.VirtualFile
import scala.tools.nsc.io.PlainFile
import java.io.File
//import scala.tools.eclipse.util.EclipseFile
//import scala.tools.eclipse.util.EclipseResource
import org.scalaide.core.compiler.ScalaPresentationCompiler
import play.templates.GeneratedSourceVirtual
import play.twirl.compiler.GeneratedSourceVirtual
import org.scalaide.logging.HasLogger
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scala.collection.mutable
import org.scalaide.play2.PlayProject

/**
* presentation compiler for template files
Expand Down Expand Up @@ -118,7 +113,7 @@ class TemplatePresentationCompiler(playProject: PlayProject) extends HasLogger {
}).flatten

def destroy() = {
CompilerUsing.templateCompiler.TemplateAsFunctionCompiler.shutdownPresentationCompiler()
CompilerUsing.templateCompiler.TemplateAsFunctionCompiler.PresentationCompiler.shutdown()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.eclipse.jface.text.Region
import org.scalaide.editor.util.EditorHelper
import org.eclipse.wst.sse.ui.contentassist.{ICompletionProposalComputer, CompletionProposalInvocationContext}
import org.eclipse.core.runtime.IProgressMonitor
import play.templates.ScalaTemplateParser
import org.scalaide.play2.templateeditor.AbstractTemplateEditor
import org.scalaide.play2.templateeditor.TemplateEditor
import org.scalaide.play2.templateeditor.TemplateCompilationUnitProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import scala.util.parsing.input.CharSequenceReader
import scala.util.parsing.input.Positional
import scala.util.parsing.input.OffsetPosition
import scala.util.parsing.input.NoPosition
import play.templates.ScalaTemplateParser
import play.templates.TreeNodes._
import play.twirl.parser.TwirlParser
import play.twirl.parser.TreeNodes._

/**
* A helper for using tmeplate parser
*/
object TemplateParsing {
implicit def stringToCharSeq(str: String) = new CharSequenceReader(str)

val parser = new ScalaTemplateParser(true)
val parser = new TwirlParser(true)

sealed abstract class PlayTemplate(input: Positional, kind: String) {
val offset = input.pos match {
Expand Down Expand Up @@ -78,11 +78,11 @@ object TemplateParsing {
}

def handleTemplate(template: Template): List[PlayTemplate] = template match {
case Template(name, comment, params, imports, defs, sub, content) =>
case Template(name, comment, params, topImports, imports, defs, sub, content) =>
val namePart = if (name != null && name.str.length != 0) List(ScalaCode(name)) else List()
val commentPart = comment.map(CommentCode(_)).toList
val paramsPart = if (params.pos != NoPosition) List(ScalaCode(params)) else List()
val importsPart = imports.map(ScalaCode(_)).toList
val importsPart = (topImports ++ imports).map(ScalaCode(_)).toList
val defsPart = defs.flatMap(handleDef).toList
val subsPart = sub.flatMap(handleTemplate).toList
val contentPart = content.flatMap(handleTemplateTree).toList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent
import org.eclipse.wst.sse.core.internal.util.Debug
import org.eclipse.wst.xml.core.internal.parser.regions.RegionUpdateRule
import org.eclipse.wst.sse.core.internal.util.Utilities
import play.templates.ScalaTemplateParser
import org.eclipse.jface.text.TypedRegion
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
Expand Down
Loading