Skip to content
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
79 changes: 79 additions & 0 deletions src/lib/vendor/codemirror/custom-extensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { EditorState } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { afterEach, describe, expect, it } from 'vitest';

import { getLineBreakExtension } from './custom-extensions';

describe('getLineBreakExtension', () => {
let view: EditorView;

afterEach(() => {
view?.destroy();
});

it('should not mutate document content containing \\n escape sequences', async () => {
const content = '{"key":"Hello\\nworld"}';

view = new EditorView({
state: EditorState.create({
doc: content,
extensions: [getLineBreakExtension(false)],
}),
});

await new Promise((resolve) => setTimeout(resolve, 50));

expect(view.state.doc.toString()).toBe(content);
});

it('should not mutate document content with multiple \\n sequences', async () => {
const content = '{"key":"line1\\nline2\\nline3"}';

view = new EditorView({
state: EditorState.create({
doc: content,
extensions: [getLineBreakExtension(false)],
}),
});

await new Promise((resolve) => setTimeout(resolve, 50));

expect(view.state.doc.toString()).toBe(content);
});

it('should not mutate document content with even backslashes before n', async () => {
const content = '{"key":"Hello\\\\nworld"}';

view = new EditorView({
state: EditorState.create({
doc: content,
extensions: [getLineBreakExtension(false)],
}),
});

await new Promise((resolve) => setTimeout(resolve, 50));

expect(view.state.doc.toString()).toBe(content);
});

it('should preserve valid JSON after extension processes content', async () => {
const content = '{"message":"Hello\\nworld","count":1}';

view = new EditorView({
state: EditorState.create({
doc: content,
extensions: [getLineBreakExtension(false)],
}),
});

await new Promise((resolve) => setTimeout(resolve, 50));

expect(() => JSON.parse(view.state.doc.toString())).not.toThrow();
});

it('should return empty array when editable is true', () => {
const result = getLineBreakExtension(true);

expect(result).toEqual([]);
});
});
48 changes: 37 additions & 11 deletions src/lib/vendor/codemirror/custom-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { typescript } from '@codemirror/legacy-modes/mode/javascript';
import { python } from '@codemirror/legacy-modes/mode/python';
import { ruby } from '@codemirror/legacy-modes/mode/ruby';
import { shell } from '@codemirror/legacy-modes/mode/shell';
import { EditorView } from '@codemirror/view';
import type { DecorationSet, ViewUpdate } from '@codemirror/view';
import {
Decoration,
EditorView,
MatchDecorator,
ViewPlugin,
WidgetType,
} from '@codemirror/view';
import { tags } from '@lezer/highlight';
import colors from 'tailwindcss/colors';

Expand Down Expand Up @@ -135,17 +142,36 @@ export const highlightStyles = HighlightStyle.define(
{ themeType: 'light' },
);

export const getLineBreakExtension = (editable: boolean) =>
EditorView.updateListener.of((update) => {
if (editable) return;
class LineBreakWidget extends WidgetType {
toDOM() {
return document.createElement('br');
}
}

const newText = update.state.doc.toString().replace(/\\n/g, '\n');
if (newText !== update.state.doc.toString()) {
update.view.dispatch({
changes: { from: 0, to: update.state.doc.length, insert: newText },
});
}
});
const lineBreakDecorator = new MatchDecorator({
regexp: /\\n/g,
decoration: Decoration.replace({ widget: new LineBreakWidget() }),
});

export const getLineBreakExtension = (editable: boolean) => {
if (editable) return [];

return ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = lineBreakDecorator.createDeco(view);
}
update(update: ViewUpdate) {
this.decorations = lineBreakDecorator.updateDeco(
update,
this.decorations,
);
}
},
{ decorations: (v) => v.decorations },
);
};

export const getLanguageExtension = (language: EditorLanguage) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ⚠️ Property 'text' does not exist on type '{ json: LanguageSupport; java: LanguageSupport; go: LanguageSupport; php: LanguageSupport; python: StreamLanguage; shell: StreamLanguage<...>; dotnet: StreamLanguage<...>; ruby: StreamLanguage<...>; typescript: StreamLanguage<...>; }'.

({
Expand Down
Loading