Skip to content

Add a right-side gutter CodeMirror extension #2561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
# index.html ends up weirdly formatted
/app/index.html
/tests/index.html

# code-mirror-gutter-rs is cloned from @codemirror/view
/app/utils/code-mirror-gutter-rs.ts
4 changes: 3 additions & 1 deletion app/components/code-mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { languages } from '@codemirror/language-data';
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';

function generateHTMLElement(src: string): HTMLElement {
const div = document.createElement('div');
Expand Down Expand Up @@ -66,7 +67,8 @@ const OPTION_HANDLERS: { [key: string]: OptionHandler } = {
drawSelection: ({ drawSelection: enabled }) => (enabled ? [drawSelection()] : []),
dropCursor: ({ dropCursor: enabled }) => (enabled ? [dropCursor()] : []),
editable: ({ editable }) => [EditorView.editable.of(!!editable)],
highlightActiveLine: ({ highlightActiveLine: enabled }) => (enabled ? [highlightActiveLine(), highlightActiveLineGutter()] : []),
highlightActiveLine: ({ highlightActiveLine: enabled }) =>
enabled ? [highlightActiveLine(), highlightActiveLineGutter(), highlightActiveLineGutterRS()] : [],
highlightNewlines: ({ highlightNewlines: enabled }) => (enabled ? [highlightNewlines()] : []),
highlightSelectionMatches: ({ highlightSelectionMatches: enabled }) => (enabled ? [highlightSelectionMatches()] : []),
highlightSpecialChars: ({ highlightSpecialChars: enabled }) => (enabled ? [highlightSpecialChars()] : []),
Expand Down
26 changes: 26 additions & 0 deletions app/utils/code-mirror-collapse-unchanged-gutter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BlockInfo, EditorView, gutter, GutterMarker, type WidgetType } from '@codemirror/view';
import { uncollapseUnchanged } from '@codemirror/merge';
import { gutter as gutterRS, GutterMarker as GutterMarkerRS } from 'codecrafters-frontend/utils/code-mirror-gutter-rs';

function isCollapseUnchangedWidget(widget: WidgetType) {
return 'type' in widget && widget.type === 'collapsed-unchanged-code';
Expand Down Expand Up @@ -43,6 +44,23 @@ export class CollapseUnchangedGutterMarker extends GutterMarker {
}
}

export class CollapseUnchangedGutterMarkerRS extends GutterMarkerRS {
line: BlockInfo;
view: EditorView;
widget: WidgetType;

constructor(view: EditorView, widget: WidgetType, line: BlockInfo) {
super();
this.line = line;
this.view = view;
this.widget = widget;
}

toDOM(view: EditorView) {
return renderGutterElement(view, this.widget, this.line);
}
}

export function collapseUnchangedGutter() {
return [
gutter({
Expand All @@ -52,5 +70,13 @@ export function collapseUnchangedGutter() {
return isCollapseUnchangedWidget(widget) ? new CollapseUnchangedGutterMarker(view, widget, line) : null;
},
}),

gutterRS({
class: 'cm-collapseUnchangedGutter',

widgetMarker(view, widget, line) {
return isCollapseUnchangedWidget(widget) ? new CollapseUnchangedGutterMarkerRS(view, widget, line) : null;
},
}),
];
}
Loading