Skip to content

Commit

Permalink
Adding the formatting to the encoding of multiline strings
Browse files Browse the repository at this point in the history
### What's done:
- Added newline alignment for multiline strings
  • Loading branch information
orchestr7 committed May 7, 2023
1 parent c92b3e0 commit ef6e3d9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class TomlMainDecoder(
// branch, we should throw an exception as it is not expected at all, and we should catch this in tests
else ->
throw InternalDecodingException(
"Node of type [${node::class}] should not be processed in TomlDecoder.decodeValue(): <${node}>."
"Node of type [${node::class}] should not be processed in TomlDecoder.decodeValue(): <$node>."
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public sealed class TomlNode(
* print the structure of parsed AST tree
* Important: as prettyPrint calls toString() of the node, and not just prints the value, but emits and reconstruct a source string,
* so in some cases (for example in case of multiline strings) it can work incorrectly.
*
* @param emitLine - if true - will print line number in this debug print
*/
@Suppress("DEBUG_PRINT")
public fun prettyPrint(emitLine: Boolean = false) {
Expand All @@ -191,6 +193,7 @@ public sealed class TomlNode(
}

/**
* @param emitLine - if true - will print line number in this debug print
* @return the string with AST tree visual representation
*/
public fun prettyStr(emitLine: Boolean = false): String {
Expand Down Expand Up @@ -295,17 +298,17 @@ public sealed class TomlNode(
internal fun print(emitLine: Boolean = false): String =
"${this::class.simpleName} ($this)${if (emitLine) "[line:${this.lineNo}]" else ""}\n"


public companion object {
// number of spaces that is used to indent levels
internal const val INDENTING_LEVEL = 4

/**
* recursive print the tree using the current node
*
* @param node
* @param level
* @param result
* @param node that will be printed
* @param level depth of hierarchy for print
* @param result string builder where the result is stored
* @param emitLine if true - will print line number in this debug print
*/
public fun prettyPrint(
node: TomlNode,
Expand All @@ -317,7 +320,7 @@ public sealed class TomlNode(
// we are using print() method here instead of toString()
result.append("$spaces - ${node.print(emitLine)}")
node.children.forEach { child ->
prettyPrint(child, result, emitLine, level + 1, )
prettyPrint(child, result, emitLine, level + 1)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ public abstract class TomlEmitter(config: TomlOutputConfig) {
val quotes = if (isLiteral) "'''" else "\"\"\""

emit(quotes)
.emitNewLine()
.emit(string)
.emitNewLine()
.emit(quotes)
} else {
val quote = if (isLiteral) '\'' else '"'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,22 @@ class EncodingAnnotationTest {
value = File(),
expectedToml = """
mlTextA = $tripleQuotes
\tMultiline
text!
$tripleQuotes
mlTextB = $tripleQuotes
Text with escaped quotes ""\"\
and line break
$tripleQuotes
mlTextC = '''
"Multiline
text!"
'''
""".trimIndent()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.akuleshov7.ktoml.encoders

import com.akuleshov7.ktoml.annotations.TomlLiteral
import com.akuleshov7.ktoml.annotations.TomlMultiline
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.test.Test
Expand Down Expand Up @@ -81,6 +82,38 @@ class PrimitiveEncoderTest {
)
}

@Test
fun multilineStringsSpecifications() {
@Serializable
data class MultilineLiteralStr(
@TomlMultiline
@TomlLiteral
val a: String
)

@Serializable
data class MultilineBasicStr(
@TomlMultiline
val a: String
)

assertEncodedEquals(
value = MultilineLiteralStr("test \n test \n test \'\'\'"),
expectedToml = """
|a = '''
|test
| test
| test ''\'
|'''
""".trimMargin()
)

assertEncodedEquals(
value = MultilineBasicStr("test \n test \n test \'\'\'"),
expectedToml = "a = \"\"\"\ntest \n test \n test \'\'\'\n\"\"\""
)
}

@Test
fun jsWholeDoubleRegression() {
@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.akuleshov7.ktoml.parsers

import com.akuleshov7.ktoml.Toml
import com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class SetLineNoTest {
@Test
fun checkingLineNumbers() {
val string = """
fun checkingLineNumbersGeneral() {
val string =
"""
# comment 1
Expand All @@ -22,7 +21,7 @@ class SetLineNoTest {
[[a.b]] # comment 5
test = 1
mls = '''
mlls = '''
1
2
3
Expand All @@ -45,14 +44,43 @@ class SetLineNoTest {
| - TomlArrayOfTables ([[a.b]])[line:10]
| - TomlArrayOfTablesElement (technical_node)[line:10]
| - TomlKeyValuePrimitive (test=1)[line:11]
| - TomlKeyValuePrimitive (mls=''' 1
| - TomlKeyValuePrimitive (mlls='''
| 1
| 2
| 3
| ''')[line:13]
|
|''')[line:13]
| - TomlKeyValueArray (mla=[ "a", "b", "c" ])[line:18]
|
""".trimMargin(),
parsedToml.prettyStr(true)
)
}

@Test
fun checkingLineNumbers() {
val string = "\n\n" +
"mlls = '''\n" +
"1\n" +
"\n" +
"2\n" +
"3" +
"'''"
val parsedToml = Toml.tomlParser.parseString(string)
parsedToml.prettyPrint(true)

assertEquals(
"""
| - TomlFile (rootNode)[line:0]
| - TomlKeyValuePrimitive (mlls='''
|1
|
|2
|3
|''')[line:3]
|
""".trimMargin(),
parsedToml.prettyStr(true)
)
}
}

0 comments on commit ef6e3d9

Please sign in to comment.