欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决 Monaco Editor 添加自定义提示后导致内置单词消失问题

程序员文章站 2022-05-26 17:54:03
...

像这样添加自定义提示后,内置单词提示消失了

this.monaco.languages.registerCompletionItemProvider('javascript', {
			provideCompletionItems: function(model, position) {

				// var textUntilPosition = model.getValueInRange({startLineNumber: position.lineNumber, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
				// var match = textUntilPosition.indexOf('.') == -1//textUntilPosition.match(/cu/gim);
				// var suggestions = match ? _this._comp_cfg : [];
				var suggestions = _this._comp_cfg ;
				return {
					suggestions: suggestions
				};
		
				return ;
			},
			// 光标选中当前自动补全item时触发动作,一般情况下无需处理
			resolveCompletionItem(item, token) {
				return null;
			}
		});

解决方法,修改以下文件:
editor.main.js 下的这个函数将
将:

if (lenBefore !== result.length || token.isCancellationRequested) {
                    break;
}

改为:

if (lenBefore !== result.length || token.isCancellationRequested) {
                    // break;
 }

修改的函数代码

function provideSuggestionItems(model, position, options = CompletionOptions.default, context = { triggerKind: 0 /* Invoke */ }, token = cancellation_1.CancellationToken.None) {
        return __awaiter(this, void 0, void 0, function* () {
            // const t1 = Date.now();
            position = position.clone();
            const word = model.getWordAtPosition(position);
            const defaultReplaceRange = word ? new range_1.Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn) : range_1.Range.fromPositions(position);
            const defaultRange = { replace: defaultReplaceRange, insert: defaultReplaceRange.setEndPosition(position.lineNumber, position.column) };
            const result = [];
            const disposables = new lifecycle_1.DisposableStore();
            let needsClipboard = false;
            const onCompletionList = (provider, container) => {
                if (!container) {
                    return;
                }
                for (let suggestion of container.suggestions) {
                    if (!options.kindFilter.has(suggestion.kind)) {
                        // fill in default range when missing
                        if (!suggestion.range) {
                            suggestion.range = defaultRange;
                        }
                        // fill in default sortText when missing
                        if (!suggestion.sortText) {
                            suggestion.sortText = typeof suggestion.label === 'string' ? suggestion.label : suggestion.label.name;
                        }
                        if (!needsClipboard && suggestion.insertTextRules && suggestion.insertTextRules & 4 /* InsertAsSnippet */) {
                            needsClipboard = snippetParser_1.SnippetParser.guessNeedsClipboard(suggestion.insertText);
                        }
                        result.push(new CompletionItem(position, suggestion, container, provider));
                    }
                }
                if (lifecycle_1.isDisposable(container)) {
                    disposables.add(container);
                }
            };
            // ask for snippets in parallel to asking "real" providers. Only do something if configured to
            // do so - no snippet filter, no special-providers-only request
            const snippetCompletions = (() => __awaiter(this, void 0, void 0, function* () {
                if (!_snippetSuggestSupport || options.kindFilter.has(27 /* Snippet */)) {
                    return;
                }
                if (options.providerFilter.size > 0 && !options.providerFilter.has(_snippetSuggestSupport)) {
                    return;
                }
                const list = yield _snippetSuggestSupport.provideCompletionItems(model, position, context, token);
                onCompletionList(_snippetSuggestSupport, list);
            }))();
            // add suggestions from contributed providers - providers are ordered in groups of
            // equal score and once a group produces a result the process stops
            // get provider groups, always add snippet suggestion provider
            for (let providerGroup of modes.CompletionProviderRegistry.orderedGroups(model)) {
                // for each support in the group ask for suggestions
                let lenBefore = result.length;
                yield Promise.all(providerGroup.map((provider) => __awaiter(this, void 0, void 0, function* () {
                    if (options.providerFilter.size > 0 && !options.providerFilter.has(provider)) {
                        return;
                    }
                    try {
                        const list = yield provider.provideCompletionItems(model, position, context, token);
                        onCompletionList(provider, list);
                    }
                    catch (err) {
                        errors_1.onUnexpectedExternalError(err);
                    }
                })));
                if (lenBefore !== result.length || token.isCancellationRequested) {
                    // break;
                }
            }
            yield snippetCompletions;
            if (token.isCancellationRequested) {
                disposables.dispose();
                return Promise.reject(errors_1.canceled());
            }
            // console.log(`${result.length} items AFTER ${Date.now() - t1}ms`);
            return new CompletionItemModel(result.sort(getSuggestionComparator(options.snippetSortOrder)), needsClipboard, disposables);
        });
    }