diff --git a/app/components/code-mirror.hbs b/app/components/code-mirror.hbs index e243f23ef5..43cff566cc 100644 --- a/app/components/code-mirror.hbs +++ b/app/components/code-mirror.hbs @@ -22,6 +22,7 @@ {{did-update this.optionDidChange "indentWithTab" @indentWithTab}} {{did-update this.optionDidChange "languageOrFilename" @filename}} {{did-update this.optionDidChange "languageOrFilename" @language}} + {{did-update this.optionDidChange "lineCommentsOrLineData" (array @lineComments @lineData)}} {{did-update this.optionDidChange "lineNumbers" @lineNumbers}} {{did-update this.optionDidChange "lineSeparator" @lineSeparator}} {{did-update this.optionDidChange "lineWrapping" @lineWrapping}} diff --git a/app/components/code-mirror.ts b/app/components/code-mirror.ts index f6e5f91f1b..c16b773335 100644 --- a/app/components/code-mirror.ts +++ b/app/components/code-mirror.ts @@ -39,6 +39,7 @@ import { markdown } from '@codemirror/lang-markdown'; import { highlightNewlines } from 'codecrafters-frontend/utils/code-mirror-highlight-newlines'; import { collapseUnchangedGutter } from 'codecrafters-frontend/utils/code-mirror-collapse-unchanged-gutter'; import { highlightActiveLineGutter as highlightActiveLineGutterRS } from 'codecrafters-frontend/utils/code-mirror-gutter-rs'; +import { lineComments, type LineDataCollection } from 'codecrafters-frontend/utils/code-mirror-line-comments'; function generateHTMLElement(src: string): HTMLElement { const div = document.createElement('div'); @@ -54,7 +55,7 @@ enum FoldGutterIcon { type DocumentUpdateCallback = (newValue: string) => void; -type Argument = boolean | string | number | undefined | Extension | DocumentUpdateCallback; +type Argument = boolean | string | number | undefined | Extension | DocumentUpdateCallback | LineDataCollection; type OptionHandler = (args: Signature['Args']['Named']) => Extension[] | Promise; @@ -78,6 +79,7 @@ const OPTION_HANDLERS: { [key: string]: OptionHandler } = { indentOnInput: ({ indentOnInput: enabled }) => (enabled ? [indentOnInput()] : []), indentUnit: ({ indentUnit: indentUnitText }) => (indentUnitText !== undefined ? [indentUnit.of(indentUnitText)] : []), indentWithTab: ({ indentWithTab: enabled }) => (enabled ? [keymap.of([indentWithTab])] : []), + lineCommentsOrLineData: ({ lineData, lineComments: enabled }) => (enabled && lineData ? [lineComments(lineData)] : []), lineNumbers: ({ lineNumbers: enabled }) => (enabled ? [lineNumbers()] : []), foldGutter: ({ foldGutter: enabled }) => enabled @@ -266,6 +268,14 @@ export interface Signature { * Enable indentation of lines or selection using TAB and Shift+TAB keys, otherwise editor loses focus when TAB is pressed */ indentWithTab?: boolean; + /** + * Enable line comments + */ + lineComments?: boolean; + /** + * Line data containing comments counts or other line-related metadata + */ + lineData?: LineDataCollection; /** * Enable the line numbers gutter */ diff --git a/app/controllers/demo/code-mirror.ts b/app/controllers/demo/code-mirror.ts index 10b4b981f5..9df380cf04 100644 --- a/app/controllers/demo/code-mirror.ts +++ b/app/controllers/demo/code-mirror.ts @@ -6,6 +6,36 @@ import { type Extension } from '@codemirror/state'; import type DarkModeService from 'codecrafters-frontend/services/dark-mode'; import { codeCraftersDark, codeCraftersLight } from 'codecrafters-frontend/utils/code-mirror-themes'; import EXAMPLE_DOCUMENTS, { ExampleDocument } from 'codecrafters-frontend/utils/code-mirror-documents'; +import { LineData, LineDataCollection } from 'codecrafters-frontend/utils/code-mirror-line-comments'; + +function generateRandomLineData(linesCount = 0) { + function getRandomInt(inclusiveMin: number, exclusiveMax: number) { + const minCeiled = Math.ceil(inclusiveMin); + const maxFloored = Math.floor(exclusiveMax); + + return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); + } + + const lineData = Array.from({ length: linesCount }).map(function (_v, lineNumber) { + const rnd = Math.random(); + + let commentsCount; + + if (rnd < 0.05) { + commentsCount = getRandomInt(100, 1000); + } else if (rnd < 0.1) { + commentsCount = getRandomInt(10, 100); + } else if (rnd < 0.8) { + commentsCount = 0; + } else { + commentsCount = getRandomInt(1, 10); + } + + return new LineData({ commentsCount, lineNumber }); + }); + + return new LineDataCollection(lineData); +} const THEME_EXTENSIONS: { [key: string]: Extension; @@ -54,6 +84,7 @@ const OPTION_DEFAULTS = { indentWithTab: true, language: true, lineNumbers: true, + lineComments: false, lineSeparator: true, lineWrapping: true, maxHeight: true, @@ -108,6 +139,7 @@ export default class DemoCodeMirrorController extends Controller { 'indentUnit', 'indentWithTab', 'language', + 'lineComments', 'lineNumbers', 'lineSeparator', 'lineWrapping', @@ -160,6 +192,7 @@ export default class DemoCodeMirrorController extends Controller { @tracked indentUnits = INDENT_UNITS; @tracked indentWithTab = OPTION_DEFAULTS.indentWithTab; @tracked language = OPTION_DEFAULTS.language; + @tracked lineComments = OPTION_DEFAULTS.lineComments; @tracked lineNumbers = OPTION_DEFAULTS.lineNumbers; @tracked lineSeparator = OPTION_DEFAULTS.lineSeparator; @tracked lineSeparators = LINE_SEPARATORS; @@ -197,6 +230,10 @@ export default class DemoCodeMirrorController extends Controller { return this.documents[this.selectedDocumentIndex] || ExampleDocument.createEmpty(); } + get selectedDocumentLineData() { + return generateRandomLineData(this.selectedDocument.document.split(/$/gm).length); + } + get selectedIndentUnit() { return this.indentUnits[this.selectedIndentUnitIndex]; } diff --git a/app/routes/demo/code-mirror.ts b/app/routes/demo/code-mirror.ts index 76caf6fa7d..55536ec0f1 100644 --- a/app/routes/demo/code-mirror.ts +++ b/app/routes/demo/code-mirror.ts @@ -25,6 +25,7 @@ const QUERY_PARAMS = [ 'indentUnit', 'indentWithTab', 'language', + 'lineComments', 'lineNumbers', 'lineSeparator', 'lineWrapping', diff --git a/app/templates/demo/code-mirror.hbs b/app/templates/demo/code-mirror.hbs index 5696def426..ff08ce1e7d 100644 --- a/app/templates/demo/code-mirror.hbs +++ b/app/templates/demo/code-mirror.hbs @@ -167,6 +167,12 @@ foldGutter + + + - -