microsoft / microsoft/monaco-editor

[Bug] "No suggestions" persists while typing if the completion helper is set to return an empty list for empty content

Open
#3,557 7 comments 0 reactions 1 assignee View on GitHub

@jrieken is already working on this.

Since Feb 20, 2023.

bug suggest
Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Reproducible in vscode.dev or in VS Code Desktop?
  • Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
Monaco Editor Playground Link

link

Monaco Editor Playground Code
// Register a new language
monaco.languages.register({ id: "mySpecialLanguage" });

// Register a tokens provider for the language
monaco.languages.setMonarchTokensProvider("mySpecialLanguage", {
	tokenizer: {
		root: [
			[/\[error.*/, "custom-error"],
			[/\[notice.*/, "custom-notice"],
			[/\[info.*/, "custom-info"],
			[/\[[a-zA-Z 0-9:]+\]/, "custom-date"],
		],
	},
});

// Define a new theme that contains only rules that match this language
monaco.editor.defineTheme("myCoolTheme", {
	base: "vs",
	inherit: false,
	rules: [
		{ token: "custom-info", foreground: "808080" },
		{ token: "custom-error", foreground: "ff0000", fontStyle: "bold" },
		{ token: "custom-notice", foreground: "FFA500" },
		{ token: "custom-date", foreground: "008800" },
	],
	colors: {
		"editor.foreground": "#000000",
	},
});

// Register a completion item provider for the new language
monaco.languages.registerCompletionItemProvider("mySpecialLanguage", {
	provideCompletionItems: (model, position, context, token) => {
        var textUntilPosition = model.getValueInRange({
			startLineNumber: 1,
			startColumn: 1,
			endLineNumber: position.lineNumber,
			endColumn: position.column,
		});

        // return no suggestions for empty input
		if (textUntilPosition.length === 0) {
            return {suggestions:[]};
        }
        
        var suggestions = [
			{
				label: "simpleText",
				kind: monaco.languages.CompletionItemKind.Text,
				insertText: "simpleText",
			},
			{
				label: "testing",
				kind: monaco.languages.CompletionItemKind.Keyword,
				insertText: "testing(${1:condition})",
				insertTextRules:
					monaco.languages.CompletionItemInsertTextRule
						.InsertAsSnippet,
			},
			{
				label: "ifelse",
				kind: monaco.languages.CompletionItemKind.Snippet,
				insertText: [
					"if (${1:condition}) {",
					"\t$0",
					"} else {",
					"\t",
					"}",
				].join("\n"),
				insertTextRules:
					monaco.languages.CompletionItemInsertTextRule
						.InsertAsSnippet,
				documentation: "If-Else Statement",
			},
		];
		return { suggestions: suggestions };
	},
});

monaco.editor.create(document.getElementById("container"), {
	theme: "myCoolTheme",
	value: getCode(),
	language: "mySpecialLanguage",
});

function getCode() {
	return "";
}
Reproduction Steps
  1. In the playground, in an empty editor, hit Ctrl+Space to trigger Monaco suggestions - there should be "No suggestions"
  2. Begin typing (ex: if). It won't trigger the completionProvider even with words that normally trigger completionProvider .
    It only happens after you hit Ctrl+Space in an empty editor.

If you type "i" first and then hit Ctrl+Space, completionProvider works fine.
image

We believe the issue is in this line: https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/browser/suggestModel.ts#L672
It checks if the user has started typing new character by comparing the end of last word with a start of new word. It does that comparison by checking if end is less than the start of the new word. This gets tricky when you type the "i" and the previous value was an empty string. This prevents it from triggering it.

Actual (Problematic) Behavior

After you hit Ctrl+Space in an empty editor, "No suggestions" persists.

Expected Behavior

Suggestions should show, and the "No suggestions" flyout is cleared.

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.