monaco-editor
当使用多个或者打开多次编辑器时,输入代码,会发现有多个重复提示,
此时 monaco.languages.registerCompletionItemProvider 已经注册多次了,使用一个全局变量控制多次注册
// 判断已注册的,避免重复注册
将注册记录可以挂到 函数上 monaco.languages.registerCompletionItemProvider
if(!monaco.languages.registerCompletionItemProvider[language]) { monaco.languages.registerCompletionItemProvider[language] = true;
monaco.languages.registerCompletionItemProvider(language, { provideCompletionItems: async function (model, position) { let suggestions = []; const langLoader = await monaco.languages .getLanguages() .find((item) => { return item.id === language; }) .loader(); const keywords = langLoader.language.keywords; keywords.forEach((item) => { suggestions.push({ label: item, kind: monaco.languages.CompletionItemKind.Snippet, insertText: item, }); }); return { suggestions }; }, }); }