Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 311 lines (276 sloc) 13.358 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/* sbt -- Simple Build Tool
* Copyright 2010 Mark Harrah
*/
package xsbt.api

import xsbti.api._

trait Show[A]
{
def show(a: A): String
}

final class ShowLazy[A](delegate: => Show[A]) extends Show[A]
{
private lazy val s = delegate
def show(a: A) = s.show(a)
}

import ShowAPI._

object ShowAPI
{
def Show[T](implicit s: Show[T]): Show[T] = s
def show[T](t: T)(implicit s: Show[T]): String = s.show(t)

def bounds(lower: Type, upper: Type)(implicit t: Show[Type]): String =
">: " + t.show(lower) + " <: " + t.show(upper)

import ParameterModifier._
def parameterModifier(base: String, pm: ParameterModifier): String =
pm match
{
case Plain => base
case Repeated => base + "*"
case ByName => "=> " + base
}

def concat[A](list: Seq[A], as: Show[A], sep: String): String = mapSeq(list, as).mkString(sep)
def commas[A](list: Seq[A], as: Show[A]): String = concat(list, as, ", ")
def spaced[A](list: Seq[A], as: Show[A]): String = concat(list, as, " ")
def lines[A](list: Seq[A], as: Show[A]): String = mapSeq(list, as).mkString("\n")
def mapSeq[A](list: Seq[A], as: Show[A]): Seq[String] = list.map(as.show)
}

trait ShowBase
{
implicit def showAnnotation(implicit as: Show[AnnotationArgument], t: Show[Type]): Show[Annotation] =
new Show[Annotation] { def show(a: Annotation) = "@" + t.show(a.base) + (if(a.arguments.isEmpty) "" else "(" + commas(a.arguments, as) + ")") }

implicit def showAnnotationArgument: Show[AnnotationArgument] =
new Show[AnnotationArgument] { def show(a: AnnotationArgument) = a.name + " = " + a.value }

import Variance._
implicit def showVariance: Show[Variance] =
new Show[Variance] { def show(v: Variance) = v match { case Invariant => ""; case Covariant => "+"; case Contravariant => "-" } }

implicit def showSource(implicit ps: Show[Package], ds: Show[Definition]): Show[SourceAPI] =
new Show[SourceAPI] { def show(a: SourceAPI) = lines(a.packages, ps) + "\n" + lines(a.definitions, ds) }

implicit def showPackage: Show[Package] =
new Show[Package] { def show(pkg: Package) = "package " + pkg.name }

implicit def showAccess(implicit sq: Show[Qualified]): Show[Access] =
new Show[Access]
{
def show(a: Access) =
a match
{
case p: Public => ""
case q: Qualified => sq.show(q)
}
}
implicit def showQualified(implicit sq: Show[Qualifier]): Show[Qualified] =
new Show[Qualified]
{
def show(q: Qualified) =
((q match
{
case p: Protected => "protected"
case p: Private => "private"
})
+ sq.show(q.qualifier) )
}
implicit def showQualifier: Show[Qualifier] =
new Show[Qualifier]
{
def show(q: Qualifier) =
q match
{
case _: Unqualified => ""
case _: ThisQualifier => "[this]"
case i: IdQualifier => "[" + i.value + "]"
}
}
implicit def showModifiers: Show[Modifiers] =
new Show[Modifiers]
{
def show(m: Modifiers) =
{
val mods =
(m.isOverride, "override") ::
(m.isFinal, "final") ::
(m.isSealed, "sealed") ::
(m.isImplicit, "implicit") ::
(m.isAbstract, "abstract") ::
(m.isLazy, "lazy") ::
Nil
mods.filter(_._1).map(_._2).mkString(" ")
}
}

implicit def showDefinitionType: Show[DefinitionType] =
new Show[DefinitionType] {
import DefinitionType._
def show(dt: DefinitionType) =
dt match
{
case Trait => "trait"
case ClassDef => "class"
case Module => "object"
case PackageModule => "package object"
}
}
}
trait ShowDefinitions
{
implicit def showVal(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], t: Show[Type]): Show[Val] =
new Show[Val] { def show(v: Val) = definitionBase(v, "val")(acs, ms, ans) + ": " + t.show(v.tpe) }

implicit def showVar(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], t: Show[Type]): Show[Var] =
new Show[Var] { def show(v: Var) = definitionBase(v, "var")(acs, ms, ans) + ": " + t.show(v.tpe) }

implicit def showDef(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], tp: Show[Seq[TypeParameter]], vp: Show[Seq[ParameterList]], t: Show[Type]): Show[Def] =
new Show[Def] { def show(d: Def) = parameterizedDef(d, "def")(acs, ms, ans, tp) + vp.show(d.valueParameters) + ": " + t.show(d.returnType) }

implicit def showClassLike(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], tp: Show[Seq[TypeParameter]], dt: Show[DefinitionType], s: Show[Structure], t: Show[Type]): Show[ClassLike] =
new Show[ClassLike] { def show(cl: ClassLike) = parameterizedDef(cl, dt.show(cl.definitionType))(acs, ms, ans, tp) + " requires " + t.show(cl.selfType) + " extends " + s.show(cl.structure) }

implicit def showTypeAlias(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], tp: Show[Seq[TypeParameter]], t: Show[Type]): Show[TypeAlias] =
new Show[TypeAlias] { def show(ta: TypeAlias) = parameterizedDef(ta, "type")(acs, ms, ans, tp) + " = " + t.show(ta.tpe) }

implicit def showTypeDeclaration(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], tp: Show[Seq[TypeParameter]], t: Show[Type]): Show[TypeDeclaration] =
new Show[TypeDeclaration] { def show(td: TypeDeclaration) = parameterizedDef(td, "type")(acs, ms, ans, tp) + bounds(td.lowerBound, td.upperBound) }
def showClassLikeSimple(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], tp: Show[Seq[TypeParameter]], dt: Show[DefinitionType]): Show[ClassLike] =
new Show[ClassLike] { def show(cl: ClassLike) = parameterizedDef(cl, dt.show(cl.definitionType))(acs, ms, ans, tp) }

def parameterizedDef(d: ParameterizedDefinition, label: String)(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation], tp: Show[Seq[TypeParameter]]): String =
definitionBase(d, label)(acs, ms, ans) + tp.show(d.typeParameters)
def definitionBase(d: Definition, label: String)(implicit acs: Show[Access], ms: Show[Modifiers], ans: Show[Annotation]): String =
space(spaced(d.annotations, ans)) + space(acs.show(d.access)) + space(ms.show(d.modifiers)) + space(label) + d.name
def space(s: String) = if(s.isEmpty) s else s + " "
}
trait ShowDefinition
{
implicit def showDefinition(implicit vl: Show[Val], vr: Show[Var], ds: Show[Def], cl: Show[ClassLike], ta: Show[TypeAlias], td: Show[TypeDeclaration]): Show[Definition] =
new Show[Definition]
{
def show(d: Definition) =
d match
{
case v: Val => vl.show(v)
case v: Var => vr.show(v)
case d: Def => ds.show(d)
case c: ClassLike => cl.show(c)
case t: TypeAlias => ta.show(t)
case t: TypeDeclaration => td.show(t)
}
}
}
trait ShowType
{
implicit def showType(implicit s: Show[SimpleType], a: Show[Annotated], st: Show[Structure], c: Show[Constant], e: Show[Existential], po: Show[Polymorphic]): Show[Type] =
new Show[Type]
{
def show(t: Type) =
t match
{
case q: SimpleType => s.show(q)
case q: Constant => c.show(q)
case q: Annotated => a.show(q)
case q: Structure => st.show(q)
case q: Existential => e.show(q)
case q: Polymorphic => po.show(q)
}
}

implicit def showSimpleType(implicit pr: Show[Projection], pa: Show[ParameterRef], si: Show[Singleton], et: Show[EmptyType], p: Show[Parameterized]): Show[SimpleType] =
new Show[SimpleType] {
def show(t: SimpleType) =
t match
{
case q: Projection => pr.show(q)
case q: ParameterRef => pa.show(q)
case q: Singleton => si.show(q)
case q: EmptyType => et.show(q)
case q: Parameterized => p.show(q)
}
}
}
trait ShowBasicTypes
{
implicit def showSingleton(implicit p: Show[Path]): Show[Singleton] =
new Show[Singleton] { def show(s: Singleton) = p.show(s.path) }
implicit def showEmptyType: Show[EmptyType] =
new Show[EmptyType] { def show(e: EmptyType) = "<empty>" }
implicit def showParameterRef: Show[ParameterRef] =
new Show[ParameterRef] { def show(p: ParameterRef) = "<" + p.id + ">" }
}
trait ShowTypes
{
implicit def showStructure(implicit t: Show[Type], d: Show[Definition]): Show[Structure] =
new Show[Structure] {
def show(s: Structure) =
// don't show inherited to avoid dealing with cycles
concat(s.parents, t, " with ") + "\n{\n" + lines(s.declared, d) + "\n}"
}
implicit def showAnnotated(implicit as: Show[Annotation], t: Show[Type]): Show[Annotated] =
new Show[Annotated] { def show(a: Annotated) = spaced(a.annotations, as) + " " + t.show(a.baseType) }
implicit def showProjection(implicit t: Show[SimpleType]): Show[Projection] =
new Show[Projection] { def show(p: Projection) = t.show(p.prefix) + "#" + p.id }
implicit def showParameterized(implicit t: Show[Type]): Show[Parameterized] =
new Show[Parameterized] { def show(p: Parameterized) = t.show(p.baseType) + mapSeq(p.typeArguments, t).mkString("[", ", ", "]") }
implicit def showConstant(implicit t: Show[Type]): Show[Constant] =
new Show[Constant] { def show(c: Constant) = t.show(c.baseType) + "(" + c.value + ")" }
implicit def showExistential(implicit t: Show[Type], tp: Show[TypeParameter]): Show[Existential] =
new Show[Existential] {
def show(e: Existential) =
t.show(e.baseType) + e.clause.map(t => "type " + tp.show(t)).mkString(" forSome { ", "; ", "}")
}
implicit def showPolymorphic(implicit t: Show[Type], tps: Show[Seq[TypeParameter]]): Show[Polymorphic] =
new Show[Polymorphic] { def show(p: Polymorphic) = t.show(p.baseType) + tps.show(p.parameters) }

}

trait ShowPath
{
implicit def showPath(implicit pc: Show[PathComponent]): Show[Path] =
new Show[Path] { def show(p: Path) = mapSeq(p.components, pc).mkString(".") }

implicit def showPathComponent(implicit sp: Show[Path]): Show[PathComponent] =
new Show[PathComponent] {
def show(p: PathComponent) =
p match
{
case s: Super => "super[" + sp.show(s.qualifier) + "]"
case _: This => "this"
case i: Id => i.id
}
}
}

trait ShowValueParameters
{
implicit def showParameterLists(implicit pl: Show[ParameterList]): Show[Seq[ParameterList]] =
new Show[Seq[ParameterList]] { def show(p: Seq[ParameterList]) = concat(p,pl, "") }
implicit def showParameterList(implicit mp: Show[MethodParameter]): Show[ParameterList] =
new Show[ParameterList] { def show(pl: ParameterList) = "(" + (if(pl.isImplicit) "implicit " else "") + commas(pl.parameters, mp) + ")" }

implicit def showMethodParameter(implicit t: Show[Type]): Show[MethodParameter] =
new Show[MethodParameter] {
def show(mp: MethodParameter) =
mp.name + ": " + parameterModifier(t.show(mp.tpe), mp.modifier) + (if(mp.hasDefault) "= ..." else "")
}
}
trait ShowTypeParameters
{
implicit def showTypeParameters(implicit as: Show[TypeParameter]): Show[Seq[TypeParameter]] =
new Show[Seq[TypeParameter]] { def show(tps: Seq[TypeParameter]) = if(tps.isEmpty) "" else mapSeq(tps, as).mkString("[", ",", "]") }
implicit def showTypeParameter(implicit as: Show[Annotation], tp: Show[Seq[TypeParameter]], t: Show[Type], v: Show[Variance]): Show[TypeParameter] =
new Show[TypeParameter] {
def show(tps: TypeParameter) =
spaced(tps.annotations, as) + " " + v.show(tps.variance) + tps.id + tp.show(tps.typeParameters) + " " + bounds(tps.lowerBound, tps.upperBound)
}
}

// this class is a hack to resolve some diverging implicit errors.
// I'm pretty sure the cause is the Show[Seq[T]] dominating Show[X] issue.
// It could probably be reduced a bit if that is the case (below was trial and error)
object DefaultShowAPI extends ShowBase with ShowBasicTypes with ShowValueParameters
{
def apply(d: Definition) = ShowAPI.show(d)
def apply(d: Type) = ShowAPI.show(d)

implicit lazy val showVal: Show[Val] = Cyclic.showVal
implicit lazy val showVar: Show[Var] = Cyclic.showVar
implicit lazy val showClassLike: Show[ClassLike] = Cyclic.showClassLike
implicit lazy val showTypeDeclaration: Show[TypeDeclaration] = Cyclic.showTypeDeclaration
implicit lazy val showTypeAlias: Show[TypeAlias] = Cyclic.showTypeAlias
implicit lazy val showDef: Show[Def] = Cyclic.showDef

implicit lazy val showProj: Show[Projection] = Cyclic.showProjection
implicit lazy val showPoly: Show[Polymorphic] = Cyclic.showPolymorphic

implicit lazy val showSimple: Show[SimpleType] = new ShowLazy(Cyclic.showSimpleType)
implicit lazy val showAnnotated: Show[Annotated] = Cyclic.showAnnotated
implicit lazy val showExistential: Show[Existential] = Cyclic.showExistential
implicit lazy val showConstant: Show[Constant] = Cyclic.showConstant
implicit lazy val showParameterized: Show[Parameterized] = Cyclic.showParameterized

implicit lazy val showTypeParameters: Show[Seq[TypeParameter]] = new ShowLazy(Cyclic.showTypeParameters)
implicit lazy val showTypeParameter: Show[TypeParameter] = Cyclic.showTypeParameter

implicit lazy val showDefinition: Show[Definition] = new ShowLazy(Cyclic.showDefinition)
implicit lazy val showType: Show[Type] = new ShowLazy(Cyclic.showType)
implicit lazy val showStructure: Show[Structure] = new ShowLazy(Cyclic.showStructure)

implicit lazy val showPath: Show[Path] = new ShowLazy(Cyclic.showPath)
implicit lazy val showPathComponent: Show[PathComponent] = Cyclic.showPathComponent

private object Cyclic extends ShowTypes with ShowType with ShowPath with ShowDefinition with ShowDefinitions with ShowTypeParameters
}
Something went wrong with that request. Please try again.