@@ -58,6 +58,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
5858 try op finally myCtx = savedCtx
5959 }
6060
61+ inline def inContextBracket (inline op : Text ): Text =
62+ val savedCtx = myCtx
63+ try op finally myCtx = savedCtx
64+
6165 def withoutPos (op : => Text ): Text = {
6266 val savedPrintPos = printPos
6367 printPos = false
@@ -308,7 +312,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
308312 blockText(block.stats :+ block.expr)
309313
310314 protected def blockText [T >: Untyped ](trees : List [Tree [T ]]): Text =
311- (" {" ~ toText(trees, " \n " ) ~ " }" ).close
315+ inContextBracket {
316+ (" {" ~ toText(trees, " \n " ) ~ " }" ).close
317+ }
312318
313319 protected def typeApplyText [T >: Untyped ](tree : TypeApply [T ]): Text = {
314320 val funText = toTextLocal(tree.fun)
@@ -598,7 +604,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
598604 typeDefText(tparamsTxt, optText(rhs)(" = " ~ _))
599605 }
600606 recur(rhs, " " , true )
601- case Import (expr, selectors) =>
607+ case tree @ Import (expr, selectors) =>
608+ myCtx = myCtx.importContext(tree, tree.symbol)
602609 keywordText(" import " ) ~ importText(expr, selectors)
603610 case Export (expr, selectors) =>
604611 keywordText(" export " ) ~ importText(expr, selectors)
@@ -965,7 +972,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
965972 }
966973 else impl.body
967974
968- val bodyText = " {" ~~ selfText ~ toTextGlobal(primaryConstrs ::: body, " \n " ) ~ " }"
975+ val bodyText = inContextBracket(
976+ " {" ~~ selfText ~ toTextGlobal(primaryConstrs ::: body, " \n " ) ~ " }" )
969977
970978 prefix ~
971979 keywordText(" extends" ).provided(! ofNew && impl.parents.nonEmpty) ~~ parentsText ~
@@ -988,7 +996,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
988996 protected def packageDefText (tree : PackageDef ): Text = {
989997 val statsText = tree.stats match {
990998 case (pdef : PackageDef ) :: Nil => toText(pdef)
991- case _ => toTextGlobal(tree.stats, " \n " )
999+ case _ => inContextBracket( toTextGlobal(tree.stats, " \n " ) )
9921000 }
9931001 val bodyText =
9941002 if (currentPrecedence == TopLevelPrec ) " \n " ~ statsText else " {" ~ statsText ~ " }"
0 commit comments