import React, { useEffect, useRef } from 'react';
import {
keymap,
highlightSpecialChars,
drawSelection,
highlightActiveLine,
dropCursor,
rectangularSelection,
crosshairCursor,
lineNumbers,
highlightActiveLineGutter,
EditorView,
} from '@codemirror/view';
import type { Extension } from '@codemirror/state';
import { EditorState } from '@codemirror/state';
import {
defaultHighlightStyle,
syntaxHighlighting,
indentOnInput,
bracketMatching,
foldGutter,
foldKeymap,
} from '@codemirror/language';
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
import { searchKeymap, highlightSelectionMatches } from '@codemirror/search';
import {
autocompletion,
completionKeymap,
closeBrackets,
closeBracketsKeymap,
} from '@codemirror/autocomplete';
import { lintKeymap } from '@codemirror/lint';
import { pythonLanguage } from '@codemirror/lang-python';
import { python } from '@codemirror/lang-python';
import type { CompletionContext } from '@codemirror/autocomplete';
function myCompletions(context: CompletionContext) {
const word = context.matchBefore(/\w*/);
if (!word) return null;
if (word.from == word.to && !context.explicit) return null;
return {
from: word.from,
options: [
{ label: 'match', type: 'keyword' },
{ label: 'hello', type: 'variable', info: '(World)' },
{ label: 'magic', type: 'text', apply: '⠁⭒*.✩.*⭒⠁', detail: 'macro' },
],
};
}
const App = () => {
const ref = useRef<HTMLDivElement>(null);
const extensions: Extension[] = [
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
history(),
foldGutter(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
indentOnInput(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
bracketMatching(),
closeBrackets(),
autocompletion(),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightSelectionMatches(),
keymap.of([
...closeBracketsKeymap,
...defaultKeymap,
...searchKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
...lintKeymap,
]),
python(),
pythonLanguage.data.of({
autocomplete: myCompletions,
}),
];
useEffect(() => {
if (ref.current) {
const view = new EditorView({
state: EditorState.create({
doc: 'some code',
extensions,
}),
parent: ref.current,
});
}
}, []);
return <div ref={ref} />;
};
export default App;
when I type some code, error