Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rendering of field names with backticks #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pprint/src-2.13/pprint/ProductSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
}
}

Expand Down
19 changes: 18 additions & 1 deletion pprint/src-3/ProductSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
}
}

Expand Down
7 changes: 7 additions & 0 deletions pprint/test/src/test/pprint/DerivationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)"""
)
}
}
}