Skip to content

Add onDidChangeSemanticTokens support to DocumentRangeSemanticTokensProvider#268148

Merged
aeschli merged 7 commits intomainfrom
copilot/fix-c345ee31-b032-4b6a-97fe-2e8116c704ee
Oct 9, 2025
Merged

Add onDidChangeSemanticTokens support to DocumentRangeSemanticTokensProvider#268148
aeschli merged 7 commits intomainfrom
copilot/fix-c345ee31-b032-4b6a-97fe-2e8116c704ee

Conversation

Copy link
Contributor

Copilot AI commented Sep 24, 2025

Fixes missing onDidChangeSemanticTokens event support in DocumentRangeSemanticTokensProvider to provide API parity with DocumentSemanticTokensProvider.

Problem

The DocumentRangeSemanticTokensProvider interface lacked the onDidChangeSemanticTokens event that is available in DocumentSemanticTokensProvider. This prevented extensions from notifying VS Code when range-specific semantic tokens needed to be refreshed, forcing unnecessary re-computation and limiting performance optimizations.

This was originally reported against the Language Server Protocol via the C# team (microsoft/vscode-languageserver-node#1672).

Solution

Added optional onDidChangeSemanticTokens?: Event<void> property to DocumentRangeSemanticTokensProvider following the same pattern as the document provider:

export interface DocumentRangeSemanticTokensProvider {
	onDidChange?: Event<void>;  // New optional event
	getLegend(): SemanticTokensLegend;
	provideDocumentRangeSemanticTokens(model: ITextModel, range: Range, token: CancellationToken): ProviderResult<SemanticTokens>;
}

Implementation

  • Interface: Added optional onDidChange event property to maintain backward compatibility
  • Main Thread: Updated MainThreadDocumentRangeSemanticTokensProvider to accept and handle event emitters
  • Registration: Enhanced registration logic to support optional event subscriptions with proper cleanup
  • Protocol: Added $emitDocumentRangeSemanticTokensEvent method for cross-process communication
  • Extension Host: Updated registration to forward events from extensions to main thread
  • API: Updated VS Code extension API definition to expose the new capability

Usage

Extensions can now implement event-driven semantic token refreshing:

class MyRangeProvider implements vscode.DocumentRangeSemanticTokensProvider {
	private _onDidChangeSemanticTokens = new vscode.EventEmitter<void>();
	readonly onDidChangeSemanticTokens = this._onDidChangeSemanticTokens.event;

	constructor() {
		// Refresh tokens when configuration changes
		vscode.workspace.onDidChangeConfiguration(() => {
			this._onDidChangeSemanticTokens.fire();
		});
	}

	provideDocumentRangeSemanticTokens(document, range, token) {
		// Implementation here
	}
}

Testing

Added comprehensive tests verifying both scenarios:

  • Providers with onDidChangeSemanticTokens event work correctly
  • Providers without the event continue to work (backward compatibility)

The changes are fully backward compatible - existing range semantic token providers continue to work unchanged, while new providers can opt into event-driven refreshing for better performance.

Fixes #268140


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] DocumentRangeSemanticTokensProvider has no support for onDidChangeSemanticTokens Add onDidChangeSemanticTokens support to DocumentRangeSemanticTokensProvider Sep 24, 2025
Copilot AI requested a review from aeschli September 24, 2025 11:15
@aeschli aeschli marked this pull request as ready for review September 29, 2025 09:42
Copilot AI review requested due to automatic review settings September 29, 2025 09:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds onDidChangeSemanticTokens event support to DocumentRangeSemanticTokensProvider to provide API parity with DocumentSemanticTokensProvider, enabling extensions to notify VS Code when range-specific semantic tokens need refreshing.

Key changes:

  • Added optional onDidChange event property to the DocumentRangeSemanticTokensProvider interface
  • Updated registration and protocol methods to handle event subscriptions with proper cleanup
  • Enhanced main thread provider to accept and forward events across the extension host boundary

Reviewed Changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 1 comment.

File Description
src/vs/editor/common/languages.ts Added optional onDidChange event property to DocumentRangeSemanticTokensProvider interface
src/vs/workbench/api/common/extHost.protocol.ts Updated protocol methods to support event handles for range semantic tokens
src/vs/workbench/api/common/extHostLanguageFeatures.ts Enhanced registration logic to handle optional event subscriptions and forward events to main thread
src/vs/workbench/api/browser/mainThreadLanguageFeatures.ts Added event emission handling and updated provider constructor to accept event parameter

const handle = this._addNewAdapter(new DocumentRangeSemanticTokensAdapter(this._documents, provider), extension);
this._proxy.$registerDocumentRangeSemanticTokensProvider(handle, this._transformDocumentSelector(selector, extension), legend);
return this._createDisposable(handle);
const eventHandle = (typeof provider.onDidChangeSemanticTokens === 'function' ? this._nextHandle() : undefined);
Copy link

Copilot AI Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type check should verify if provider.onDidChangeSemanticTokens is an Event object, not a function. Events in VS Code are objects with subscription methods, not functions themselves. Consider checking typeof provider.onDidChangeSemanticTokens !== 'undefined' instead.

Copilot uses AI. Check for mistakes.
@vs-code-engineering vs-code-engineering bot added this to the September 2025 milestone Sep 29, 2025
Copy link
Member

@alexr00 alexr00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeschli, this seems reasonable, but we should go through API review in October.

@aeschli aeschli modified the milestones: September 2025, October 2025 Sep 29, 2025
@aeschli aeschli merged commit 24ed19d into main Oct 9, 2025
28 checks passed
@aeschli aeschli deleted the copilot/fix-c345ee31-b032-4b6a-97fe-2e8116c704ee branch October 9, 2025 09:02
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Nov 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DocumentRangeSemanticTokensProvider has no support for onDidChangeSemanticTokens

5 participants