Skip to content

Commit 3a424f4

Browse files
authored
Add custom copyState to editor StreamParser implementations (#4449)
1 parent 5f4efb5 commit 3a424f4

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
ensureSyntaxTree,
3+
StreamLanguage,
4+
type StreamParser,
5+
syntaxTree,
6+
} from '@codemirror/language';
7+
import { EditorState } from '@codemirror/state';
8+
9+
import { commonMode, fluentMode, webextMode } from './editFieldModes';
10+
11+
const LONG_DOC = `<a ${'x '.repeat(300)}\n${'y '.repeat(30)}>`;
12+
13+
function fullyParsed(mode: StreamParser<any>, doc: string): EditorState {
14+
const state = EditorState.create({
15+
doc,
16+
extensions: [StreamLanguage.define(mode)],
17+
});
18+
ensureSyntaxTree(state, state.doc.length, 5000);
19+
return state;
20+
}
21+
22+
describe.each([
23+
['fluent', fluentMode],
24+
['common', commonMode],
25+
['webext', webextMode],
26+
])('%s mode', (_name, mode: StreamParser<any>) => {
27+
test('applies an edit resuming from a parser state snapshot (#4438)', () => {
28+
let state = fullyParsed(mode, LONG_DOC);
29+
state = state.update({
30+
changes: { from: state.doc.length, insert: 'X' },
31+
}).state;
32+
ensureSyntaxTree(state, state.doc.length, 5000);
33+
34+
expect(state.doc.toString()).toBe(`${LONG_DOC}X`);
35+
expect(syntaxTree(state).toString()).toBe(
36+
syntaxTree(fullyParsed(mode, `${LONG_DOC}X`)).toString(),
37+
);
38+
});
39+
});

‎translate/src/modules/translationform/utils/editFieldModes.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const fluentMode: StreamParser<Array<'expression' | 'literal' | 'tag'>> =
55
name: 'fluent',
66
languageData: { closeBrackets: { brackets: ['(', '[', '{', '"', '<'] } },
77
startState: () => [],
8+
copyState: (state) => [...state],
89
token(stream, state) {
910
const ch = stream.next();
1011
switch (state.at(-1)) {
@@ -95,6 +96,7 @@ export const commonMode: StreamParser<Array<'literal' | 'tag'>> = {
9596
name: 'common',
9697
languageData: { closeBrackets: { brackets: ['(', '[', '{', '"', '<'] } },
9798
startState: () => [],
99+
copyState: (state) => [...state],
98100
token(stream, state) {
99101
if (
100102
stream.match(printf) ||

0 commit comments

Comments
 (0)