8000 Printer improvements to fuse JS emitting and printing by gzm0 · Pull Request #4920 · scala-js/scala-js · GitHub
[go: up one dir, main page]

Skip to content

Printer improvements to fuse JS emitting and printing #4920

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

Merged
merged 9 commits into from
Jan 28, 2024
Prev Previous commit
Next Next commit
Print trailing newline as part of statement
This is necessary so we can partially print substatements more
easily (which we'll do in a subsequent PR). It will also allow us to
fully remove the concept of "top-level" tree.
  • Loading branch information
gzm0 committed Jan 28, 2024
commit bd58bbbb4e2e2cf3fb5fb2c7643f0dc0dd8940e0
8000
Original file line numberDiff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ object Printers {

protected def println(): Unit = {
out.write('\n')
}

protected def printIndent(): Unit = {
val indentArray = this.indentArray
val indentMargin = this.indentMargin
val bigEnoughIndentArray =
Expand All @@ -63,7 +66,6 @@ object Printers {

def printTopLevelTree(tree: Tree): Unit = {
printStat(tree)
println()
}

private def printRow(ts: List[Tree], start: Char, end: Char): Unit = {
Expand All @@ -79,24 +81,22 @@ object Printers {
}

private def printBlock(tree: Tree): Unit = {
print('{'); indent();
print('{'); indent(); println()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This change should be in the next commit. I've moved it.

tree match {
case Skip() =>
// do not print anything

case tree: Block =>
var rest = tree.stats
while (rest.nonEmpty) {
println()
printStat(rest.head)
rest = rest.tail
}

case _ =>
println()
printStat(tree)
}
undent(); println(); print('}')
undent(); printIndent(); print('}')
}

private def printSig(args: List[ParamDef], restParam: Option[ParamDef]): Unit = {
Expand All @@ -120,12 +120,22 @@ object Printers {
private def printArgs(args: List[Tree]): Unit =
printRow(args, '(', ')')

private def printStat(tree: Tree): Unit =
/** Prints a stat including leading indent and trailing newline. */
private def printStat(tree: Tree): Unit = {
printIndent()
printTree(tree, isStat = true)
println()
}

private def print(tree: Tree): Unit =
printTree(tree, isStat = false)

/** Print the "meat" of a tree.
*
* Even if it is a stat:
* - No leading indent.
* - No trailing newline.
*/
def printTree(tree: Tree, isStat: Boolean): Unit = {
def printSeparatorIfStat() = {
if (isStat)
Expand All @@ -144,12 +154,12 @@ object Printers {
} else {
print("/** ")
print(lines.head)
println()
println(); printIndent()
var rest = lines.tail
while (rest.nonEmpty) {
print(" * ")
print(rest.head)
println()
println(); printIndent()
rest = rest.tail
}
print(" */")
Expand Down Expand Up @@ -326,7 +336,7 @@ object Printers {
while (rest.nonEmpty) {
val next = rest.head
rest = rest.tail
println()
println(); printIndent()
print("case ")
print(next._1)
print(':')
Expand All @@ -339,13 +349,13 @@ object Printers {
default match {
case Skip() =>
case _ =>
println()
println(); printIndent()
print("default: ")
printBlock(default)
}

undent()
println()
println(); printIndent()
print('}')

case Debugger() =>
Expand Down Expand Up @@ -509,16 +519,17 @@ object Printers {
while (rest.nonEmpty) {
val x = rest.head
rest = rest.tail
printIndent()
print(x._1)
print(": ")
print(x._2)
if (rest.nonEmpty) {
print(',')
println()
}
println()
}
undent()
println()
printIndent()
print('}')
if (isStat)
print(");")
Expand Down Expand Up @@ -637,14 +648,13 @@ object Printers {
print(" extends ")
print(optParentClass.get)
}
print(" {"); indent()
print(" {"); indent(); println()
var rest = members
while (rest.nonEmpty) {
println()
printStat(rest.head)
rest = rest.tail
}
undent(); println(); print('}')
undent(); printIndent(); print('}')

case MethodDef(static, name, params, restParam, body) =>
if (static)
Expand Down Expand Up @@ -801,6 +811,13 @@ object Printers {
override protected def println(): Unit = {
super.println()
sourceMap.nextLine()
column = 0
}

override protected def printIndent(): Unit = {
assert(column == 0)

super.printIndent()
column = this.getIndentMargin()
}

Expand Down
0