+
Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,27 @@ trait SealedHierarchiesPlatform extends SealedHierarchies { this: DefinitionsPla
private def extractSealedSubtypes[A: Type]: List[(String, ?<[A])] = {
forceTypeSymbolInitialization[A]

def hasPosition: Boolean = {
val position = Type[A].tpe.typeSymbol.pos
!(position.line == 0 && position.column == 0)
}

def extractRecursively(t: TypeSymbol): List[TypeSymbol] =
if (t.asClass.isSealed) t.asClass.knownDirectSubclasses.toList.map(_.asType).flatMap(extractRecursively)
else List(t)

val order =
val positionOrder =
Ordering.fromLessThan[Position]((a, b) => a.line < b.line || (a.line == b.line && a.column < b.column))

// calling .distinct here as `knownDirectSubclasses` returns duplicates for multiply-inherited types
extractRecursively(Type[A].tpe.typeSymbol.asType).distinct
.sortBy(_.pos)(order)
// calling .distinct here as `children` returns duplicates for multiply-inherited types
val allChildren = extractRecursively(Type[A].tpe.typeSymbol.asType).distinct
val sortedChildren = if (hasPosition) {
allChildren.sortBy(_.pos)(positionOrder)
} else {
allChildren.sortBy(_.name.toString)
}

sortedChildren
.map(typeSymbol => subtypeName(typeSymbol) -> subtypeTypeOf[A](typeSymbol))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ trait SealedHierarchiesPlatform extends SealedHierarchies { this: DefinitionsPla
else None

private def extractSealedSubtypes[A: Type]: List[(String, ?<[A])] = {
def hasPosition: Boolean =
TypeRepr.of[A].typeSymbol.pos.exists(pos => scala.util.Try(pos.start).isSuccess)

def extractRecursively(sym: Symbol): List[Symbol] =
if sym.flags.is(Flags.Sealed) then sym.children.flatMap(extractRecursively)
else if sym.flags.is(Flags.Enum) then List(sym.typeRef.typeSymbol)
else if sym.flags.is(Flags.Module) then List(sym.typeRef.typeSymbol.moduleClass)
else List(sym)

val order = Ordering.Option(Ordering.fromLessThan[Position] { (a, b) =>
val positionOrder = Ordering.Option(Ordering.fromLessThan[Position] { (a, b) =>
a.startLine < b.startLine || (a.startLine == b.startLine && a.startColumn < b.startColumn)
})

// calling .distinct here as `children` returns duplicates for multiply-inherited types
extractRecursively(TypeRepr.of[A].typeSymbol).distinct
.sortBy(_.pos.filter(pos => scala.util.Try(pos.start).isSuccess))(order)
val allChildren = extractRecursively(TypeRepr.of[A].typeSymbol).distinct
val sortedChildren = if hasPosition then {
allChildren.sortBy(_.pos)(positionOrder)
} else {
allChildren.sortBy(_.name)
}

sortedChildren
.map(typeSymbol => subtypeName(typeSymbol) -> subtypeTypeOf[A](typeSymbol))
}

Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载