Skip to content

Commit 714eec8

Browse files
committed
fix(editor): normalize line endings in code display and closed #423
Handle Windows-style CRLF line endings (\r\n) by converting them to Unix-style LF (\n) in the editor. This ensures consistent code display and prevents issues with line number handling and text processing.
1 parent 1b86dc8 commit 714eec8

File tree

1 file changed

+7
-1
lines changed
  • core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code

1 file changed

+7
-1
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/EditorUtil.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ object EditorUtil {
3232
fileName: String?,
3333
disposable: Disposable,
3434
): EditorEx {
35+
// 处理可能的混合行分隔符问题,特别是Windows风格的\r\n
36+
// Handle potential mixed line separators, especially Windows-style \r\n
3537
var editorText = text
38+
if (text.contains("\r\n")) {
39+
editorText = text.replace("\r\n", "\n")
40+
}
41+
3642
val language = ideaLanguage ?: PlainTextLanguage.INSTANCE
3743
val ext = if (language.displayName == PlainTextLanguage.INSTANCE.displayName) {
3844
CodeFence.lookupFileExt(language.displayName)
@@ -50,7 +56,7 @@ object EditorUtil {
5056
}
5157

5258
if (isShowLineNo) {
53-
val newLines = text.lines().map { it.replace(LINE_NO_REGEX, "") }
59+
val newLines = editorText.lines().map { it.replace(LINE_NO_REGEX, "") }
5460
editorText = newLines.joinToString("\n")
5561
}
5662

0 commit comments

Comments
 (0)