Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions translate/src/modules/translationform/utils/editFieldModes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
ensureSyntaxTree,
StreamLanguage,
type StreamParser,
syntaxTree,
} from '@codemirror/language';
import { EditorState } from '@codemirror/state';

import { commonMode, fluentMode, webextMode } from './editFieldModes';

const LONG_DOC = `<a ${'x '.repeat(300)}\n${'y '.repeat(30)}>`;

function fullyParsed(mode: StreamParser<any>, doc: string): EditorState {
const state = EditorState.create({
doc,
extensions: [StreamLanguage.define(mode)],
});
ensureSyntaxTree(state, state.doc.length, 5000);
return state;
}

describe.each([
['fluent', fluentMode],
['common', commonMode],
['webext', webextMode],
])('%s mode', (_name, mode: StreamParser<any>) => {
test('applies an edit resuming from a parser state snapshot (#4438)', () => {
let state = fullyParsed(mode, LONG_DOC);
state = state.update({
changes: { from: state.doc.length, insert: 'X' },
}).state;
ensureSyntaxTree(state, state.doc.length, 5000);

expect(state.doc.toString()).toBe(`${LONG_DOC}X`);
expect(syntaxTree(state).toString()).toBe(
syntaxTree(fullyParsed(mode, `${LONG_DOC}X`)).toString(),
);
});
});
6 changes: 6 additions & 0 deletions translate/src/modules/translationform/utils/editFieldModes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { StreamParser } from '@codemirror/language';

// Avoids saving {} for snapshots.
Comment thread
ayshushus marked this conversation as resolved.
Resolved
// https://github.com/mozilla/pontoon/issues/4438
const copyStack = <T>(state: T[]): T[] => state.slice();

export const fluentMode: StreamParser<Array<'expression' | 'literal' | 'tag'>> =
{
name: 'fluent',
languageData: { closeBrackets: { brackets: ['(', '[', '{', '"', '<'] } },
startState: () => [],
copyState: copyStack,
Comment thread
ayshushus marked this conversation as resolved.
Resolved
token(stream, state) {
const ch = stream.next();
switch (state.at(-1)) {
Expand Down Expand Up @@ -95,6 +100,7 @@ export const commonMode: StreamParser<Array<'literal' | 'tag'>> = {
name: 'common',
languageData: { closeBrackets: { brackets: ['(', '[', '{', '"', '<'] } },
startState: () => [],
copyState: copyStack,
token(stream, state) {
if (
stream.match(printf) ||
Expand Down