From fd6aaa8546bcf66ec42ca3719536778d2f089ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 8 Aug 2022 14:59:28 +0200 Subject: [PATCH] Enforce LF for line endings in the editor (#1337) --- .../hooks/cell_editor/live_editor/monaco.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/assets/js/hooks/cell_editor/live_editor/monaco.js b/assets/js/hooks/cell_editor/live_editor/monaco.js index 7691a6540..811272812 100644 --- a/assets/js/hooks/cell_editor/live_editor/monaco.js +++ b/assets/js/hooks/cell_editor/live_editor/monaco.js @@ -3,6 +3,36 @@ import { CommandsRegistry } from "monaco-editor/esm/vs/platform/commands/common/ import ElixirOnTypeFormattingEditProvider from "./elixir/on_type_formatting_edit_provider"; import { theme, highContrast } from "./theme"; +import { PieceTreeTextBufferFactory } from "monaco-editor/esm/vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder"; + +// Force LF for line ending. +// +// Monaco infers EOL based on the text content if any, otherwise uses +// a system dependent value (CRLF for Windows). Then, the content is +// always normalized to use that EOL. We need to ensure consistent +// behaviour for collaborative editing to work. We already enforce +// LF when importing/exporting Live Markdown, so the easiest approach +// is to enforce it in the editor as well. +// +// There is no direct configuration to accomplish this, so we use an +// override of [1] instead. There is also a long-running discussion +// around EOL in [2]. +// +// An alternative approach would be to disable line normalization and +// possibly set the default EOL to LF (used when there is no content +// to infer EOL from). Currently neither of those is configurable and +// requires more complex overrides. +// +// [1]: https://github.com/microsoft/vscode/blob/34f184263de048a6283af1d9eb9faab84da4547d/src/vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder.ts#L27-L40 +// [2]: https://github.com/microsoft/vscode/issues/127 +if (PieceTreeTextBufferFactory.prototype._getEOL) { + PieceTreeTextBufferFactory.prototype._getEOL = function (defaultEOL) { + return "\n"; + }; +} else { + throw new Error("failed to override line endings to LF"); +} + monaco.languages.registerOnTypeFormattingEditProvider( "elixir", ElixirOnTypeFormattingEditProvider