Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode: Support onDidChangeFoldingRanges events for FoldingRangeProvider #11320

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,8 @@ export interface LanguagesMain {
$emitCodeLensEvent(eventHandle: number, event?: any): void;
$registerOutlineSupport(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], displayName?: string): void;
$registerWorkspaceSymbolProvider(handle: number, pluginInfo: PluginInfo): void;
$registerFoldingRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[]): void;
$registerFoldingRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], eventHandle?: number): void;
$emitFoldingRangeEvent(eventHandle: number, event?: any): void;
$registerSelectionRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[]): void;
$registerDocumentColorProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[]): void;
$registerRenameProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], supportsResolveInitialValues: boolean): void;
Expand Down
17 changes: 16 additions & 1 deletion packages/plugin-ext/src/main/browser/languages-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,27 @@ export class LanguagesMainImpl implements LanguagesMain, Disposable {
return this.proxy.$provideOnTypeFormattingEdits(handle, model.uri, position, ch, options, token);
}

$registerFoldingRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[]): void {
$registerFoldingRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], eventHandle: number): void {
const languageSelector = this.toLanguageSelector(selector);
const provider = this.createFoldingRangeProvider(handle);

if (typeof eventHandle === 'number') {
const emitter = new Emitter<monaco.languages.FoldingRangeProvider>();
this.register(eventHandle, emitter);
provider.onDidChange = emitter.event;
}

this.register(handle, (monaco.languages.registerFoldingRangeProvider as RegistrationFunction<monaco.languages.FoldingRangeProvider>)(languageSelector, provider));
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
$emitFoldingRangeEvent(eventHandle: number, event?: any): void {
const obj = this.services.get(eventHandle);
if (obj instanceof Emitter) {
obj.fire(event);
}
}

createFoldingRangeProvider(handle: number): monaco.languages.FoldingRangeProvider {
return {
provideFoldingRanges: (model, context, token) => this.provideFoldingRanges(handle, model, context, token)
Expand Down
13 changes: 11 additions & 2 deletions packages/plugin-ext/src/plugin/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,17 @@ export class LanguagesExtImpl implements LanguagesExt {
// ### Folding Range Provider begin
registerFoldingRangeProvider(selector: theia.DocumentSelector, provider: theia.FoldingRangeProvider, pluginInfo: PluginInfo): theia.Disposable {
const callId = this.addNewAdapter(new FoldingProviderAdapter(provider, this.documents));
this.proxy.$registerFoldingRangeProvider(callId, pluginInfo, this.transformDocumentSelector(selector));
return this.createDisposable(callId);
const eventHandle = typeof provider.onDidChangeFoldingRanges === 'function' ? this.nextCallId() : undefined;

this.proxy.$registerFoldingRangeProvider(callId, pluginInfo, this.transformDocumentSelector(selector), eventHandle);
let result = this.createDisposable(callId);

if (eventHandle !== undefined && provider.onDidChangeFoldingRanges) {
const subscription = provider.onDidChangeFoldingRanges(() => this.proxy.$emitFoldingRangeEvent(eventHandle));
result = Disposable.from(result, subscription);
}

return result;
}

$provideFoldingRange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ export class LanguagesMainPluginMetrics extends LanguagesMainImpl {
super.$registerOnTypeFormattingProvider(handle, pluginInfo, selector, autoFormatTriggerCharacters);
}

override $registerFoldingRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[]): void {
override $registerFoldingRangeProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[], eventHandle: number): void {
this.registerPluginWithFeatureHandle(handle, pluginInfo.id);
super.$registerFoldingRangeProvider(handle, pluginInfo, selector);
super.$registerFoldingRangeProvider(handle, pluginInfo, selector, eventHandle);
}

override $registerDocumentColorProvider(handle: number, pluginInfo: PluginInfo, selector: SerializedDocumentFilter[]): void {
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7415,6 +7415,12 @@ export module '@theia/plugin' {
* [Folding](https://code.visualstudio.com/docs/editor/codebasics#_folding) in the editor.
*/
export interface FoldingRangeProvider {

/**
* An optional event to signal that the folding ranges from this provider have changed.
*/
onDidChangeFoldingRanges?: Event<void>;

/**
* Returns a list of folding ranges or null and undefined if the provider
* does not want to participate or was cancelled.
Expand Down