Skip to content

Commit 40bffaf

Browse files
committed
Reformat with scalafmt 3.7.17
Executed command: scalafmt --non-interactive
1 parent ed74940 commit 40bffaf

21 files changed

+341
-229
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: 64 additions & 45 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,7 +74,7 @@ object Macros {
7374
import quotes.reflect.*
7475
tpe match {
7576
case AnnotatedType(AppliedType(_, Seq(arg)), x)
76-
if x.tpe =:= defn.RepeatedAnnot.typeRef => Some(arg)
77+
if x.tpe =:= defn.RepeatedAnnot.typeRef => Some(arg)
7778
case _ => None
7879
}
7980
}
@@ -85,14 +86,18 @@ object Macros {
8586
}
8687
}
8788

88-
def createMainData[T: Type, B: Type](using Quotes)
89-
(method: quotes.reflect.Symbol,
90-
mainAnnotation: quotes.reflect.Term,
91-
annotatedParamsLists: List[List[quotes.reflect.Symbol]]): Expr[MainData[T, B]] = {
89+
def createMainData[T: Type, B: Type](using Quotes)(
90+
method: quotes.reflect.Symbol,
91+
mainAnnotation: quotes.reflect.Term,
92+
annotatedParamsLists: List[List[quotes.reflect.Symbol]]
93+
): Expr[MainData[T, B]] = {
9294

9395
import quotes.reflect.*
94-
val params = method.paramSymss.headOption.getOrElse(report.throwError("Multiple parameter lists not supported"))
95-
val defaultParams = if (params.exists(_.flags.is(Flags.HasDefault))) getDefaultParams(method) else Map.empty
96+
val params = method.paramSymss.headOption.getOrElse(
97+
report.throwError("Multiple parameter lists not supported")
98+
)
99+
val defaultParams =
100+
if (params.exists(_.flags.is(Flags.HasDefault))) getDefaultParams(method) else Map.empty
96101
val argSigsExprs = params.zip(annotatedParamsLists.flatten).map { paramAndAnnotParam =>
97102
val param = paramAndAnnotParam._1
98103
val annotParam = paramAndAnnotParam._2
@@ -102,7 +107,9 @@ object Macros {
102107
case VarargTypeRepr(AsType('[t])) => TypeRepr.of[Leftover[t]]
103108
case _ => paramTpe
104109
}
105-
val arg = annotParam.getAnnotation(argAnnotation).map(_.asExprOf[mainargs.arg]).getOrElse('{ new mainargs.arg() })
110+
val arg = annotParam.getAnnotation(argAnnotation).map(_.asExprOf[mainargs.arg]).getOrElse('{
111+
new mainargs.arg()
112+
})
106113
readerTpe.asType match {
107114
case '[t] =>
108115
def applyAndCast(f: Expr[Any] => Expr[Any], arg: Expr[B]): Expr[t] = {
@@ -117,10 +124,10 @@ object Macros {
117124
case err: Exception =>
118125
report.errorAndAbort(
119126
s"""Failed to convert default value for parameter ${param.name},
120-
|expected type: ${paramTpe.show},
121-
|but default value ${expr.show} is of type: ${expr.asTerm.tpe.widen.show}
122-
|while converting type caught an exception with message: ${err.getMessage}
123-
|There might be a bug in mainargs.""".stripMargin,
127+
|expected type: ${paramTpe.show},
128+
|but default value ${expr.show} is of type: ${expr.asTerm.tpe.widen.show}
129+
|while converting type caught an exception with message: ${err.getMessage}
130+
|There might be a bug in mainargs.""".stripMargin,
124131
param.pos.getOrElse(Position.ofMacroExpansion)
125132
)
126133
recoveredType
@@ -137,7 +144,10 @@ object Macros {
137144
method.pos.getOrElse(Position.ofMacroExpansion)
138145
)
139146
}
140-
'{ (ArgSig.create[t, B](${ Expr(param.name) }, ${ arg }, ${ defaultParam })(using ${ tokensReader })) }
147+
'{
148+
(ArgSig.create[t, B](${ Expr(param.name) }, ${ arg }, ${ defaultParam })(using
149+
${ tokensReader }))
150+
}
141151
}
142152
}
143153
val argSigs = Expr.ofList(argSigsExprs)
@@ -149,28 +159,35 @@ object Macros {
149159

150160
'{ (b: B, params: Seq[Any]) => ${ callOf('b, 'params) } }
151161
}
152-
'{ MainData.create[T, B](${ Expr(method.name) }, ${ mainAnnotation.asExprOf[mainargs.main] }, ${ argSigs }, ${ invokeRaw }) }
162+
'{
163+
MainData.create[T, B](
164+
${ Expr(method.name) },
165+
${ mainAnnotation.asExprOf[mainargs.main] },
166+
${ argSigs },
167+
${ invokeRaw }
168+
)
169+
}
153170
}
154171

155-
/** Call a method given by its symbol.
156-
*
157-
* E.g.
158-
*
159-
* assuming:
160-
*
161-
* def foo(x: Int, y: String)(z: Int)
162-
*
163-
* val argss: List[List[Any]] = ???
164-
*
165-
* then:
166-
*
167-
* call(<symbol of foo>, '{argss})
168-
*
169-
* will expand to:
170-
*
171-
* foo(argss(0)(0), argss(0)(1))(argss(1)(0))
172-
*
173-
*/
172+
/**
173+
* Call a method given by its symbol.
174+
*
175+
* E.g.
176+
*
177+
* assuming:
178+
*
179+
* def foo(x: Int, y: String)(z: Int)
180+
*
181+
* val argss: List[List[Any]] = ???
182+
*
183+
* then:
184+
*
185+
* call(<symbol of foo>, '{argss})
186+
*
187+
* will expand to:
188+
*
189+
* foo(argss(0)(0), argss(0)(1))(argss(1)(0))
190+
*/
174191
private def call(using Quotes)(
175192
methodOwner: Expr[Any],
176193
method: quotes.reflect.Symbol,
@@ -195,23 +212,25 @@ object Macros {
195212
for (i <- params.indices.toList) yield {
196213
val param = params(i)
197214
val tpe = methodType.memberType(param)
198-
val untypedRef = '{ $ref(${Expr(i)}) }
215+
val untypedRef = '{ $ref(${ Expr(i) }) }
199216
tpe match {
200217
case VarargTypeRepr(AsType('[t])) =>
201218
Typed(
202219
'{ $untypedRef.asInstanceOf[Leftover[t]].value }.asTerm,
203220
Inferred(AppliedType(defn.RepeatedParamClass.typeRef, List(TypeRepr.of[t])))
204221
)
205222
case _ => tpe.asType match
206-
case '[t] => '{ $untypedRef.asInstanceOf[t] }.asTerm
223+
case '[t] => '{ $untypedRef.asInstanceOf[t] }.asTerm
207224
}
208225
}
209226

210227
methodOwner.asTerm.select(method).appliedToArgs(accesses(args)).asExpr
211228
}
212229

213230
/** Lookup default values for a method's parameters. */
214-
private def getDefaultParams(using Quotes)(method: quotes.reflect.Symbol): Map[quotes.reflect.Symbol, Expr[Any] => Expr[Any]] = {
231+
private def getDefaultParams(using
232+
Quotes
233+
)(method: quotes.reflect.Symbol): Map[quotes.reflect.Symbol, Expr[Any] => Expr[Any]] = {
215234
// Copy pasted from Cask.
216235
// https://github.com/com-lihaoyi/cask/blob/65b9c8e4fd528feb71575f6e5ef7b5e2e16abbd9/cask/src-3/cask/router/Macros.scala#L38
217236
import quotes.reflect._
@@ -224,7 +243,7 @@ object Macros {
224243

225244
val idents = method.owner.tree.asInstanceOf[ClassDef].body
226245

227-
idents.foreach{
246+
idents.foreach {
228247
case deff @ DefDef(Name(idx), _, _, _) =>
229248
val expr = (owner: Expr[Any]) => Select(owner.asTerm, deff.symbol).asExpr
230249
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)