From e042faae929178c4208243a194a74f696c295b4d Mon Sep 17 00:00:00 2001 From: Vasyl Marchuk Date: Sat, 21 Dec 2024 16:39:41 +0200 Subject: [PATCH 01/18] Strip "No newline at end of file" from diffs --- app/utils/parse-diff-as-document.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/utils/parse-diff-as-document.ts b/app/utils/parse-diff-as-document.ts index ca9414026..5ad2b5737 100644 --- a/app/utils/parse-diff-as-document.ts +++ b/app/utils/parse-diff-as-document.ts @@ -5,6 +5,10 @@ export default function parseDiffAsDocument(diff: string = '') { const original = []; for (const line of diffLines) { + if (line === '\\ No newline at end of file') { + continue; + } + if (line.startsWith('-')) { original.push(line.substring(1)); } else if (line.startsWith('+')) { From c51208331d9f8b2182e2d181ae7531e28380b8d2 Mon Sep 17 00:00:00 2001 From: Vasyl Marchuk Date: Sun, 22 Dec 2024 16:32:12 +0200 Subject: [PATCH 02/18] Add a test for stripping "No newline at end of file" from diffs --- tests/unit/utils/parse-diff-as-document-test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/utils/parse-diff-as-document-test.ts b/tests/unit/utils/parse-diff-as-document-test.ts index 79dfed46b..345eaaab2 100644 --- a/tests/unit/utils/parse-diff-as-document-test.ts +++ b/tests/unit/utils/parse-diff-as-document-test.ts @@ -13,4 +13,10 @@ module('Unit | Utility | parse-diff-as-document', function () { assert.strictEqual(result.current, '', 'current document is an empty string'); assert.strictEqual(result.original, '', 'original document is an empty string'); }); + + test('it strips "\\ No newline at end of file" messages from the diff', async function (assert) { + const result = parseDiffAsDocument(' 1234\n\\ No newline at end of file\n+2345\n 6789\n-0123\n'); + assert.strictEqual(result.current, '1234\n2345\n6789\n', 'current document is parsed correctly'); + assert.strictEqual(result.original, '1234\n6789\n0123\n', 'original document is parsed correctly'); + }); }); From 129ec24fbd1cb33eb5af676406047823f14889f1 Mon Sep 17 00:00:00 2001 From: Vasyl Marchuk Date: Thu, 26 Dec 2024 12:01:01 +0200 Subject: [PATCH 03/18] Add new CodeMirror option `@highlightNewlines` using a custom extension --- app/components/code-mirror.hbs | 1 + app/components/code-mirror.ts | 6 ++ app/controllers/demo/code-mirror.ts | 3 + app/routes/demo/code-mirror.ts | 1 + app/templates/demo/code-mirror.hbs | 5 ++ app/utils/code-mirror-highlight-newlines.ts | 68 +++++++++++++++++++ .../components/code-mirror-test.js | 12 ++++ 7 files changed, 96 insertions(+) create mode 100644 app/utils/code-mirror-highlight-newlines.ts diff --git a/app/components/code-mirror.hbs b/app/components/code-mirror.hbs index 6591a281e..e243f23ef 100644 --- a/app/components/code-mirror.hbs +++ b/app/components/code-mirror.hbs @@ -11,6 +11,7 @@ {{did-update this.optionDidChange "editable" @editable}} {{did-update this.optionDidChange "foldGutter" @foldGutter}} {{did-update this.optionDidChange "highlightActiveLine" @highlightActiveLine}} + {{did-update this.optionDidChange "highlightNewlines" @highlightNewlines}} {{did-update this.optionDidChange "highlightSelectionMatches" @highlightSelectionMatches}} {{did-update this.optionDidChange "highlightSpecialChars" @highlightSpecialChars}} {{did-update this.optionDidChange "highlightTrailingWhitespace" @highlightTrailingWhitespace}} diff --git a/app/components/code-mirror.ts b/app/components/code-mirror.ts index d0d9652ab..41112dfb3 100644 --- a/app/components/code-mirror.ts +++ b/app/components/code-mirror.ts @@ -36,6 +36,7 @@ import { } from '@codemirror/language'; import { languages } from '@codemirror/language-data'; import { markdown } from '@codemirror/lang-markdown'; +import { highlightNewlines } from 'codecrafters-frontend/utils/code-mirror-highlight-newlines'; function generateHTMLElement(src: string): HTMLElement { const div = document.createElement('div'); @@ -65,6 +66,7 @@ const OPTION_HANDLERS: { [key: string]: OptionHandler } = { dropCursor: ({ dropCursor: enabled }) => (enabled ? [dropCursor()] : []), editable: ({ editable }) => [EditorView.editable.of(!!editable)], highlightActiveLine: ({ highlightActiveLine: enabled }) => (enabled ? [highlightActiveLine(), highlightActiveLineGutter()] : []), + highlightNewlines: ({ highlightNewlines: enabled }) => (enabled ? [highlightNewlines()] : []), highlightSelectionMatches: ({ highlightSelectionMatches: enabled }) => (enabled ? [highlightSelectionMatches()] : []), highlightSpecialChars: ({ highlightSpecialChars: enabled }) => (enabled ? [highlightSpecialChars()] : []), highlightTrailingWhitespace: ({ highlightTrailingWhitespace: enabled }) => (enabled ? [highlightTrailingWhitespace()] : []), @@ -228,6 +230,10 @@ export interface Signature { * Enable inline highlighting of changes in the diff */ highlightChanges?: boolean; + /** + * Enable highlighting of new line symbols + */ + highlightNewlines?: boolean; /** * Enable highlighting of current selection matches in the document */ diff --git a/app/controllers/demo/code-mirror.ts b/app/controllers/demo/code-mirror.ts index 7ac788457..10b4b981f 100644 --- a/app/controllers/demo/code-mirror.ts +++ b/app/controllers/demo/code-mirror.ts @@ -43,6 +43,7 @@ const OPTION_DEFAULTS = { foldGutter: true, highlightActiveLine: true, highlightChanges: false, + highlightNewlines: false, highlightSelectionMatches: true, highlightSpecialChars: true, highlightTrailingWhitespace: true, @@ -97,6 +98,7 @@ export default class DemoCodeMirrorController extends Controller { 'foldGutter', 'highlightActiveLine', 'highlightChanges', + 'highlightNewlines', 'highlightSelectionMatches', 'highlightSpecialChars', 'highlightTrailingWhitespace', @@ -147,6 +149,7 @@ export default class DemoCodeMirrorController extends Controller { @tracked foldGutter = OPTION_DEFAULTS.foldGutter; @tracked highlightActiveLine = OPTION_DEFAULTS.highlightActiveLine; @tracked highlightChanges = OPTION_DEFAULTS.highlightChanges; + @tracked highlightNewlines = OPTION_DEFAULTS.highlightNewlines; @tracked highlightSelectionMatches = OPTION_DEFAULTS.highlightSelectionMatches; @tracked highlightSpecialChars = OPTION_DEFAULTS.highlightSpecialChars; @tracked highlightTrailingWhitespace = OPTION_DEFAULTS.highlightTrailingWhitespace; diff --git a/app/routes/demo/code-mirror.ts b/app/routes/demo/code-mirror.ts index 3d7d99e6e..76caf6fa7 100644 --- a/app/routes/demo/code-mirror.ts +++ b/app/routes/demo/code-mirror.ts @@ -15,6 +15,7 @@ const QUERY_PARAMS = [ 'foldGutter', 'highlightActiveLine', 'highlightChanges', + 'highlightNewlines', 'highlightSelectionMatches', 'highlightSpecialChars', 'highlightTrailingWhitespace', diff --git a/app/templates/demo/code-mirror.hbs b/app/templates/demo/code-mirror.hbs index a0045a64a..5696def42 100644 --- a/app/templates/demo/code-mirror.hbs +++ b/app/templates/demo/code-mirror.hbs @@ -260,6 +260,10 @@ highlightTrailingWhitespace +