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

Limit when companion extends FunctionN #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
scalaz-deriving
Copyright 2017 - 2018 Sam Halliday
Copyright 2017 - 2019 Sam Halliday

This project is licensed under the terms of the LGPL 3.0 or any later
version, see LICENSE.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package scalaz.plugins.deriving

import scala.Predef.ArrowAssoc
import scala.deprecated
import scala.collection.immutable.Map
import scala.collection.immutable.{ Map, StringOps }
import scala.collection.breakOut
import scala.reflect.internal.util._
import scala.tools.nsc._
Expand Down Expand Up @@ -55,10 +55,50 @@ abstract class AnnotationPlugin(override val global: Global) extends Plugin {
def isIde: Boolean = global.isInstanceOf[tools.nsc.interactive.Global]
def isScaladoc: Boolean = global.isInstanceOf[tools.nsc.doc.ScaladocGlobal]

private case class PrettyPrinter(
level: Int,
inQuotes: Boolean,
backslashed: Boolean
) {
val indent = List.fill(level)(" ").mkString

def transform(char: Char): (PrettyPrinter, String) = {
val (pp, f): (PrettyPrinter, PrettyPrinter => String) = char match {
case '"' if inQuotes && !backslashed =>
(copy(inQuotes = false), _ => s"$char")
case '"' if !inQuotes => (copy(inQuotes = true), _ => s"$char")
case '\\' if inQuotes && !backslashed =>
(copy(backslashed = true), _ => s"$char")

case ',' if !inQuotes => (this, p => s",\n${p.indent}")
case '(' if !inQuotes =>
(copy(level = level + 1), p => s"(\n${p.indent}")
case ')' if !inQuotes =>
(copy(level = level - 1), p => s"\n${p.indent})")
case _ => (this, _ => s"$char")
}
(pp, f(pp))
}
}

private def prettyPrint(raw: String): String =
new StringOps(raw)
.foldLeft((PrettyPrinter(0, false, false), new StringBuilder(""))) {
case ((pp, sb), char) =>
val (newPP, res) = pp.transform(char)
(newPP, sb.append(res))
}
._2
.toString
.replaceAll("""\(\s+\)""", "()")

private def showTree(tree: Tree, pretty: Boolean): String =
if (pretty) prettyPrint(showRaw(tree)) else showRaw(tree)

// best way to inspect a tree, just call this
def debug(name: String, tree: Tree): Unit =
def debug(name: String, tree: Tree, pretty: Boolean = false): Unit =
Predef.println(
s"====\n$name ${tree.id} ${tree.pos}:\n${showCode(tree)}\n${showRaw(tree)}"
s"====\n$name ${tree.id} ${tree.pos}:\n${showCode(tree)}\n${showTree(tree, pretty)}"
)

// recovers the final part of an annotation
Expand Down Expand Up @@ -139,6 +179,20 @@ abstract class AnnotationPlugin(override val global: Global) extends Plugin {
case _ => false
}

private def getConstructor(clazz: ClassDef): Option[DefDef] =
clazz.impl.collect {
case d @ DefDef(_, nme.CONSTRUCTOR, _, _, _, _) => d
}.headOption

// A simple constructor is one with only one parameter group and no implicit parameters
private def hasSimpleConstructor(clazz: ClassDef): Boolean =
getConstructor(clazz) match {
case Some(DefDef(_, _, _, ps :: Nil, _, _))
if !ps.exists(_.mods.hasFlag(Flag.IMPLICIT)) =>
true
case _ => false
}

/** generates a zero-functionality companion for a class */
private def genCompanion(clazz: ClassDef): ModuleDef = {
val mods =
Expand All @@ -158,6 +212,7 @@ abstract class AnnotationPlugin(override val global: Global) extends Plugin {
def sup =
if (isCase &&
clazz.tparams.isEmpty &&
hasSimpleConstructor(clazz) &&
addSuperFunction(clazz) &&
accessors.size <= 22) {
AppliedTypeTree(
Expand Down