Skip to content

Commit 1d3a9ba

Browse files
committed
Reformat with scalafmt 3.7.17
Executed command: scalafmt --non-interactive
1 parent a9da25e commit 1d3a9ba

21 files changed

+360
-246
lines changed

mainargs/src-2/Macros.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Macros(val c: Context) {
9898

9999
val defaults = for ((arg0, i) <- flattenedArgLists.zipWithIndex) yield {
100100
val arg = TermName(c.freshName())
101-
hasDefault(i) match{
101+
hasDefault(i) match {
102102
case None => q"_root_.scala.None"
103103
case Some(defaultName) =>
104104
q"_root_.scala.Some[$curCls => ${arg0.info}](($arg: $curCls) => $arg.${newTermName(defaultName)}: ${arg0.info})"

mainargs/src-3/Macros.scala

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

mainargs/src-3/ParserForClassCompanionVersionSpecific.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package mainargs
22

33
import scala.language.experimental.macros
44

5-
private [mainargs] trait ParserForClassCompanionVersionSpecific {
5+
private[mainargs] trait ParserForClassCompanionVersionSpecific {
66
inline def apply[T]: ParserForClass[T] = ${ Macros.parserForClass[T] }
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package mainargs
22

3-
private [mainargs] trait ParserForMethodsCompanionVersionSpecific {
3+
private[mainargs] trait ParserForMethodsCompanionVersionSpecific {
44
inline def apply[B](base: B): ParserForMethods[B] = ${ Macros.parserForMethods[B]('base) }
55
}

mainargs/src/Invoker.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Invoker {
55
cep: TokensReader.Class[T],
66
args: Seq[String],
77
allowPositional: Boolean,
8-
allowRepeats: Boolean,
8+
allowRepeats: Boolean
99
): Result[T] = construct(cep, args, allowPositional, allowRepeats, Util.nullNameMapper)
1010

1111
def construct[T](
@@ -40,9 +40,9 @@ object Invoker {
4040
Right(ParamResult.Success(Flag(kvs.contains(a)).asInstanceOf[T]))
4141
case r: TokensReader.Simple[T] => Right(makeReadCall(kvs, base, a, r))
4242
case r: TokensReader.Constant[T] => Right(r.read() match {
43-
case Left(s) => ParamResult.Failure(Seq(Result.ParamError.Failed(a, Nil, s)))
44-
case Right(v) => ParamResult.Success(v)
45-
})
43+
case Left(s) => ParamResult.Failure(Seq(Result.ParamError.Failed(a, Nil, s)))
44+
case Right(v) => ParamResult.Success(v)
45+
})
4646
case r: TokensReader.Leftover[T, _] => Right(makeReadVarargsCall(a, extras, r))
4747
case r: TokensReader.Class[T] =>
4848
Left(
@@ -91,7 +91,8 @@ object Invoker {
9191
mains: MethodMains[B],
9292
args: Seq[String],
9393
allowPositional: Boolean,
94-
allowRepeats: Boolean): Either[Result.Failure.Early, (MainData[Any, B], Result[Any])] = {
94+
allowRepeats: Boolean
95+
): Either[Result.Failure.Early, (MainData[Any, B], Result[Any])] = {
9596
runMains(mains, args, allowPositional, allowRepeats, Util.nullNameMapper)
9697
}
9798
def runMains[B](
@@ -126,16 +127,20 @@ object Invoker {
126127
case Seq(main) => groupArgs(main, args)
127128
case multiple =>
128129
args.toList match {
129-
case List() => Left(Result.Failure.Early.SubcommandNotSpecified(multiple.map(_.name(nameMapper))))
130+
case List() =>
131+
Left(Result.Failure.Early.SubcommandNotSpecified(multiple.map(_.name(nameMapper))))
130132
case head :: tail =>
131133
if (head.startsWith("-")) {
132134
Left(Result.Failure.Early.SubcommandSelectionDashes(head))
133135
} else {
134-
multiple.find{ m =>
136+
multiple.find { m =>
135137
val name = m.name(nameMapper)
136138
name == head || (m.mainName.isEmpty && m.defaultName == head)
137139
} match {
138-
case None => Left(Result.Failure.Early.UnableToFindSubcommand(multiple.map(_.name(nameMapper)), head))
140+
case None => Left(Result.Failure.Early.UnableToFindSubcommand(
141+
multiple.map(_.name(nameMapper)),
142+
head
143+
))
139144
case Some(main) => groupArgs(main, tail)
140145
}
141146
}

0 commit comments

Comments
 (0)