diff --git a/.eslintignore b/.eslintignore index b46872a4f..dac65b224 100644 --- a/.eslintignore +++ b/.eslintignore @@ -894,9 +894,6 @@ packages/app-mobile/components/NoteBodyViewer/hooks/useSource.js.map packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js.map -packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.d.ts -packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js -packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js.map packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js.map @@ -927,6 +924,12 @@ packages/app-mobile/components/NoteEditor/CodeMirror/markdownReformatter.test.js packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js.map +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.d.ts +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js.map +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.d.ts +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js.map packages/app-mobile/components/NoteEditor/CodeMirror/theme.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/theme.js packages/app-mobile/components/NoteEditor/CodeMirror/theme.js.map diff --git a/.gitignore b/.gitignore index e36e0b244..665a9c6ca 100644 --- a/.gitignore +++ b/.gitignore @@ -882,9 +882,6 @@ packages/app-mobile/components/NoteBodyViewer/hooks/useSource.js.map packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js.map -packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.d.ts -packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js -packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js.map packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js.map @@ -915,6 +912,12 @@ packages/app-mobile/components/NoteEditor/CodeMirror/markdownReformatter.test.js packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js.map +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.d.ts +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js.map +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.d.ts +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js +packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js.map packages/app-mobile/components/NoteEditor/CodeMirror/theme.d.ts packages/app-mobile/components/NoteEditor/CodeMirror/theme.js packages/app-mobile/components/NoteEditor/CodeMirror/theme.js.map diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.bulletedVsChecklist.test.ts b/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.bulletedVsChecklist.test.ts index 1976b5d36..8668c878b 100644 --- a/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.bulletedVsChecklist.test.ts +++ b/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.bulletedVsChecklist.test.ts @@ -3,7 +3,7 @@ */ import { EditorSelection } from '@codemirror/state'; import { ListType } from '../types'; -import createEditor from './createEditor'; +import createEditor from './testUtil/createEditor'; import { toggleList } from './markdownCommands'; describe('markdownCommands.bulletedVsChecklist', () => { diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.toggleList.test.ts b/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.toggleList.test.ts index 77bad5880..9fb071af4 100644 --- a/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.toggleList.test.ts +++ b/packages/app-mobile/components/NoteEditor/CodeMirror/markdownCommands.toggleList.test.ts @@ -7,7 +7,7 @@ import { increaseIndent, toggleList, } from './markdownCommands'; import { ListType } from '../types'; -import createEditor from './createEditor'; +import createEditor from './testUtil/createEditor'; describe('markdownCommands.toggleList', () => { it('should remove the same type of list', () => { diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror/markdownMathParser.test.ts b/packages/app-mobile/components/NoteEditor/CodeMirror/markdownMathParser.test.ts index 7288cdc51..975cd052b 100644 --- a/packages/app-mobile/components/NoteEditor/CodeMirror/markdownMathParser.test.ts +++ b/packages/app-mobile/components/NoteEditor/CodeMirror/markdownMathParser.test.ts @@ -1,15 +1,14 @@ -import { ensureSyntaxTree } from '@codemirror/language'; +import { markdown } from '@codemirror/lang-markdown'; +import { syntaxTree } from '@codemirror/language'; import { SyntaxNode } from '@lezer/common'; import { EditorState } from '@codemirror/state'; import { blockMathTagName, inlineMathContentTagName, inlineMathTagName, MarkdownMathExtension } from './markdownMathParser'; import { GFM as GithubFlavoredMarkdownExt } from '@lezer/markdown'; -import { markdown } from '@codemirror/lang-markdown'; - -const syntaxTreeCreateTimeout = 100; // ms +import forceFullParse from './testUtil/forceFullParse'; // Creates an EditorState with math and markdown extensions const createEditorState = (initialText: string): EditorState => { - return EditorState.create({ + const editorState = EditorState.create({ doc: initialText, extensions: [ markdown({ @@ -17,13 +16,16 @@ const createEditorState = (initialText: string): EditorState => { }), ], }); + forceFullParse(editorState); + + return editorState; }; // Returns a list of all nodes with the given name in the given editor's syntax tree. // Attempts to create the syntax tree if it doesn't exist. const findNodesWithName = (editor: EditorState, nodeName: string) => { const result: SyntaxNode[] = []; - ensureSyntaxTree(editor, syntaxTreeCreateTimeout)?.iterate({ + syntaxTree(editor).iterate({ enter: (node) => { if (node.name === nodeName) { result.push(node.node); diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.ts b/packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.ts similarity index 79% rename from packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.ts rename to packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.ts index 327c8b404..c4a8c2b4d 100644 --- a/packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.ts +++ b/packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.ts @@ -1,9 +1,10 @@ import { markdown } from '@codemirror/lang-markdown'; import { GFM as GithubFlavoredMarkdownExt } from '@lezer/markdown'; -import { forceParsing, indentUnit } from '@codemirror/language'; +import { indentUnit } from '@codemirror/language'; import { SelectionRange, EditorSelection, EditorState } from '@codemirror/state'; import { EditorView } from '@codemirror/view'; -import { MarkdownMathExtension } from './markdownMathParser'; +import { MarkdownMathExtension } from '../markdownMathParser'; +import forceFullParse from './forceFullParse'; // Creates and returns a minimal editor with markdown extensions const createEditor = (initialText: string, initialSelection: SelectionRange): EditorView => { @@ -19,7 +20,7 @@ const createEditor = (initialText: string, initialSelection: SelectionRange): Ed ], }); - forceParsing(editor); + forceFullParse(editor.state); return editor; }; diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.ts b/packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.ts new file mode 100644 index 000000000..b757f71fb --- /dev/null +++ b/packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.ts @@ -0,0 +1,18 @@ +import { ensureSyntaxTree, syntaxTreeAvailable } from '@codemirror/language'; +import { EditorState } from '@codemirror/state'; + +// Forces a full parse of a CodeMirror editor. This is intended for unit testing. +// If not in a unit-test consider using ensureSyntaxTree or forceParsing. +// This will throw if no language is configured for the editor. +const forceFullParse = (editorState: EditorState) => { + const timeout = 3000; // ms + ensureSyntaxTree(editorState, editorState.doc.length, timeout); + + if (!syntaxTreeAvailable(editorState)) { + throw new Error( + `Unable to generate a syntax tree in ${timeout}. Is the editor configured to parse a language?` + ); + } +}; + +export default forceFullParse;