summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/scratchpad/SourceInserter.scala
blob: 5eeab53fca84f44ada2ef4a2ee73b7068b0ca026 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package scala.tools.nsc
package scratchpad

import java.io.Writer
import scala.reflect.internal.util.SourceFile
import scala.reflect.internal.Chars._

object SourceInserter {
  def stripRight(cs: Array[Char]): Array[Char] = {
    val lines =
      new String(cs) split "\n"
    def leftPart(str: String) =
      (str split """//>|//\|""").head
    def isContinuation(str: String) =
      ((str contains "//>") || (str contains "//|")) && (leftPart(str) forall isWhitespace)
    def stripTrailingWS(str: String) =
      str take (str lastIndexWhere (!isWhitespace(_))) + 1
    val prefixes =
      lines filterNot isContinuation map leftPart map stripTrailingWS
    (prefixes mkString "\n").toArray
  }
}