diff --git a/src/main/java/org/nixos/idea/psi/NixStringLiteralEscaper.kt b/src/main/java/org/nixos/idea/psi/NixStringLiteralEscaper.kt index 9fce9a03..b8241889 100644 --- a/src/main/java/org/nixos/idea/psi/NixStringLiteralEscaper.kt +++ b/src/main/java/org/nixos/idea/psi/NixStringLiteralEscaper.kt @@ -3,6 +3,7 @@ package org.nixos.idea.psi import com.intellij.openapi.util.TextRange import com.intellij.psi.LiteralTextEscaper import com.intellij.psi.PsiLanguageInjectionHost +import org.intellij.lang.annotations.Language import org.nixos.idea.psi.impl.AbstractNixString class NixStringLiteralEscaper(host: AbstractNixString) : LiteralTextEscaper(host) { @@ -10,7 +11,6 @@ class NixStringLiteralEscaper(host: AbstractNixString) : LiteralTextEscaper outChars.append(c) '\\' -> { - if (index == chars.length) return null + if (index == chars.length) return false c = chars[index++] when (c) { // Linefeed, carriage-return and tab characters can @@ -123,11 +121,11 @@ class NixStringLiteralEscaper(host: AbstractNixString) : LiteralTextEscaper outChars.append('\t') 'r' -> outChars.append('\r') 'v' -> outChars.append(0x0b.toChar()) - else -> return null + else -> return false } } - else -> return null + else -> return false } if (sourceOffsets != null) { sourceOffsets[outChars.length - outOffset] = index @@ -137,7 +135,7 @@ class NixStringLiteralEscaper(host: AbstractNixString) : LiteralTextEscaper - if (index != 0) leadingSpace + line else line - } + .map { (index, line) -> if (index != 0) leadingSpace + line else line } + + // if the first line was removed in the fragment, add it back to preserve a multiline string + val withLeadingBlankLine = if (lines.first().isNotEmpty()) listOf("") + withIndent else withIndent - originalNode?.replaceWithText(withoutQuotesWithIndent) + originalNode?.replaceWithText(withLeadingBlankLine.joinToString(separator = System.lineSeparator())) return this }