Skip to content

Commit

Permalink
Sort and filter share providers (#183377)
Browse files Browse the repository at this point in the history
  • Loading branch information
joyceerhl committed May 24, 2023
1 parent 1fc4a6b commit d564334
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/vs/workbench/api/browser/mainThreadShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ export class MainThreadShare implements MainThreadShareShape {
this.proxy = extHostContext.getProxy(ExtHostContext.ExtHostShare);
}

$registerShareProvider(handle: number, selector: IDocumentFilterDto[], id: string, label: string): void {
$registerShareProvider(handle: number, selector: IDocumentFilterDto[], id: string, label: string, priority: number): void {
const provider: IShareProvider = {
id,
label,
selector,
priority,
provideShare: async (item: IShareableItem) => {
return URI.revive(await this.proxy.$provideShare(handle, item, new CancellationTokenSource().token));
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ export interface MainThreadSearchShape extends IDisposable {
}

export interface MainThreadShareShape extends IDisposable {
$registerShareProvider(handle: number, selector: IDocumentFilterDto[], id: string, label: string): void;
$registerShareProvider(handle: number, selector: IDocumentFilterDto[], id: string, label: string, priority: number): void;
$unregisterShareProvider(handle: number): void;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ExtHostShare implements ExtHostShareShape {
registerShareProvider(selector: vscode.DocumentSelector, provider: vscode.ShareProvider): vscode.Disposable {
const handle = ExtHostShare.handlePool++;
this.providers.set(handle, provider);
this.proxy.$registerShareProvider(handle, DocumentSelector.from(selector, this.uriTransformer), provider.id, provider.label);
this.proxy.$registerShareProvider(handle, DocumentSelector.from(selector, this.uriTransformer), provider.id, provider.label, provider.priority);
return {
dispose: () => {
this.proxy.$unregisterShareProvider(handle);
Expand Down
5 changes: 4 additions & 1 deletion src/vs/workbench/contrib/share/browser/shareService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { CancellationToken } from 'vs/base/common/cancellation';
import { IDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { score } from 'vs/editor/common/languageSelector';
import { localize } from 'vs/nls';
import { ISubmenuItem } from 'vs/platform/actions/common/actions';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
Expand Down Expand Up @@ -46,7 +47,9 @@ export class ShareService implements IShareService {
}

async provideShare(item: IShareableItem, token: CancellationToken): Promise<URI | undefined> {
const providers = [...this._providers.values()];
const providers = [...this._providers.values()]
.filter((p) => score(p.selector, item.resourceUri, '', true, undefined, undefined) > 0)
.sort((a, b) => a.priority - b.priority);

if (providers.length === 0) {
return undefined;
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/share/common/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IShareableItem {
export interface IShareProvider {
readonly id: string;
readonly label: string;
readonly priority: number;
readonly selector: LanguageSelector;
prepareShare?(item: IShareableItem, token: CancellationToken): Thenable<boolean | undefined>;
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | undefined>;
Expand Down

0 comments on commit d564334

Please sign in to comment.