diff --git a/src/main/java/org/nixos/idea/util/NixIndStringUtil.kt b/src/main/java/org/nixos/idea/util/NixIndStringUtil.kt index 4b5e9d45..805f8e9a 100644 --- a/src/main/java/org/nixos/idea/util/NixIndStringUtil.kt +++ b/src/main/java/org/nixos/idea/util/NixIndStringUtil.kt @@ -36,18 +36,31 @@ object NixIndStringUtil { } when (c) { - // ''' is escaped to '' - // '' is the string delimiter + // ''\ escapes any character, but we can only cover known ones in advance: '\'' -> when { + // ''' is escaped to '' prev2Chars() == "''" -> append("''") + // '' is the string delimiter + else -> continue + } + + '\\' -> when { + prev2Chars() == "''" -> continue prevChar() == '\'' -> continue else -> append(c) } - // ''\ escapes any character, but we can only cover known ones in advance: - 'r' -> if (prev3Chars() == "''\\") append('\r') else append(c) - 'n' -> if (prev3Chars() == "''\\") append('\n') else append(c) - 't' -> if (prev3Chars() == "''\\") append('\t') else append(c) - else -> append(c) + + '$' -> if (prevChar() == '$') append(c) else continue + '{' -> if (prevChar() == '$') append("\${") else append(c) + + else -> if (prev3Chars() == "''\\") when (c) { + 'r' -> if (prev3Chars() == "''\\") append('\r') else append(c) + 'n' -> if (prev3Chars() == "''\\") append('\n') else append(c) + 't' -> if (prev3Chars() == "''\\") append('\t') else append(c) + else -> append("''\\").append(c) + } else { + append(c) + } } } } diff --git a/src/test/java/org/nixos/idea/util/NixIndStringUtilTest.java b/src/test/java/org/nixos/idea/util/NixIndStringUtilTest.java index 6eb0d759..f4c99a24 100644 --- a/src/test/java/org/nixos/idea/util/NixIndStringUtilTest.java +++ b/src/test/java/org/nixos/idea/util/NixIndStringUtilTest.java @@ -14,41 +14,28 @@ import static org.junit.jupiter.api.Assertions.assertEquals; final class NixIndStringUtilTest { -// @ParameterizedTest(name = "[{index}] {0} -> {1}") -// @CsvSource(textBlock = """ -// '' , "" -// abc , "abc" -// " , "\\\"" -// \\ , "\\\\" -// \\x , "\\\\x" -// a${b}c , "a\\${b}c" -// '\n' , "\\n" -// '\r' , "\\r" -// '\t' , "\\t" -// # supplementary character, i.e. character form a supplementary plane, -// # which needs a surrogate pair to be represented in UTF-16 -// \uD83C\uDF09 , "\uD83C\uDF09" -// """) -// void quote(String unescaped, String expectedResult) { -// assertEquals(expectedResult, NixStringUtil.quote(unescaped)); -// } - @ParameterizedTest(name = "[{index}] {0} -> {1}") - @CsvSource(textBlock = """ - '' , '' + @CsvSource(quoteCharacter = '|', textBlock = """ + || , || abc , abc - " , \\" - \\ , \\\\ - \\x , \\\\x - a${b}c , a\\${b}c - '\n' , \\n - '\r' , \\r - '\t' , \\t + " , " + \\ , \\ + \\x , \\x + a${b}c , a${b}c + |\n| , |\n| + |\r| , |\r| + |\t| , |\t| + |''\\t| , |\t| + |''\\r| , |\r| + |''\\n| , |\n| + |'''| , |''| + $$ , $ # supplementary character, i.e. character form a supplementary plane, # which needs a surrogate pair to be represented in UTF-16 \uD83C\uDF09 , \uD83C\uDF09 """) void escape(String unescaped, String expectedResult) { + NixIndStringUtil.INSTANCE.escape("''\\t"); var str = NixIndStringUtil.INSTANCE.escape(unescaped); assertEquals(expectedResult, str); }