vscode: CompletionItem commands no longer work if added during resolve (regression)

This code produces a completion item and adds a command during resolve (which just shows an alert).

In the latest VS Code, this command does not fire when the completion item is selected, unless it is attached in the original provideCompletionItems call.

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
	context.subscriptions.push(vscode.commands.registerCommand('vscode-repro-completion-commands.helloWorld', () => {
		vscode.window.showInformationMessage('Hello World from vscode-repro-completion-commands!');
	}));

	context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
		{ scheme: 'file' },
		{
			provideCompletionItems(doc, pos, token) {
				const completionItem = new vscode.CompletionItem("ProcessResult");
				// Attaching the command here works:
				// completionItem.command = {
				// 	command: "vscode-repro-completion-commands.helloWorld",
				// 	title: "add imports",
				// };

				return new vscode.CompletionList(
					[completionItem],
					false,
				);
			},

			resolveCompletionItem(oldItem) {
				const completionItem = new vscode.CompletionItem("ProcessResult");
				// Attaching the command here does not work.
				completionItem.command = {
					command: "vscode-repro-completion-commands.helloWorld",
					title: "add imports",
				};

				return completionItem;
			}
		},
	));
}

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 30 (30 by maintainers)

Commits related to this issue

Most upvoted comments

It’s not enough for us to merely change textEdit.newText in item resolution. Please see @333fred’s explanation using completion of a method override item in the screenshot I pasted above.

I actually think that, if we could change the text edit during resolve, it should be doable (we may want to change more than just the text, for example changing whether the edit is a template or what the range of the edit is). In other words, we’re using it as a workaround for lazily resolving the entire textEdit, not just the text.

Btw, it also seems odd that mutating the provided completion item during resolve works. I could patch that into the LSP middleware to “fix” my issue for now, but presumable it’s a bug.

I do hope you’ll reconsider this though (or at least give language servers/SDKs time to update). I feel like the issue it causes is far worse then the problem it’s solving (like I said, I’ve had zero complaints about it not working, because realistically it’s probably incredibly rare), and the refactor to work differently isn’t trivial.