Skip to content

Commit

Permalink
added apply methods to import selectors in Quotes #21225
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Jan 26, 2025
1 parent f7e5df5 commit 77d3f4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end SimpleSelectorTypeTest

object SimpleSelector extends SimpleSelectorModule:
def apply(name: String): SimpleSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(name.toTermName)))
def unapply(x: SimpleSelector): Some[String] = Some(x.name.toString)
end SimpleSelector

Expand All @@ -1713,6 +1715,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end RenameSelectorTypeTest

object RenameSelector extends RenameSelectorModule:
def apply(fromName: String, toName: String): RenameSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(fromName.toTermName), untpd.Ident(toName.toTermName)))
def unapply(x: RenameSelector): (String, String) = (x.fromName, x.toName)
end RenameSelector

Expand All @@ -1738,6 +1742,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end OmitSelectorTypeTest

object OmitSelector extends OmitSelectorModule:
def apply(name: String): OmitSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(name.toTermName), untpd.Ident(nme.WILDCARD)))
def unapply(x: OmitSelector): Some[String] = Some(x.imported.name.toString)
end OmitSelector

Expand All @@ -1758,6 +1764,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end GivenSelectorTypeTest

object GivenSelector extends GivenSelectorModule:
def apply(bound: Option[TypeTree]): GivenSelector =
withDefaultPos(untpd.ImportSelector(
untpd.Ident(nme.EMPTY),
bound = bound.map(tpt => untpd.TypedSplice(tpt)).getOrElse(EmptyTree)
))
def unapply(x: GivenSelector): Some[Option[TypeTree]] =
Some(GivenSelectorMethods.bound(x))
end GivenSelector
Expand Down
4 changes: 4 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val SimpleSelector` */
trait SimpleSelectorModule { this: SimpleSelector.type =>
@experimental def apply(name: String): SimpleSelector
def unapply(x: SimpleSelector): Some[String]
}

Expand All @@ -2570,6 +2571,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val RenameSelector` */
trait RenameSelectorModule { this: RenameSelector.type =>
@experimental def apply(fromName: String, toName: String): RenameSelector
def unapply(x: RenameSelector): (String, String)
}

Expand Down Expand Up @@ -2597,6 +2599,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val OmitSelector` */
trait OmitSelectorModule { this: OmitSelector.type =>
@experimental def apply(name: String): OmitSelector
def unapply(x: OmitSelector): Some[String]
}

Expand All @@ -2621,6 +2624,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val GivenSelector` */
trait GivenSelectorModule { this: GivenSelector.type =>
@experimental def apply(bound: Option[TypeTree]): GivenSelector
def unapply(x: GivenSelector): Some[Option[TypeTree]]
}

Expand Down

0 comments on commit 77d3f4b

Please sign in to comment.