This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
WIP: Very early preview of generator merging #45
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.0.4 | ||
sbt.version = 1.1.0-RC4 | ||
74 changes: 74 additions & 0 deletions
74
scalagen/src/main/scala/org/scalameta/scalagen/GeneratorApplication.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.scalameta.scalagen | ||
|
||
import scala.meta._ | ||
import scala.meta.contrib._ | ||
import scala.meta.gen._ | ||
|
||
/** | ||
* All transformations, manipulations etc. should completely noop, by default. | ||
* | ||
* They should return the exact identical object thats inputed. | ||
* | ||
* Structural equality is slow, thus we want to remain as efficient as possible here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's possible to implement much faster structural equality than what we currently have, it will still be nut slow compared to referential equality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another undocumented point is that |
||
*/ | ||
object GeneratorApplication { | ||
|
||
def apply(c: Defn.Class, g: Generator): Defn.Class = { | ||
|
||
generateCompanionExtensions(c, g) | ||
|
||
val extended: Defn.Class = c.prepend(g.extend(c)) | ||
val transformed: Defn.Class = g.manipulate(c) | ||
val transmuted: List[Stat] = g.transmute(c) | ||
|
||
val wasExtended = extended ne c | ||
val wasTransformed = transformed ne c | ||
val wasTransmuted = !(transmuted.lengthCompare(1) == 0 && (transmuted.head eq c)) | ||
|
||
if (wasTransformedMultipleTimes(wasExtended, wasTransformed, wasTransmuted)) { | ||
abortDueToMultipleTransform(c, g) | ||
} | ||
|
||
if (wasExtended) { | ||
extended | ||
} else if (wasTransformed) { | ||
transformed | ||
} else if (wasTransmuted) { | ||
populateTransmutations(c, transmuted, g) | ||
c | ||
} else { | ||
c | ||
} | ||
} | ||
|
||
private def abortDueToMultipleTransform(c: Defn.Class, g: Generator) = { | ||
abort( | ||
s"""Generator: ${g.name} tried to transform the tree ${c.name.syntax} multiple times | ||
| | ||
|This error can appear when a generator implements more then one of the following | ||
| | ||
| extend() | ||
| transmute() | ||
| manipulate() | ||
| | ||
""".stripMargin) | ||
} | ||
|
||
private def wasTransformedMultipleTimes(booleans: Boolean*) = | ||
booleans.count(_ == true) > 1 | ||
|
||
private def populateTransmutations(t: Tree, stats: List[Stat], g: Generator): Unit = { | ||
t.owner match { | ||
case Some(own) => ??? | ||
case _ => | ||
} | ||
} | ||
|
||
private def generateCompanionExtensions(c: Defn.Class, g: Generator): Unit = { | ||
g.extendCompanion(c) match { | ||
case Nil => | ||
case other => ??? | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
scalagen/src/main/scala/scala/meta/gen/implicits/ExtractAndReplaceExtras.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package scala.meta.gen.implicits | ||
|
||
import scala.meta.contrib._ | ||
|
||
trait ExtractAndReplaceExtras { | ||
|
||
implicit class XtensionExtractAndReplace[A](a: A) { | ||
def prepend[B](bs: List[B])(implicit rev: Replace[A, B], eev: Extract[A, B]): A = | ||
replaceShortCircuited(bs ::: eev.extract(a)) | ||
|
||
def replaceShortCircuited[B](bs: List[B])(implicit rev: Replace[A, B], eev: Extract[A, B]): A = | ||
if (eev.extract(a) eq bs) { | ||
a | ||
} else { | ||
rev.replace(a, bs) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
scalagen/src/main/scala/scala/meta/gen/implicits/implicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package scala.meta.gen.implicits | ||
|
||
trait implicits extends Traversal with Owner with FindCompanion | ||
trait implicits extends Traversal with Owner with FindCompanion with ExtractAndReplaceExtras |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.1.0 is out now! 🎉