From 3088586ac332f45c878f392665bb405b71e876cd Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 22 Mar 2017 03:22:20 +0300 Subject: [PATCH] Add runnable samples for trimIndent and trimMargin. #KT-9786 Fixed --- .../samples/test/samples/text/strings.kt | 27 ++++++++++++ libraries/stdlib/src/kotlin/text/Indent.kt | 42 +++++++------------ 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/libraries/stdlib/samples/test/samples/text/strings.kt b/libraries/stdlib/samples/test/samples/text/strings.kt index 4314bfa3b93..7afe2dbdaef 100644 --- a/libraries/stdlib/samples/test/samples/text/strings.kt +++ b/libraries/stdlib/samples/test/samples/text/strings.kt @@ -24,4 +24,31 @@ class Strings { assertPrints("Word".repeat(0), "") } + @Sample + fun trimIndent() { + val withoutIndent = + """ + ABC + 123 + 456 + """.trimIndent() + assertPrints(withoutIndent, "ABC\n123\n456") + } + + @Sample + fun trimMargin() { + val withoutMargin1 = """ABC + |123 + |456""".trimMargin() + assertPrints(withoutMargin1, "ABC\n123\n456") + + val withoutMargin2 = """ + #XYZ + #foo + #bar + """.trimMargin("#") + assertPrints(withoutMargin2, "XYZ\nfoo\nbar") + } + + } \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/text/Indent.kt b/libraries/stdlib/src/kotlin/text/Indent.kt index 9cba59539f1..24ad518219e 100644 --- a/libraries/stdlib/src/kotlin/text/Indent.kt +++ b/libraries/stdlib/src/kotlin/text/Indent.kt @@ -5,21 +5,16 @@ package kotlin.text /** * Trims leading whitespace characters followed by [marginPrefix] from every line of a source string and removes - * first and last lines if they are blank (notice difference blank vs empty). + * the first and the last lines if they are blank (notice difference blank vs empty). * - * Doesn't affect line if it doesn't contain [marginPrefix] except first and last blank lines. + * Doesn't affect a line if it doesn't contain [marginPrefix] except the first and the last blank lines. * - * Doesn't preserve original line endings. + * Doesn't preserve the original line endings. * - * Example - * ```kotlin - * assertEquals("ABC\n123\n456", """ABC - * |123 - * |456""".trimMargin()) - * ``` + * @param marginPrefix non-blank string, which is used as a margin delimiter. Default is `|` (pipe character). * - * @param marginPrefix non-blank string, characters to be used as a margin delimiter. Default is `|` (pipe character). - * @return deindented String + * @sample samples.text.Strings.trimMargin + * @see trimIndent * @see kotlin.text.isWhitespace */ public fun String.trimMargin(marginPrefix: String = "|"): String = @@ -27,6 +22,8 @@ public fun String.trimMargin(marginPrefix: String = "|"): String = /** * Detects indent by [marginPrefix] as it does [trimMargin] and replace it with [newIndent]. + * + * @param marginPrefix non-blank string, which is used as a margin delimiter. Default is `|` (pipe character). */ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: String = "|"): String { require(marginPrefix.isNotBlank()) { "marginPrefix must be non-blank string." } @@ -44,25 +41,18 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St } /** - * Detects a common minimal indent of all the input lines, removes it from every line and also removes first and last + * Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last * lines if they are blank (notice difference blank vs empty). * - * Note that blank lines do not affect detected indent level. + * Note that blank lines do not affect the detected indent level. * - * Please keep in mind that if there are non-blank lines with no leading whitespace characters (no indent at all) then the - * common indent is 0 so this function may do nothing so it is recommended to keep first line empty (will be dropped). + * In case if there are non-blank lines with no leading whitespace characters (no indent at all) then the + * common indent is 0, and therefore this function doesn't change the indentation. * - * Doesn't preserve original line endings. + * Doesn't preserve the original line endings. * - * Example - * ```kotlin - * assertEquals("ABC\n123\n456", """ - * ABC - * 123 - * 456""".trimIndent()) - * ``` - * - * @return deindented String + * @sample samples.text.Strings.trimIndent + * @see trimMargin * @see kotlin.text.isBlank */ public fun String.trimIndent(): String = replaceIndent("") @@ -84,7 +74,7 @@ public fun String.replaceIndent(newIndent: String = ""): String { /** * Prepends [indent] to every line of the original string. * - * Doesn't preserve original line endings. + * Doesn't preserve the original line endings. */ public fun String.prependIndent(indent: String = " "): String = lineSequence()