From 8fbf1907cec7fa89ac9aeb8182d1ac3510f36fac Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sat, 17 Aug 2019 13:08:12 -0700 Subject: [PATCH] Allow custom show for a scope I am writing a plugin that uses mangled task keys that are very hard to read. It is helpful to be able to override the show config for these scopes so that they look reasonable in supershell and in error reporting. --- main-settings/src/main/scala/sbt/Def.scala | 14 +++++---- main-settings/src/main/scala/sbt/Scope.scala | 32 +++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/main-settings/src/main/scala/sbt/Def.scala b/main-settings/src/main/scala/sbt/Def.scala index a55ea4979..7db51382a 100644 --- a/main-settings/src/main/scala/sbt/Def.scala +++ b/main-settings/src/main/scala/sbt/Def.scala @@ -54,12 +54,14 @@ object Def extends Init[Scope] with TaskMacroExtra { keyNameColor: Option[String] = None, ): Show[ScopedKey[_]] = Show[ScopedKey[_]]( - key => - Scope.display( - key.scope, - withColor(key.key.label, keyNameColor), - ref => displayRelative2(current, ref) - ) + key => { + val color: String => String = withColor(_, keyNameColor) + key.scope.extra.toOption + .flatMap(_.get(Scope.customShowString).map(color)) + .getOrElse { + Scope.display(key.scope, color(key.key.label), ref => displayRelative2(current, ref)) + } + } ) private[sbt] def showShortKey( diff --git a/main-settings/src/main/scala/sbt/Scope.scala b/main-settings/src/main/scala/sbt/Scope.scala index d241dcdfa..bdf026b3e 100644 --- a/main-settings/src/main/scala/sbt/Scope.scala +++ b/main-settings/src/main/scala/sbt/Scope.scala @@ -178,6 +178,8 @@ object Scope { ): String = displayMasked(scope, sep, showProject, mask, false) + val customShowString = AttributeKey[String]("scope-custom-show-string") + /** * unified slash style introduced in sbt 1.1.0. * By default, sbt will no longer display the Zero-config, @@ -193,20 +195,22 @@ object Scope { showZeroConfig: Boolean ): String = { import scope.{ project, config, task, extra } - val zeroConfig = if (showZeroConfig) "Zero /" else "" - val configPrefix = config.foldStrict(display, zeroConfig, "./") - val taskPrefix = task.foldStrict(_.label + " /", "", "./") - val extras = extra.foldStrict(_.entries.map(_.toString).toList, Nil, Nil) - val postfix = if (extras.isEmpty) "" else extras.mkString("(", ", ", ")") - if (scope == GlobalScope) "Global / " + sep + postfix - else - mask.concatShow( - appendSpace(projectPrefix(project, showProject)), - appendSpace(configPrefix), - appendSpace(taskPrefix), - sep, - postfix - ) + extra.toOption.flatMap(_.get(customShowString)).getOrElse { + val zeroConfig = if (showZeroConfig) "Zero /" else "" + val configPrefix = config.foldStrict(display, zeroConfig, "./") + val taskPrefix = task.foldStrict(_.label + " /", "", "./") + val extras = extra.foldStrict(_.entries.map(_.toString).toList, Nil, Nil) + val postfix = if (extras.isEmpty) "" else extras.mkString("(", ", ", ")") + if (scope == GlobalScope) "Global / " + sep + postfix + else + mask.concatShow( + appendSpace(projectPrefix(project, showProject)), + appendSpace(configPrefix), + appendSpace(taskPrefix), + sep, + postfix + ) + } } private[sbt] def appendSpace(s: String): String =