-
Notifications
You must be signed in to change notification settings - Fork 22
/
BlockChompingIndicator.scala
61 lines (55 loc) · 2.19 KB
/
BlockChompingIndicator.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.virtuslab.yaml.internal.load.reader.token
/** Chomping controls how final line breaks and trailing empty lines are interpreted. YAML provides
* three chomping methods:
*
* 1. Strip - stripping is specified by the “-” chomping indicator. In this case, the final line
* break and any trailing empty lines are excluded from the scalar’s content.
*
* 2. Clip - clipping is the default behavior used if no explicit chomping indicator is specified.
* In this case, the final line break character is preserved in the scalar’s content. However, any
* trailing empty lines are excluded from the scalar’s content.
*
* 3. Keep - keeping is specified by the “+” chomping indicator. In this case, the final line break
* and any trailing empty lines are considered to be part of the scalar’s content. These additional
* lines are not subject to folding.
*/
sealed abstract class BlockChompingIndicator(indicator: Char) {
def removeBlankLinesAtEnd(scalar: String): String
}
case object BlockChompingIndicator {
/**
* Final break and any trailing empty lines are included to content
* key: scalar
(this and the following empty lines will be added to content)
*/
case object Keep extends BlockChompingIndicator('+') {
override def removeBlankLinesAtEnd(scalar: String): String = scalar
}
/**
* Final break is excluded from content
* key: scalar
(this and the following empty lines will be dropped)
*/
case object Strip extends BlockChompingIndicator('-') {
override def removeBlankLinesAtEnd(scalar: String): String =
scalar.takeRight(1) match {
case "\n" =>
removeBlankLinesAtEnd(scalar.dropRight(1))
case _ => scalar
}
}
/**
* Final break is included to content, rest of empty lines are excluded. Default behaviour
* key: key
(preserve empty line)
(this and the following empty lines will be dropped)
*/
case object Clip extends BlockChompingIndicator(' ') {
override def removeBlankLinesAtEnd(scalar: String): String =
scalar.takeRight(2) match {
case "\n\n" =>
removeBlankLinesAtEnd(scalar.dropRight(1))
case _ => scalar
}
}
}