@@ -11,8 +11,9 @@ object Macros {
1111 val annotatedMethodsWithMainAnnotations = allMethods.flatMap { methodSymbol =>
1212 methodSymbol.getAnnotation(mainAnnotation).map(methodSymbol -> _)
1313 }.sortBy(_._1.pos.map(_.start))
14- val mainDatas = Expr .ofList(annotatedMethodsWithMainAnnotations.map { (annotatedMethod, mainAnnotationInstance) =>
15- createMainData[Any , B ](annotatedMethod, mainAnnotationInstance)
14+ val mainDatas = Expr .ofList(annotatedMethodsWithMainAnnotations.map {
15+ (annotatedMethod, mainAnnotationInstance) =>
16+ createMainData[Any , B ](annotatedMethod, mainAnnotationInstance)
1617 })
1718
1819 ' {
@@ -26,13 +27,13 @@ object Macros {
2627 import quotes .reflect ._
2728 val typeReprOfB = TypeRepr .of[B ]
2829 val companionModule = typeReprOfB match {
29- case TypeRef (a,b) => TermRef (a,b)
30+ case TypeRef (a, b) => TermRef (a, b)
3031 }
3132 val typeSymbolOfB = typeReprOfB.typeSymbol
3233 val companionModuleType = typeSymbolOfB.companionModule.tree.asInstanceOf [ValDef ].tpt.tpe.asType
3334 val companionModuleExpr = Ident (companionModule).asExpr
3435 val mainAnnotationInstance = typeSymbolOfB.getAnnotation(mainAnnotation).getOrElse {
35- ' {new mainargs.main()}.asTerm // construct a default if not found.
36+ ' { new mainargs.main() }.asTerm // construct a default if not found.
3637 }
3738 val ctor = typeSymbolOfB.primaryConstructor
3839 val ctorParams = ctor.paramSymss.flatten
@@ -58,13 +59,13 @@ object Macros {
5859 // parameters, so use those for getting the annotations instead
5960 TypeRepr .of[B ].typeSymbol.primaryConstructor.paramSymss
6061 )
61- val erasedMainData = ' {$mainData.asInstanceOf [MainData [B , Any ]]}
62+ val erasedMainData = ' { $mainData.asInstanceOf [MainData [B , Any ]] }
6263 ' { new ParserForClass [B ]($erasedMainData, () => $ { Ident (companionModule).asExpr }) }
6364 }
6465
65- def createMainData [T : Type , B : Type ](using Quotes )
66- ( method : quotes.reflect. Symbol ,
67- mainAnnotation : quotes.reflect.Term ): Expr [MainData [T , B ]] = {
66+ def createMainData [T : Type , B : Type ](using
67+ Quotes
68+ )( method : quotes.reflect. Symbol , mainAnnotation : quotes.reflect.Term ): Expr [MainData [T , B ]] = {
6869 createMainData[T , B ](method, mainAnnotation, method.paramSymss)
6970 }
7071
@@ -73,9 +74,12 @@ object Macros {
7374 import quotes .reflect .*
7475 tpe match {
7576 case AnnotatedType (AppliedType (_, Seq (arg)), x) // Scala 3 repeated parameter
76- if x.tpe =:= defn.RepeatedAnnot .typeRef => Some (arg)
77- case AppliedType (TypeRef (pre, " <repeated>" ), Seq (arg)) // Scala 2 repeated parameter, can be read from Scala 3
78- if pre =:= defn.ScalaPackage .termRef => Some (arg)
77+ if x.tpe =:= defn.RepeatedAnnot .typeRef => Some (arg)
78+ case AppliedType (
79+ TypeRef (pre, " <repeated>" ),
80+ Seq (arg)
81+ ) // Scala 2 repeated parameter, can be read from Scala 3
82+ if pre =:= defn.ScalaPackage .termRef => Some (arg)
7983 case _ => None
8084 }
8185 }
@@ -87,14 +91,18 @@ object Macros {
8791 }
8892 }
8993
90- def createMainData [T : Type , B : Type ](using Quotes )
91- (method : quotes.reflect.Symbol ,
92- mainAnnotation : quotes.reflect.Term ,
93- annotatedParamsLists : List [List [quotes.reflect.Symbol ]]): Expr [MainData [T , B ]] = {
94+ def createMainData [T : Type , B : Type ](using Quotes )(
95+ method : quotes.reflect.Symbol ,
96+ mainAnnotation : quotes.reflect.Term ,
97+ annotatedParamsLists : List [List [quotes.reflect.Symbol ]]
98+ ): Expr [MainData [T , B ]] = {
9499
95100 import quotes .reflect .*
96- val params = method.paramSymss.headOption.getOrElse(report.throwError(" Multiple parameter lists not supported" ))
97- val defaultParams = if (params.exists(_.flags.is(Flags .HasDefault ))) getDefaultParams(method) else Map .empty
101+ val params = method.paramSymss.headOption.getOrElse(
102+ report.throwError(" Multiple parameter lists not supported" )
103+ )
104+ val defaultParams =
105+ if (params.exists(_.flags.is(Flags .HasDefault ))) getDefaultParams(method) else Map .empty
98106 val argSigsExprs = params.zip(annotatedParamsLists.flatten).map { paramAndAnnotParam =>
99107 val param = paramAndAnnotParam._1
100108 val annotParam = paramAndAnnotParam._2
@@ -104,7 +112,9 @@ object Macros {
104112 case VarargTypeRepr (AsType (' [t])) => TypeRepr .of[Leftover [t]]
105113 case _ => paramTpe
106114 }
107- val arg = annotParam.getAnnotation(argAnnotation).map(_.asExprOf[mainargs.arg]).getOrElse(' { new mainargs.arg() })
115+ val arg = annotParam.getAnnotation(argAnnotation).map(_.asExprOf[mainargs.arg]).getOrElse(' {
116+ new mainargs.arg()
117+ })
108118 readerTpe.asType match {
109119 case ' [t] =>
110120 def applyAndCast (f : Expr [Any ] => Expr [Any ], arg : Expr [B ]): Expr [t] = {
@@ -119,10 +129,10 @@ object Macros {
119129 case err : Exception =>
120130 report.errorAndAbort(
121131 s """ Failed to convert default value for parameter ${param.name},
122- |expected type: ${paramTpe.show},
123- |but default value ${expr.show} is of type: ${expr.asTerm.tpe.widen.show}
124- |while converting type caught an exception with message: ${err.getMessage}
125- |There might be a bug in mainargs. """ .stripMargin,
132+ |expected type: ${paramTpe.show},
133+ |but default value ${expr.show} is of type: ${expr.asTerm.tpe.widen.show}
134+ |while converting type caught an exception with message: ${err.getMessage}
135+ |There might be a bug in mainargs. """ .stripMargin,
126136 param.pos.getOrElse(Position .ofMacroExpansion)
127137 )
128138 recoveredType
@@ -139,7 +149,10 @@ object Macros {
139149 method.pos.getOrElse(Position .ofMacroExpansion)
140150 )
141151 }
142- ' { (ArgSig .create[t, B ]($ { Expr (param.name) }, $ { arg }, $ { defaultParam })(using $ { tokensReader })) }
152+ ' {
153+ (ArgSig .create[t, B ]($ { Expr (param.name) }, $ { arg }, $ { defaultParam })(using
154+ $ { tokensReader }))
155+ }
143156 }
144157 }
145158 val argSigs = Expr .ofList(argSigsExprs)
@@ -151,28 +164,35 @@ object Macros {
151164
152165 ' { (b : B , params : Seq [Any ]) => $ { callOf(' b , ' params ) } }
153166 }
154- ' { MainData .create[T , B ]($ { Expr (method.name) }, $ { mainAnnotation.asExprOf[mainargs.main] }, $ { argSigs }, $ { invokeRaw }) }
167+ ' {
168+ MainData .create[T , B ](
169+ $ { Expr (method.name) },
170+ $ { mainAnnotation.asExprOf[mainargs.main] },
171+ $ { argSigs },
172+ $ { invokeRaw }
173+ )
174+ }
155175 }
156176
157- /** Call a method given by its symbol.
158- *
159- * E.g.
160- *
161- * assuming:
162- *
163- * def foo(x: Int, y: String)(z: Int)
164- *
165- * val argss: List[List[Any]] = ???
166- *
167- * then:
168- *
169- * call(<symbol of foo>, '{argss})
170- *
171- * will expand to:
172- *
173- * foo(argss(0)(0), argss(0)(1))(argss(1)(0))
174- *
175- */
177+ /**
178+ * Call a method given by its symbol.
179+ *
180+ * E.g.
181+ *
182+ * assuming:
183+ *
184+ * def foo(x: Int, y: String)(z: Int)
185+ *
186+ * val argss: List[List[Any]] = ???
187+ *
188+ * then:
189+ *
190+ * call(<symbol of foo>, '{argss})
191+ *
192+ * will expand to:
193+ *
194+ * foo(argss(0)(0), argss(0)(1))(argss(1)(0))
195+ */
176196 private def call (using Quotes )(
177197 methodOwner : Expr [Any ],
178198 method : quotes.reflect.Symbol ,
@@ -197,23 +217,25 @@ object Macros {
197217 for (i <- params.indices.toList) yield {
198218 val param = params(i)
199219 val tpe = methodType.memberType(param)
200- val untypedRef = ' { $ref($ {Expr (i)}) }
220+ val untypedRef = ' { $ref($ { Expr (i) }) }
201221 tpe match {
202222 case VarargTypeRepr (AsType (' [t])) =>
203223 Typed (
204224 ' { $untypedRef.asInstanceOf [Leftover [t]].value }.asTerm,
205225 Inferred (AppliedType (defn.RepeatedParamClass .typeRef, List (TypeRepr .of[t])))
206226 )
207227 case _ => tpe.asType match
208- case ' [t] => ' { $untypedRef.asInstanceOf [t] }.asTerm
228+ case ' [t] => ' { $untypedRef.asInstanceOf [t] }.asTerm
209229 }
210230 }
211231
212232 methodOwner.asTerm.select(method).appliedToArgs(accesses(args)).asExpr
213233 }
214234
215235 /** Lookup default values for a method's parameters. */
216- private def getDefaultParams (using Quotes )(method : quotes.reflect.Symbol ): Map [quotes.reflect.Symbol , Expr [Any ] => Expr [Any ]] = {
236+ private def getDefaultParams (using
237+ Quotes
238+ )(method : quotes.reflect.Symbol ): Map [quotes.reflect.Symbol , Expr [Any ] => Expr [Any ]] = {
217239 // Copy pasted from Cask.
218240 // https://github.com/com-lihaoyi/cask/blob/65b9c8e4fd528feb71575f6e5ef7b5e2e16abbd9/cask/src-3/cask/router/Macros.scala#L38
219241 import quotes .reflect ._
@@ -226,7 +248,7 @@ object Macros {
226248
227249 val idents = method.owner.tree.asInstanceOf [ClassDef ].body
228250
229- idents.foreach{
251+ idents.foreach {
230252 case deff @ DefDef (Name (idx), _, _, _) =>
231253 val expr = (owner : Expr [Any ]) => Select (owner.asTerm, deff.symbol).asExpr
232254 defaults += (params(idx.toInt - 1 ) -> expr)
0 commit comments