From 35aa75dac29b2673e4334a18bb764edb979f2d78 Mon Sep 17 00:00:00 2001 From: Flavio Brasil Date: Fri, 29 Dec 2023 17:49:36 -0800 Subject: [PATCH] fix rendering of field names with backticks --- pprint/src-2.13/pprint/ProductSupport.scala | 19 ++++++++++++++++++- pprint/src-3/ProductSupport.scala | 19 ++++++++++++++++++- .../src/test/pprint/DerivationTests.scala | 7 +++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/pprint/src-2.13/pprint/ProductSupport.scala b/pprint/src-2.13/pprint/ProductSupport.scala index be854a8..1d88bee 100644 --- a/pprint/src-2.13/pprint/ProductSupport.scala +++ b/pprint/src-2.13/pprint/ProductSupport.scala @@ -2,6 +2,17 @@ package pprint object ProductSupport { + private def isIdentifier(name: String) = { + def isStart(c: Char) = (c == '_') || (c == '$') || Character.isUnicodeIdentifierStart(c) + def isPart(c: Char) = (c == '$') || Character.isUnicodeIdentifierPart(c) + name.toList match { + case first :: rest if(isStart(first)) => + rest.forall(isPart) + case _ => + false + } + } + def treeifyProductElements(x: Product, walker: Walker, escapeUnicode: Boolean, @@ -11,8 +22,14 @@ object ProductSupport { .zipWithIndex .map { case (name, i) => + val key = + if(!isIdentifier(name)) { + s"`$name`" + } else { + name + } val elem = x.productElement(i) - Tree.KeyValue(name, walker.treeify(elem, escapeUnicode, showFieldNames)) + Tree.KeyValue(key, walker.treeify(elem, escapeUnicode, showFieldNames)) } } diff --git a/pprint/src-3/ProductSupport.scala b/pprint/src-3/ProductSupport.scala index be854a8..1d88bee 100644 --- a/pprint/src-3/ProductSupport.scala +++ b/pprint/src-3/ProductSupport.scala @@ -2,6 +2,17 @@ package pprint object ProductSupport { + private def isIdentifier(name: String) = { + def isStart(c: Char) = (c == '_') || (c == '$') || Character.isUnicodeIdentifierStart(c) + def isPart(c: Char) = (c == '$') || Character.isUnicodeIdentifierPart(c) + name.toList match { + case first :: rest if(isStart(first)) => + rest.forall(isPart) + case _ => + false + } + } + def treeifyProductElements(x: Product, walker: Walker, escapeUnicode: Boolean, @@ -11,8 +22,14 @@ object ProductSupport { .zipWithIndex .map { case (name, i) => + val key = + if(!isIdentifier(name)) { + s"`$name`" + } else { + name + } val elem = x.productElement(i) - Tree.KeyValue(name, walker.treeify(elem, escapeUnicode, showFieldNames)) + Tree.KeyValue(key, walker.treeify(elem, escapeUnicode, showFieldNames)) } } diff --git a/pprint/test/src/test/pprint/DerivationTests.scala b/pprint/test/src/test/pprint/DerivationTests.scala index ff98d07..f0f9ec0 100644 --- a/pprint/test/src/test/pprint/DerivationTests.scala +++ b/pprint/test/src/test/pprint/DerivationTests.scala @@ -168,5 +168,12 @@ object DerivationTests extends TestSuite{ """C2(List(C1("hello", List("world"))))""" ) } + test("field name with backticks"){ + case class Test(`with backticks`: Boolean, withoutBackticks: Boolean) + Check.blackWhiteFields( + Test(true, false), + """Test(`with backticks` = true, withoutBackticks = false)""" + ) + } } }