Hey I’m trying to add codemirror to my project and I’ve a use case where I need to add new auto complete items to existing array as user types in editor…
for now I’ve implemented till this
initTextSuggestionCodeMirror() {
var autocomplete = autocompletion({
activateOnTyping: true,
override: [this.myCompletions],
});
this.view = new EditorView({
doc: this.code,
extensions: [
oneDark,
minimalSetup,
// Transaction filters can inspect transactions and
// add/replace them with other transactions. If a
// transaction would have made the document more than
// one line long, it is filtered out.
EditorState.transactionFilter.of((tr) => {
return tr.newDoc.lines > 1 ? [] : [tr];
}),
autocomplete,
keymap.of([...defaultKeymap]),
],
parent: this.leeraTextEditorContainer.nativeElement,
});
}
and My completion function is
myCompletions(context: CompletionContext): CompletionResult | null {
let word = context.matchBefore(/\w*/);
if (word?.from == word?.to && !context.explicit) return null;
return {
from: word?.from as number,
options: [
{ label: 'match', type: 'keyword' },
{ label: 'hello', type: 'variable', info: '(World)' },
{ label: 'magic', type: 'text', apply: '⠁⭒*.✩.*⭒⠁', detail: 'macro' },
],
};
}
So I need a add more values to this options array dynamically as user types… It’s a readonly Completion[] so as soon as I give it a reference to another array as options: “this.my_dynamic_array” it breaks and competition doesn’t work at all…
options: [
{ label: 'match', type: 'keyword' },
{ label: 'hello', type: 'variable', info: '(World)' },
{ label: 'magic', type: 'text', apply: '⠁⭒*.✩.*⭒⠁', detail: 'macro' },
],