scoop:scoop.core:0.1.0
src/debug_session.scoop
Selected production source · src/debug_session.scoop
1
internal class DebugSession(private val writer: TextWriter, private val options: DebugOptions) {2
private val path: ArrayList<Any> = ArrayList<Any>()3
private val ignore: AnnotationKey = annotationKey<DebugIgnore>()4
private val redact: AnnotationKey = annotationKey<DebugRedact>()6
internal fun write(view: ValueView, depth: Long, enter: Boolean) {7
val identity = view.referenceIdentity8
val hasIdentity = when (identity) { case Some(_) -> true; case None -> false }9
if (enter && hasIdentity) {10
for (ancestor in path) {11
if (ancestor === identity!!) {12
writer.write("<cycle " + view.type.displayName + ">")13
return14
}15
}16
}17
if (depth >= options.maxDepth) {18
writer.write("<" + view.type.displayName + " …>")19
return20
}21
if (enter && hasIdentity) { path.add(identity!!) }22
when (view.shape()) {23
case ValueShape.Scalar(ScalarValue.Boolean(val value)) -> { writer.write(value.toString()) }24
case ValueShape.Scalar(ScalarValue.Signed(val value)) -> { writer.write(value.toString()) }25
case ValueShape.Scalar(ScalarValue.Unsigned(val value)) -> { writer.write(value.toString()) }26
case ValueShape.Scalar(ScalarValue.Float(val value)) -> { writer.write(value.toString()) }27
case ValueShape.Scalar(ScalarValue.Double(val value)) -> { writer.write(value.toString()) }28
case ValueShape.Scalar(ScalarValue.Char(val value)) -> { writer.write(debugQuoted(value.toString(), '\'', 1L)) }29
case ValueShape.Scalar(ScalarValue.String(val value)) -> { writer.write(debugQuoted(value, '"', options.maxStringLength)) }30
case ValueShape.Scalar(ScalarValue.Unit) -> { writer.write("Unit") }31
case ValueShape.Record(val record) -> {32
writer.write(view.type.displayName + "(")33
writeMembers(record.members, record.base, depth)34
}35
case ValueShape.Variant(val variant) -> {36
writer.write(view.type.displayName + "." + variant.info.name)37
when (variant.info.payload) {38
case VariantPayload.Unit -> {}39
else -> {40
writer.write("(")41
writeMembers(variant.members, None, depth)42
}43
}44
}45
case ValueShape.IntegerEnum(val value) -> {46
writer.write(view.type.displayName + "." + value.info.name + "(")47
writer.write(debugInteger(value.info.value))48
writer.write(")")49
}50
case ValueShape.Tuple(val tuple) -> {51
writer.write("(")52
writeElements(tuple.size, { index -> tuple.element(index) }, ")", tuple.size == 1L, depth)53
}54
case ValueShape.Sequence(val sequence) -> {55
writer.write("[")56
writeElements(sequence.size, { index -> sequence.element(index) }, "]", false, depth)57
}58
case ValueShape.Opaque(val value) -> { writer.write("<opaque " + value.description + ">") }59
}60
if (enter && hasIdentity) { path.removeAt(path.size - 1L) }61
}63
private fun separator(index: Long, depth: Long) {64
if (index != 0L) { writer.write(",") }65
if (options.layout == DebugLayout.Pretty) {66
newline(depth + 1L)67
} else {68
if (index != 0L) { writer.write(" ") }69
}70
}72
private fun newline(depth: Long) {73
writer.write("\n")74
for (index in 0L until depth) { writer.write(options.indent) }75
}77
private fun close(text: String, count: Long, depth: Long) {78
if (count != 0L && options.layout == DebugLayout.Pretty) { newline(depth) }79
writer.write(text)80
}82
private fun writeElements(size: Long, element: (Long) -> ValueView, end: String, singleton: Boolean, depth: Long) {83
var count = 0L84
while (count < size && count < options.maxItems) {85
separator(count, depth)86
write(element(count), depth + 1L, true)87
count = count + 1L88
}89
if (count < size) {90
separator(count, depth)91
writer.write("…")92
count = count + 1L93
} else {94
if (singleton) { writer.write(",") }95
}96
close(end, count, depth)97
}99
private fun writeMembers(members: Array<MemberView>, base: Option<ValueView>, depth: Long) {100
var count = 0L101
if (when (base) { case Some(_) -> true; case None -> false }) {102
separator(count, depth)103
if (options.maxItems == 0L) {104
writer.write("…")105
close(")", 1L, depth)106
return107
}108
writer.write("base=")109
write(base!!, depth + 1L, false)110
count = 1L111
}112
for (member in members) {113
val annotations = when (member.info) {114
case ObservedMember.Field(val info) -> info.annotations115
case ObservedMember.Property(val info) -> info.annotations116
}117
if (findAnnotations(annotations, ignore).size != 0L) { continue }118
separator(count, depth)119
if (count == options.maxItems) {120
writer.write("…")121
count = count + 1L122
break123
}124
val name = when (member.info) {125
case ObservedMember.Field(val info) -> info.name126
case ObservedMember.Property(val info) -> MemberName.Named(info.name)127
}128
when (name) {129
case MemberName.Named(val text) -> { writer.write(text + "=") }130
case MemberName.Positional(_) -> {}131
}132
val redactions = findAnnotations(annotations, redact)133
if (redactions.size == 0L) {134
write(member.read(), depth + 1L, true)135
} else {136
val replacement = when (redactions[0L].arguments[0L].value) {137
case AnnotationValue.String(val text) -> text138
else -> { throw IllegalStateException() }139
}140
writer.write(debugQuoted(replacement, '"', options.maxStringLength))141
}142
count = count + 1L143
}144
close(")", count, depth)145
}146
}