@@ -32,6 +32,7 @@ import dotty.tools.dotc.util.SourcePosition
3232import dotty .tools .dotc .ast .untpd .{MemberDef , Modifiers , PackageDef , RefTree , Template , TypeDef , ValOrDefDef }
3333import cc .*
3434import dotty .tools .dotc .parsing .JavaParsers
35+ import dotty .tools .dotc .transform .TreeExtractors .BinaryOp
3536
3637class RefinedPrinter (_ctx : Context ) extends PlainPrinter (_ctx) {
3738
@@ -387,6 +388,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
387388
388389 def optDotPrefix (tree : This ) = optText(tree.qual)(_ ~ " ." ) provided ! isLocalThis(tree)
389390
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+
390395 def caseBlockText (tree : Tree ): Text = tree match {
391396 case Block (stats, expr) => toText(stats :+ expr, " \n " )
392397 case expr => toText(expr)
@@ -478,6 +483,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
478483 optDotPrefix(tree) ~ keywordStr(" this" ) ~ idText(tree)
479484 case Super (qual : This , mix) =>
480485 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))
481493 case app @ Apply (fun, args) =>
482494 if (fun.hasType && fun.symbol == defn.throwMethod)
483495 changePrec (GlobalPrec ) {
0 commit comments