@@ -32,6 +32,7 @@ import dotty.tools.dotc.util.SourcePosition
32
32
import dotty .tools .dotc .ast .untpd .{MemberDef , Modifiers , PackageDef , RefTree , Template , TypeDef , ValOrDefDef }
33
33
import cc .*
34
34
import dotty .tools .dotc .parsing .JavaParsers
35
+ import dotty .tools .dotc .transform .TreeExtractors .BinaryOp
35
36
36
37
class RefinedPrinter (_ctx : Context ) extends PlainPrinter (_ctx) {
37
38
@@ -387,6 +388,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
387
388
388
389
def optDotPrefix (tree : This ) = optText(tree.qual)(_ ~ " ." ) provided ! isLocalThis(tree)
389
390
391
+ /** Should a binary operation with this operator be printed infix? */
392
+ def isInfix (op : Symbol ) =
393
+ op.exists && (op.isDeclaredInfix || op.name.isOperatorName)
394
+
390
395
def caseBlockText (tree : Tree ): Text = tree match {
391
396
case Block (stats, expr) => toText(stats :+ expr, " \n " )
392
397
case expr => toText(expr)
@@ -478,6 +483,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
478
483
optDotPrefix(tree) ~ keywordStr(" this" ) ~ idText(tree)
479
484
case Super (qual : This , mix) =>
480
485
optDotPrefix(qual) ~ keywordStr(" super" ) ~ optText(mix)(" [" ~ _ ~ " ]" )
486
+ case BinaryOp (l, op, r) if isInfix(op) =>
487
+ val isRightAssoc = op.name.endsWith(" :" )
488
+ val opPrec = parsing.precedence(op.name)
489
+ val leftPrec = if isRightAssoc then opPrec + 1 else opPrec
490
+ val rightPrec = if ! isRightAssoc then opPrec + 1 else opPrec
491
+ changePrec(opPrec):
492
+ atPrec(leftPrec)(toText(l)) ~ " " ~ toText(op.name) ~ " " ~ atPrec(rightPrec)(toText(r))
481
493
case app @ Apply (fun, args) =>
482
494
if (fun.hasType && fun.symbol == defn.throwMethod)
483
495
changePrec (GlobalPrec ) {
@@ -504,7 +516,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
504
516
}
505
517
}
506
518
case Typed (expr, tpt) =>
507
- changePrec(InfixPrec ) {
519
+ changePrec(DotPrec ) {
508
520
if isWildcardStarArg(tree) then
509
521
expr match
510
522
case Ident (nme.WILDCARD_STAR ) =>
0 commit comments