Skip to content

Commit ae88403

Browse files
committed
Move SyntaxHighlight to scala.quoted
1 parent 3a4620e commit ae88403

File tree

13 files changed

+320
-172
lines changed

13 files changed

+320
-172
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionImpl.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.core._
55
import dotty.tools.dotc.util.{SourcePosition, Spans}
66

7+
import scala.quoted.show.SyntaxHighlight
8+
79
object ReflectionImpl {
810

911
def apply(rootContext: Contexts.Context): scala.tasty.Reflection =
@@ -16,7 +18,10 @@ object ReflectionImpl {
1618
val refl = new scala.tasty.Reflection(new KernelImpl(ctx, tree.sourcePos))
1719
val reflCtx = ctx.asInstanceOf[refl.Context]
1820
val reflTree = tree.asInstanceOf[refl.Tree]
19-
new refl.SourceCodePrinter(ctx.settings.color.value == "always").showTree(reflTree)(reflCtx)
21+
val syntaxHighlight =
22+
if (ctx.settings.color.value == "always") SyntaxHighlight.ANSI
23+
else SyntaxHighlight.plain
24+
new refl.SourceCodePrinter(syntaxHighlight).showTree(reflTree)(reflCtx)
2025
}
2126

2227
}

compiler/src/dotty/tools/dotc/util/NameTransformer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package util
44

55
import core.Names._
66
import collection.mutable
7-
import scala.tasty.util.Chars
7+
import scala.internal.Chars
88

99
import scala.annotation.internal.sharable
1010

library/src-3.x/scala/quoted/Expr.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package scala
22

33
package quoted {
44

5+
import scala.quoted.show.SyntaxHighlight
6+
57
sealed abstract class Expr[+T] {
68

79
/** Evaluate the contents of this expression and return the result.
@@ -18,9 +20,13 @@ package quoted {
1820
import scala.internal.quoted._
1921

2022
implicit class ExprOps[T](expr: Expr[T]) {
23+
2124
/** Show a source code like representation of this expression */
22-
def show(implicit qctx: QuoteContext): String = qctx.show(expr, false)
23-
def showFormatted(implicit qctx: QuoteContext): String = qctx.show(expr, true)
25+
def show(implicit qctx: QuoteContext): String = qctx.show(expr, SyntaxHighlight.plain)
26+
27+
/** Show a source code like representation of this expression */
28+
def show(syntaxHighlight: SyntaxHighlight)(implicit qctx: QuoteContext): String = qctx.show(expr, syntaxHighlight)
29+
2430
}
2531

2632
/** Converts a tuple `(T1, ..., Tn)` to `(Expr[T1], ..., Expr[Tn])` */

library/src-3.x/scala/quoted/Type.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala
22

33
package quoted {
44
import scala.internal.quoted.TaggedType
5+
import scala.quoted.show.SyntaxHighlight
56

67
sealed abstract class Type[T <: AnyKind] {
78
type `$splice` = T
@@ -11,9 +12,13 @@ package quoted {
1112
object Type {
1213

1314
implicit class TypeOps[T](tpe: Type[T]) {
14-
/** Show a source code like representation of this expression */
15-
def show(implicit qctx: QuoteContext): String = qctx.show(tpe, false)
16-
def showFormatted(implicit qctx: QuoteContext): String = qctx.show(tpe, true)
15+
16+
/** Show a source code like representation of this type */
17+
def show(implicit qctx: QuoteContext): String = qctx.show(tpe, SyntaxHighlight.plain)
18+
19+
/** Show a source code like representation of this type */
20+
def show(syntaxHighlight: SyntaxHighlight)(implicit qctx: QuoteContext): String = qctx.show(tpe, syntaxHighlight)
21+
1722
}
1823

1924
implicit val UnitTag: Type[Unit] = new TaggedType[Unit]

library/src-3.x/scala/quoted/package.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package object quoted {
1616
*/
1717
def run[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): T = toolbox.run(expr given _)
1818

19-
def show[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): String = run(expr.show.toExpr)
20-
2119
object autolift {
2220
implicit def autoToExpr[T: Liftable](x: T): Expr[T] = x.toExpr
2321
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package scala.internal
2+
3+
import scala.annotation.switch
4+
import java.lang.{Character => JCharacter}
5+
import java.lang.Character.LETTER_NUMBER
6+
import java.lang.Character.LOWERCASE_LETTER
7+
import java.lang.Character.OTHER_LETTER
8+
import java.lang.Character.TITLECASE_LETTER
9+
import java.lang.Character.UPPERCASE_LETTER
10+
11+
/** Contains constants and classifier methods for characters */
12+
object Chars {
13+
14+
final val LF = '\u000A'
15+
final val FF = '\u000C'
16+
final val CR = '\u000D'
17+
final val SU = '\u001A'
18+
19+
/** Convert a character digit to an Int according to given base,
20+
* -1 if no success
21+
*/
22+
def digit2int(ch: Char, base: Int): Int = {
23+
val num = (
24+
if (ch <= '9') ch - '0'
25+
else if ('a' <= ch && ch <= 'z') ch - 'a' + 10
26+
else if ('A' <= ch && ch <= 'Z') ch - 'A' + 10
27+
else -1
28+
)
29+
if (0 <= num && num < base) num else -1
30+
}
31+
/** Buffer for creating '\ u XXXX' strings. */
32+
private[this] val char2uescapeArray = Array[Char]('\\', 'u', 0, 0, 0, 0)
33+
34+
/** Convert a character to a backslash-u escape */
35+
def char2uescape(c: Char): String = {
36+
@forceInline def hexChar(ch: Int): Char =
37+
(( if (ch < 10) '0' else 'A' - 10 ) + ch).toChar
38+
39+
char2uescapeArray(2) = hexChar((c >> 12) )
40+
char2uescapeArray(3) = hexChar((c >> 8) % 16)
41+
char2uescapeArray(4) = hexChar((c >> 4) % 16)
42+
char2uescapeArray(5) = hexChar((c ) % 16)
43+
44+
new String(char2uescapeArray)
45+
}
46+
47+
/** Is character a line break? */
48+
def isLineBreakChar(c: Char): Boolean = (c: @switch) match {
49+
case LF|FF|CR|SU => true
50+
case _ => false
51+
}
52+
53+
/** Is character a whitespace character (but not a new line)? */
54+
def isWhitespace(c: Char): Boolean =
55+
c == ' ' || c == '\t' || c == CR
56+
57+
/** Can character form part of a doc comment variable $xxx? */
58+
def isVarPart(c: Char): Boolean =
59+
'0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
60+
61+
/** Can character start an alphanumeric Scala identifier? */
62+
def isIdentifierStart(c: Char): Boolean =
63+
(c == '_') || (c == '$') || JCharacter.isUnicodeIdentifierStart(c)
64+
65+
/** Can character form part of an alphanumeric Scala identifier? */
66+
def isIdentifierPart(c: Char): Boolean =
67+
(c == '$') || JCharacter.isUnicodeIdentifierPart(c)
68+
69+
/** Is character a math or other symbol in Unicode? */
70+
def isSpecial(c: Char): Boolean = {
71+
val chtp = JCharacter.getType(c)
72+
chtp == JCharacter.MATH_SYMBOL.toInt || chtp == JCharacter.OTHER_SYMBOL.toInt
73+
}
74+
75+
def isValidJVMChar(c: Char): Boolean =
76+
!(c == '.' || c == ';' || c =='[' || c == '/')
77+
78+
def isValidJVMMethodChar(c: Char): Boolean =
79+
!(c == '.' || c == ';' || c =='[' || c == '/' || c == '<' || c == '>')
80+
81+
private final val otherLetters = Set[Char]('\u0024', '\u005F') // '$' and '_'
82+
private final val letterGroups = {
83+
import JCharacter._
84+
Set[Byte](LOWERCASE_LETTER, UPPERCASE_LETTER, OTHER_LETTER, TITLECASE_LETTER, LETTER_NUMBER)
85+
}
86+
def isScalaLetter(ch: Char): Boolean = letterGroups(JCharacter.getType(ch).toByte) || otherLetters(ch)
87+
88+
/** Can character form part of a Scala operator name? */
89+
def isOperatorPart(c : Char) : Boolean = (c: @switch) match {
90+
case '~' | '!' | '@' | '#' | '%' |
91+
'^' | '*' | '+' | '-' | '<' |
92+
'>' | '?' | ':' | '=' | '&' |
93+
'|' | '/' | '\\' => true
94+
case c => isSpecial(c)
95+
}
96+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package scala.quoted
22

3+
import scala.quoted.show.SyntaxHighlight
4+
35
class QuoteContext(reflection: tasty.Reflection) {
46

5-
def show[T](expr: Expr[T], formatted: Boolean): String = {
7+
def show[T](expr: Expr[T], syntaxHighlight: SyntaxHighlight): String = {
68
import reflection._
7-
if (formatted) expr.unseal.showFormatted
8-
else expr.unseal.show
9+
expr.unseal.show(syntaxHighlight)
910
}
1011

11-
def show[T](tpe: Type[T], formatted: Boolean): String = {
12+
def show[T](tpe: Type[T], syntaxHighlight: SyntaxHighlight): String = {
1213
import reflection._
13-
if (formatted) tpe.unseal.showFormatted
14-
else tpe.unseal.show
14+
tpe.unseal.show(syntaxHighlight)
1515
}
1616

1717
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package scala.quoted.show
2+
3+
trait SyntaxHighlight {
4+
def highlightKeyword(str: String): String
5+
def highlightTypeDef(str: String): String
6+
def highlightLiteral(str: String): String
7+
def highlightValDef(str: String): String
8+
def highlightOperator(str: String): String
9+
def highlightAnnotation(str: String): String
10+
def highlightString(str: String): String
11+
def highlightTripleQs: String
12+
}
13+
14+
object SyntaxHighlight {
15+
16+
def ANSI: SyntaxHighlight = new SyntaxHighlight {
17+
// Keep in sync with SyntaxHighlighting
18+
private val NoColor = Console.RESET
19+
private val CommentColor = Console.BLUE
20+
private val KeywordColor = Console.YELLOW
21+
private val ValDefColor = Console.CYAN
22+
private val LiteralColor = Console.RED
23+
private val StringColor = Console.GREEN
24+
private val TypeColor = Console.MAGENTA
25+
private val AnnotationColor = Console.MAGENTA
26+
27+
def highlightKeyword(str: String): String = KeywordColor + str + NoColor
28+
def highlightTypeDef(str: String): String = TypeColor + str + NoColor
29+
def highlightLiteral(str: String): String = LiteralColor + str + NoColor
30+
def highlightValDef(str: String): String = ValDefColor + str + NoColor
31+
def highlightOperator(str: String): String = TypeColor + str + NoColor
32+
def highlightAnnotation(str: String): String = AnnotationColor + str + NoColor
33+
def highlightString(str: String): String = StringColor + str + NoColor
34+
def highlightTripleQs: String = Console.RED_B + "???" + NoColor
35+
}
36+
37+
def plain: SyntaxHighlight = new SyntaxHighlight {
38+
def highlightKeyword(str: String): String = str
39+
def highlightTypeDef(str: String): String = str
40+
def highlightLiteral(str: String): String = str
41+
def highlightValDef(str: String): String = str
42+
def highlightOperator(str: String): String = str
43+
def highlightAnnotation(str: String): String = str
44+
def highlightString(str: String): String = str
45+
def highlightTripleQs: String = "???"
46+
}
47+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scala.quoted
2+
3+
package object show {
4+
5+
def apply[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): String = run(expr.show.toExpr)
6+
7+
}

0 commit comments

Comments
 (0)