Skip to content

Commit 1f3bf3b

Browse files
committedJan 18, 2025
[WIP] Add lineComments CodeMirror extension - external comments
1 parent 2357c76 commit 1f3bf3b

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed
 

‎app/components/code-mirror.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{{did-update this.optionDidChange "indentWithTab" @indentWithTab}}
2323
{{did-update this.optionDidChange "languageOrFilename" @filename}}
2424
{{did-update this.optionDidChange "languageOrFilename" @language}}
25-
{{did-update this.optionDidChange "lineComments" @lineComments}}
25+
{{did-update this.optionDidChange "lineCommentsOrComments" (array @comments @lineComments)}}
2626
{{did-update this.optionDidChange "lineNumbers" @lineNumbers}}
2727
{{did-update this.optionDidChange "lineSeparator" @lineSeparator}}
2828
{{did-update this.optionDidChange "lineWrapping" @lineWrapping}}

‎app/components/code-mirror.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { languages } from '@codemirror/language-data';
3838
import { markdown } from '@codemirror/lang-markdown';
3939
import { highlightNewlines } from 'codecrafters-frontend/utils/code-mirror-highlight-newlines';
4040
import { collapseUnchangedGutter } from 'codecrafters-frontend/utils/code-mirror-collapse-unchanged-gutter';
41-
import { lineComments } from 'codecrafters-frontend/utils/code-mirror-line-comments';
41+
import { lineComments, type LineCommentsCollection } from 'codecrafters-frontend/utils/code-mirror-line-comments';
4242

4343
function generateHTMLElement(src: string): HTMLElement {
4444
const div = document.createElement('div');
@@ -54,7 +54,7 @@ enum FoldGutterIcon {
5454

5555
type DocumentUpdateCallback = (newValue: string) => void;
5656

57-
type Argument = boolean | string | number | undefined | Extension | DocumentUpdateCallback;
57+
type Argument = boolean | string | number | undefined | Extension | DocumentUpdateCallback | LineCommentsCollection;
5858

5959
type OptionHandler = (args: Signature['Args']['Named']) => Extension[] | Promise<Extension[]>;
6060

@@ -77,7 +77,7 @@ const OPTION_HANDLERS: { [key: string]: OptionHandler } = {
7777
indentOnInput: ({ indentOnInput: enabled }) => (enabled ? [indentOnInput()] : []),
7878
indentUnit: ({ indentUnit: indentUnitText }) => (indentUnitText !== undefined ? [indentUnit.of(indentUnitText)] : []),
7979
indentWithTab: ({ indentWithTab: enabled }) => (enabled ? [keymap.of([indentWithTab])] : []),
80-
lineComments: ({ lineComments: enabled }) => (enabled ? [lineComments()] : []),
80+
lineCommentsOrComments: ({ comments = [], lineComments: enabled }) => (enabled ? [lineComments(comments)] : []),
8181
lineNumbers: ({ lineNumbers: enabled }) => (enabled ? [lineNumbers()] : []),
8282
foldGutter: ({ foldGutter: enabled }) =>
8383
enabled
@@ -202,6 +202,10 @@ export interface Signature {
202202
* Enable highlighting of matching brackets
203203
*/
204204
bracketMatching?: boolean;
205+
/**
206+
* Comments to show for each line in the editor, if `lineComments` are enabled
207+
*/
208+
comments?: LineCommentsCollection;
205209
/**
206210
* Automatically close brackets when typing
207211
*/

‎app/controllers/demo/code-mirror.ts

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@ import { type Extension } from '@codemirror/state';
66
import type DarkModeService from 'codecrafters-frontend/services/dark-mode';
77
import { codeCraftersDark, codeCraftersLight } from 'codecrafters-frontend/utils/code-mirror-themes';
88
import EXAMPLE_DOCUMENTS, { ExampleDocument } from 'codecrafters-frontend/utils/code-mirror-documents';
9+
import { LineComment } from 'codecrafters-frontend/utils/code-mirror-line-comments';
10+
11+
function getRandomInt(inclusiveMin: number, exclusiveMax: number) {
12+
const minCeiled = Math.ceil(inclusiveMin);
13+
const maxFloored = Math.floor(exclusiveMax);
14+
15+
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
16+
}
17+
18+
function generateRandomComments(linesCount = 0) {
19+
return Array.from({ length: linesCount }).map(function (_v, lineNumber) {
20+
const rnd = Math.random();
21+
22+
let commentsCount;
23+
24+
if (rnd < 0.05) {
25+
commentsCount = getRandomInt(100, 1000);
26+
} else if (rnd < 0.1) {
27+
commentsCount = getRandomInt(10, 100);
28+
} else if (rnd < 0.8) {
29+
commentsCount = 0;
30+
} else {
31+
commentsCount = getRandomInt(1, 10);
32+
}
33+
34+
return Array.from<string>({ length: commentsCount }).map(
35+
(_v, index) => new LineComment({ lineNumber, author: 'Darth Programmius', text: `Comment #${index}` }),
36+
);
37+
});
38+
}
939

1040
const THEME_EXTENSIONS: {
1141
[key: string]: Extension;
@@ -200,6 +230,10 @@ export default class DemoCodeMirrorController extends Controller {
200230
return this.documents[this.selectedDocumentIndex] || ExampleDocument.createEmpty();
201231
}
202232

233+
get selectedDocumentComments() {
234+
return generateRandomComments(this.selectedDocument.document.split(/$/gm).length);
235+
}
236+
203237
get selectedIndentUnit() {
204238
return this.indentUnits[this.selectedIndentUnitIndex];
205239
}

‎app/templates/demo/code-mirror.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@
344344
@history={{this.history}}
345345
@indentOnInput={{this.indentOnInput}}
346346
@indentWithTab={{this.indentWithTab}}
347+
@comments={{if this.lineComments this.selectedDocumentComments}}
347348
@lineComments={{this.lineComments}}
348349
@lineNumbers={{this.lineNumbers}}
349350
@lineWrapping={{this.lineWrapping}}

‎app/utils/code-mirror-line-comments.ts

+4-33
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,9 @@ import {
66
highlightActiveLineGutter as highlightActiveLineGutterRS,
77
} from 'codecrafters-frontend/utils/code-mirror-gutter-rs';
88

9-
function getRandomInt(inclusiveMin: number, exclusiveMax: number) {
10-
const minCeiled = Math.ceil(inclusiveMin);
11-
const maxFloored = Math.floor(exclusiveMax);
9+
export type LineCommentsCollection = (undefined | LineComment[])[];
1210

13-
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
14-
}
15-
16-
function generateRandomComments(linesCount = 0) {
17-
return Array.from({ length: linesCount }).map(function (_v, lineNumber) {
18-
const rnd = Math.random();
19-
20-
let commentsCount;
21-
22-
if (rnd < 0.05) {
23-
commentsCount = getRandomInt(100, 1000);
24-
} else if (rnd < 0.1) {
25-
commentsCount = getRandomInt(10, 100);
26-
} else if (rnd < 0.8) {
27-
commentsCount = 0;
28-
} else {
29-
commentsCount = getRandomInt(1, 10);
30-
}
31-
32-
return Array.from<string>({ length: commentsCount }).map(
33-
(_v, index) => new LineComment({ lineNumber, author: 'Darth Programmius', text: `Comment #${index}` }),
34-
);
35-
});
36-
}
37-
38-
type LineCommentsCollection = (undefined | LineComment[])[];
39-
40-
class LineComment {
11+
export class LineComment {
4112
lineNumber: number;
4213
text: string;
4314
author: string;
@@ -143,9 +114,9 @@ class CommentButtonGutterMarker extends GutterMarkerRS {
143114
}
144115
}
145116

146-
export function lineComments() {
117+
export function lineComments(comments: LineCommentsCollection) {
147118
return [
148-
lineCommentsFacet.compute(['doc'], (state) => generateRandomComments(state.doc.lines)),
119+
lineCommentsFacet.of(comments),
149120

150121
lineCommentsStateField,
151122

0 commit comments

Comments
 (0)
Failed to load comments.