11package dotty .xml .interpolator
22package internal
33
4- import scala .language .implicitConversions
54import scala .quoted ._
65
7- import dotty . xml . interpolator . internal . Tree ._
6+ import Tree .*
87
9- object Expand {
8+ object Expand :
109
11- def apply (nodes : Seq [Node ])(implicit ctx : XmlContext , q : Quotes ): Expr [scala.xml.Node | scala.xml.NodeBuffer ] = {
12- if ( nodes.size == 1 ) expandNode(nodes.head).asInstanceOf [Expr [scala.xml.Node ]]
10+ def apply (nodes : Seq [Node ])(using XmlContext , Quotes ): Expr [scala.xml.Node | scala.xml.NodeBuffer ] =
11+ if nodes.size == 1 then expandNode(nodes.head).asInstanceOf [Expr [scala.xml.Node ]]
1312 else expandNodes(nodes)
14- }
1513
16- private def expandNode (node : Node )(implicit ctx : XmlContext , q : Quotes ): Expr [Any ] = {
17- node match {
14+ private def expandNode (node : Node )(using XmlContext , Quotes ): Expr [Any ] =
15+ node match
1816 case group : Group => expandGroup(group)
1917 case elem : Elem => expandElem(elem)
2018 case text : Text => expandText(text)
@@ -24,33 +22,32 @@ object Expand {
2422 case procInstr : ProcInstr => expandProcInstr(procInstr)
2523 case entityRef : EntityRef => expandEntityRef(entityRef)
2624 case unparsed : Unparsed => expandUnparsed(unparsed)
27- }
28- }
25+ end expandNode
2926
30- private def expandNodes (nodes : Seq [Node ])(implicit ctx : XmlContext , q : Quotes ): Expr [scala.xml.NodeBuffer ] = {
31- nodes.foldLeft(' { new _root_. scala.xml.NodeBuffer () })(( expr, node) => ' { $expr &+ $ {expandNode(node)} } )
32- }
27+ private def expandNodes (nodes : Seq [Node ])(using XmlContext , Quotes ): Expr [scala.xml.NodeBuffer ] =
28+ nodes.foldLeft(' { scala.xml.NodeBuffer () }): ( expr, node) =>
29+ ' { $expr &+ $ { expandNode(node) } }
3330
34- private def expandGroup (group : Group )(implicit ctx : XmlContext , q : Quotes ): Expr [scala.xml.Group ] =
35- ' { new _root_. scala.xml.Group ($ {expandNodes(group.nodes)}) }
31+ private def expandGroup (group : Group )(using XmlContext , Quotes ): Expr [scala.xml.Group ] =
32+ ' { scala.xml.Group ($ { expandNodes(group.nodes) }) }
3633
37- private def expandElem (elem : Elem )(implicit ctx : XmlContext , q : Quotes ): Expr [scala.xml.Elem ] = {
34+ private def expandElem (elem : Elem )(using ctx : XmlContext , q : Quotes ): Expr [scala.xml.Elem ] =
3835 val (namespaces, attributes) = elem.attributes.partition(_.isNamespace)
39- val prefix = if ( elem.prefix.nonEmpty) Expr (elem.prefix) else ' { null : String }
36+ val prefix = if elem.prefix.nonEmpty then Expr (elem.prefix) else ' { null }
4037 val label = Expr (elem.label)
4138 val attributes1 = expandAttributes(attributes)
4239 val scope = expandNamespaces(namespaces)
4340 val empty = Expr (elem.end.isEmpty)
44- val child = expandNodes(elem.children)(new XmlContext (ctx.args, scope), q)
45- if ( elem.children.isEmpty)
46- ' { new _root_. scala.xml.Elem ($prefix, $label, $attributes1, $scope, $empty) }
41+ val child = expandNodes(elem.children)(using new XmlContext (ctx.args, scope), q)
42+ if elem.children.isEmpty then
43+ ' { new scala.xml.Elem ($prefix, $label, $attributes1, $scope, $empty) }
4744 else
48- ' { new _root_. scala.xml.Elem ($prefix, $label, $attributes1, $scope, $empty, _root_. scala.xml.NodeSeq .seqToNodeSeq($child): _ * ) }
49- }
45+ ' { new scala.xml.Elem ($prefix, $label, $attributes1, $scope, $empty, scala.xml.NodeSeq .seqToNodeSeq($child)* ) }
46+ end expandElem
5047
51- private def expandAttributes (attributes : Seq [Attribute ])(implicit ctx : XmlContext , q : Quotes ): Expr [scala.xml.MetaData ] = {
48+ private def expandAttributes (attributes : Seq [Attribute ])(using XmlContext , Quotes ): Expr [scala.xml.MetaData ] =
5249 import quotes .reflect ._
53- attributes.foldRight(' { _root_.scala.xml.Null }: Expr [scala.xml.MetaData ])(( attribute, rest) => {
50+ attributes.foldRight(' { _root_.scala.xml.Null }: Expr [scala.xml.MetaData ]): ( attribute, rest) =>
5451 val value = attribute.value match {
5552 case Seq (v) => expandNode(v)
5653 case vs => expandNodes(vs)
@@ -71,57 +68,52 @@ object Expand {
7168 */
7269
7370 val term = value.asTerm
74- if ( term.tpe <:< TypeRepr .of[String ]) {
71+ if term.tpe <:< TypeRepr .of[String ] then
7572 val value = term.asExprOf[String ]
76- if ( attribute.prefix.isEmpty) ' { new _root_. scala.xml.UnprefixedAttribute ($ {Expr (attribute.key)}, $value, $rest) }
77- else ' { new _root_. scala.xml.PrefixedAttribute ($ {Expr (attribute.prefix)}, $ {Expr (attribute.key)}, $value, $rest) }
78- } else if ( term.tpe <:< TypeRepr .of[collection.Seq [scala.xml.Node ]]) {
73+ if attribute.prefix.isEmpty then ' { scala.xml.UnprefixedAttribute ($ { Expr (attribute.key) }, $value, $rest) }
74+ else ' { scala.xml.PrefixedAttribute ($ { Expr (attribute.prefix) }, $ { Expr (attribute.key) }, $value, $rest) }
75+ else if term.tpe <:< TypeRepr .of[collection.Seq [scala.xml.Node ]] then
7976 val value = term.asExprOf[collection.Seq [scala.xml.Node ]]
80- if ( attribute.prefix.isEmpty) ' { new _root_. scala.xml.UnprefixedAttribute ($ {Expr (attribute.key)}, $value, $rest) }
81- else ' { new _root_. scala.xml.PrefixedAttribute ($ {Expr (attribute.prefix)}, $ {Expr (attribute.key)}, $value, $rest) }
82- } else {
77+ if attribute.prefix.isEmpty then ' { scala.xml.UnprefixedAttribute ($ { Expr (attribute.key) }, $value, $rest) }
78+ else ' { scala.xml.PrefixedAttribute ($ { Expr (attribute.prefix) }, $ { Expr (attribute.key) }, $value, $rest) }
79+ else
8380 val value = term.asExprOf[Option [collection.Seq [scala.xml.Node ]]]
84- if (attribute.prefix.isEmpty) ' { new _root_.scala.xml.UnprefixedAttribute ($ {Expr (attribute.key)}, $value, $rest) }
85- else ' { new _root_.scala.xml.PrefixedAttribute ($ {Expr (attribute.prefix)}, $ {Expr (attribute.key)}, $value, $rest) }
86- }
87- })
88- }
81+ if attribute.prefix.isEmpty then ' { scala.xml.UnprefixedAttribute ($ { Expr (attribute.key) }, $value, $rest) }
82+ else ' { scala.xml.PrefixedAttribute ($ { Expr (attribute.prefix) }, $ { Expr (attribute.key) }, $value, $rest) }
83+ end expandAttributes
8984
90- private def expandNamespaces (namespaces : Seq [Attribute ])(implicit ctx : XmlContext , q : Quotes ): Expr [scala.xml. NamespaceBinding ] = {
85+ private def expandNamespaces (namespaces : Seq [Attribute ])(using XmlContext , Quotes ): Expr [Scope ] =
9186 import quotes .reflect ._
92- namespaces.foldLeft(ctx.scope)(( rest, namespace) => {
93- val prefix = if ( namespace.prefix.nonEmpty) Expr (namespace.key) else ' { null : String }
94- val uri = (namespace.value.head: @ unchecked) match {
87+ namespaces.foldLeft(ctx.scope): ( rest, namespace) =>
88+ val prefix = if namespace.prefix.nonEmpty then Expr (namespace.key) else ' { null }
89+ val uri = (namespace.value.head: @ unchecked) match
9590 case Text (text) => Expr (text)
9691 case Placeholder (id) =>
97- val call = ' { $ {ctx.args(id)}(using _root_. scala.xml.TopScope ) }
92+ val call = ' { $ {ctx.args(id)}(using scala.xml.TopScope ) }
9893 Expr .betaReduce(call).asExprOf[String ]
99- }
100- ' { new _root_.scala.xml.NamespaceBinding ($prefix, $uri, $rest) }
101- })
102- }
94+ ' { scala.xml.NamespaceBinding ($prefix, $uri, $rest) }
95+ end expandNamespaces
10396
10497 private def expandText (text : Text )(using Quotes ): Expr [scala.xml.Text ] =
105- ' { new _root_. scala.xml.Text ($ {Expr (text.text)}) }
98+ ' { scala.xml.Text ($ { Expr (text.text) }) }
10699
107100 private def expandComment (comment : Comment )(using Quotes ): Expr [scala.xml.Comment ] =
108- ' { new _root_. scala.xml.Comment ($ {Expr (comment.text)}) }
101+ ' { scala.xml.Comment ($ { Expr (comment.text) }) }
109102
110- private def expandPlaceholder (placeholder : Placeholder )(implicit ctx : XmlContext , q : Quotes ): Expr [Any ] = {
103+ private def expandPlaceholder (placeholder : Placeholder )(using XmlContext , Quotes ): Expr [Any ] =
111104 val arg = ctx.args(placeholder.id)
112- val scope = ctx.scope
113- Expr .betaReduce(' { $arg(using $scope) })
114- }
105+ Expr .betaReduce(' { $arg(using $ { ctx.scope }) })
115106
116107 private def expandPCData (pcdata : PCData )(using Quotes ): Expr [scala.xml.PCData ] =
117- ' { new _root_. scala.xml.PCData ($ {Expr (pcdata.data)}) }
108+ ' { scala.xml.PCData ($ { Expr (pcdata.data) }) }
118109
119110 private def expandProcInstr (instr : ProcInstr )(using Quotes ): Expr [scala.xml.ProcInstr ] =
120- ' { new _root_. scala.xml.ProcInstr ($ {Expr (instr.target)}, $ {Expr (instr.proctext)}) }
111+ ' { scala.xml.ProcInstr ($ { Expr (instr.target) }, $ { Expr (instr.proctext) }) }
121112
122113 private def expandEntityRef (ref : EntityRef )(using Quotes ): Expr [scala.xml.EntityRef ] =
123- ' { new _root_. scala.xml.EntityRef ($ {Expr (ref.name)}) }
114+ ' { scala.xml.EntityRef ($ { Expr (ref.name) }) }
124115
125116 private def expandUnparsed (unparsed : Unparsed )(using Quotes ): Expr [scala.xml.Unparsed ] =
126- ' { new _root_.scala.xml.Unparsed ($ {Expr (unparsed.data)}) }
127- }
117+ ' { scala.xml.Unparsed ($ { Expr (unparsed.data) }) }
118+
119+ end Expand
0 commit comments