Skip to content

Cleanup scaladoc quotes #12324

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

Merged
merged 9 commits into from
May 6, 2021
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
21 changes: 12 additions & 9 deletions scaladoc/src/dotty/tools/scaladoc/tasty/BasicSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import collection.JavaConverters._
import dotty.tools.scaladoc._
import scala.quoted._

import SymOps._
import ScaladocSupport._

trait BasicSupport:
self: TastyParser =>
import qctx.reflect._
object SymOps extends SymOps[qctx.type](qctx)
export SymOps._

def parseAnnotation(annotTerm: Term): Annotation =
object SymOpsWithLinkCache extends SymOpsWithLinkCache
export SymOpsWithLinkCache._


def parseAnnotation(using Quotes)(annotTerm: reflect.Term): Annotation =
import reflect._
import dotty.tools.dotc.ast.Trees.{SeqLiteral}
val dri = annotTerm.tpe.typeSymbol.dri
def inner(t: Term): List[Annotation.AnnotationParameter] = t match {
Expand All @@ -24,18 +29,17 @@ trait BasicSupport:
case other => List(Annotation.UnresolvedParameter(None, other.show))
}


val params = annotTerm match
case Apply(target, appliedWith) => {
appliedWith.flatMap(inner)
}

Annotation(dri, params)

extension (sym: Symbol)
extension (using Quotes)(sym: reflect.Symbol)
def documentation = sym.docstring.map(parseComment(_, sym.tree))

def source(using Quotes) =
def source =
val path = sym.pos.map(_.sourceFile.jpath).filter(_ != null).map(_.toAbsolutePath)
path.map(TastyMemberSource(_, sym.pos.get.startLine))

Expand All @@ -49,5 +53,4 @@ trait BasicSupport:
}.map(parseAnnotation)

def isLeftAssoc: Boolean = !sym.name.endsWith(":")


end extension
29 changes: 20 additions & 9 deletions scaladoc/src/dotty/tools/scaladoc/tasty/ClassLikeSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ import collection.JavaConverters._
import dotty.tools.scaladoc._
import dotty.tools.scaladoc.{Signature => DSignature}

import scala.quoted._

import SymOps._
import NameNormalizer._
import SyntheticsSupport._

trait ClassLikeSupport:
self: TastyParser =>
import qctx.reflect._

private def bareClasslikeKind(symbol: Symbol): Kind =
if symbol.flags.is(Flags.Module) then Kind.Object
else if symbol.flags.is(Flags.Trait) then Kind.Trait(Nil, Nil)
else if symbol.flags.is(Flags.Enum) then Kind.Enum(Nil, Nil)
else if symbol.flags.is(Flags.Enum) && symbol.flags.is(Flags.Case) then Kind.EnumCase(Kind.Object)
else Kind.Class(Nil, Nil)
private given qctx.type = qctx

private def bareClasslikeKind(using Quotes)(symbol: reflect.Symbol): Kind =
import reflect._
if symbol.flags.is(Flags.Module) then Kind.Object
else if symbol.flags.is(Flags.Trait) then Kind.Trait(Nil, Nil)
else if symbol.flags.is(Flags.Enum) then Kind.Enum(Nil, Nil)
else if symbol.flags.is(Flags.Enum) && symbol.flags.is(Flags.Case) then Kind.EnumCase(Kind.Object)
else Kind.Class(Nil, Nil)

private def kindForClasslike(classDef: ClassDef): Kind =
def typeArgs = classDef.getTypeParams.map(mkTypeArgument(_))
Expand Down Expand Up @@ -208,14 +217,16 @@ trait ClassLikeSupport:
}
).map(_.copy(inheritedFrom = inheritance))

extension (c: ClassDef)
extension (using Quotes)(c: reflect.ClassDef)

def membersToDocument = c.body.filterNot(_.symbol.isHiddenByVisibility)

def getNonTrivialInheritedMemberTrees =
c.symbol.getmembers.filterNot(s => s.isHiddenByVisibility || s.maybeOwner == c.symbol)
.filter(s => s.maybeOwner != defn.ObjectClass && s.maybeOwner != defn.AnyClass)
.map(_.tree)

extension (c: ClassDef)
def extractMembers: Seq[Member] = {
val inherited = c.getNonTrivialInheritedMemberTrees.collect {
case dd: DefDef if !dd.symbol.isClassConstructor && !(dd.symbol.isSuperBridgeMethod || dd.symbol.isDefaultHelperMethod) => dd
Expand Down Expand Up @@ -265,7 +276,7 @@ trait ClassLikeSupport:
if parentSymbol != defn.ObjectClass && parentSymbol != defn.AnyClass
yield (parentTree, parentSymbol)

def getConstructors: List[Symbol] = membersToDocument.collect {
def getConstructors: List[Symbol] = c.membersToDocument.collect {
case d: DefDef if d.symbol.isClassConstructor && c.constructor.symbol != d.symbol => d.symbol
}.toList

Expand Down Expand Up @@ -444,7 +455,7 @@ trait ClassLikeSupport:
modifiers = modifiers,
annotations = symbol.getAnnotations(),
signature = signature,
sources = symbol.source(using qctx),
sources = symbol.source,
origin = origin,
inheritedFrom = inheritedFrom,
graph = graph,
Expand Down
47 changes: 23 additions & 24 deletions scaladoc/src/dotty/tools/scaladoc/tasty/JavadocAnchorCreator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ import scala.quoted._
import dotty.tools.scaladoc.util.Escape._
import scala.util.matching.Regex

trait JavadocAnchorCreator:
self: SymOps[_] =>
object JavadocAnchorCreator:

import self.q.reflect._
private def javadocPrimitives(using Quotes)(sym: reflect.Symbol) =
import reflect.*
if sym == defn.IntClass then Some("int")
else if sym == defn.FloatClass then Some("float")
else if sym == defn.DoubleClass then Some("double")
else if sym == defn.LongClass then Some("long")
else if sym == defn.ByteClass then Some("byte")
else if sym == defn.BooleanClass then Some("boolean")
else if sym == defn.CharClass then Some("char")
else if sym == defn.ShortClass then Some("short")
else if sym == defn.ObjectClass then Some("java.lang.Object")
else None

private val javadocPrimitivesMap = Map(
defn.IntClass -> "int",
defn.FloatClass -> "float",
defn.DoubleClass -> "double",
defn.LongClass -> "long",
defn.ByteClass -> "byte",
defn.BooleanClass -> "boolean",
defn.CharClass -> "char",
defn.ShortClass -> "short",
defn.ObjectClass -> "java.lang.Object"
)

private def transformPrimitiveType(tpe: TypeRepr): String = tpe.classSymbol
.flatMap(javadocPrimitivesMap.get)
private def transformPrimitiveType(using Quotes)(tpe: reflect.TypeRepr): String = tpe.classSymbol
.flatMap(javadocPrimitives)
.filter(_ => !tpe.typeSymbol.isTypeParam)
.getOrElse(tpe.show)

private def transformType(tpe: TypeRepr): String = tpe.simplified match {
case AppliedType(tpe, typeList) if tpe.classSymbol.fold(false)(_ == defn.ArrayClass) => transformType(typeList.head) + ":A"
case AppliedType(tpe, typeList) if tpe.classSymbol.fold(false)(_ == defn.RepeatedParamClass) => transformType(typeList.head) + "..."
case AppliedType(tpe, typeList) => transformPrimitiveType(tpe)
case other => transformPrimitiveType(other)
}
private def transformType(using Quotes)(tpe: reflect.TypeRepr): String =
import reflect.*
tpe.simplified match
case AppliedType(tpe, typeList) if tpe.classSymbol.fold(false)(_ == defn.ArrayClass) => transformType(typeList.head) + ":A"
case AppliedType(tpe, typeList) if tpe.classSymbol.fold(false)(_ == defn.RepeatedParamClass) => transformType(typeList.head) + "..."
case AppliedType(tpe, typeList) => transformPrimitiveType(tpe)
case other => transformPrimitiveType(other)

def getJavadocType(s: TypeRepr) = transformType(s)
def getJavadocType(using Quotes)(s: reflect.TypeRepr) = transformType(s)
10 changes: 7 additions & 3 deletions scaladoc/src/dotty/tools/scaladoc/tasty/NameNormalizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import dotty.tools.scaladoc._
import dotty.tools.dotc.core.StdNames.nme.keywords
import dotty.tools.dotc.core.Names.termName

trait NameNormalizer { self: TastyParser =>
import qctx.reflect._
extension (s: Symbol) def normalizedName: String = {
import scala.quoted._
import SymOps._

object NameNormalizer {

extension (using Quotes)(s: reflect.Symbol) def normalizedName: String = {
import reflect.*
val withoutGivenPrefix = if s.isGiven then s.name.stripPrefix("given_") else s.name
val withoutObjectSuffix = if s.flags.is(Flags.Module) then withoutGivenPrefix.stripSuffix("$") else withoutGivenPrefix
val constructorNormalizedName = if s.isClassConstructor then "this" else withoutObjectSuffix
Expand Down
4 changes: 4 additions & 0 deletions scaladoc/src/dotty/tools/scaladoc/tasty/PackageSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package tasty

import collection.JavaConverters._

import SymOps._

trait PackageSupport:
self: TastyParser =>
import qctx.reflect._

private given qctx.type = qctx

def parsePackage(pck: PackageClause): (String, Member) =
val name = pck.symbol.fullName
(name, Member(name, pck.symbol.dri, Kind.Package))
Expand Down
20 changes: 10 additions & 10 deletions scaladoc/src/dotty/tools/scaladoc/tasty/ScalaDocSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import scala.jdk.CollectionConverters._
import dotty.tools.scaladoc.Scaladoc.CommentSyntax
import dotty.tools.scaladoc.tasty.comments.Comment

trait ScaladocSupport { self: TastyParser =>
import qctx.reflect._
import scala.quoted._

def parseCommentString(comment: String, sym: Symbol, pos: Option[Position]): Comment =
object ScaladocSupport:

def parseCommentString(using Quotes, DocContext)(comment: String, sym: reflect.Symbol, pos: Option[reflect.Position]): Comment =
import reflect.report
val preparsed = comments.Preparser.preparse(comments.Cleaner.clean(comment))

val commentSyntax =
Expand All @@ -22,22 +24,22 @@ trait ScaladocSupport { self: TastyParser =>

CommentSyntax.default
}
case None => ctx.args.defaultSyntax
case None => summon[DocContext].args.defaultSyntax
}

val parser = commentSyntax match {
case CommentSyntax.Wiki =>
comments.WikiCommentParser(comments.Repr(qctx)(sym))
comments.WikiCommentParser(comments.Repr(quotes)(sym))
case CommentSyntax.Markdown =>
comments.MarkdownCommentParser(comments.Repr(qctx)(sym))
comments.MarkdownCommentParser(comments.Repr(quotes)(sym))
}
parser.parse(preparsed)

def parseComment(docstring: String, tree: Tree): Comment =
def parseComment(using Quotes, DocContext)(docstring: String, tree: reflect.Tree): Comment =
val commentString: String =
if tree.symbol.isClassDef || tree.symbol.owner.isClassDef then
import dotty.tools.dotc
given ctx: dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx

val sym = tree.symbol.asInstanceOf[dotc.core.Symbols.Symbol]

Expand All @@ -47,5 +49,3 @@ trait ScaladocSupport { self: TastyParser =>
docstring

parseCommentString(commentString, tree.symbol, Some(tree.pos))

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import scala.quoted._
import dotty.tools.scaladoc.util.Escape._
import scala.util.matching.Regex

trait Scaladoc2AnchorCreator:
self: SymOps[_] =>
object Scaladoc2AnchorCreator:

import self.q.reflect._

implicit private val printer: Printer[Tree] = Printer.TreeShortCode

def getScaladoc2Type(t: Tree) = {
(t match {
case d: DefDef => d.show.split("def", 2)(1)
case t: TypeDef => t.show.split("type", 2)(1)
case v: ValDef => v.show.split("val|var", 2)(1)
}).replace(" ","")
}
def getScaladoc2Type(using Quotes)(t: reflect.Tree) =
import reflect.*
val regex = t match
case d: DefDef => "def"
case t: TypeDef => "type"
case v: ValDef => "val|var"
t.show(using Printer.TreeShortCode).split(regex, 2)(1).replace(" ","")
Loading