Skip to content

Commit

Permalink
TokenStructure: print only variable payload (#3671)
Browse files Browse the repository at this point in the history
All fixed-value tokens (whitespace, keywords, symbols etc) can be fully
described by their type and don't need to show underlying syntax.

Also, don't drop parens for variable payload which happens to be empty.
  • Loading branch information
kitbellew committed Mar 23, 2024
1 parent 40f5690 commit 8b19031
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 497 deletions.
Expand Up @@ -2,28 +2,39 @@ package scala.meta
package internal
package prettyprinters

import scala.meta.internal.tokens.Chars
import scala.meta.prettyprinters._
import Show.{sequence => s, wrap => w}
import scala.meta.tokens._
import scala.meta.prettyprinters.Show.{sequence => s}
import scala.meta.tokens.Token
import scala.meta.tokens.Token._

object TokenStructure {
def apply[T <: Token]: Structure[T] = {
Structure[Token] { x =>
implicit val dialect = x.dialect
val syntax = x match {
case _: Token.Tab => "\\t"
case _: Token.CR => "\\r"
case _: Token.LF => "\\n"
case _: Token.FF => "\\f"
case _: Token.LFLF => "\\n\\n"
case x => x.text
val text = x match {
case t: Ident => t.value
case t: Constant.Char => Chars.escape(t.value)
case t: Constant.String => Chars.escape(t.value)
case t: Constant.Symbol => Chars.escape(t.value.name)
case t: NumericConstant[_] => t.value.toString
case t: Interpolation.Id => Chars.escape(t.value)
case t: Interpolation.Start => t.text
case t: Interpolation.Part => Chars.escape(t.value)
case t: Interpolation.End => t.text
case t: Xml.Part => Chars.escape(t.value)
case t: Comment => Chars.escape(t.value)
case t: Ellipsis => t.rank.toString
case t: Unquote => t.text
case _ => null
}
val label = {
val name = x.getClass.getName
val idx = name.lastIndexOf(".Token$")
if (idx < 0) x.name else name.substring(idx + 7).replace("$", ".")
}
s(label, w("(", syntax, ")"), " [", x.start.toString, "..", x.end.toString, ")")
val syntax = if (text eq null) s() else s("(", text, ")")
s(label, syntax, " [", x.start.toString, "..", x.end.toString, ")")
}
}
}
98 changes: 49 additions & 49 deletions tests/shared/src/test/scala/scala/meta/tests/parsers/XmlSuite.scala
Expand Up @@ -52,13 +52,13 @@ class XmlSuite extends ParseSuite {
|Xml.Start [0..0)
|Xml.Part(<foo>) [0..5)
|Xml.SpliceStart [5..5)
|LeftBrace({) [5..6)
|LeftBrace [5..6)
|Ident(bar) [6..9)
|RightBrace(}) [9..10)
|RightBrace [9..10)
|Xml.SpliceEnd [10..10)
|Xml.Part(</foo>) [10..16)
|Xml.End [16..16)
|Space( ) [16..17)
|Space [16..17)
|EOF [17..17)
|""".stripMargin
)
Expand All @@ -68,15 +68,15 @@ class XmlSuite extends ParseSuite {
|Xml.Start [0..0)
|Xml.Part(<b>) [0..3)
|Xml.SpliceStart [3..3)
|LeftBrace({) [3..4)
|LeftBrace [3..4)
|Constant.Int(1) [4..5)
|RightBrace(}) [5..6)
|RightBrace [5..6)
|Xml.SpliceEnd [6..6)
|Xml.Part [6..6)
|Xml.Part() [6..6)
|Xml.SpliceStart [6..6)
|LeftBrace({) [6..7)
|LeftBrace [6..7)
|Constant.Int(2) [7..8)
|RightBrace(}) [8..9)
|RightBrace [8..9)
|Xml.SpliceEnd [9..9)
|Xml.Part(</b>) [9..13)
|Xml.End [13..13)
Expand All @@ -89,13 +89,13 @@ class XmlSuite extends ParseSuite {
|Xml.Start [0..0)
|Xml.Part(<foo>) [0..5)
|Xml.SpliceStart [5..5)
|LeftBrace({) [5..6)
|Constant.String("{") [6..9)
|Space( ) [9..10)
|LeftBrace [5..6)
|Constant.String({) [6..9)
|Space [9..10)
|Ident(+) [10..11)
|Space( ) [11..12)
|Ident(`{`) [12..15)
|RightBrace(}) [15..16)
|Space [11..12)
|Ident({) [12..15)
|RightBrace [15..16)
|Xml.SpliceEnd [16..16)
|Xml.Part(</foo>) [16..22)
|Xml.End [22..22)
Expand All @@ -108,9 +108,9 @@ class XmlSuite extends ParseSuite {
|Xml.Start [0..0)
|Xml.Part(<foo/>) [0..6)
|Xml.End [6..6)
|LeftBrace({) [6..7)
|LeftBrace [6..7)
|Constant.Int(1) [7..8)
|RightBrace(}) [8..9)
|RightBrace [8..9)
|EOF [9..9)
|""".stripMargin
)
Expand All @@ -127,64 +127,64 @@ class XmlSuite extends ParseSuite {
checkToken(
trickyXml,
"""|BOF [0..0)
|LeftBrace({) [0..1)
|LF(\n) [1..2)
|KwVal(val) [2..5)
|Space( ) [5..6)
|LeftBrace [0..1)
|LF [1..2)
|KwVal [2..5)
|Space [5..6)
|Ident(x) [6..7)
|Space( ) [7..8)
|Equals(=) [8..9)
|Space( ) [9..10)
|Space [7..8)
|Equals [8..9)
|Space [9..10)
|Xml.Start [10..10)
|Xml.Part(<div href=) [10..20)
|Xml.SpliceStart [20..20)
|LeftBrace({) [20..21)
|Constant.String("/") [21..24)
|Space( ) [24..25)
|LeftBrace [20..21)
|Constant.String(/) [21..24)
|Space [24..25)
|Ident(+) [25..26)
|Space( ) [26..27)
|Space [26..27)
|Ident(url) [27..30)
|RightBrace(}) [30..31)
|RightBrace [30..31)
|Xml.SpliceEnd [31..31)
|Xml.Part(>Hello ) [31..38)
|Xml.SpliceStart [38..38)
|LeftBrace({) [38..39)
|LeftBrace [38..39)
|Ident(name) [39..43)
|RightBrace(}) [43..44)
|RightBrace [43..44)
|Xml.SpliceEnd [44..44)
|Xml.Part(</div>) [44..50)
|Xml.End [50..50)
|Semicolon(;) [50..51)
|LF(\n) [51..52)
|KwVal(val) [52..55)
|Space( ) [55..56)
|Semicolon [50..51)
|LF [51..52)
|KwVal [52..55)
|Space [55..56)
|Ident(noSemicolon) [56..67)
|Space( ) [67..68)
|Equals(=) [68..69)
|Space( ) [69..70)
|Space [67..68)
|Equals [68..69)
|Space [69..70)
|Xml.Start [70..70)
|Xml.Part(<h1>) [70..74)
|Xml.SpliceStart [74..74)
|LeftBrace({) [74..75)
|LeftBrace [74..75)
|Ident(msg) [75..78)
|Space( ) [78..79)
|Space [78..79)
|Ident(infix) [79..84)
|Space( ) [84..85)
|Space [84..85)
|Ident(upper) [85..90)
|RightBrace(}) [90..91)
|RightBrace [90..91)
|Xml.SpliceEnd [91..91)
|Xml.Part(</h1>) [91..96)
|Xml.End [96..96)
|LF(\n) [96..97)
|KwVal(val) [97..100)
|Space( ) [100..101)
|LF [96..97)
|KwVal [97..100)
|Space [100..101)
|Ident(y) [101..102)
|Space( ) [102..103)
|Equals(=) [103..104)
|Space( ) [104..105)
|Space [102..103)
|Equals [103..104)
|Space [104..105)
|Constant.Int(2) [105..106)
|LF(\n) [106..107)
|RightBrace(}) [107..108)
|LF [106..107)
|RightBrace [107..108)
|EOF [108..108)
|""".stripMargin
)
Expand Down
Expand Up @@ -419,7 +419,7 @@ class PublicSuite extends TreeSuiteBase {
val tokens = "foo + bar".tokenize.get
assertEquals(
tokens.structure,
"Tokens(BOF [0..0), Ident(foo) [0..3), Space( ) [3..4), Ident(+) [4..5), Space( ) [5..6), Ident(bar) [6..9), EOF [9..9))"
"Tokens(BOF [0..0), Ident(foo) [0..3), Space [3..4), Ident(+) [4..5), Space [5..6), Ident(bar) [6..9), EOF [9..9))"
)
}

Expand Down
Expand Up @@ -1659,7 +1659,7 @@ class SyntacticSuite extends scala.meta.tests.parsers.ParseSuite {

assertEquals(
arg1.tokens.structure,
"Tokens(LeftBrace({) [5..6), Ident(b) [6..7), RightBrace(}) [7..8))"
"Tokens(LeftBrace [5..6), Ident(b) [6..7), RightBrace [7..8))"
)

assertEquals(
Expand Down

0 comments on commit 8b19031

Please sign in to comment.