Skip to content

Scaladoc/Inkuire-M3 #13454

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 16 commits into from
Sep 7, 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
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ object Build {
import _root_.scala.sys.process._
import _root_.scala.concurrent._
import ExecutionContext.Implicits.global
val inkuireVersion = "1.0.0-M2"
val inkuireVersion = "1.0.0-M3"
val inkuireLink = s"https://github.com/VirtusLab/Inkuire/releases/download/$inkuireVersion/inkuire.js"
val inkuireDestinationFile = (Compile / resourceManaged).value / "dotty_res" / "scripts" / "inkuire.js"
sbt.IO.touch(inkuireDestinationFile)
Expand Down
3 changes: 2 additions & 1 deletion scaladoc-js/src/searchbar/PageEntry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ case class InkuireMatch(
functionName: String,
packageLocation: String,
pageLocation: String,
entryType: String
entryType: String,
mq: Int
)

object PageEntry {
Expand Down
13 changes: 12 additions & 1 deletion scaladoc-js/src/searchbar/SearchbarComponent.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotty.tools.scaladoc

import org.scalajs.dom._
import org.scalajs.dom.ext._
import org.scalajs.dom.html.Input
import scala.scalajs.js.timers._
import scala.concurrent.duration._
Expand Down Expand Up @@ -40,6 +41,7 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
val wrapper = document.createElement("div").asInstanceOf[html.Div]
wrapper.classList.add("scaladoc-searchbar-result")
wrapper.classList.add("monospace")
wrapper.setAttribute("mq", m.mq.toString)

val resultDiv = document.createElement("div").asInstanceOf[html.Div]
resultDiv.classList.add("scaladoc-searchbar-result-row")
Expand Down Expand Up @@ -132,7 +134,16 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
loading.appendChild(animation)
properResultsDiv.appendChild(loading)
inkuireEngine.query(query) { (m: InkuireMatch) =>
properResultsDiv.appendChild(m.toHTML)
val next = properResultsDiv.children.foldLeft[Option[Element]](None) {
case (acc, child) if !acc.isEmpty => acc
case (_, child) =>
Option.when(child.hasAttribute("mq") && Integer.parseInt(child.getAttribute("mq")) > m.mq)(child)
}
next.fold {
properResultsDiv.appendChild(m.toHTML)
} { next =>
properResultsDiv.insertBefore(m.toHTML, next)
}
} { (s: String) =>
animation.classList.remove("loading")
properResultsDiv.appendChild(s.toHTMLError)
Expand Down
3 changes: 2 additions & 1 deletion scaladoc-js/src/searchbar/engine/InkuireJSSearchEngine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class InkuireJSSearchEngine {
d.functionName.asInstanceOf[String],
d.packageLocation.asInstanceOf[String],
d.pageLocation.asInstanceOf[String],
d.entryType.asInstanceOf[String]
d.entryType.asInstanceOf[String],
d.mq.asInstanceOf[Int]
)
}

Expand Down
4 changes: 4 additions & 0 deletions scaladoc-testcases/src/tests/anImplicitClass.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tests.anImplicitClass

implicit class AddingOps(n: Int):
def inc: Int = n + 1
6 changes: 6 additions & 0 deletions scaladoc-testcases/src/tests/caseClassesWithVars.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tests.caseClassesWithVars

case class Sheep(var numberOfLegs: Int):
var name: String = "Lawrence"

class Goat(var numberOfLegs: Int)
5 changes: 5 additions & 0 deletions scaladoc-testcases/src/tests/justmethods.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tests.justmethods

trait Animal

def whatSoundDoIMake(animal: Animal): String = "IoIo"
11 changes: 11 additions & 0 deletions scaladoc-testcases/src/tests/syntaxes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.syntaxes

class DoingStuffOps[A](a: A):
def doStuff: Unit = ()

trait DoingStuffSyntax:
implicit def toDoingStuffOps[A](a: A): DoingStuffOps[A] = DoingStuffOps(a)

trait AllSyntaxes extends DoingStuffSyntax

object doingstuff extends AllSyntaxes
60 changes: 56 additions & 4 deletions scaladoc/src/dotty/tools/scaladoc/Inkuire.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import dotty.tools.scaladoc.util._
object Inkuire {

var db = InkuireDb(Seq.empty, Map.empty, Seq.empty, Map.empty)
var implicitConversions: Seq[(Option[TypeLike], Type)] = Seq.empty

def beforeSave(): Unit = {
db = db.copy(
functions = db.functions.sortBy(_.hashCode),
functions = functions.sortBy(_.hashCode),
types = db.types.toSeq.sortBy(_._1.uuid).toMap,
implicitConversions = db.implicitConversions.sortBy(_._1.uuid)
implicitConversions = implicitConversions.collect {
case (Some(from), to) => from -> to
}.sortBy(_._1.hashCode)
)
}

Expand All @@ -34,10 +37,59 @@ object Inkuire {
case _ => e
}

def functions: Seq[ExternalSignature] = Inkuire.db.functions.flatMap { func =>
val fromConversions = Inkuire.implicitConversions.filter { ic =>
func.signature.receiver.nonEmpty && matchingICTypes(ic._2, func.signature.receiver.get.typ)
}.map {
case (Some(from), to) =>
func.copy(
signature = func.signature.copy(
receiver = func.signature.receiver.map { rcvr =>
Contravariance(
newReceiver(rcvr.typ, to, from)
)
}
)
)
case (None, to) =>
func.copy(
signature = func.signature.copy(
receiver = None
)
)
}
Seq(func) ++ fromConversions
}
.distinct

def matchingICTypes(a: TypeLike, b: TypeLike): Boolean = (a, b) match {
case (a: Type, b: Type) if a.params.size == 0 && b.params.size == 0 && a.itid == b.itid => true
case (a: Type, b: Type) if a.params.size == 1 && b.params.size == 1 && a.itid == b.itid =>
a.params.head.typ.isInstanceOf[Type] && a.params.head.typ.asInstanceOf[Type].isVariable &&
b.params.head.typ.isInstanceOf[Type] && b.params.head.typ.asInstanceOf[Type].isVariable
case _ => false
}

def newReceiver(old: TypeLike, to: TypeLike, from: TypeLike): TypeLike = (old, to) match {
case (a: Type, b: Type) if a.params.size == 0 && b.params.size == 0 && a.itid == b.itid => from
case (a: Type, b: Type) if a.params.size == 1 && b.params.size == 1 && a.itid == b.itid
&& a.params.head.typ.isInstanceOf[Type] && a.params.head.typ.asInstanceOf[Type].isVariable &&
b.params.head.typ.isInstanceOf[Type] && b.params.head.typ.asInstanceOf[Type].isVariable =>
if from.isInstanceOf[Type] && from.asInstanceOf[Type].params.size == 1 && from.asInstanceOf[Type].params.head.typ.isInstanceOf[Type] && from.asInstanceOf[Type].params.head.typ.asInstanceOf[Type].isVariable then
from.asInstanceOf[Type].copy(
params = Seq(Contravariance(a.params.head.typ.asInstanceOf[Type]))
)
else if from.isInstanceOf[Type] && from.asInstanceOf[Type].isVariable then
a.params.head.typ.asInstanceOf[Type]
else
from
case _ => old
}

case class InkuireDb(
functions: Seq[ExternalSignature],
types: Map[ITID, (Type, Seq[Type])],
implicitConversions: Seq[(ITID, Type)],
implicitConversions: Seq[(TypeLike, Type)],
typeAliases: Map[ITID, TypeLike]
)

Expand Down Expand Up @@ -164,7 +216,7 @@ object Inkuire {
)
}

private def serializeConversion(conversion: (ITID, Type)): JSON = {
private def serializeConversion(conversion: (TypeLike, Type)): JSON = {
jsonList(
Seq(
serialize(conversion._1),
Expand Down
128 changes: 1 addition & 127 deletions scaladoc/src/dotty/tools/scaladoc/tasty/ClassLikeSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,133 +100,7 @@ trait ClassLikeSupport:
deprecated = classDef.symbol.isDeprecated()
)

if summon[DocContext].args.generateInkuire then {

val classType: Inkuire.Type = classDef.asInkuire(Set.empty).asInstanceOf[Inkuire.Type]

def varName(t: Inkuire.TypeLike): Option[String] = t match {
case tpe: Inkuire.Type => Some(tpe.name.name)
case tl: Inkuire.TypeLambda => varName(tl.result)
case _ => None
}

val variableNames: Set[String] = classType.params.map(_.typ)
.flatMap(varName(_).toList).toSet

val parents: Seq[Inkuire.Type] = classDef.parents.map(_.asInkuire(variableNames).asInstanceOf[Inkuire.Type])

val isModule = classDef.symbol.flags.is(Flags.Module)

if !isModule then Inkuire.db = Inkuire.db.copy(types = Inkuire.db.types.updated(classType.itid.get, (classType, parents)))

classDef.symbol.declaredTypes
.filter(viableSymbol)
.foreach {
case typeSymbol: Symbol if typeSymbol.flags.is(Flags.Opaque) =>
val typ = typeSymbol.tree.asInkuire(variableNames)
if typ.isInstanceOf[Inkuire.Type] then {
val t = typ.asInstanceOf[Inkuire.Type]
Inkuire.db = Inkuire.db.copy(types = Inkuire.db.types.updated(t.itid.get, (t, Seq.empty)))
}
case typeSymbol: Symbol if !typeSymbol.isClassDef =>
val typeDef = typeSymbol.tree.asInstanceOf[TypeDef]
val typ = typeSymbol.tree.asInkuire(variableNames)
if typ.isInstanceOf[Inkuire.Type] then {
val t = typ.asInstanceOf[Inkuire.Type]
val rhsTypeLike = typeDef.rhs.asInkuire(variableNames)
Inkuire.db = Inkuire.db.copy(
typeAliases = Inkuire.db.typeAliases.updated(t.itid.get, rhsTypeLike),
types = Inkuire.db.types.updated(t.itid.get, (t, Seq.empty))
)
}
if typeDef.rhs.symbol.flags.is(Flags.JavaDefined) then
val typJava = typeDef.rhs.asInkuire(variableNames)
if typJava.isInstanceOf[Inkuire.Type] then {
val tJava = typJava.asInstanceOf[Inkuire.Type]
Inkuire.db = Inkuire.db.copy(types = Inkuire.db.types.updated(tJava.itid.get, (tJava, Seq.empty)))
}
case _ =>
}

def viableSymbol(s: Symbol): Boolean =
!s.flags.is(Flags.Private) &&
!s.flags.is(Flags.Protected) &&
!s.flags.is(Flags.Override) &&
!s.flags.is(Flags.Synthetic)

classDef.symbol.declaredMethods
.filter(viableSymbol)
.foreach {
case implicitConversion: Symbol if implicitConversion.flags.is(Flags.Implicit)
&& classDef.symbol.flags.is(Flags.Module)
&& implicitConversion.owner.fullName == ("scala.Predef$") =>
val defdef = implicitConversion.tree.asInstanceOf[DefDef]
val to = defdef.returnTpt.asInkuire(variableNames)
val from = defdef.paramss.flatMap(_.params).collectFirst {
case v: ValDef => v.tpt.asInkuire(variableNames)
}
(from, to) match
case (Some(from: Inkuire.Type), to: Inkuire.Type) => Inkuire.db = Inkuire.db.copy(implicitConversions = Inkuire.db.implicitConversions :+ (from.itid.get -> to))
case _ =>

case methodSymbol: Symbol =>
val defdef = methodSymbol.tree.asInstanceOf[DefDef]
val methodVars = defdef.paramss.flatMap(_.params).collect {
case TypeDef(name, _) => name
}
val vars = variableNames ++ methodVars
val receiver: Option[Inkuire.TypeLike] =
Some(classType)
.filter(_ => !isModule)
.orElse(methodSymbol.extendedSymbol.flatMap(s => partialAsInkuire(vars).lift(s.tpt)))
val sgn = Inkuire.ExternalSignature(
signature = Inkuire.Signature(
receiver = receiver,
arguments = methodSymbol.nonExtensionTermParamLists.collect {
case tpc@TermParamClause(params) if !tpc.isImplicit && !tpc.isGiven => params //TODO [Inkuire] Implicit parameters
}.flatten.map(_.tpt.asInkuire(vars)),
result = defdef.returnTpt.asInkuire(vars),
context = Inkuire.SignatureContext(
vars = vars.toSet,
constraints = Map.empty //TODO [Inkuire] Type bounds
)
),
name = methodSymbol.name,
packageName = methodSymbol.dri.location,
uri = methodSymbol.dri.externalLink.getOrElse(""),
entryType = "def"
)
val curriedSgn = sgn.copy(signature = Inkuire.curry(sgn.signature))
Inkuire.db = Inkuire.db.copy(functions = Inkuire.db.functions :+ curriedSgn)
}

classDef.symbol.declaredFields
.filter(viableSymbol)
.foreach {
case valSymbol: Symbol =>
val valdef = valSymbol.tree.asInstanceOf[ValDef]
val receiver: Option[Inkuire.TypeLike] =
Some(classType)
.filter(_ => !isModule)
val sgn = Inkuire.ExternalSignature(
signature = Inkuire.Signature(
receiver = receiver,
arguments = Seq.empty,
result = valdef.tpt.asInkuire(variableNames),
context = Inkuire.SignatureContext(
vars = variableNames.toSet,
constraints = Map.empty //TODO [Inkuire] Type bounds
)
),
name = valSymbol.name,
packageName = valSymbol.dri.location,
uri = valSymbol.dri.externalLink.getOrElse(""),
entryType = "val"
)
val curriedSgn = sgn.copy(signature = Inkuire.curry(sgn.signature))
Inkuire.db = Inkuire.db.copy(functions = Inkuire.db.functions :+ curriedSgn)
}
}
if summon[DocContext].args.generateInkuire then doInkuireStuff(classDef)

if signatureOnly then baseMember else baseMember.copy(
members = classDef.extractPatchedMembers.sortBy(m => (m.name, m.kind.name)),
Expand Down
Loading